windows 下redis配置一主两从三哨兵模式以及springboot集成

配置主要分为三部分,主节点的配置,从节点的配置,以及三个哨兵的配置,话不多说,说一下主节点的配置:

master节点主要配置bind  0.0.0.0  port 6379 和允许后台模式运行的方式 和dbfilename "dump6380.rdb"

从节点的配置主要比主节点的配置多了slaveof 192.168.1.3 6379

接下来说哨兵的配置文件

port 26379
#master
sentinel myid a9708ea9aa43b79f38ddf8a9b6db9c327e3ec0e4
sentinel monitor master 192.168.1.3 6380 2
sentinel down-after-milliseconds master 5000
sentinel config-epoch master 5
protected-mode no
bind 0.0.0.0

先启动主节点,再启动从节点,再启动哨兵

接下来说springboot如何配置

1.yaml的配置

redis:
database: 0
host: 127.0.0.1
port: 6379
password:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
timeout: 0
sentinel:
master: master
nodes: 127.0.0.1:26380,127.0.0.1:26381

2配置文件的配置

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>

3.配置文件的配置

package com.w3cz;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.connection.RedisNode;
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.serializer.StringRedisSerializer;

import java.lang.reflect.Method;

/**
* @author zhangzhiqin
*/
@Configuration
@EnableAutoConfiguration
@EnableCaching //加上这个注解是的支持缓存注解
public class RedisCacheConfig extends CachingConfigurerSupport {

@Value("${redis.host}")
private String host;

@Value("${redis.port}")
private int port;

@Value("${redis.timeout}")
private int timeout = 0;

@Value("${redis.database}")
private int database;

@Value("${redis.password}")
private String password;

@Value("${redis.sentinel.nodes}")
private String redisNodes;

@Value("${redis.sentinel.master}")
private String master;


/**
* redis哨兵配置
* @return
*/
@Bean
public RedisSentinelConfiguration redisSentinelConfiguration(){
RedisSentinelConfiguration configuration = new RedisSentinelConfiguration();
String[] host = redisNodes.split(",");
for(String redisHost : host){
String[] item = redisHost.split(":");
String ip = item[0];
String port = item[1];
configuration.addSentinel(new RedisNode(ip, Integer.parseInt(port)));
}
configuration.setMaster(master);
return configuration;
}

/**
* 连接redis的工厂类
*
* @return
*/
@Bean
public JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory(redisSentinelConfiguration());
factory.setHostName(host);
factory.setPort(port);
factory.setTimeout(timeout);
factory.setPassword(password);
factory.setDatabase(database);
return factory;
}

/**
* 配置RedisTemplate
* 设置添加序列化器
* key 使用string序列化器
* value 使用Json序列化器
* 还有一种简答的设置方式,改变defaultSerializer对象的实现。
*
* @return
*/
@Bean
public RedisTemplate<Object, Object> redisTemplate() {
//StringRedisTemplate的构造方法中默认设置了stringSerializer
RedisTemplate<Object, Object> template = new RedisTemplate<>();
//设置开启事务
template.setEnableTransactionSupport(true);
//set key serializer
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);

template.setConnectionFactory(jedisConnectionFactory());
template.afterPropertiesSet();
return template;
}

 

/**
* 自定义生成redis-key
*
* @return
*/
@Override
public KeyGenerator keyGenerator() {
return new KeyGenerator() {
@Override
public Object generate(Object o, Method method, Object... objects) {
StringBuilder sb = new StringBuilder();
sb.append(o.getClass().getName()).append(".");
sb.append(method.getName()).append(".");
for (Object obj : objects) {
sb.append(obj.toString());
}
System.out.println("keyGenerator=" + sb.toString());
return sb.toString();
}
};
}
}

 

posted @ 2021-02-09 12:33  逆水乘舟,不进则退  阅读(465)  评论(0编辑  收藏  举报