redis哨兵集群搭建及spring使用

首页

搭建一主两从集群

  1. 在/root/redis/conf文件夹下新建配置文件redisMaster.conf、redisSlave1.conf、redisSlave2.conf
# redisMaster.conf
port 6382
bind 0.0.0.0
replica-announce-ip 35.10.208.28 #对外暴漏ip,设置为本机ip即可。不设置的话,后期redistemplate链接报错
replica-announce-port 6382


# redisSlave1.conf
port 6383
slaveof 35.10.208.28 6382
replica-announce-ip 35.10.208.28
replica-announce-port 6383


#redisSlave2.conf
port 6384
slaveof 35.10.208.28 6382
replica-announce-ip 35.10.208.28
replica-announce-port 6384
  1. docker命令搭建集群
docker run -itd --privileged=true --name redis-master1 -v /root/redis/conf/:/usr/local/etc/redis -p 6382:6382 redis:latest redis-server /usr/local/etc/redis/redisMaster.conf
docker run -itd --privileged=true --name redis-slave11 -v /root/redis/conf/:/usr/local/etc/redis -p 6383:6383 redis:latest redis-server /usr/local/etc/redis/redisSlave1.conf
docker run -itd --privileged=true --name redis-slave22 -v /root/redis/conf/:/usr/local/etc/redis -p 6384:6384 redis:latest redis-server /usr/local/etc/redis/redisSlave2.conf

搭建哨兵

  1. 增加配置文件sentinel1.conf,sentinel2.conf,sentinel3.conf
# sentinel1.conf
port 16379
sentinel monitor master 35.10.208.28 6383 2
dir "/"
logfile "sentinel1.log"

#sentinel2.conf
port 16380
sentinel monitor master 35.10.208.28 6383 2
dir "/"
logfile "sentinel2.log"


#sentinel3.conf
port 16381
sentinel monitor master 35.10.208.28 6383 2
dir "/"
logfile "sentinel3.log"
  1. docker命令搭建集群
docker run -itd --privileged=true --name redis-sentinel1 -v /root/redis/conf/:/usr/local/etc/redis -p 16379:16379 redis:latest redis-sentinel /usr/local/etc/redis/sentinel1.conf
docker run -itd --privileged=true --name redis-sentinel2 -v /root/redis/conf/:/usr/local/etc/redis -p 16380:16380 redis:latest redis-sentinel /usr/local/etc/redis/sentinel2.conf
docker run -itd --privileged=true --name redis-sentinel3 -v /root/redis/conf/:/usr/local/etc/redis -p 16381:16381 redis:latest redis-sentinel /usr/local/etc/redis/sentinel3.conf

使用springboot连接集群

配置pom文件

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

配置文件

spring:
  redis:
    sentinel:
      master: master
      nodes:
        - 35.10.208.28:16379
        - 35.10.208.28:16380
        - 35.10.208.28:16381

配置读写分离

    @Bean
    public LettuceClientConfigurationBuilderCustomizer clientConfigurationBuilderCustomizer() {
        return clientConfigurationBuilder -> clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
    }

使用redistemplate

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @GetMapping("/get/{key}")
    public String hi(@PathVariable String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @GetMapping("/set/{key}/{value}")
    public String hi(@PathVariable String key, @PathVariable String value) {
        redisTemplate.opsForValue().set(key, value);
        return "success";
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值