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
@SpringBootApplication
public class RedisMain7777 {
public static void main(String[] args) {
SpringApplication.run(RedisMain7777.class, args);
}
}
RedisConfig
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory)
{
RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(lettuceConnectionFactory);
//设置key序列化方式string
redisTemplate.setKeySerializer(new StringRedisSerializer());
//设置value的序列化方式json,使用GenericJackson2JsonRedisSerializer替换默认序列化
redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
OrderController
@RestController
public class OrderController
{
@Resource
private OrderService orderService;
@RequestMapping(value = "/order/add",method = RequestMethod.POST)
public void addOrder()
{
orderService.addOrder();
}
@RequestMapping(value = "/order/{id}", method = RequestMethod.GET)
public String findUserById(@PathVariable Integer id)
{
return orderService.getOrderById(id);
}
}
OrderSerive
@Service
public class OrderService
{
public static final String ORDER_KEY = "order:";
@Resource
private RedisTemplate redisTemplate;
public void addOrder()
{
int keyId = ThreadLocalRandom.current().nextInt(1000)+1;
String orderNo = UUID.randomUUID().toString();
redisTemplate.opsForValue().set(ORDER_KEY+keyId,"京东订单"+ orderNo);
System.out.println(("=====>编号" + keyId + "的订单流水生成:" + orderNo));
}
public String getOrderById(Integer id)
{
return (String)redisTemplate.opsForValue().get(ORDER_KEY + id);
}
}
application.yml
server:
port: 7777
spring:
application:
name: redis7_study
redis:
password: 111111
database: 0
lettuce:
pool:
max-active: 8 # 连接池最大连接数默认值为8
max-wait: -1ms # 连接池最大阻塞时间(使用负值表示没有限制)默认值为-1
max-idle: 8 # 连接池中最大空闲连接数默认值为8
min-idle: 0 # 连接池中的最小空闲连接数,默认值为0
cluster:
# 感知集群的主从切换
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:7301
max-redirects: 3
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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>