Sentinel版是master/slave集群的加强版,sentinel自己是哨兵的意思,常规的master/slave集群中一旦master节点宕机,整个集群也就不能提供服务了。但sentinel版的出现,就是为了解决这个问题,当master宕机的时候,会自动从slave节点中选举新的master节点,以继续工做。java
本实例中采用windows版1master+2slave+3sentinel配置。web
master节点redis配置:redis
port 6379spring
bind 127.0.0.1apache
复制两个redis.conf文件,做为slave节点的配置:windows
redis6380.conf:springboot
port 6380app
bind 127.0.0.1maven
slaveof 127.0.0.1 6379 --指定主节点IP+portspring-boot
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
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);
}
}