这简直是个坑,网上很多都系搭建在虚拟机的集群,按照那个的结果就是集群搭建成功了但是SpringBoot程序访问不了集群。
1.服务器上安装纯净的redis,在/usr.local/redis下,配置文件在/usr/local/redis/ets/redis.conf
2.修改安装的redis的配置文件
mkdir /usr/local/redis-cluster cd redis-cluster/ mkdir -p 9001/data 9002/data 9003/data 9004/data 9005/data 9006/data |
cd /usr/local/redis-cluster
mkdir bin
cd /usr/local/redis-3.2.9/src
cp mkreleasehdr.sh redis-benchmark redis-check-aof redis-cli redis-server redis-trib.rb /usr/local/redis-cluster/bin
cp -r /usr/local/redis /usr/local/redis-cluster/9001
port 9001(每个节点的端口号)
dir /usr/local/redis-cluster/9001/data/(数据文件存放位置):conf文件最后添加:
pidfile /var/run/redis_9001.pid(pid 9001和port要对应)
cluster-enabled yes(启动集群模式)
cluster-config-file nodes-9001.conf(9001和port要对应)
cluster-node-timeout 15000
appendonly yes
我们已经完成了一个节点了,其实接下来就是机械化的再完成另外五个节点,其实可以这么做:把 9001 实例 复制到另外五个文件夹中,唯一要修改的就是 redis.conf 中的所有和端口的相关的信息即可,其实就那么四个位置。开始操作,看图:
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9002
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9003
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9004
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9005
\cp -rf /usr/local/redis-cluster/9001/* /usr/local/redis-cluster/9006
修改 9002-9006 的 redis.conf 文件
其实非常简单了,你通过搜索会发现其实只有四个点需要修改,我们全局替换下吧,进入相应的节点文件夹,做替换就好了。
vi /usr/local/redis-cluster/9002/redis/etc/redis.conf
在命令模式下:输入冒号粘贴下面命令后按enter即可,保存退出,发现四个地方被替换了
%s/9001/9002
其实我们也就是替换了下面这四行:
port 9002
dir /usr/local/redis-cluster/9002/data/
cluster-config-file nodes-9002.conf
pidfile /var/run/redis_9002.pid
对9003-9006按照上面的方法一次替换相应的9001为对应的端口
vi /usr/local/redis-cluster/9003/redis/etc/redis.conf
vi /usr/local/redis-cluster/9004/redis/etc/redis.conf
vi /usr/local/redis-cluster/9005/redis/etc/redis.conf
vi /usr/local/redis-cluster/9006/redis/etc/redis.conf
%s/9001/9002
%s/9001/9003
%s/9001/9004
%s/9001/9005
%s/9001/9006
开启腾讯云的端口:9001-9006和19001-19006
启动9001-9006六个节点
/usr/local/redis/bin/redis-server /usr/local/redis-cluster/9001/redis/etc/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis-cluster/9002/redis/etc/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis-cluster/9003/redis/etc/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis-cluster/9004/redis/etc/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis-cluster/9005/redis/etc/redis.conf
/usr/local/redis/bin/redis-server /usr/local/redis-cluster/9006/redis/etc/redis.conf
出错:
redis源码里有redis-trib.rb脚本,需要使用这个脚本搭建集群,脚本执行需要使用 ruby 命令,所以我们需要安装 ruby 和相关接口。
yum install ruby
yum install rubygems
上传文件:redis-3.2.1.gem(随便哪个文件夹)运行命令:gem install -l redis-3.2.1.gem
集群环境测试:
/usr/local/redis-cluster/bin/redis-trib.rb create --replicas 1 xxxx:9001 xxxx:9002 xxxx:9003 xxxx:9004 xxxx:9005 xxxx:9006
简单解释一下这个命令:调用 ruby 命令来进行创建集群,--replicas 1 表示主从复制比例为 1:1,即一个主节点对应一个从节点;然后,默认给我们分配好了每个主节点和对应从节点服务,以及 solt 的大小,因为在 Redis 集群中有且仅有 16383 个 solt ,默认情况会给我们平均分配,当然你可以指定,后续的增减节点也可以重新分配。
M: 10222dee93f6a1700ede9f5424fccd6be0b2fb73 为主节点Id
S: 9ce697e49f47fec47b3dc290042f3cc141ce5aeb xxxx:9004 replicates 10222dee93f6a1700ede9f5424fccd6be0b2fb73 从节点下对应主节点Id
目前来看,9001-9003 为主节点,9004-9006 为从节点,并向你确认是否同意这么配置。输入 yes 后,会开始集群创建。
就可以成功搭建集群了。
下面是搭建里遇到的问题:
1.出现:
要先启动6个redis才能用rubby构建集群
2.出现:
客户端连接的时候:
命令后加参数-c
其他结点访问:
3.关闭6个redis后重启,用rubby构建集群出现:
删除每个redis里的三个文件:
清除每个redis的数据,并且重置集群
出现:
说明没有开放19001-19006端口
下面是springboot整合redis集群:
spring:
redis:
database: 0
# host: 129.28.150.115
# port: 6379
# password: 123456
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
timeout: 10000
cluster:
nodes:
- 129.28.150.115:9001
- 129.28.150.115:9002
- 129.28.150.115:9003
- 129.28.150.115:9004
- 129.28.150.115:9005
- 129.28.150.115:9006
@RestController
public class RedisController {
@Autowired
private RedisService redisService;
@RequestMapping("/set")
public String set(String key ,String obj){
return redisService.set(key, obj)==true?"success":"fail";
}
@RequestMapping("/get")
public String get(String key){
return redisService.get(key);
}
}
@Service
public class RedisService {
@Autowired
private RedisTemplate redisTemplate;
public boolean set(String key,String object){
try {
redisTemplate.opsForValue().set(key, object);
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
public String get(String key){
Object object = redisTemplate.opsForValue().get(key);
if(object==null){
return "get null";
}
return (String)object;
}
}
@SpringBootConfiguration
public class RedisClusterConfig {
@Autowired
private RedisClusterConfigProperties clusterProperties;
@Bean
public RedisClusterConfiguration getClusterConfig() {
RedisClusterConfiguration rcc = new RedisClusterConfiguration(clusterProperties.getNodes());
rcc.setMaxRedirects(clusterProperties.getMaxRedirects().intValue());
return rcc;
}
@Bean
public JedisConnectionFactory getConnectionFactory(RedisClusterConfiguration cluster) {
return new JedisConnectionFactory(cluster);
}
@Bean
public RedisTemplate getRedisTemplate(JedisConnectionFactory factory) {
RedisTemplate redisTemplate = new RedisTemplate();
redisTemplate.setConnectionFactory(factory);
RedisSerializer redisSerializer = new StringRedisSerializer();
redisTemplate.setKeySerializer(redisSerializer);
return redisTemplate;
}
@Bean
public StringRedisTemplate getStringRedisTemplate(RedisConnectionFactory factory) {
StringRedisTemplate stringTemplate = new StringRedisTemplate();
stringTemplate.setConnectionFactory(factory);
RedisSerializer redisSerializer = new StringRedisSerializer();// Long���Ͳ����Ի�����쳣��Ϣ;
stringTemplate.setKeySerializer(redisSerializer);
stringTemplate.setHashKeySerializer(redisSerializer);
stringTemplate.setValueSerializer(redisSerializer);
return stringTemplate;
}
}
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisClusterConfigProperties {
/**
* master nodes
*/
private List nodes;
/**
* max redirects
*/
private Integer maxRedirects = 3;
public List getNodes() {
return nodes;
}
public void setNodes(List nodes) {
this.nodes = nodes;
}
public Integer getMaxRedirects() {
return maxRedirects;
}
public void setMaxRedirects(Integer maxRedirects) {
this.maxRedirects = maxRedirects;
}
}
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
4.0.0
com.cl
SpringBootRedisCluster
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.0.0.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-redis
redis.clients
jedis
org.apache.maven.plugins
maven-compiler-plugin
1.8
org.springframework.boot
spring-boot-maven-plugin
com.cl.App
repackage
测试结果: