Springboot缓存实现(redis_sentinel版实现)

Sentinel含义

Sentinel版是master/slave集群的增强版,sentinel本身是哨兵的意思,常规的master/slave集群中一旦master节点宕机,整个集群也就不能提供服务了。但sentinel版的出现,就是为了解决这个问题,当master宕机的时候,会自动从slave节点中选举新的master节点,以继续工作。

redis_sintinel配置

本实例中采用windows版1master+2slave+3sentinel配置。

master节点redis配置:

port 6379

bind 127.0.0.1

复制两个redis.conf文件,作为slave节点的配置:

redis6380.conf:

port 6380

bind 127.0.0.1

slaveof 127.0.0.1 6379 --指定主节点IP+port

 

redis6381.conf:

port 6381

bind 127.0.0.1

slaveof 127.0.0.1 6379  --指定主节点IP+port

Sentinel节点配置:

在redis文件夹中加入sentinel26379.conf、sentinel26380.conf、sentinel26381.conf三个文件。

配置以sentinel26379.conf为例:

port 26379

#告诉sentinel去监听地址为ip:port的一个master,mymaster指的是master节点的名字,

#可以自定义。2指的是有多少个slave认为master节点失效,才真正认为master以失效。

sentinel monitor mymaster 127.0.0.1 6379 2

#这个配置项指定了需要多少失效时间,一个master才会被这个sentinel主观地认为是不可

#用的。 单位是毫秒,默认为30秒。

sentinel down-after-milliseconds mymaster 30000

#这个配置项指定了在发生failover主备切换时最多可以有多少个slave同时对新的master

#进行 同步,这个数字越小,完成failover所需的时间就越长,但是如果这个数字越大,就

#意味着越 多的slave因为replication而不可用。可以通过将这个值设为 1 来保证每次只有一#个slave 处于不能处理命令请求的状态。

sentinel parallel-syncs mymaster 1

 

sentinel26380.conf、sentinel26381.conf配置与sentinel26379.conf类似。

启动redis:

> redis-server.exe redis.conf

> redis-server.exe redis6380.conf

> redis-server.exe redis6381.conf

启动sentinel:

> redis-server.exe sentinel26379.conf --sentinel

> redis-server.exe sentinel26380.conf --sentinel

> redis-server.exe sentinel26381.conf --sentinel

Springboot+redis(sentinel)实例

Pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.sunzy.springcache</groupId>
    <artifactId>springcache</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

 

RedisSentinelConfig:主要生产RedisTemplate实例,注入到容器。

 

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.util.Arrays;

@Configuration
@EnableCaching
public class RedisSentinelConfig {

    private LettuceConnectionFactory getConncetionFactory() {
        RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration();
        redisSentinelConfiguration.setPassword("");
        redisSentinelConfiguration.setMaster("mymaster");

        RedisNode[] redisNodes = new RedisNode[3];
        redisNodes[0] = new RedisNode("127.0.0.1",26379);
        redisNodes[1] = new RedisNode("127.0.0.1",26380);
        redisNodes[2] = new RedisNode("127.0.0.1",26381);
        redisSentinelConfiguration.setSentinels(Arrays.asList(redisNodes));

        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(redisSentinelConfiguration);
        return lettuceConnectionFactory;
    }

    @Bean
    public RedisTemplate<String ,Object> getRedisTemplate() {
        RedisTemplate<String,Object> redisTemplate = null;
        RedisConnectionFactory redisConnectionFactory = getConncetionFactory();
        if(redisConnectionFactory != null) {
            redisTemplate = new RedisTemplate<>();
            redisTemplate.setConnectionFactory(redisConnectionFactory);
            redisTemplate.setKeySerializer(new StringRedisSerializer());
            redisTemplate.setValueSerializer(new StringRedisSerializer());
            redisTemplate.afterPropertiesSet();
        }

        return redisTemplate;
    }
}

 

CacheController:一个demo使用例子。

 

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.TimeUnit;

@RestController
public class CacheController {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @RequestMapping("/set")
    public void set() {
        redisTemplate.opsForValue().set("a","1");
        // 设置失效时间
        redisTemplate.expire("a",10, TimeUnit.SECONDS);
    }

    @RequestMapping("/get")
    public String get() {
        return redisTemplate.opsForValue().get("a").toString();
    }
}

 

App:启动springboot主类

 

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

转载于:https://my.oschina.net/u/3759047/blog/3025424

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值