docker redis 高可用哨兵+主从

docker redis 高可用

废话不多说,直接上yml文件

version: "3.2"
services:
#服务名
  redis1:
#镜像名称
    image: redis:6.0.9
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
        delay: 60s
      placement:
        constraints:
#指定docker 节点
          - node.role == manager
#开放端口
    ports:
      - "36379:6379"
#追加命令
    command: redis-server --timeout 0 --tcp-keepalive 60 --bind 0.0.0.0 --slave-announce-ip 192.168.1.32 --slave-announce-port 36379 --requirepass password@123 --masterauth password@123 --masterauth password@123

  redis2:
    image: redis:6.0.9
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
        delay: 60s
      placement:
        constraints:
          - node.role == manager    
    ports:
      - "36380:6379"
    command: redis-server --timeout 0 --tcp-keepalive 60 --bind 0.0.0.0 --slave-announce-ip 192.168.1.32 --slave-announce-port 36380 --slaveof 192.168.1.32 36379 --requirepass password@123 --masterauth password@123

  redis3:
    image: redis:6.0.9
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
        delay: 60s
      placement:
        constraints:
          - node.role == manager    
    ports:
      - "36381:6379"
    command: redis-server --timeout 0 --tcp-keepalive 60 --bind 0.0.0.0 --slave-announce-ip 192.168.1.32 --slave-announce-port 36381 --slaveof 192.168.1.32 36379  --requirepass password@123 --masterauth password@123
    

  sentinel1:
    image: redis:6.0.9
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
      placement:
        constraints:
          - node.role == manager    
    ports:
      - "36479:26379"
    command: /bin/bash -c "touch /data/redis_sentinel.conf && redis-server /data/redis_sentinel.conf --sentinel monitor mysentinel 192.168.1.32 36379 2 --sentinel down-after-milliseconds mysentinel 5000 --sentinel parallel-syncs mysentinel 1 --sentinel failover-timeout mysentinel 180000 --sentinel announce-ip 192.168.1.32 --sentinel announce-port 36479 --sentinel auth-pass mysentinel password@123"

  sentinel2:
    image: redis:6.0.9
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
      placement:
        constraints:
          - node.role == manager                
    ports:
      - "36480:26379"
    command: /bin/bash -c "touch /data/redis_sentinel.conf && redis-server /data/redis_sentinel.conf --sentinel monitor mysentinel 192.168.1.32 36379 2 --sentinel down-after-milliseconds mysentinel 5000 --sentinel parallel-syncs mysentinel 1 --sentinel failover-timeout mysentinel 180000 --sentinel announce-ip 192.168.1.32 --sentinel announce-port 36480 --sentinel auth-pass mysentinel password@123"

  sentinel3:
    image: redis:6.0.9
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
      placement:
        constraints:
          - node.role == manager                
    ports:
      - "36481:26379"
    command: /bin/bash -c "touch /data/redis_sentinel.conf && redis-server /data/redis_sentinel.conf --sentinel monitor mysentinel 192.168.1.32 36379 2 --sentinel down-after-milliseconds mysentinel 5000 --sentinel parallel-syncs mysentinel 1 --sentinel failover-timeout mysentinel 180000 --sentinel announce-ip 192.168.1.32 --sentinel announce-port 36481 --sentinel auth-pass mysentinel password@123"

如果docker 不是集群模式 执行

docker swarm init  
docker stack deploy --compose-file=xxx.yml redis-sentinel

命令详解

--当客户端闲置多长时间后关闭连接,如果指定为0,表示关闭该功能
timeout 0 
--保活检测,设置为0不检测,默认为300 
tcp-keepalive 60 
--绑定的主机地址
bind 0.0.0.0 
--解决docker 部署可能存在的:由于端口或IP映射导致的无法连接的问题
slave-announce-ip 192.168.1.32
slave-announce-port 36379
-- 设置密码
requirepass password@123 
--当master服务设置了密码保护时,slav服务连接master的密码
masterauth password@123 
--设置当本机为slav服务时,设置master服务的IP地址及端口,在Redis启动时,它会自动从master进行数据同步
slaveof 192.168.1.32 36379 
--监控的IP 端口号 名称 sentinel通过投票后认为mater宕机的数量,此处为至少2个
sentinel monitor mysentinel 192.168.1.32 36379 2

--解决docker 部署可能存在的:由于端口或IP映射导致的无法连接的问题
sentinel announce-ip 192.168.1.32
sentinel announce-port 36481
--如果redis有密码需要指定密码
sentinel auth-pass mysentinel password@123
--30秒ping不通主节点的信息,主观认为master宕机
sentinel down-after-milliseconds mymaster 30000
--故障转移后重新主从复制,1表示串行,>1并行
sentinel parallel-syncs mymaster 1
--故障转移开始,三分钟内没有完成,则认为转移失败
sentinel failover-timeout mymaster 180000

哨兵版

java springboot配置,我这里使用的是1.x版本

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

import java.util.Set;

/**
 * redis缓存集群哨兵模式配置
 *
 * @author luozy date 2019/7/9
 */
@Data
@Configuration
@PropertySource(value = {"classpath:application-redis.properties"})
@ConfigurationProperties(prefix = "spring.redis-dic")
public class DictRedisSentinelConfig {

    //   @Autowired
    //  private RedisParam redisParam;

    private int dbIndex;

    private String password;

    private int timeout;

    private Long expire;

    private String sentinelMaster;

    private Set<String> sentinelNodes;

    private int maxIdle;

    private int minIdle;

    private int maxActive;

    private int maxWait;

    private boolean testOnBorrow;


    /**
     * 配置redis连接工厂
     *
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "spring.redis-dic.sentinelMasterEnabled", matchIfMissing = false, havingValue = "true")
    public RedisConnectionFactory dictRedisConnectionFactory() {

        //哨兵配置
        RedisSentinelConfiguration configuration = new RedisSentinelConfiguration(sentinelMaster, sentinelNodes);

        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(configuration);
        jedisConnectionFactory.setDatabase(dbIndex);
        jedisConnectionFactory.setPassword(password);
        jedisConnectionFactory.setTimeout(timeout);
        jedisConnectionFactory.setPoolConfig(setPoolConfig(maxIdle, minIdle, maxActive, maxWait, testOnBorrow));
        return jedisConnectionFactory;
    }

    /**
     * 设置连接池属性
     *
     * @param maxIdle
     * @param minIdle
     * @param maxActive
     * @param maxWait
     * @param testOnBorrow
     * @return
     */
    public JedisPoolConfig setPoolConfig(int maxIdle, int minIdle, int maxActive, int maxWait, boolean testOnBorrow) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxWaitMillis(maxWait);
        poolConfig.setTestOnBorrow(testOnBorrow);
        //  poolConfig.setTestOnReturn(true);
        return poolConfig;
    }

    /**
     * 配置redisTemplate 注入方式使用@Resource(name="dictRedisTemplate") 方式注入
     *
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "spring.redis-dic.sentinelMasterEnabled", matchIfMissing = false, havingValue = "true")
    public RedisTemplate<String, Object> dictRedisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(dictRedisConnectionFactory());
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(
                Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // 设置键(key)的序列化方式
        template.setKeySerializer(new StringRedisSerializer());
        // 设置值(value)的序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    @ConditionalOnProperty(name = "spring.redis-dic.sentinelMasterEnabled", matchIfMissing = false, havingValue = "true")
    CacheManager dicCacheManager() {
        RedisCacheManager redisCacheManager = new RedisCacheManager(dictRedisTemplate());
        redisCacheManager.setUsePrefix(true);
        redisCacheManager.setDefaultExpiration(expire);
        return redisCacheManager;
    }

    @Bean
    @ConditionalOnProperty(name = "spring.redis-dic.sentinelMasterEnabled", matchIfMissing = false, havingValue = "true")
    public RedisMessageListenerContainer redisMessageListenerContainer() {

        RedisMessageListenerContainer container = new RedisMessageListenerContainer();

        container.setConnectionFactory(dictRedisConnectionFactory());

        return container;

    }

}

单节点版

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;

/**
 * 枚举字典redis缓存单节点配置
 *
 * @author luozy
 * date 2019/7/9
 */
@Data
@Configuration
@PropertySource(value = {"classpath:application-redis.properties"})
@ConfigurationProperties(prefix = "spring.redis-dic")
public class RedisConfig {


    private int dbIndex;

    private String host;

    private int port;

    private String password;

    private int timeout;

    private Long expire;

    private int maxIdle;

    private int minIdle;

    private int maxActive;

    private int maxWait;

    /**
     * 配置redis连接工厂
     *
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "spring.redis-dic.sentinelMasterEnabled", matchIfMissing = true, havingValue = "false")
    public RedisConnectionFactory dictRedisConnectionFactory() {
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setDatabase(dbIndex);
        jedisConnectionFactory.setHostName(host);
        jedisConnectionFactory.setPort(port);
        jedisConnectionFactory.setPassword(password);
        jedisConnectionFactory.setTimeout(timeout);
        jedisConnectionFactory.setPoolConfig(setPoolConfig(maxIdle, minIdle, maxActive, maxWait, true));
        return jedisConnectionFactory;
    }


    /**
     * 设置连接池属性
     *
     * @param maxIdle
     * @param minIdle
     * @param maxActive
     * @param maxWait
     * @param testOnBorrow
     * @return
     */
    public JedisPoolConfig setPoolConfig(int maxIdle, int minIdle, int maxActive, int maxWait, boolean testOnBorrow) {
        JedisPoolConfig poolConfig = new JedisPoolConfig();
        poolConfig.setMaxIdle(maxIdle);
        poolConfig.setMinIdle(minIdle);
        poolConfig.setMaxTotal(maxActive);
        poolConfig.setMaxWaitMillis(maxWait);
        poolConfig.setTestOnBorrow(testOnBorrow);
        return poolConfig;
    }

    /**
     * 配置redisTemplate 注入方式使用@Resource(name="") 方式注入
     *
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "spring.redis-dic.sentinelMasterEnabled", matchIfMissing = true, havingValue = "false")
    public RedisTemplate<String, Object> dictRedisTemplate() {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(dictRedisConnectionFactory());
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        //设置键(key)的序列化方式
        template.setKeySerializer(new StringRedisSerializer());
        //设置值(value)的序列化方式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    @ConditionalOnProperty(name = "spring.redis-dic.sentinelMasterEnabled", matchIfMissing = true, havingValue = "false")
    CacheManager dicCacheManager() {
        RedisCacheManager redisCacheManager = new RedisCacheManager(dictRedisTemplate());
        redisCacheManager.setUsePrefix(true);
        redisCacheManager.setDefaultExpiration(expire);
        return redisCacheManager;
    }

    @Bean
    @ConditionalOnProperty(name = "spring.redis-dic.sentinelMasterEnabled", matchIfMissing = true, havingValue = "false")
    public RedisMessageListenerContainer redisMessageListenerContainer() {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(dictRedisConnectionFactory());
        return container;
    }

}

配置文件

#\u679A\u4E3E\u7F13\u5B58\u914D\u7F6E
spring.cache.type=redis
spring.redis-dic.host=15.22.68.89
spring.redis-dic.port=6379
#
spring.redis-dic.sentinelMasterEnabled=false
spring.redis-dic.sentinelMaster=mysentinel
spring.redis-dic.sentinelNodes=15.22.68.89:36479,15.22.68.89:36480,15.22.68.89:36481
spring.redis-dic.password=password@123
spring.redis-dic.timeout=0
spring.redis-dic.dbIndex=11
spring.redis-dic.expire=300
spring.redis-dic.maxIdle=-1
spring.redis-dic.minIdle=-1
spring.redis-dic.maxActive=100
spring.redis-dic.maxWait=100
spring.redis-dic.testOnBorrow=true
spring.data.redis.repositories.enabled=false

springboot cache+redis

启动类增加
@EnableCaching

 @Cacheable(value = "test", condition = "#id !=null", key = "#id", unless = "#result == null")
    public String test(String id) {
         System.out.println("。。。。");
        return deviceRepository.findByCode(code);
    }

pom

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,427评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,551评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 165,747评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,939评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,955评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,737评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,448评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,352评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,834评论 1 317
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,992评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,133评论 1 351
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,815评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,477评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,022评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,147评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,398评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,077评论 2 355

推荐阅读更多精彩内容