Redis单机数据迁移至Sentinel集群
Posted 秋水_cnblogs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Redis单机数据迁移至Sentinel集群相关的知识,希望对你有一定的参考价值。
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>com.asdc</groupId> 7 <artifactId>multiRedisDatabase</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>multiRedisDatabase</name> 12 <description>Demo project for Spring Boot</description> 13 14 <parent> 15 <groupId>org.springframework.boot</groupId> 16 <artifactId>spring-boot-starter-parent</artifactId> 17 <version>1.5.16.RELEASE</version> 18 <relativePath /> <!-- lookup parent from repository --> 19 </parent> 20 21 <properties> 22 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 23 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 24 <java.version>1.8</java.version> 25 </properties> 26 27 <dependencies> 28 <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> 29 <dependency> 30 <groupId>com.alibaba</groupId> 31 <artifactId>fastjson</artifactId> 32 <version>1.2.47</version> 33 </dependency> 34 35 <dependency> 36 <groupId>org.springframework.boot</groupId> 37 <artifactId>spring-boot-starter-data-redis</artifactId> 38 </dependency> 39 <dependency> 40 <groupId>org.springframework.boot</groupId> 41 <artifactId>spring-boot-starter-web</artifactId> 42 </dependency> 43 44 <dependency> 45 <groupId>org.springframework.boot</groupId> 46 <artifactId>spring-boot-starter-test</artifactId> 47 <scope>test</scope> 48 </dependency> 49 </dependencies> 50 51 <build> 52 <plugins> 53 <plugin> 54 <groupId>org.springframework.boot</groupId> 55 <artifactId>spring-boot-maven-plugin</artifactId> 56 </plugin> 57 </plugins> 58 </build> 59 60 61 </project>
application.properties
# Redis数据库索引(默认为0) spring.redis.database=10 # Redis服务器地址 spring.redis.hostName=127.0.0.1 # Redis服务器连接端口 spring.redis.port=6379 # Redis服务器连接密码(默认为空) spring.redis.password= # 连接池最大连接数(使用负值表示没有限制) spring.redis.pool.max-active=1024 # 连接池最大阻塞等待时间(使用负值表示没有限制) spring.redis.pool.max-wait=-1 # 连接池中的最大空闲连接 spring.redis.pool.max-idle=200 # 连接池中的最小空闲连接 spring.redis.pool.min-idle=0 # 连接超时时间(毫秒) spring.redis.timeout=15000 spring.redis.sentinel.master=mymaster spring.redis.sentinel.nodes=127.0.0.1,127.0.0.1,127.0.0.1 spring.redis.sentinel.port=26379
1 package com.asdc.multiRedisDatabase.config; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 import org.springframework.stereotype.Component; 5 6 /** 7 * @Description: Redis哨兵配置实体类 8 * @author: Jennifer 9 * @date: 2018年9月27日 上午10:10:34 10 */ 11 @Component 12 @ConfigurationProperties(prefix = "spring.redis.sentinel") 13 public class RedisSentinelProperties { 14 private String master; 15 private String nodes; 16 private Integer port; 17 18 public String getMaster() { 19 return master; 20 } 21 22 public void setMaster(String master) { 23 this.master = master; 24 } 25 26 public String getNodes() { 27 return nodes; 28 } 29 30 public void setNodes(String nodes) { 31 this.nodes = nodes; 32 } 33 34 @Override 35 public String toString() { 36 return "RedisProperties [master=" + master + ", nodes=" + nodes + ", port=" + port + "]"; 37 } 38 39 public void setPort(Integer port) { 40 this.port = port; 41 } 42 43 public Integer getPort() { 44 return port; 45 } 46 47 }
1 package com.asdc.multiRedisDatabase.config; 2 3 import org.springframework.beans.factory.annotation.Value; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.Primary; 8 import org.springframework.data.redis.connection.RedisConnectionFactory; 9 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 10 import org.springframework.data.redis.core.RedisTemplate; 11 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 12 import org.springframework.data.redis.serializer.RedisSerializer; 13 import org.springframework.data.redis.serializer.StringRedisSerializer; 14 15 import com.fasterxml.jackson.annotation.JsonAutoDetect; 16 import com.fasterxml.jackson.annotation.PropertyAccessor; 17 import com.fasterxml.jackson.databind.ObjectMapper; 18 19 import redis.clients.jedis.JedisPoolConfig; 20 21 @Configuration 22 @EnableAutoConfiguration 23 public class RedisAloneConfig { 24 25 @Bean(name = "aloneStringRedisTemplate") 26 @Primary 27 public RedisTemplate<String, Object> redisTemplate(@Value("${spring.redis.hostName}") String hostName, 28 @Value("${spring.redis.port}") int port, @Value("${spring.redis.password}") String password, 29 @Value("${spring.redis.pool.max-idle}") int maxIdle, @Value("${spring.redis.pool.max-active}") int maxTotal, 30 @Value("${spring.redis.database}") int index, @Value("${spring.redis.pool.max-wait}") long maxWaitMillis) { 31 // 设置序列化 32 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>( 33 Object.class); 34 ObjectMapper om = new ObjectMapper(); 35 om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 36 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 37 jackson2JsonRedisSerializer.setObjectMapper(om); 38 39 RedisTemplate<String, Object> temple = new RedisTemplate<String, Object>(); 40 temple.setConnectionFactory( 41 this.connectionFactory(hostName, port, password, maxIdle, maxTotal, index, maxWaitMillis)); 42 RedisSerializer<?> stringSerializer = new StringRedisSerializer(); 43 temple.setKeySerializer(stringSerializer);// key序列化 44 temple.setValueSerializer(jackson2JsonRedisSerializer);// value序列化 45 temple.setHashKeySerializer(stringSerializer);// Hash key序列化 46 temple.setHashValueSerializer(jackson2JsonRedisSerializer);// Hash value序列化 47 return temple; 48 } 49 50 public RedisConnectionFactory connectionFactory(String hostName, int port, String password, int maxIdle, 51 int maxTotal, int index, long maxWaitMillis) { 52 JedisConnectionFactory jedis = new JedisConnectionFactory(); 53 jedis.setHostName(hostName); 54 jedis.setPort(port); 55 jedis.setPassword(password); 56 jedis.setDatabase(index); 57 jedis.setPoolConfig(this.poolCofig(maxIdle, maxTotal, maxWaitMillis)); 58 // 初始化连接pool 59 jedis.afterPropertiesSet(); 60 RedisConnectionFactory factory = jedis; 61 return factory; 62 } 63 64 public JedisPoolConfig poolCofig(int maxIdle, int maxTotal, long maxWaitMillis) { 65 JedisPoolConfig poolCofig = new JedisPoolConfig(); 66 poolCofig.setMaxIdle(maxIdle); 67 poolCofig.setMaxTotal(maxTotal); 68 poolCofig.setMaxWaitMillis(maxWaitMillis); 69 poolCofig.setTestOnBorrow(false); 70 return poolCofig; 71 } 72 73 }
1 package com.asdc.multiRedisDatabase.config; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 5 import org.springframework.context.annotation.Bean; 6 import org.springframework.context.annotation.Configuration; 7 import org.springframework.context.annotation.Primary; 8 import org.springframework.data.redis.connection.RedisConnectionFactory; 9 import org.springframework.data.redis.connection.RedisSentinelConfiguration; 10 import org.springframework.data.redis.connection.jedis.JedisConnectionFactory; 11 import org.springframework.data.redis.core.RedisTemplate; 12 import org.springframework.data.redis.core.StringRedisTemplate; 13 import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; 14 import org.springframework.data.redis.serializer.RedisSerializer; 15 import org.springframework.data.redis.serializer.StringRedisSerializer; 16 17 import com.fasterxml.jackson.annotation.JsonAutoDetect; 18 import com.fasterxml.jackson.annotation.PropertyAccessor; 19 import com.fasterxml.jackson.databind.ObjectMapper; 20 21 import redis.clients.jedis.JedisPoolConfig; 22 23 @Configuration 24 @EnableAutoConfiguration 25 public class RedisSentinelConfig { 26 27 @Autowired 28 private RedisSentinelProperties properties; 29 30 @Bean(name = "firstRedisTemplate") 31 @Primary 32 public RedisTemplate<Object, Object> redisTemplate() { 33 return getRedisTemplate(getConnectionFactory()); 34 } 35 36 @Bean(name = "firstStringRedisTemplate") 37 @Primary 38 public StringRedisTemplate stringRedisTemplate() { 39 return getStringRedisTemplate(getConnectionFactory()); 40 } 41 42 private RedisTemplate<Object, Object> getRedisTemplate(RedisConnectionFactory connectionFactory) { 43 RedisTemplate<Object, Object> template = new RedisTemplate<Object, Object>(); 44 template.setConnectionFactory(connectionFactory); 45 setSerializer(template); 46 template.afterPropertiesSet(); 47 return template; 48 } 49 50 private void setSerializer(RedisTemplate<Object, Object> template) { 51 Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>( 52 Object.class); 53 ObjectMapper om = new ObjectMapper(); 54 om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); 55 om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); 56 jackson2JsonRedisSerializer.setObjectMapper(om); 57 template.setKeySerializer(jackson2JsonRedisSerializer); 58 template.setValueSerializer(jackson2JsonRedisSerializer); 59 template.setHashValueSerializer(jackson2JsonRedisSerializer); 60 } 61 62 private StringRedisTemplate getStringRedisTemplate(RedisConnectionFactory connectionFactory) { 63 StringRedisTemplate template = new StringRedisTemplate(); 64 template.setConnectionFactory(connectionFactory); 65 setStringSerializer(template); 66 template.afterPropertiesSet(); 67 return template; 68 } 69 70 private void setStringSerializer(StringRedisTemplate template) { 71 RedisSerializer<String> stringSerializer = new StringRedisSerializer(); 72 template.setKeySerializer(stringSerializer); 73 template.setValueSerializer(stringSerializer); 74 template.setHashKeySerializer(stringSerializer); 75 template.setHashValueSerializer(stringSerializer); 76 template.afterPropertiesSet(); 77 } 78 79 @Bean 80 public JedisConnectionFactory getConnectionFactory() { 81 JedisPoolConfig config = new JedisPoolConfig(); 82 JedisConnectionFactory factory = new JedisConnectionFactory(getRedisSentinelConfig(), config); 83 factory.setPoolConfig(config); 84 return factory; 85 } 86 87 @Bean 88 public RedisSentinelConfiguration getRedisSentinelConfig() { 89 RedisSentinelConfiguration sentinelConfiguration = new RedisSentinelConfiguration(); 90 sentinelConfiguration.setMaster(properties.getMaster()); 91 String sentinelHost = properties.getNodes(); 92 Integer sentinelPort = properties.getPort(); 93 String[] hosts = sentinelHost.split(","); 94 for (int i = 0; i < hosts.length; i++) { 95 sentinelConfiguration.sentinel(hosts[i], sentinelPort); 96 } 97 return sentinelConfiguration; 98 } 99 100 }
1 package com.asdc.multiRedisDatabase.service; 2 3 import java.util.List; 4 import java.util.Map; 5 import java.util.Set; 6 import java.util.concurrent.TimeUnit; 7 8 import javax.annotation.PostConstruct; 9 import javax.annotation.Resource; 10 11 import org.springframework.dao.DataAccessException; 12 import org.springframework.data.redis.connection.RedisConnection; 13 import org.springframework.data.redis.core.HashOperations; 14 import org.springframework.data.redis.core.ListOperations; 15 import org.springframework.data.redis.core.RedisCallback; 16 import org.springframework.data.redis.core.RedisTemplate; 17 import org.springframework.data.redis.core.StringRedisTemplate; 18 import org.springframework.data.redis.core.ValueOperations; 19 import org.springframework.data.redis.core.ZSetOperations; 20 import org.springframework.data.redis.serializer.RedisSerializer; 21 import org.springframework.data.redis.serializer.StringRedisSerializer; 22 import org.springframework.stereotype.Component; 23 24 @Component 25 @SuppressWarnings({ "unchecked", "rawtypes" }) 26 public class RedisService { 27 28 @Resource(name = "firstStringRedisTemplate") 29 private StringRedisTemplate stringRedisTemplate; 30 31 @Resource(name = "firstStringRedisTemplate") 32 private ValueOperations<String, String> stringRedisOperations; 33 34 @Resource(name = "firstRedisTemplate") 35 private RedisTemplate redisTemplate; 36 37 @Resource(name = "firstRedisTemplate") 38 private ValueOperations<String, String> redisOperations; 39 40 @Resource(name = "firstStringRedisTemplate") 41 private HashOperations<String, String, String> hMapOps; 42 43 @Resource(name = "firstStringRedisTemplate") 44 ZSetOperations<String, String> zSetOps; 45 46 @Resource(name = "firstRedisTemplate") 47 ListOperations<String, Object> listOps; 48 49 @Resource(name = "aloneStringRedisTemplate") 50 private RedisTemplate<String, Object> aloneRedisTemplate; 51 52 public RedisTemplate<String, Object> getTemplate() { 53 return aloneRedisTemplate; 54 } 55 56 /** 57 * StringRedisTemplate<br> 58 * 放入redis,使用默认的过期时间<br> 59 * 60 * @param key 61 * @param value 62 */ 63 public void setStringValue(final String key, String value) { 64 stringRedisOperations.set(key, value); 65 } 66 67 /** 68 * StringRedisTemplate<br> 69 * 从redis取出value 70 * 71 * @param key 72 * @return 73 */ 74 public String getStringValue(final String key) { 75 return stringRedisOperations.get(key); 76 } 77 78 /** 79 * StringRedisTemplate<br> 80 * 放入redis,并设置过期时间(分钟) 81 * 82 * @param key 83 * @param value 84 * @param expireTime 85 */ 86 public void setStringValue(final String key, String value, Long expireTime) { 87 stringRedisOperations.set(key, value); 88 stringRedisTemplate.expire(key, expireTime, TimeUnit.MINUTES); 89 } 90 91 /** 92 * StringRedisTemplate<br> 93 * 判断是否存在key 94 * 95 * @param key 96 * @return 97 */ 98 public boolean hasKey(final String key) { 99 return stringRedisTemplate.hasKey(key); 100 } 101 102 /** 103 * StringRedisTemplate<br> 104 * 删除相应的value 105 * 106 * @param key 107 */ 108 public void remove(final String key) { 109 stringRedisTemplate.delete(key); 110 } 111 112 public StringRedisTemplate geStringRedisTemplate() { 113 return this.stringRedisTemplate; 114 } 115 116 public RedisTemplate geRedisTemplate() { 117 return this.redisTemplate; 118 } 119 120 /** 121 * StringRedisTemplate<br> 122 * 批量删除对应的value 123 * 124 * @param keys 125 */ 126 public void remove(final String... keys) { 127 for (String key : keys) { 128 remove(key); 129 } 130 } 131 132 /** 133 * StringRedisTemplate<br> 134 * 返回 StringRedisOperation 135 * 136 * @return 137 */ 138 public ValueOperations<String, String> getOperations() { 139 return this.stringRedisOperations; 140 } 141 142 /** 143 * RedisTemplate<br> 144 * 获取对象类型的值 145 * 146 * @param key 147 * @return 148 */ 149 public Object getObjectValue(String key) { 150 return redisOperations.get(key); 151 } 152 153 /** 154 * RedisTemplate<br> 155 * 添加对象类型数据 156 * 157 * @param key 158 * @param object 159 单机redis升级及数据迁移方案#yyds干货盘点#Redis集群原理专题介绍一下常用的Redis集群机制方案的介绍和分析