1、问题写在前面
1.1、问题描述:Redis集群节点地址发现失败
Unable to connect to [172.17.0.4:7303]: connection timed out: /172.17.0.4:7303
1.2、解决方案:
redis.conf 中添加配置
cluster-announce-ip 192.168.56.11
1.3、方案出处:官网
redis.conf
在Docker等容器中部署时,Redis集群节点地址发现失败,因为地址是NAT-ted或因为端口被转发。为了让Redis Cluster在这样的环境中正常工作,需要一个静态配置让每个节点都知道其公网地址
2、Spring Boot 连接 Redis 集群实践
实践 Demo 源码
2.1 、文件结构
文件树
├─java
│ └─com
│ └─yeswater
│ └─redis7
│ │ RedisMain7777.java
│ │
│ ├─config
│ │ RedisConfig.java
│ │
│ ├─controller
│ │ OrderController.java
│ │
│ └─service
│ OrderService.java
│
└─resources
application.yml
2.2、演示
set
console
get
2.3、代码
RedisMain7777.java
@SpringBootApplicationpublicclassRedisMain7777{publicstaticvoidmain(String[] args){SpringApplication.run(RedisMain7777.class, args);}}
RedisConfig
@ConfigurationpublicclassRedisConfig{@BeanpublicRedisTemplate<String,Object>redisTemplate(LettuceConnectionFactory lettuceConnectionFactory){RedisTemplate<String,Object> redisTemplate =newRedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);//设置key序列化方式string
redisTemplate.setKeySerializer(newStringRedisSerializer());//设置value的序列化方式json,使用GenericJackson2JsonRedisSerializer替换默认序列化
redisTemplate.setValueSerializer(newGenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(newStringRedisSerializer());
redisTemplate.setHashValueSerializer(newGenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();return redisTemplate;}}
OrderController
@RestControllerpublicclassOrderController{@ResourceprivateOrderService orderService;@RequestMapping(value ="/order/add",method =RequestMethod.POST)publicvoidaddOrder(){
orderService.addOrder();}@RequestMapping(value ="/order/{id}", method =RequestMethod.GET)publicStringfindUserById(@PathVariableInteger id){return orderService.getOrderById(id);}}
OrderSerive
@ServicepublicclassOrderService{publicstaticfinalStringORDER_KEY="order:";@ResourceprivateRedisTemplate redisTemplate;publicvoidaddOrder(){int keyId =ThreadLocalRandom.current().nextInt(1000)+1;String orderNo =UUID.randomUUID().toString();
redisTemplate.opsForValue().set(ORDER_KEY+keyId,"京东订单"+ orderNo);System.out.println(("=====>编号"+ keyId +"的订单流水生成:"+ orderNo));}publicStringgetOrderById(Integer id){return(String)redisTemplate.opsForValue().get(ORDER_KEY+ id);}}
application.yml
server:port:7777spring:application:name: redis7_study
redis:password:111111database:0lettuce:pool:max-active:8# 连接池最大连接数默认值为8 max-wait:-1ms # 连接池最大阻塞时间(使用负值表示没有限制)默认值为-1max-idle:8# 连接池中最大空闲连接数默认值为8min-idle:0# 连接池中的最小空闲连接数,默认值为0cluster:# 感知集群的主从切换refresh:adaptive:true#支持集群拓扑动态感应刷新,自适应拓扑刷新是否使用所有可用的更新,默认false关闭period:2000#定时刷新cluster:nodes:- 192.168.56.11:7306# cluster-announce-ip 192.168.56.11:7306- 192.168.56.11:7305- 192.168.56.11:7304- 192.168.56.11:7303- 192.168.56.11:7302- 192.168.56.11:7301max-redirects:3
pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.atguigu.redis7</groupId><artifactId>redis7_study</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.6.10</version><relativePath/></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><junit.version>4.12</junit.version><log4j.version>1.2.17</log4j.version><lombok.version>1.16.18</lombok.version></properties><dependencies><!--SpringBoot通用依赖模块--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--SpringBoot与Redis整合依赖--><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.projectlombok</groupId><artifactId>lombok</artifactId><version>${lombok.version}</version><optional>true</optional></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
标签:
spring boot
redis
本文转载自: https://blog.csdn.net/weixin_44296614/article/details/139867499
版权归原作者 すあ 所有, 如有侵权,请联系我们删除。
版权归原作者 すあ 所有, 如有侵权,请联系我们删除。