doker搭建redis- culster集群
- 下载doker等过程不再介绍
- 本文搭建环境是mac os 与centos上部署的方法几乎相同,可以参考下
- 首先docker下载redis镜像
docker pull redis
- docker中创建虚拟网卡
docker network create redis-net
- 可以查看虚拟网卡状态
docker network ls
docker network inspect redis-net
- 在Mac上创建本地文件redis-cluster.tmpl,然后写入内容
port ${PORT}
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 宿主机ip地址
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes
port:节点端口,即对外提供通信的端口
cluster-enabled:是否启用集群
cluster-config-file:集群配置文件
cluster-node-timeout:连接超时时间
cluster-announce-ip:集群各节点IP地址
cluster-announce-port:集群节点映射端口
cluster-announce-bus-port:集群总线端口
appendonly:持久化模式
- 该文件的作用是为了之后自动生成配置文件
- 终端命令行进入到redis-cluster.tmpl所在文件夹, 使用命令来生成6个redis配置文件
for port in $(seq 8010 8015); \
do \
mkdir -p ./${port}/conf \
&& PORT=${port} envsubst < ./redis-cluster.tmpl > ./${port}/conf/redis.conf \
&& mkdir -p ./${port}/data; \
done
- 使用命令来生成六个docker容器,其中/WorkSpace/redis是生成的配置文件所在的位置
for port in $(seq 8010 8015); \
do \
docker run -it -d -p ${port}:${port} -p 1${port}:1${port} \
--privileged=true -v /WorkSpace/redis/${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
--privileged=true -v /WorkSpace/redis/${port}/data:/data \
--restart always --name redis-${port} --net redis-net \
--sysctl net.core.somaxconn=1024 redis redis-server /usr/local/etc/redis/redis.conf; \
done
- 进入其中一个容器节点
docker exec -it redis-8010 bash
- 执行如下命令,将所有节点的ip:port全部写下来执行
cd /usr/local/bin/
redis-cli --cluster create 宿主机ip:8010 宿主机ip:8011 ……
- 中途会要输入yes,等待执行完毕即可
注意
Mac os下使用命令来生成文件和docker容器有可能报错
-bash: envsubst: command not found
需要升级下Homebrew,然后使用命令
brew upgrade gettext
brew link --force gettext
可能出现权限不足的情况
You should change the ownership of these directories to your user.
sudo chown -R $(whoami) /usr/local/include /usr/local/lib /usr/local/lib/pkgconfig
将sudo后面的东西复制下来 执行一次,输入密码即可
springboot 集成redis集群
依赖
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'redis.clients:jedis'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
testImplementation('org.springframework.boot:spring-boot-starter-test') {
exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
}
}
- redis.clients:jedis可能在spring initializr里面找不到,需要手动输入,不需要写版本号会和springboot版本自动匹配
配置
server:
port: 4010
spring:
application:
name: cluster-redis-project
redis:
cluster:
nodes: 宿主机ip:8010,宿主机ip:8011,……
- 在nodes处写入6个所有redis节点的ip和端口号,用逗号隔开,我这里总共是有6个这里就不全部写出来了
redis的使用
@EnableCaching
@SpringBootApplication
public class ClusterRedisProjectApplication {
public static void main(String[] args) {
SpringApplication.run(ClusterRedisProjectApplication.class, args);
}
}
@RestController
public class ClusterRedisController {
@Resource
private RedisTemplate<String, Object> redisTemplate;
@GetMapping("/")
public String serValue(@Param("name") String name, @Param("value") String value) {
redisTemplate.boundValueOps(name).set(value);
return "成功";
}
@GetMapping("/get")
public Object getValue(@Param("name") String name){
return redisTemplate.boundValueOps(name).get();
}
}
- 在这里简单用RedisTemplate尝试下,发现存储的数据在所有节点里面都能看到即可
网友评论