栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Redis学习--------哨兵集群搭建,RedisTemplate连接

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Redis学习--------哨兵集群搭建,RedisTemplate连接

1.哨兵集群搭建
		IP						Port				Role
192.168.127.101					27001				Sentinel
192.168.127.101					27002				Sentinel
192.168.127.101					27003				Sentinel

192.168.127.101					7001				Master
192.168.127.101					7002				slave
192.168.127.101					7003				slave

修改配置文件 redis.conf

bind 192.168.127.101
port 7001
replicaof 192.168.127.101 7001
daemonize no
dir "/opt/study/redis/7001"

操作指令

mkdir 7001 7002 7003
-- 修改7001(7002.7003同理)
cp redis.conf 7001/
cd  7001/
vi  redis.conf

修改配置文件sentinel.conf

port 27001
pidfile "/opt/study/redis/27001/redis-sentinel.pid"
sentinel announce-ip "192.168.127.101"
dir "/opt/study/redis/27001"

sentinel monitor mymaster 192.168.127.101 7001 2

操作指令

mkdir 27001 27002 27003
cp sentinel.conf 27001/
cd 27001/
vi sentinel.conf

启动指令

启动redis集群
redis-server ../../7001/redis.conf
redis-server ../../7002/redis.conf
redis-server ../../7003/redis.conf

启动哨兵集群
redis-sentinel ../../27001/sentinel.conf
redis-sentinel ../../27002/sentinel.conf
redis-sentinel ../../27003/sentinel.conf

哨兵集群
redis集群

故障转移:

2. RedisTemplate连接哨兵集群 2.1 引入pom依赖

			org.springframework.boot
			spring-boot-starter-data-redis
		
2.2 配置文件
spring:
  redis:
    sentinel:
      nodes:
      - 192.168.127.101:27001
      - 192.168.127.101:27002
      - 192.168.127.101:27003
      master: mymaster

2.3 修改配置类 实现读写分离
  @Bean
    public LettuceClientConfigurationBuilderCustomizer lettuceClientConfigurationBuilderCustomizer(){
        return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
    }
2.4 测试

测试controller

@RestController
public class RedisController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/set/{key}/{value}")
    public String setKey(@PathVariable("key") String key, @PathVariable("value") String value){
        stringRedisTemplate.opsForValue().set(key, value);
        return "success";
    }

    @GetMapping("/get/{key}")
    public String getKey(@PathVariable("key")String key) {
        String value = stringRedisTemplate.opsForValue().get(key);
        return value;
    }
}

设置值:

读取值:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/922078.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号