需要 6 台 redis 服务器。搭建伪集群。
需要 6 个 redis 实例。
mkder redis
yum install gcc-c++
yum install rubygems
wget http://download.redis.io/releases/redis-4.0.8.tar.gz
tar -zxvf redis-4.0.8.tar.gz
make
注意这里需要创建文件夹rediscluster
make install PREFIX=/user/redis/rediscluster/redis1
/user/redis/rediscluster/redis1
/user/redis/rediscluster/redis2
/user/redis/rediscluster/redis3
/user/redis/rediscluster/redis4
/user/redis/rediscluster/redis5
/user/redis/rediscluster/redis6
[root@localhost redis-4.0.8]# cp redis.conf
/user/redis/rediscluster/redis1/bin
[root@localhost redis-4.0.8]# cp redis.conf /user/redis/rediscluster/redis2/bin
[root@localhost redis-4.0.8]# cp redis.conf /user/redis/rediscluster/redis3/bin
[root@localhost redis-4.0.8]# cp redis.conf /user/redis/rediscluster/redis4/bin
[root@localhost redis-4.0.8]# cp redis.conf /user/redis/rediscluster/redis5/bin
[root@localhost redis-4.0.8]# cp redis.conf /user/redis/rediscluster/redis6/bin
将cluster-enabled yes 前的注释去掉
在redis1/bin下启动
./redis-server redis.conf
查看下是否起来
ps -ef | grep redis
上传redis-3.0.0.gem ,安装 ruby用于搭建redis集群的脚本 这里自己下载一个 上传上去
gem install redis-3.0.0\ .gem
进入redis源码目录中的src目录 执行下面的命令
./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
不清楚的看图
启动成功
先查询进程
ps -ef | grep redis
这里需要注意删除redis进程
kill -9 ****
然后在自己的每个/user/redis/rediscluster/redis2/bin/redis.conf把这个端口号#掉
在阿里云上面7001到7006添加到安全组
./redis-server redis.conf
ps -ef | grep redis
./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002
127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006
成功如下
//引入依赖
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.1.RELEASE
com.example.rediscluster
redisclusterdemo
0.0.1-SNAPSHOT redisclusterdemo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-data-redis
redis.clients
jedis
2.9.0
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
自己的端口号
spring: redis:
cluster:
nodes: 127.0.0.1:7001,127.0.0.1.250:7002,127.0.0.1.250:7003,127.0.0.1.250:7004,127.0.0.1:7005,127.0.0.1.250:7006
max-redirects: 6
// 测试 package com.example.rediscluster.redisclusterdemo;
import org.junit.jupiter.api.Test; import
org.springframework.beans.factory.annotation.Autowired; import
org.springframework.boot.test.context.SpringBootTest; import
org.springframework.data.redis.core.HashOperations; import
org.springframework.data.redis.core.RedisTemplate;import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest public class RedisTest {
@Autowired
private RedisTemplate redisTemplate;
@Test
public void test1(){
System.out.println(redisTemplate.hasKey("name"));
redisTemplate.opsForValue().set("name", "123214");
String name = (String) redisTemplate.opsForValue().get("name");
System.out.println(name);
redisTemplate.opsForValue().set("name2", "123214");
String name2 = (String) redisTemplate.opsForValue().get("name");
System.out.println(name2);
redisTemplate.opsForValue().set("name3", "123214");
String name3 = (String) redisTemplate.opsForValue().get("name");
System.out.println(name3);
redisTemplate.opsForValue().set("name4", "123214");
String name4 = (String) redisTemplate.opsForValue().get("name");
System.out.println(name4);
HashOperations hashOperations = redisTemplate.opsForHash();
hashOperations.put("user", "test", "测试");
System.out.println(hashOperations.get("user", "test"));
}
}