Spring-Cloud集成redis-cluster
前言
系统之前接入的是单点redis,为了条系统高可用能力,特增加集成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")
@Data
public 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.
*/
@Service
public 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;
}
}
Spring-Cloud集成redis-cluster的更多相关文章
- Spring+Dubbo集成Redis的两种解决方案
当下我们的系统数据库压力都非常大,解决数据库的瓶颈问题势在必行,为了解决数据库的压力等需求,我们常用的是各种缓存,比如redis,本文就来简单讲解一下如何集成redis缓存存储,附github源码. ...
- (35)Spring Boot集成Redis实现缓存机制【从零开始学Spring Boot】
[本文章是否对你有用以及是否有好的建议,请留言] 本文章牵涉到的技术点比较多:Spring Data JPA.Redis.Spring MVC,Spirng Cache,所以在看这篇文章的时候,需要对 ...
- Spring Boot 2.X(六):Spring Boot 集成Redis
Redis 简介 什么是 Redis Redis 是目前使用的非常广泛的免费开源内存数据库,是一个高性能的 key-value 数据库. Redis 与其他 key-value 缓存(如 Memcac ...
- SpringBoot(十一): Spring Boot集成Redis
1.在 pom.xml 中配置相关的 jar 依赖: <!-- 加载 spring boot redis 包 --> <dependency> <groupId>o ...
- spring boot集成redis基础入门
redis 支持持久化数据,不仅支持key-value类型的数据,还拥有list,set,zset,hash等数据结构的存储. 可以进行master-slave模式的数据备份 更多redis相关文档请 ...
- Spring Boot集成Redis集群(Cluster模式)
目录 集成jedis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 集成spring-data-redis 引入依赖 配置绑定 注册 获取redis客户端 使用 验证 异常处理 同样的, ...
- Spring Cloud集成相关优质项目推荐
Spring Cloud Config 配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储.Git以及Subversion. Spring Cloud Bus 事件.消 ...
- 【分布式事务】spring cloud集成lcn解决分布式事务
参考地址:https://blog.csdn.net/u010882691/article/details/82256587 参考地址:https://blog.csdn.net/oyh1203/ar ...
- spring cloud集成 consul源码分析
1.简介 1.1 Consul is a tool for service discovery and configuration. Consul is distributed, highly ava ...
- 【spring boot】【redis】spring boot 集成redis的发布订阅机制
一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...
随机推荐
- Linux下面sysstat的安装与简介
https://blog.51cto.com/smoke520/2160073 在Linux系统下获取sysstat-10.0.5.tar.gz的两种方式: 方式一: 下载sysstat-10.0 ...
- vim 从嫌弃到依赖(9)——命令模式进阶
上一篇文章更新还是在51前,最近发生了很多事情了,全国各地的疫情又有蔓延的趋势,北京朝阳区都已经开始实施居家办公.各位小伙伴请注意安全,安全平安的度过这个疫情. 废话不多说了,接着上次的内容往下写. ...
- 8.3 NtGlobalFlag
NtGlobalFlag 是一个Windows内核全局标记,在Windows调试方案中经常用到.这个标记定义了一组系统的调试参数,包括启用或禁用调试技术的开关.造成崩溃的错误代码和处理方式等等.通过改 ...
- 驱动开发:运用VAD隐藏R3内存思路
在进程的_EPROCESS中有一个_RTL_AVL_TREE类型的VadRoot成员,它是一个存放进程内存块的二叉树结构,如果我们找到了这个二叉树中我们想要隐藏的内存,直接将这个内存在二叉树中抹去,其 ...
- AIX6.1修改时区/修改时间
环境 AIX6.1 修改时间 格式:date -n mmddHHMMYY #mm表示月分,dd表示日期,HH表示小时,MM表示分钟,YY表示年份. 例子:date -n 1204171622 #修 ...
- .NET 云原生架构师训练营(模块二 基础巩固 引入)--学习笔记
2.1 引入 http协议 web server && web application framework .net 与 .net core asp .net core web api ...
- .NET Core开发实战(第34课:MediatR:轻松实现命令查询职责分离模式(CQRS))--学习笔记(下)
34 | MediatR:轻松实现命令查询职责分离模式(CQRS) 实际上我们在定义我的查询的时候,也可以这样定义,例如我们定义一个 MyOrderQuery,把订单的所有名称都输出出去 namesp ...
- 文心一言 VS 讯飞星火 VS chatgpt (198)-- 算法导论14.3 6题
六.用go语言,说明如何来维护一个支持操作MIN-GAP的一些数的动态集Q,使得该操作能给出Q中两个最接近的数之间的差值.例如,Q=(1,5,9,15,18,22),则MIN-GAP返回18-15=3 ...
- NC235745 拆路
题目链接 题目 题目描述 有 \(n\) 个城镇,城镇之间有 \(m\) 条道路相连,道路可以看成无向边.每一个城镇都有自己的一个繁荣度 \(v_i\) ,一个城镇 \(u\) 受到的影响 \(p\) ...
- PCIE详解
老男孩读PCIe之一:从PCIe速度说起 从今天开始,老男孩要开始讲PCIe了.对我来说,这是个很大的挑战:首先,我自己本身,对PCIe并没有做到胸有成竹,我的PCIe知识也只是停留在理论阶段,我并没 ...