redis-sentinel 搭建
記錄java
1.設置主從redis集羣(主從方式),redis sentinel 哨兵的監控,java程序的jedis的鏈接。linux
3臺linux機器 web
主 192.168.102.236 redis
從 192.168.102.245 192.168.102.235spring
2.redis.conf設置----------------------緩存
主:cookie
#設置不是保護模式 //很重要 設置後主從節點交換纔會有效果
protected-mode nosession
#不綁定ip地址
#bind 0.0.0.0 內網訪問app
bind 192.168.102.236測試
port 6379
從:
#設置不是保護模式
protected-mode no
port 6379
#設置監控的主redis
slaveof 192.168.102.236 6379
bind 192.168.102.235 bind 192.168.102.245
3.snetinel.conf 設置 ------------- 主從同樣 3臺機器一塊兒配置
#端口
port 26379
#這是非保護獲取,這樣其餘遠程端就可訪問sentinel
protected-mode no
#設置要監控的master的名字,ip地址,端口,與失去多少sentinel鏈接就切換其餘redis做爲master
sentinel monitor mymaster 192.168.102.236 6379 2
sentinel down-after-milliseconds mymaster 5000
sentinel failover-timeout mymaster 60000
sentinel parallel-syncs mymaster 1
redis-server /root/redis-3.2.9/redis.conf 啓動 236 235 245 3臺機器
redis-sentinel /root/redis-3.2.9/sentinel.conf 啓動 236 235 245 3臺機器
注意 : 設置主從模式後 刪除key只能在master上操做
--基本命令
keys * --查看全部的key
get key
set key value
flushdb 清除全部的key
4.測試sentinel是否搭建成功
192.168.102.245機器 redis-cli -h 192.168.102.245 info Replication
# Replication
role:slave
master_host:192.168.102.236
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:54938
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
若是236redis服務被停掉 隨機選出slave做爲master
192.168.102.245機器 redis-cli -h 192.168.102.245 info Replication
# Replication
role:master
master_host:192.168.102.245
master_port:6379
master_link_status:up
master_last_io_seconds_ago:1
master_sync_in_progress:0
slave_repl_offset:54938
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
主從切換成功 redis裏面全部的keys共享
java鏈接配置
spring.redis.xml配置
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="maxTotal" value="1000" /> <property name="maxIdle" value="50" /> <property name="maxWaitMillis" value="50000" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="sentinelConfiguration" class="org.springframework.data.redis.connection.RedisSentinelConfiguration"> <property name="master"> <bean class="org.springframework.data.redis.connection.RedisNode"> <property name="name" value="mymaster"></property> </bean> </property> <property name="sentinels"> <set> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.102.235"></constructor-arg> <constructor-arg name="port" value="26379"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.102.236"></constructor-arg> <constructor-arg name="port" value="26379"></constructor-arg> </bean> <bean class="org.springframework.data.redis.connection.RedisNode"> <constructor-arg name="host" value="192.168.102.237"></constructor-arg> <constructor-arg name="port" value="26379"></constructor-arg> </bean> </set> </property> </bean> <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"> <property name="poolConfig" > <ref bean="jedisPoolConfig"/> </property> <constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg> </bean> <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer" ref="stringRedisSerializer" /> <property name="hashKeySerializer" ref="stringRedisSerializer" /> </bean> <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"> <property name="connectionFactory" ref="jedisConnectionFactory" /> <property name="keySerializer" ref="stringRedisSerializer" /> <property name="valueSerializer" ref="genericToStringSerializer" /> <property name="hashKeySerializer" ref="genericToStringSerializer" /> </bean> <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" /> <bean id="genericToStringSerializer" class="org.springframework.data.redis.serializer.GenericToStringSerializer"> <constructor-arg type="java.lang.Class" value="java.lang.Long"></constructor-arg> </bean>
java配置
package com.bingkun.weixin.cache; import com.bingkun.weixin.common.utils.PropertiesUtil; import com.bingkun.weixin.constants.ApolloConstants; import com.ctrip.framework.apollo.Config; import com.ctrip.framework.apollo.ConfigService; import org.springframework.cache.CacheManager; import org.springframework.cache.annotation.CachingConfigurerSupport; import org.springframework.cache.annotation.EnableCaching; 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.RedisConnectionFactory; 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.GenericToStringSerializer; import org.springframework.data.redis.serializer.StringRedisSerializer; import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession; import org.springframework.session.web.http.CookieHttpSessionStrategy; import org.springframework.session.web.http.CookieSerializer; import org.springframework.session.web.http.DefaultCookieSerializer; import org.weakref.jmx.internal.guava.collect.Sets; import redis.clients.jedis.JedisPoolConfig; import java.util.*; /** * Created by chenxiaobian on 2017/6/6. */ @Configuration @EnableCaching @EnableRedisHttpSession public class SpringRedisConfig extends CachingConfigurerSupport { @Bean public CookieSerializer cookieSerializer() { DefaultCookieSerializer defaultCookieSerializer = new DefaultCookieSerializer(); defaultCookieSerializer.setCookieName("adminSessionId"); // defaultCookieSerializer.setCookiePath("/management"); return defaultCookieSerializer; } @Bean public CookieHttpSessionStrategy cookieHttpSessionStrategy(CookieSerializer cookieSerializer) { CookieHttpSessionStrategy cookieHttpSessionStrategy = new CookieHttpSessionStrategy(); cookieHttpSessionStrategy.setCookieSerializer(cookieSerializer); return cookieHttpSessionStrategy; } // @Bean // public JedisPoolConfig jedisPoolConfig() { // JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); // jedisPoolConfig.setMaxTotal(1000); // jedisPoolConfig.setMaxIdle(50); // jedisPoolConfig.setMaxWaitMillis(50000); // jedisPoolConfig.setTestOnBorrow(true); // return jedisPoolConfig; // } // // @Bean // public RedisSentinelConfiguration sentinelConfiguration() { // // RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration(); // // sentinelConfiguration.setMaster("mymaster"); // // sentinelConfiguration.setSentinels(getRedisNodeSet()); // // return sentinelConfiguration; // } // // public Set<RedisNode> getRedisNodeSet() { // Config config = ConfigService.getConfig(ApolloConstants.REDIS_CONFIG_COMMON); // // String redisIp1 = config.getProperty(ApolloConstants.SENTINEL_REDIS_ONE_IP, ""); // Integer redisPort1 = config.getIntProperty(ApolloConstants.SENTINEL_REDIS_ONE_PORT, 26379); // // String redisIp2 = config.getProperty(ApolloConstants.SENTINEL_REDIS_TWO_IP, ""); // Integer redisPort2 = config.getIntProperty(ApolloConstants.SENTINEL_REDIS_TWO_PORT, 26379); // // String redisIp3 = config.getProperty(ApolloConstants.SENTINEL_REDIS_THREE_IP, ""); // Integer redisPort3 = config.getIntProperty(ApolloConstants.SENTINEL_REDIS_THREE_PORT, 26379); // // Set<RedisNode> redisNodeSet = Sets.newHashSet(); // RedisNode redisNode1 = new RedisNode(redisIp1, redisPort1); // RedisNode redisNode2 = new RedisNode(redisIp2, redisPort2); // RedisNode redisNode3 = new RedisNode(redisIp3, redisPort3); // redisNodeSet.add(redisNode1); // redisNodeSet.add(redisNode2); // redisNodeSet.add(redisNode3); // return redisNodeSet; // } // // // @Bean // public JedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig, RedisSentinelConfiguration sentinelConfiguration) { // JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(sentinelConfiguration); // redisConnectionFactory.setPoolConfig(jedisPoolConfig); // return redisConnectionFactory; // } @Bean public JedisConnectionFactory redisConnectionFactory() { JedisConnectionFactory redisConnectionFactory = new JedisConnectionFactory(); redisConnectionFactory.setHostName(getRedisUrl()); redisConnectionFactory.setPort(Integer.parseInt(getRedisPort())); redisConnectionFactory.setPassword(getRedisPassword()); return redisConnectionFactory; } @Bean public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory cf) { RedisTemplate<String, String> redisTemplate = new RedisTemplate<String, String>(); redisTemplate.setConnectionFactory(cf); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); return redisTemplate; } @Bean public RedisTemplate<String, Long> longRedisTemplate(RedisConnectionFactory cf) { final RedisTemplate<String, Long> template = new RedisTemplate<String, Long>(); template.setConnectionFactory(cf); template.setKeySerializer(new StringRedisSerializer()); template.setHashValueSerializer(new GenericToStringSerializer<Long>(Long.class)); template.setValueSerializer(new GenericToStringSerializer<Long>(Long.class)); return template; } // @Bean // public RedissonClient redissonClient(){ // org.redisson.config.Config config = new org.redisson.config.Config(); // String format = "redis://%s:%s"; // String redisAddress = String.format(format, getRedisUrl(), getRedisPort()); // String password = StringUtils.isEmpty(getRedisPassword())? null : getRedisPassword(); // config.useSingleServer() // .setAddress(redisAddress) // .setPassword(password); // return Redisson.create(config); // } @Bean public CacheManager cacheManager(RedisTemplate<String, String> redisTemplate) { RedisCacheManager cacheManager = new RedisCacheManager(redisTemplate); // 啓用前綴後,生成的key會自動加上cache名稱,至關於命名空間。最終的緩存key爲cachename:key的方式。 cacheManager.setUsePrefix(true); ArrayList<String> cacheNames = new ArrayList<String>(); cacheNames.add("campInfo"); cacheNames.add(ConfigService.getAppConfig().getProperty("DUPLICATE_REMOVAL", "")); cacheManager.setCacheNames(cacheNames); Map<String, Long> cacheExp = new HashMap<String, Long>(); cacheExp.put("campInfo", new Long(172800)); cacheExp.put(ConfigService.getAppConfig().getProperty("DUPLICATE_REMOVAL", ""), new Long(60)); cacheManager.setExpires(cacheExp); // // 設置默認失效時長爲7200秒 // cacheManager.setDefaultExpiration(7200); return cacheManager; } protected String getRedisUrl() { Config appConfig = ConfigService.getConfig("TEST1.RedisConfig"); String redisUrl = appConfig.getProperty("adminredis.ip", ""); return redisUrl; } protected String getRedisPort() { Config appConfig = ConfigService.getConfig("TEST1.RedisConfig"); String redisPort = appConfig.getProperty("adminredis.port", ""); return redisPort; } protected String getRedisPassword() { Properties properties = PropertiesUtil.getProperties("properties/redis.properties"); Config config = ConfigService.getConfig("TEST1.Common"); String env = config.getProperty("env", ""); String password = properties.getProperty(env + ".redis.password", ""); return password; } }