Spring Boot + Redis集群案例

Spring Boot + Redis集群

 1)配置redis配置文件

port 6379

daemonize yes

# bind是绑定ip,0.0.0.0是代表任何ip

bind 0.0.0.0

# 保护模式     

protected-mode no

 

# 添加节点

cluster-enabled yes

# cluster配置文件名,该文件属于自动生成,仅用于快速查找文件并查询文件内容

cluster-config-file nodes-6379.conf

#节点服务响应超时时间,用于判定该节点是否下线或切换为从节点(10000是10秒)

cluster-node-timeout 10000

 

复制并修改配置文件(sed 命令是利用脚本来处理文本文件

[root@server redis]# sed "s/6379/6380/g" redis-6379.conf >redis-6380.conf

[root@server redis]# sed "s/6379/6381/g" redis-6379.conf >redis-6381.conf

[root@server redis]# sed "s/6379/6381/g" redis-6379.conf >redis-6381.conf

[root@server redis]# sed "s/6379/6382/g" redis-6379.conf >redis-6382.conf

[root@server redis]# sed "s/6379/6383/g" redis-6379.conf >redis-6383.conf

[root@server redis]# sed "s/6379/6384/g" redis-6379.conf >redis-6384.conf

分别启动

[root@server redis]# redis-server /etc/redis/redis-6379.conf

 

查看启动情况

允许对应端口号的请求通过防火墙,并重启防火墙,查看开启端口号

[root@localhost ~]#firewall-cmd --zone=public --add-port=6379/tcp –permanent

[root@localhost ~]#firewall-cmd --zone=public --add-port=6380/tcp --permanent

[root@localhost ~]#firewall-cmd --zone=public --add-port=6381/tcp --permanent

[root@localhost ~]#firewall-cmd --zone=public --add-port=6382/tcp --permanent

[root@localhost ~]#firewall-cmd --zone=public --add-port=6383/tcp --permanent

[root@localhost ~]#firewall-cmd --zone=public --add-port=6384/tcp --permanent

[root@localhost ~]#systemctl restart firewalld.service

[root@localhost ~]#firewall-cmd --list-port

添加节点(命令后面的1,代表一主一从,如果你要一主二从,设置为2)

[root@server redis]# redis-cli --cluster create xxx.xxx.xx.xxx:6379 1 xxx.xxx.xx.xxx:6380 xxx.xxx.xx.xxx:6381 xxx.xxx.xx.xxx:6382 xxx.xxx.xx.xxx:6383 xxx.xxx.xx.xxx:6384 --cluster-replicas 1

在客户端使用命令查看节点、存储数据(会切换节点客户端

(2)Spring Boot配置Redis集群配置默认数据源连接池(lettuce),如果你想用Jedis,需要添加jedis的依赖,修改配置文件就可以

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-pool2</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>

    </dependency>
</dependencies>

application.yml配置文件

# 配置redis集群
spring:
 
redis:
   
cluster:
       
nodes: xxx.xxx.xx.xxx:6379,xxx.xxx.xx.xxx:6380,xxx.xxx.xx.xxx:6381,xxx.xxx.xx.xxx:6382,xxx.xxx.xx.xxx:6383,xxx.xxx.xx.xxx:6384
   
lettuce:
     
pool:
       
max-active: 1000 # 连接池最大连接数(使用负值表示没有限制)
     max-idle: 10 # 连接池中的最大空闲连接
     min-idle: 5 # 连接池中的最小空闲连接
     max-wait: -1 # 连接池最大阻塞等待时间(使用负值表示没有限制)

 

 

spring boot集成redis进行数据缓存功能

        @Cacheable 表明Spring在调用方法之前,首先应该在缓存中查找方法的返回值。如果这个值能够找到,就会返回缓存的值。否则的话,这个方法就会被调用,返回值会放到缓存之中

        @cacheput 表明Spring应该将方法的返回值放到缓存中。在方法的调用前并不会 检查缓存,方法始终都会被调用

        @cacheevict 表明Spring应该在缓存中清除一个或多个条目

        @caching 这是一个分组的注解,能够同时应用多个其他的缓存注解

        @cacheconfig 可以在类层级配置一些共用的缓存配置
 

测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class RedisClusterTest {

   
@Autowired
   
private RedisTemplate redisTemplate;

   
@Test
   
public void test(){

        System.
out.println("进入redis存储操作...");
       
//存入数据
       
redisTemplate.opsForValue().set("name","cch");
        System.
out.println("拿到的键值:"+redisTemplate.opsForValue().get("name"));

    }


}

 

 

 

发布了49 篇原创文章 · 获赞 31 · 访问量 2876

猜你喜欢

转载自blog.csdn.net/cjy_lean/article/details/105543521