spring boot redis做叢集

zhumeilu發表於2017-12-14
  • 新增依賴

    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-data-redis</artifactId>
      </dependency>
    <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-cache</artifactId>
      </dependency>
    複製程式碼
  • application.properties

    spring.redis.cluster.nodes=192.168.1.89:7003,192.168.1.89:7005,192.168.1.89:7002,192.168.1.89:7001,192.168.1.89:7004,192.168.1.89:7000 spring.redis.pool.max-idle=8 spring.redis.pool.min-idle=0 spring.redis.pool.max-active=8 spring.redis.pool.max-wait=-1

  • controller

      @Controller
      public class UserController {
      
          @Autowired
          private StringRedisTemplate template;
      
          @Autowired
          private UserMapper userMapper;
          @RequestMapping("/users/{id}")
          @ResponseBody
          public User get(@PathVariable Long id){
              User user = userMapper.get(id);
              ValueOperations<String, String> ops = this.template.opsForValue();
              String key = "user_"+id;
              if (!this.template.hasKey(key)) {
                  ops.set(key,user.getUsername());
              }
              return user;
          }
      }
    複製程式碼

相關文章