springboot整合redis集群过程解析

前端之家收集整理的这篇文章主要介绍了springboot整合redis集群过程解析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

简介

在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,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";
    }
  }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

原文链接:https://www.f2er.com/springboot/526476.html

猜你在找的Springboot相关文章