亲宝软件园·资讯

展开

springboot整合redis集群过程解析

人气:0

简介

在springboot使用搭建好的redis集群

添加redis和连接池依赖

  <!--redis连接池 start-->
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-pool2</artifactId>
    </dependency>
    <!--redis连接池 end-->

    <!--redis start-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <!--redis end-->

在配置文件中配置连接池和sentinel

  spring:
  redis:
    lettuce:
    pool:
      max-active: 8
      max-idle: 8
      max-wait: -1ms
      min-idle: 0
    sentinel:
    master: mymaster
    nodes: 192.168.145.128:26379,192.168.145.128:26380,192.168.145.128:26381

创建redis服务

  @Service
  public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void put(String key, Object value, long seconds) {
      redisTemplate.opsForValue().set(key,value,seconds, TimeUnit.SECONDS);
    }

    @Override
    public Object get(String key) {
      return redisTemplate.opsForValue().get(key);
    }
  }

创建redisController测试redis服务

  @RestController
  public class RedisController {

    @Autowired
    private RedisService redisService;

    @PostMapping(value = "put")
    public String put(String key,String value,long seconds){

      redisService.put(key,value,seconds);
      return "ok";
    }

    @GetMapping(value = "get")
    public Object get(String key){

      Object o=redisService.get(key);
      if(o!=null){
        return String.valueOf(o);
      }
      return "not_ok";
    }
  }
您可能感兴趣的文章:

加载全部内容

相关教程
猜你喜欢
用户评论