編程時要保持這種心態(tài):就好像將來要維護你這些代碼的人是一位殘暴的精神病患者,而且他知道你住在哪。-Martin Golding
Docker三劍客之Compose搭建Redis偽分布式集群(3)
image
來源:Docker官方文檔
Docker Compose
更對精彩內(nèi)容請關注我的公眾號(看相聲也要敲代碼)
負責實現(xiàn)對基于Docker容器的多應用服務的快速編排错忱,定義并運行多個Docker容器的應用界阁,允許用戶編寫yaml文件關聯(lián)容器,Compose默認管理對象是服務棧泡躯,通過子命令對棧中多個服務進行便捷生命周期管理。
關鍵概念:
任務:一個容器被稱為一個任務较剃,任務是獨一無二的id,同一個服務多個任務序號遞增
服務:某個相同應用鏡像的容器副本集合咕别,一個服務可以橫向擴展為多個實例
服務棧:由多個服務組成,配合完成特定業(yè)務
Docker Compose安裝Redis偽分布式集群詳細步驟
1写穴、安裝docker-compose
# 下載
$ curl -L github對應版本
# 確認安裝是否完成
$ docker-compose -v
docker-compose version 1.28.5, build c4eb3a1f
2惰拱、拉取Redis鏡像
docker pull redis:latest
3确垫、編寫redis-cluster.template模板
port ${PORT}
protected-mode no
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.28.1 # 宿主機IP即可
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
appendonly yes
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
4、創(chuàng)建本地conf文件
for port in `seq 7001 7006` ; do \
mkdir -p . redis-cluster/${port}/conf \
&& PORT=${port} envsubst < redis-cluster.template > redis-cluster/${port}/conf/redis.conf \
&& mkdir -p ./redis-cluster/${port}/data; \
done
5翔冀、編寫redis-cluster.yml
version : '3.7'
services:
redis7001:
image: 'redis'
container_name: redis7001
command:
["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7001/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7001/data:/data
ports:
- "7001:7001"
- "17001:17001"
environment:
- TZ=Asia/Shanghai
redis7002:
image: 'redis'
container_name: redis7002
command:
["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7002/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7002/data:/data
ports:
- "7002:7002"
- "17002:17002"
environment:
- TZ=Asia/Shanghai
redis7003:
image: 'redis'
container_name: redis7003
command:
["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7003/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7003/data:/data
ports:
- "7003:7003"
- "17003:17003"
environment:
- TZ=Asia/Shanghai
redis7004:
image: 'redis'
container_name: redis7004
command:
["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7004/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7004/data:/data
ports:
- "7004:7004"
- "17004:17004"
environment:
- TZ=Asia/Shanghai
redis7005:
image: 'redis'
container_name: redis7005
command:
["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7005/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7005/data:/data
ports:
- "7005:7005"
- "17005:17005"
environment:
- TZ=Asia/Shanghai
redis7006:
image: 'redis'
container_name: redis7006
command:
["redis-server", "/usr/local/etc/redis/redis.conf"]
volumes:
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7006/conf/redis.conf:/usr/local/etc/redis/redis.conf
- /F/ideaProject/metaapp-materialplatform/redis-cluster/redis-cluster/7006/data:/data
ports:
- "7006:7006"
- "17006:17006"
environment:
- TZ=Asia/Shanghai
6纤子、執(zhí)行yml文件
docker-compose -f redis-cluster.yml up -d
7、啟動redis集群
docker exec -it redis7001 redis-cli -p 7001 --cluster create 192.168.28.1:7001 192.168.28.1:7002 192.168.28.1:7003 192.168.28.1:7004 192.168.28.1:7005 192.168.28.1:7006 --cluster-replicas 1
8控硼、測試
docker exec -it redis7001 redis-cli -h 192.168.28.1 -p 7005 ping
docker exec -it redis7001 redis-cli -h 192.168.28.1 -p 7005 -c # 集群啟動需要添加-c
9艾少、簡單搭建Springboot服務使用Redis偽分布式集群
1. maven依賴
<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.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
yaml文件配置
spring:
redis:
connect-timeout: 6000
cluster:
max-redirects: 3
nodes:
- 192.168.28.1:7001
- 192.168.28.1:7002
- 192.168.28.1:7003
- 192.168.28.1:7004
- 192.168.28.1:7005
- 192.168.28.1:7006
lettuce:
pool:
max-active: 1000
max-idle: 10
min-idle: 5
max-wait: -1
image.gif
redis配置Bean
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedisConfigurer {
@Bean
public RedisTemplate<String,Object> redisClusterTemplate(LettuceConnectionFactory factory){
RedisTemplate<String,Object> template = new RedisTemplate<>();
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.setConnectionFactory(factory);
return template;
}
}
代碼測試
@RestController
public class RedisClusterController {
private final RedisTemplate<String,Object> redisTemplate;
@Autowired
public RedisClusterController(RedisTemplate<String, Object> redisTemplate) {
this.redisTemplate = redisTemplate;
}
@GetMapping("/clusterOperate")
public String clusterOperate(){
redisTemplate.opsForValue().set("www.kxscode.com","看相聲也要敲代碼");
return String.valueOf(redisTemplate.opsForValue().get("www.kxscode.com"));
}
}
遠程倉庫
參考
基于docker-compose搭建redis集群