SPRING-CLOUD集成REDIS-CLUSTER
写在前面
也不复杂,大家就按照步骤操作,假如有问题请留言哈
依赖库引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>启动类配置
@EnableScheduling@EnableCaching@MapperScan("com.haier.smarthome.intelligentfamilyconsole.mapper")public class IntelligentFamilyConsoleApplication { public static void main(String[] args) { SpringApplication.run(IntelligentFamilyConsoleApplication.class, args); }}配置类
package com.haier.smarthome.intelligentfamilyconsole.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisClusterConfiguration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.connection.RedisNode;import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import redis.clients.jedis.JedisCluster;import redis.clients.jedis.JedisPoolConfig;import javax.annotation.Resource;import java.util.ArrayList;import java.util.List;/** * @Author:zhanghongpeng * @Description:Redis 配置类 * @CreateDate:13:48 2018/7/4 */@Configuration@ConditionalOnClass(JedisCluster.class)public class RedisClusterConfig { @Resource private RedisProperties redisProperties; /** * 配置 Redis 连接池信息 */// @Bean// public JedisPoolConfig getJedisPoolConfig() {// JedisPoolConfig jedisPoolConfig =new JedisPoolConfig();// jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());// jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWait());// jedisPoolConfig.setTestOnBorrow(redisProperties.isTestOnBorrow());//// return jedisPoolConfig;// } /** * 配置 Redis Cluster 信息 */ @Bean public RedisClusterConfiguration getJedisCluster() { RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration(); redisClusterConfiguration.setMaxRedirects(redisProperties.getMaxRedirects()); List<RedisNode> nodeList = new ArrayList<>(); String[] cNodes = redisProperties.getNodes().split(","); //分割出集群节点 for(String node : cNodes) { String[] hp = node.split(":"); nodeList.add(new RedisNode(hp[0], Integer.parseInt(hp[1]))); } redisClusterConfiguration.setClusterNodes(nodeList); return redisClusterConfiguration; } /** * 配置 Redis 连接工厂 */ @Bean public JedisConnectionFactory getJedisConnectionFactory(RedisClusterConfiguration redisClusterConfiguration, JedisPoolConfig jedisPoolConfig) { JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, jedisPoolConfig); return jedisConnectionFactory; } /** * 设置数据存入redis 的序列化方式 * redisTemplate序列化默认使用的jdkSerializeable * 存储二进制字节码,导致key会出现乱码,所以自己设置序列化类 */ @Bean public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) { RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); redisTemplate.setValueSerializer(jackson2JsonRedisSerializer); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; }}package com.haier.smarthome.intelligentfamilyconsole.config;import lombok.Data;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.stereotype.Component;/** * @Author:zhanghongpeng * @Description:Redis 基本环境信息 * @CreateDate:13:48 2018/7/4 */@Component//@ConfigurationProperties(prefix = "spring.redis.cluster")@Datapublic class RedisProperties { private String nodes; private Integer commandTimeout; private Integer maxAttempts; private Integer maxRedirects; private Integer maxActive; private Integer maxWait; private Integer maxIdle; private Integer minIdle; private boolean testOnBorrow; public String getNodes() { return nodes; } public void setNodes(String nodes) { this.nodes = nodes; } public Integer getCommandTimeout() { return commandTimeout; } public void setCommandTimeout(Integer commandTimeout) { this.commandTimeout = commandTimeout; } public Integer getMaxAttempts() { return maxAttempts; } public void setMaxAttempts(Integer maxAttempts) { this.maxAttempts = maxAttempts; } public Integer getMaxRedirects() { return maxRedirects; } public void setMaxRedirects(Integer maxRedirects) { this.maxRedirects = maxRedirects; } public Integer getMaxActive() { return maxActive; } public void setMaxActive(Integer maxActive) { this.maxActive = maxActive; } public Integer getMaxWait() { return maxWait; } public void setMaxWait(Integer maxWait) { this.maxWait = maxWait; } public Integer getMaxIdle() { return maxIdle; } public void setMaxIdle(Integer maxIdle) { this.maxIdle = maxIdle; } public Integer getMinIdle() { return minIdle; } public void setMinIdle(Integer minIdle) { this.minIdle = minIdle; } public boolean isTestOnBorrow() { return testOnBorrow; } public void setTestOnBorrow(boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; }}使用测试
package com.haier.smarthome.intelligentfamilyconsole.service;import com.haier.smarthome.intelligentfamilyconsole.entity.TestModel;import net.sf.json.JSONArray;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.cache.annotation.CachePut;import org.springframework.cache.annotation.Cacheable;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Service;import java.util.ArrayList;import java.util.List;/** * 接口实现类 - Test * Created by zhanghongpeng on 2018/9/13. */@Servicepublic class RedisClusterServiceImpl implements RedisClusterService{ @Autowired private RedisTemplate redisTemplate; @Override @Cacheable(value = "TestmodelCache", key = "#key") public List<TestModel> getTestmodel(String key) { return new ArrayList<TestModel>(); } @Override @CachePut(value = "TestmodelCache", key = "#key") public List<TestModel> updateTestmodel(String key, List<TestModel> testModelList) { redisTemplate.opsForValue().set(key, testModelList); return testModelList; }}说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » SPRING-CLOUD集成REDIS-CLUSTER
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » SPRING-CLOUD集成REDIS-CLUSTER