1、拉取redis镜像
docker pull redis:5.0.2
2、使用host创建docker镜像
docker的网络类型
docker的网络类型有:
None:不为容器配置任何网络功能,没有网络 --net=none
Container:与另一个运行中的容器共享Network Namespace,--net=container:containerID
Host:与主机共享Network Namespace,--net=host
Bridge:Docker设计的NAT网络模型(默认类型)
docker create --name redis-node01 --net host -v /data/redis-data/node01:/data redis:5.0.2 --cluster-enabled yes --cluster-config-file nodes-node-01.conf --port 6380
docker create --name redis-node02 --net host -v /data/redis-data/node02:/data redis:5.0.2 --cluster-enabled yes --cluster-config-file nodes-node-02.conf --port 6381
docker create --name redis-node03 --net host -v /data/redis-data/node03:/data redis:5.0.2 --cluster-enabled yes --cluster-config-file nodes-node-03.conf --port 6382
3、启动容器
docker start redis-node01 redis-node02 redis-node03
4、创建redis集群
阿里云记得开启6380、6381、6382…端口和集群总线端口集群总线端口为16380、16381、16382…
redis端口+10000就是集群总线端口
redis-cli --cluster create 外网地址:6382 外网地址:6380 外网地址:6381 --cluster-replicas 0
5、使用cmd测试
redis-cli -c -h 你的外网地址 -p 6381
set name "hello"
get name
然后切换到6382端口测试...
6、cmd测试没问题、使用java代码测试
导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- redis依赖commons-pool 这个依赖一定要添加 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<!--<version>2.7.2</version>-->
</dependency>
//创建一个JedisCluster对象
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("外网地址", 6380));
nodes.add(new HostAndPort("外网地址", 6381));
nodes.add(new HostAndPort("外网地址", 6382));
//在nodes中指定每个节点的地址
//jedisCluster在系统中是单例的。
JedisCluster jedisCluster = new JedisCluster(nodes);
jedisCluster.set("name", "zhangsan");
jedisCluster.set("value", "100");
String name = jedisCluster.get("name");
String value = jedisCluster.get("value");
System.out.println(name);
System.out.println(value);
//系统关闭时关闭jedisCluster
jedisCluster.close();
测试结果
7、优化一下代码
7.1编写application.properties文件
# redis集群配置
# 接池最大阻塞等待时间(使用负值表示没有限制) 默认-1
spring.redis.jedis.pool.max-wait = 500
# 连接池中的最大空闲连接 默认8
spring.redis.jedis.pool.max-Idle = 100
# 连接池中的最小空闲连接 默认8
spring.redis.jedis.pool.min-Idle = 8
#集群地址
spring.redis.cluster.nodes = 你的服务器地址:6380,你的服务器地址:6382,你的服务器地址:6381
#最大重定向没有是使用
spring.redis.cluster.max-redirects=5
#最大连接数量
spring.redis.jedis.pool.max-active=100
7.2编写配置文件
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
@Data
public class ClusterConfigurationProperties {
private List<String> nodes;
//最大重定向次数
private Integer maxRedirects;
//刷新时间
private Integer refreshTime;
//最大空闲连接数
@Value("#{${spring.redis.jedis.pool.max-Idle}}")
private Integer maxIdle;
//最小空闲连接
@Value("#{${spring.redis.jedis.pool.min-Idle}}")
private Integer minIdle;
//最大连接数量
@Value("#{${spring.redis.jedis.pool.max-active}}")
private Integer maxActive;
//最大等待毫秒数
@Value("#{${spring.redis.jedis.pool.max-wait}}")
private Integer maxWait;
}
7.3、注入JedisPoolConfig对象
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class JedisConfig {
@Autowired
private ClusterConfigurationProperties clusterConfigurationProperties;
@Bean
public JedisPoolConfig jedisPoolConfig(){
// Jedis连接池配置
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
// 最大空闲连接数, 默认8个
jedisPoolConfig.setMaxIdle(clusterConfigurationProperties.getMaxIdle());
// 最大连接数, 默认8个
jedisPoolConfig.setMaxTotal(clusterConfigurationProperties.getMaxActive());
//最小空闲连接数, 默认0
jedisPoolConfig.setMinIdle(clusterConfigurationProperties.getMinIdle());
// 获取连接时的最大等待毫秒数(如果设置为阻塞时BlockWhenExhausted),如果超时就抛异常, 小于零:阻塞不确定的时间, 默认-1
jedisPoolConfig.setMaxWaitMillis(clusterConfigurationProperties.getMaxWait()); // 设置2秒
//对拿到的connection进行validateObject校验
jedisPoolConfig.setTestOnBorrow(true);
return jedisPoolConfig;
}
}
7.4、编写测试类
import com.haoke.dubbo.api.config.ClusterConfigurationProperties;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedis {
@Autowired
private ClusterConfigurationProperties clusterConfigurationProperties;
@Autowired
private JedisPoolConfig jedisPoolConfig;
@Test
public void testSave2() throws Exception {
//创建一个JedisCluster对象
List<String> hostList = clusterConfigurationProperties.getNodes();
Set<HostAndPort> nodes = new HashSet<>();
//将hostlist转为hostAndPort
for(String hostAndPortStr:hostList){
String host = hostAndPortStr.split(":")[0];
Integer prot = Integer.parseInt(hostAndPortStr.split(":")[1]);
HostAndPort hostAndPort = new HostAndPort(host,prot);
nodes.add(hostAndPort);
}
//在nodes中指定每个节点的地址
//jedisCluster在系统中是单例的。
JedisCluster jedisCluster = new JedisCluster(nodes,jedisPoolConfig);
// jedisCluster.set("name", "zhangsan");
// jedisCluster.set("value", "100");
String name = jedisCluster.get("name");
String value = jedisCluster.get("value");
System.out.println(name);
System.out.println(value);
//系统关闭时关闭jedisCluster
jedisCluster.close();
}
}