springboot2.X 集成redis+消息发布订阅
需求场景:分布式项目中,每个子项目有各自的 user 数据库, 在综合管理系统中存放这所有用户信息, 为了保持综合管理系统用户的完整性,
子系统添加用户后将用户信息以json格式保存至redis,然后发布到消息到消息通道,综合管理系统监控到子系统发布的消息前往redis
获取出用户信息保存到自己的数据库
1)redis配置
spring:
redis:
#数据库索引
database: 5
host: 127.0.0.1
port:
password: 123456
jedis:
pool:
#最大连接数
max-active:
#最大阻塞等待时间(负数表示没限制)
#最大空闲
max-idle:
#最小空闲
min-idle:
2)集成redis , 初始化redis组件
package com.bigcustomer.configs; import com.bigcustomer.utils.redisUtil.RedisService;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
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.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer; /**
* @author :CX
* @Date :Create in 2018/8/15 14:03
* @Effect :
*/ @Configuration
@EnableCaching//开启注解
public class RedisConfig extends CachingConfigurerSupport { private static Logger logger = LoggerFactory.getLogger(RedisConfig.class);
// 自定义的配置类, 存放了通道地址
@Autowired
private BaseConfig baseConfig; /**
*@参数
*@返回值
*@创建人 cx
*@创建时间
*@描述 //初始化监听器
*/
@Bean
RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
MessageListenerAdapter listenerAdapter) { RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//配置监听通道
container.addMessageListener(listenerAdapter, new PatternTopic(baseConfig.getRedisAisle()));// 通道的名字
logger.info("初始化监听成功,监听通道:【"+baseConfig.getRedisAisle()+"】");
return container;
} /**
*@参数
*@返回值
*@创建人 cx
*@创建时间
*@描述 利用反射来创建监听到消息之后的执行方法
*/
@Bean
MessageListenerAdapter listenerAdapter(RedisService receiver) {
return new MessageListenerAdapter(receiver, "receiveMessage");
} // /**
// *@参数
// *@返回值
// *@创建人 cx
// *@创建时间
// *@描述 控制线程用的
// */
// @Bean
// Receiver receiver(CountDownLatch latch) {
// return new Receiver(latch);
// }
//
// @Bean
// CountDownLatch latch() {
// return new CountDownLatch(1);
// } /**
*@参数
*@返回值
*@创建人 cx
*@创建时间
*@描述 //使用默认的工厂初始化redis操作String模板
*/
@Bean
StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
return new StringRedisTemplate(connectionFactory);
}
/**
*@参数
*@返回值
*@创建人 cx
*@创建时间
*@描述 //使用默认的工厂初始化redis操作map模板
*/
@Bean
RedisTemplate redisTemplate(RedisConnectionFactory connectionFactory) { Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<Object>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
template.setConnectionFactory(connectionFactory);
template.setKeySerializer(jackson2JsonRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashKeySerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template; }
}
3 ) 操作string 和 map 的dao封装
package com.bigcustomer.utils.redisUtil; import com.alibaba.fastjson.JSON;
import com.bigcustomer.biguser.service.BigUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component; import javax.annotation.Resource;
import java.util.Map; /**
* @author :CX
* @Date :Create in 2018/8/15 14:19
* @Effect : redisDAO封裝
*/
@Component
public class MyRedisDao { private static Logger logger = LoggerFactory.getLogger(BigUserService.class);
@Resource
private StringRedisTemplate template; @Resource
private RedisTemplate redisTemplate; //大客户信息同步到redis时保存的map的key
private final String BIG_USER_REDIS_KEY = "CM:CHANNELCUSTOMER"; /**
* @参数
* @返回值
* @创建人 cx
* @创建时间
* @描述 大客户添加成功后存到redis
*/
public boolean setMap(Map<String , Object> map) { try {
redisTemplate.opsForHash().putAll(BIG_USER_REDIS_KEY
, map);
logger.info("同步大客户信息到redis 成功!userId【" + map.get("funiqueid")+ "】");
return true;
} catch (Exception e) {
e.printStackTrace();
}
logger.info("同步大客户信息到redis 失败!userId【" + map.get("funiqueid")+ "】");
return false;
} public Object getMap(String key) { try {
Object o = redisTemplate.opsForHash().get(BIG_USER_REDIS_KEY, key);
if (null != o) {
return o;
}
} catch (Exception e) {
e.printStackTrace();
}
logger.info("获取大客户信息到失败!");
return null;
} /**
* @参数
* @返回值 存在 = true , 不纯在false
* @创建人 cx
* @创建时间
* @描述 判断是否存在 该key对应的值
*/
public boolean isNull(String key) {
return template.hasKey(key);
} /**
* @参数
* @返回值
* @创建人 cx
* @创建时间
* @描述 设置值 和 过期时间 单位秒
*/
public boolean setValue(String key, Object val, long expire) {
if (!this.isNull(key)) {
//不存在
String jsonString = JSON.toJSONString(val);
template.opsForValue().set(key, jsonString, expire);
logger.info("***************************成功在缓存中插入:" + key);
return true;
} else {
logger.info("***************************【" + key + "】已经存在缓存");
return false;
}
} /**
* @参数
* @返回值
* @创建人 cx
* @创建时间
* @描述 删除
*/
public boolean del(String key) {
return template.delete(key);
} /**
* @参数
* @返回值
* @创建人 cx
* @创建时间
* @描述 插入直接覆盖
*/
public boolean setValue(String key, Object val) {
//不存在
String jsonString = JSON.toJSONString(val);
// 去掉多余的 “
String replace = jsonString.replace("\"", "");
template.opsForValue().set(key, replace);
logger.info("***************************成功在缓存中插入:" + key);
return true;
} /**
* @参数
* @返回值
* @创建人 cx
* @创建时间
* @描述 获取对应key 的值
*/
public String getValue(String key) {
if (!this.isNull(key)) { //不存在
logger.info("***************************【" + key + "】不存在缓存");
return null;
} else {
return template.opsForValue().get(key);//根据key获取缓存中的val
}
} }
4) 消息发布和监听的服务类
package com.bigcustomer.utils.redisUtil; import com.bigcustomer.configs.BaseConfig;
import huashitech.kissucomponent.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; /**
* @author :CX
* @Date :Create in 2018/8/23 10:22
* @Effect : redis 通道消息发送和监听接受
*/
@Service
public class RedisService extends BaseService { @Autowired
private StringRedisTemplate template;
@Autowired
private BaseConfig baseConfig;
@Autowired
RedisService redisService; /**
*@参数
*@返回值
*@创建人 cx
*@创建时间
*@描述 向默认通道发送消息
*/
public void setMessage( Long funiqueid) { template.convertAndSend(baseConfig.getRedisAisle(),
baseConfig.getRedisMessageName() +funiqueid);
} /**
*@参数
*@返回值
*@创建人 cx
*@创建时间
*@描述 接受监听到的消息
*/
public void receiveMessage(String message) {
logger.info("接收redis通道消息:"+message);
}
}
5) 使用
dao.getTransactionManager().doTransaction((TransactionStatus s) -> {
//插入数据库
int insert = dao.insert(tbCmChannelcustomerModel);
// 加入缓存
HashMap<String, Object> map = new HashMap<>();
map.put(tbCmChannelcustomerModel.getFuniqueid().toString()
, JSON.toJSONString(tbCmChannelcustomerModel));
redisDao.setMap(map);
// 发布redis通知消息
redisService.setMessage(tbCmChannelcustomerModel.getFuniqueid());
});
springboot2.X 集成redis+消息发布订阅的更多相关文章
- 【spring boot】【redis】spring boot 集成redis的发布订阅机制
一.简单介绍 1.redis的发布订阅功能,很简单. 消息发布者和消息订阅者互相不认得,也不关心对方有谁. 消息发布者,将消息发送给频道(channel). 然后是由 频道(channel)将消息发送 ...
- php实现redis消息发布订阅
基础介绍 Pub/Sub功能(means Publish, Subscribe)即发布及订阅功能 基于事件的系统中,Pub/Sub是目前广泛使用的通信模型,它采用事件作为基本的通信机制,提供大规模系统 ...
- redis 实现发布订阅的功能
redis 除了作为缓存的功能外还可以用作消息中间件的功能,这片博客主要是介绍一下 redis 整合spring 实现消息的发布和订阅功能: 1:redis依赖,依赖两个包,redis 包, spri ...
- springboot集成redis实现消息发布订阅模式-双通道(跨多服务器)
基础配置参考https://blog.csdn.net/llll234/article/details/80966952 查看了基础配置那么会遇到一下几个问题: 1.实际应用中可能会订阅多个通道,而一 ...
- Redis 学习(三) —— 事务、消息发布订阅
一.Redis事务 Redis 提供的事务机制与传统的数据库事务有些不同,传统数据库事务必须维护以下特性:原子性(Atomicity), 一致性(Consistency),隔离性(Isolation) ...
- 【springboot】【redis】springboot+redis实现发布订阅功能,实现redis的消息队列的功能
springboot+redis实现发布订阅功能,实现redis的消息队列的功能 参考:https://www.cnblogs.com/cx987514451/p/9529611.html 思考一个问 ...
- redis实现消息发布/订阅
redis实现简单的消息发布/订阅模式. 消息订阅者: package org.common.component; import org.slf4j.Logger; import org.slf4j. ...
- 基于Redis消息的订阅发布应用场景
目录 基于Redis消息的订阅发布应用场景 1.应用背景 2.困境 2.1 锁表风险 2.2 实时性差 2.3 增加编程复杂性 2.4 实时效果 3.解决方案 3.1 前端传值给服务端 3.2 服务端 ...
- redis的发布订阅模式
概要 redis的每个server实例都维护着一个保存服务器状态的redisServer结构 struct redisServer { /* Pubsub */ // 字典,键为频道, ...
随机推荐
- [转]MySQL中函数CONCAT及GROUP_CONCAT
一.CONCAT()函数 CONCAT()函数用于将多个字符串连接成一个字符串. 使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为 +-- ...
- Solution of Publishing failed with multiple errors Error copying file static\
1.前言 由于系统被IT打了防病毒补丁,然后启动web项目一直出现Publishing failed with multiple errors Error copying file static... ...
- JetBrains 授权服务器(License Server URLS)
分享几个已经部署好的在线验证服务器:http://idea.iteblog.com/key.php http://idea.imsxm.com/ http://103.207.69.64:1017 h ...
- jquery submit ie6下失效的原因分析及解决方法
ie6中, $('a.btn').click(function(){ form.submit(); }) 点击失效: 分析: 微软低版本浏览器会先执行link标签的自身事件也就是href事件,这样就中 ...
- linux POSIX 信号量介绍
信号量一.什么是信号量信号量的使用主要是用来保护共享资源,使得资源在一个时刻只有一个进程(线程)使用.多线程可以同时运行多个线程函数完成功能,但是对于共享数据如果不加以锁定,随意改变共享数据的值会发生 ...
- MySQL 5.6.26几种安装包的区别
一.MySQL Installer 5.6.26 mysql-installer-community-5.6.26.0.msi, 364.2MBMySQL Installer 提供了简单易用.向导式的 ...
- python接口自动化测试十一:传参数:data与json
# 传json参数 import requests url = 'xxxxxxxx' body = { 'xxx': 'xxx', 'xxx': 'xxx' } # body是json ...
- python 全栈开发,Day142(flask标准目录结构, flask使用SQLAlchemy,flask离线脚本,flask多app应用,flask-script,flask-migrate,pipreqs)
昨日内容回顾 1. 简述flask上下文管理 - threading.local - 偏函数 - 栈 2. 原生SQL和ORM有什么优缺点? 开发效率: ORM > 原生SQL 执行效率: 原生 ...
- google gcr.io、k8s.gcr.io 国内镜像
1.首先添加docker官方的国内镜像 sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ...
- HTML编码规范 - (WEB前端命名规范)
HTML编码规范 (一)命名规则: 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wr ...