Redis 主从架构。
这种架构,主节点可以读写,从节点只能读。当主节点挂掉后,整个 redis 服务就不可用了。
哨兵。
哨兵可以解决主从架构中的单点故障问题。就是添加的 sentinel1 、sentinel2、sentinel3文件映射。当主节点出现故障时,其中的从节点会成为主节点。
version: '3.1'
services:
redis1:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis1
environment:
- TZ=Asia/Shanghai
ports:
- 7001:6379
volumes:
- ./conf/redis1.conf:/usr/local/redis/redis.conf
- ./conf/sentinel1.conf:/data/sentinel.conf
command: ["redis-server","/usr/local/redis/redis.conf"]
redis2:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis2
environment:
- TZ=Asia/Shanghai
ports:
- 7002:6379
volumes:
- ./conf/redis2.conf:/usr/local/redis/redis.conf
- ./conf/sentinel2.conf:/data/sentinel.conf
links:
- redis1:master
command: ["redis-server","/usr/local/redis/redis.conf"]
redis3:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis3
environment:
- TZ=Asia/Shanghai
ports:
- 7003:6379
volumes:
- ./conf/redis3.conf:/usr/local/redis/redis.conf
- ./conf/sentinel3.conf:/data/sentinel.conf
links:
- redis1:master
command: ["redis-server","/usr/local/redis/redis.conf"]
mkdir conf
// 创建多个文件
touch redis1.conf redis2.conf redis3.conf sentinel1.conf sentinel2.conf sentinel3.conf
// redis2.conf 和 redis3.conf 从节点写入
replicaof master 6379
// sentinel1.conf 写入
# 哨兵需要后台启动
daemonize yes
# 指定Master节点的ip和端口(主)
sentinel monitor master localhost 6379 2
# 指定Master节点的ip和端口(从)
# sentinel monitor master master 6379 2
# 哨兵每隔多久监听一次redis架构
sentinel down-after-milliseconds master 10000
// sentinel2.conf sentinel3.conf 写入
# 哨兵需要后台启动
daemonize yes
# 指定Master节点的ip和端口(主)
# sentinel monitor master localhost 6379 2
# 指定Master节点的ip和端口(从)
sentinel monitor master master 6379 2
# 哨兵每隔多久监听一次redis架构
sentinel down-after-milliseconds master 10000
// 启动 docker
docker-compose up -d
// 进入3个 redis 容器内部,启动 sentinel
docker exec -it 39 bash
redis-sentinel sentinel.conf
// 测试,关闭主节点 哨兵是否起作用
docker stop 39
// 进入另两个 redis 容器内部
docker exec -it a8 bash
redis-cli
// 查看redis 信息,其中一个从节点的 role:master 变为主节点即为成功
info
Redis 集群。
集群在保证主从加哨兵的基本功能之外,还能够提升Redis存储数据的能力。
集群是无中心的,节点数量必须是 2n + 1。
cd /opt
mkdir docker_redis_jq
cd docker_redis_jq
vi docker-compose.yml
version: "3.1"
services:
redis1:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis1
environment:
- TZ=Asia/Shanghai
ports:
- 7001:7001
- 17001:17001
volumes:
- ./conf/redis1.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]
redis2:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis2
environment:
- TZ=Asia/Shanghai
ports:
- 7002:7002
- 17002:17002
volumes:
- ./conf/redis2.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]
redis3:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis3
environment:
- TZ=Asia/Shanghai
ports:
- 7003:7003
- 17003:17003
volumes:
- ./conf/redis3.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]
redis4:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis4
environment:
- TZ=Asia/Shanghai
ports:
- 7004:7004
- 17004:17004
volumes:
- ./conf/redis4.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]
redis5:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis5
environment:
- TZ=Asia/Shanghai
ports:
- 7005:7005
- 17005:17005
volumes:
- ./conf/redis5.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]
redis6:
image: daocloud.io/library/redis:5.0.7
restart: always
container_name: redis6
environment:
- TZ=Asia/Shanghai
ports:
- 7006:7006
- 17006:17006
volumes:
- ./conf/redis6.conf:/usr/local/redis/redis.conf
command: ["redis-server","/usr/local/redis/redis.conf"]
mkdir conf
cd conf
touch redis1.conf redis2.conf redis3.conf redis4.conf redis5.conf redis6.conf
vi redis1.conf
// redis1.conf 写入
# 指定redis的端口号
port 7001
# 开启Redis集群
cluster-enabled yes
# 集群信息的文件
cluster-config-file nodes-7001.conf
# 集群的对外ip地址, 就是虚拟机 ip
cluster-announce-ip 10.36.144.110
# 集群的对外port
cluster-announce-port 7001
# 集群的总线端口
cluster-announce-bus-port 17001
//
vi redis2.conf
// redis2.conf 写入,其他文件依次类推
# 指定redis的端口号
port 7002
# 开启Redis集群
cluster-enabled yes
# 集群信息的文件
cluster-config-file nodes-7002.conf
# 集群的对外ip地址, 就是虚拟机 ip
cluster-announce-ip 10.36.144.110
# 集群的对外port
cluster-announce-port 7002
# 集群的总线端口
cluster-announce-bus-port 17002
// 然后随便进入一个 redis 容器内部,启动集群配置,注意要先启动 docker 管理的 redis,启动当前 docker 时,要先关闭其他有影响的 docker。
redis-cli --cluster create 10.36.144.110:7001 10.36.144.110:7002 10.36.144.110:7003 10.36.144.110:7004 10.36.144.110:7005 10.36.144.110:7006 --cluster-replicas 1
// 启动编辑,如果启动失败,需要先 exit 再执行当前命令。
redis-cli -h 10.36.144.110 -p 7001 -c
// 测试,如果多次设置值随机分配存储节点,说明集群有效
set a 1
Java 连接 Redis 集群。
@Test
public void t9() {
Set<HostAndPort> nodes = new HashSet<>();
nodes.add(new HostAndPort("10.36.144.128",7001));
nodes.add(new HostAndPort("10.36.144.128",7002));
nodes.add(new HostAndPort("10.36.144.128",7003));
nodes.add(new HostAndPort("10.36.144.128",7004));
nodes.add(new HostAndPort("10.36.144.128",7005));
nodes.add(new HostAndPort("10.36.144.128",7006));
JedisCluster jedisCluster = new JedisCluster(nodes);
String s = jedisCluster.set("name", "zs");
System.out.println(s);
String res = jedisCluster.get("a");
System.out.println(res);
}
ElasticSearch。
是一个使用 Java 语言并且基于 Lucene 编写的搜索引擎框架,提供了分布式的全文搜索功能,提供了统一的基于 RESTful 风格的 WEB 接口。
Lucene 本身就是一个搜索引擎的底层。
安装 ElasticSearch。虚拟机需要至少 4G 内存。
需要安装ES、Kibana 和 IK 分词器。
// 虚拟机分配内存
vi /etc/sysctl.conf
// sysctl.conf 写入
vm.max_map_count = 665600
// 使文件生效
sysctl -p
cd /opt
mkdir docker_es
cd docker_es
// 查 ip
ip a | grep ens33
// docker 配置,ip需要改为自己虚拟机 ip
vi docker-compose.yml
version: "3.1"
services:
elasticsearch:
image: daocloud.io/library/elasticsearch:6.5.4
restart: always
container_name: elasticsearch
ports:
- 9200:9200
kibana:
image: daocloud.io/library/kibana:6.5.4
restart: always
container_name: kibana
ports:
- 5601:5601
environment:
- elasticsearch_url=http://10.36.144.128:9200
depends_on:
- elasticsearch
docker-compose up -d
// 进入 es 容器内部
docker exec -it 84 bash
cd bin
// 下载 IK 分词器
./elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.5.4/elasticsearch-analysis-ik-6.5.4.zip
//退出 并 重启 ES 容器,让 IK 分词器生效
exit
docker-compose restart
测试。浏览器输入 http://10.36.144.128:5601/ 进入kibana 图形化操作 es 页面,选择 Dev Tools 添加下面 json,选中执行。注意 { 格式要另起一行。
POST _analyze
{
"analyzer": "ik_max_word",
"text": "玛尔扎哈 迪丽热巴"
}