
# Spring项目接入MAS-Redis-SDK
1. 引入依赖。 
   ![](https://support.huaweicloud.com/devg-mas/public_sys-resources/note_3.0-zh-cn.png)
   组件版本version使用最新版本，版本的获取参考[MAS-SDK版本](https://support.huaweicloud.com/devg-mas/mas-devg-0100.html)。
   ```
   <dependency>
       <groupId>com.huaweicloud.devspore</groupId>
       <artifactId>devspore-dcs</artifactId>
       <version>${mas.version}</version>
   </dependency>
   ```
   
   
2. 配置文件示例。 
   ![](https://support.huaweicloud.com/devg-mas/public_sys-resources/note_3.0-zh-cn.png)
   - 配置项详细解释参考[配置参数说明](https://support.huaweicloud.com/devg-mas/mas-devg-0030.html)。
   
   - 根据是否接入MAS服务，确定是否配置etcd部分。
     - 接入MAS服务，etcd配置必填，以及servers部分下Redis源列表与MAS服务中配置的源保持一致。
     
     - 无接入MAS服务，etcd配置无需配置，servers部分下Redis源列表以实际使用为准。
      
    
   ```
   props:
     version: v1
     app-id: xxxx
     monitor-id: xxxx
     cloud: xxxx
     region: xxxxx
     azs: az1 
   etcd:
     address: xxx.xxx.xxx.xxx:xxxx
     api-version: v3
     username: xxxx
     password: xxxx
     https-enable: false
   redis:
     nearest: dc1
     servers:
       dc1:
         hosts: xxx.xxx.xxx.xxx:xxxx
         password: xxxxxx
         type: normal
         cloud: xxxx
         region: xxxxx
         azs: az1
         pool:
           max-total: 8
           max-idle: 8
           min-idle: 8
           max-wait-millis: 10000
           time-between-eviction-runs-millis: 60000
       dc2:
         hosts: xxx.xxx.xxx.xxx:xxxx
         password: xxxxxx
         type: normal
         cloud: xxxx
         region: xxxxx
         azs: az1
         pool:
           max-total: 8
           max-idle: 8
           min-idle: 8
           max-wait-millis: 10000
           time-between-eviction-runs-millis: 60000
   route-algorithm: single-read-write
   ```
   
   
3. MAS-Redis-SDK提供了MasRedisConfigurationLoader.load方法，可以读取YAML格式的配置，生成客户端。 
   ```
   @Bean
   public MultiZoneClient createMultiZoneClient() {
       File yamlFile = new File(this.getClass().getClassLoader().getResource("devspore-cache.yaml").getFile());
       MasRedisConfiguration masRedisConfiguration = MasRedisConfigurationLoader.load(yamlFile);
       return MultiZoneRedisFactory.createMultiZoneRedis(masRedisConfiguration);
   }
   ```
   
   
4. 在需要执行Redis操作的地方引入MultiZoneClient，并使用MultiZoneClient执行Redis操作。 
   ```
   举例：
   @Autowired
   private MultiZoneClient client;
   @Override
   public void set(String key, String value) {
       client.set(key, value);
   }
   ```
   
   
