Spring-Redis基于Redis的MS消息服务
2017-11-14 14:07
447 查看
官网:messaging-redis
Spring-data-redis提供了redis的发布-订阅消息队列功能。具体实例如下:
下面是需要注入的两个bean,一个是消息容器,有多种实现,redis只是其中一种,另一个是监听器,该
监听器可以通过自定义的类指明消息处理方法来对消息进行接受和处理。
package com.tom.ms.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
/**
* redis发布订阅监听配置
* @author tomZ
* @date 2017年11月14日
* @desc TODO
*/
@Configuration
public class RedisMessageConfiguration {
private static final Logger logger = LoggerFactory.getLogger(Receiver.class);
/**
* 容器监听bean
* @param fac
* @param adapter
* @return
*/
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory fac, MessageListenerAdapter adapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(fac);
container.addMessageListener(adapter, new PatternTopic("chat"));
logger.info("Redis Message Container bean init finished.");
return container;
}
/**
* 消息监听注入到bean,必须注入,不能手动new,因为该类实现了InitializingBean,需要执行afterPropertiesSet方法,
* 注入到bean会自动执行
* @return
*/
@Bean
public MessageListenerAdapter adapter() {
return new MessageListenerAdapter(new Receiver(), "receiveMessage");
}
}
Receiver的类如下,非常简单:
package com.tom.ms.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 消息接收Pojo
* @author tomZ
* @date 2017年11月14日
* @desc TODO
*/
public class Receiver {
private static final Logger logger = LoggerFactory.getLogger(Receiver.class);
public void receiveMessage(String msg) {
logger.info("redis message service receive: "+msg);
}
}
在需要的地方使用SpringRedisTemplate来发送消息,直接注入即可。
@AutoWired
private StringRedisTempate redisTemplate;
redisTemplate.convertAndSend("chat","hi,SB");
Spring-data-redis提供了redis的发布-订阅消息队列功能。具体实例如下:
下面是需要注入的两个bean,一个是消息容器,有多种实现,redis只是其中一种,另一个是监听器,该
监听器可以通过自定义的类指明消息处理方法来对消息进行接受和处理。
package com.tom.ms.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
/**
* redis发布订阅监听配置
* @author tomZ
* @date 2017年11月14日
* @desc TODO
*/
@Configuration
public class RedisMessageConfiguration {
private static final Logger logger = LoggerFactory.getLogger(Receiver.class);
/**
* 容器监听bean
* @param fac
* @param adapter
* @return
*/
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory fac, MessageListenerAdapter adapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(fac);
container.addMessageListener(adapter, new PatternTopic("chat"));
logger.info("Redis Message Container bean init finished.");
return container;
}
/**
* 消息监听注入到bean,必须注入,不能手动new,因为该类实现了InitializingBean,需要执行afterPropertiesSet方法,
* 注入到bean会自动执行
* @return
*/
@Bean
public MessageListenerAdapter adapter() {
return new MessageListenerAdapter(new Receiver(), "receiveMessage");
}
}
Receiver的类如下,非常简单:
package com.tom.ms.redis;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 消息接收Pojo
* @author tomZ
* @date 2017年11月14日
* @desc TODO
*/
public class Receiver {
private static final Logger logger = LoggerFactory.getLogger(Receiver.class);
public void receiveMessage(String msg) {
logger.info("redis message service receive: "+msg);
}
}
在需要的地方使用SpringRedisTemplate来发送消息,直接注入即可。
@AutoWired
private StringRedisTempate redisTemplate;
redisTemplate.convertAndSend("chat","hi,SB");
相关文章推荐
- 基于哨兵【sentinel】模式的redis服务集群并与spring集成
- Java消息服务初步学习(基于Spring In Action的整理)
- 【推荐】微服务大型分布式企业框架 Springmvc+mybatis+shiro+Dubbo+ZooKeeper+Redis+KafKa
- 【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务
- spring-redis实现消息生产者发布和消费者订阅
- 使用Redis作为消息队列服务场景应用案例
- 【推荐】微服务分布式企业框架Springmvc+mybatis+shiro+Dubbo+ZooKeeper+Redis
- SpringBootService,一个基于spring boot搭建的SOA服务框架
- 基于 Spring + Dubbo 开发分布式REST服务实战
- springboot填坑之 -- spring cloud基于ip的discovery服务注册中心配置
- 基于Redis实现分布式消息队列(2)
- 微服务框架Spring Cloud之使用事件和消息队列实现分布式事务
- Spring基于注解整合Redis
- 【Dubbo实战】 Dubbo+Zookeeper+Spring整合应用篇-Dubbo基于Zookeeper实现分布式服务(二)
- 基于Redis的消息中心缓存搭建
- 【分享】微服务分布式企业框架 Springmvc+mybatis+shiro+Dubbo+ZooKeeper+Redis+KafKa
- 基于Redis实现延时队列服务
- 精华【分布式、微服务、云架构dubbo+zookeeper+springmvc+mybatis+shiro+redis】分布式大型互联网企业架构!
- 基于Spring Boot和Spring Cloud实现微服务架构学习(三)-Spring Boot应用
- 基于Spring Boot和Spring Cloud实现微服务架构学习(二)-Spring Boot总结