哈喽,大家好,我是了不起。
10年积累的成都网站设计、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计制作后付款的网站建设流程,更有通川免费网站建设让你可以放心的选择与我们合作。
Redis平常作为缓存使用较多,但是也可以作为发布订阅的消息队列来使用,本篇给大家介绍一下如何简单使用!右手就能操作
本篇我们会使用Spring Data Redis中集成的发布订阅功能来展示这个示例,
先看我们需要的依赖, 其实只需要引入spring-boot-starter-data-redis 就够了,另外再写一个接口来触发消息发布。
org.springframework.boot
spring-boot-starter-data-redis
org.springframework.boot
spring-boot-starter-webflux
Spring Data 为 Redis 提供了专用的消息传递集成,其功能和命名与 Spring Framework 中的 JMS 集成类似。
Redis 消息传递大致可分为两个功能领域:
其中主要的类都在这两个包下面,感兴趣的小伙伴可以去看看,原理就先不讲了,下期再安排吧。
org.springframework.data.redis.connection
org.springframework.data.redis.listener
发布消息我们可以直接使用RedisTemplate的 convertAndSend , 这个方法有两个参数,分别是channel, 还有消息内容。
public Long convertAndSend(String channel, Object message) {
Assert.hasText(channel, "a non-empty channel is required");
byte[] rawChannel = this.rawString(channel);
byte[] rawMessage = this.rawValue(message);
return (Long)this.execute((connection) -> {
return connection.publish(rawChannel, rawMessage);
}, true);
}
本次我们使用如下类来发布消息。作为示例就要简单粗暴。
public interface MessagePublisher {
void publish(String message);
}
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
public class RedisMessagePublisher implements MessagePublisher {
private RedisTemplate redisTemplate;
private ChannelTopic topic;
public RedisMessagePublisher() {
}
public RedisMessagePublisher(
RedisTemplate redisTemplate, ChannelTopic topic) {
this.redisTemplate = redisTemplate;
this.topic = topic;
}
public void publish(String message) {
redisTemplate.convertAndSend(topic.getTopic(), message);
}
}
订阅消息需要实现MessageListener的接口 ,onMessage的方法是收到消息后的消费方法。
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Service;
@Service
public class RedisMessageSubscriber implements MessageListener {
public void onMessage(Message message, byte[] pattern) {
System.*out*.println("Message received: " + message.toString());
}
}
// 消息订阅2
@Service("redisMessageSubscriber2")
public class RedisMessageSubscriber2 implements MessageListener {
public void onMessage(Message message, byte[] pattern) {
System.out.println("Message received2: " + message.toString());
}
}
另外就是订阅方订阅发布者,SpringDataRedis这里使用了一个消息监听容器和适配器来处理。我们直接贴出代码:
import com.north.redis.message.MessagePublisher;
import com.north.redis.message.RedisMessagePublisher;
import com.north.redis.message.RedisMessageSubscriber;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Autowired
private RedisConnectionFactory redisConnectionFactory;
@Resource
MessageListener redisMessageSubscriber2;
@Bean
public RedisTemplate redisTemplate() {
RedisTemplate template = new RedisTemplate<>();
template.setConnectionFactory(redisConnectionFactory);
// 使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
// 使用GenericJackson2JsonRedisSerializer来序列化和反序列化redis的value值
template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
template.afterPropertiesSet();
return template;
}
@Bean
MessageListenerAdapter messageListener() {
return new MessageListenerAdapter(new RedisMessageSubscriber());
}
@Bean
RedisMessageListenerContainer redisContainer() {
RedisMessageListenerContainer container
= new RedisMessageListenerContainer();
container.setConnectionFactory(redisConnectionFactory);
container.addMessageListener(messageListener(), topic());
container.addMessageListener(redisMessageSubscriber2, topic());
return container;
}
@Bean
MessagePublisher redisPublisher() {
return new RedisMessagePublisher(redisTemplate(), topic());
}
@Bean
ChannelTopic topic() {
return new ChannelTopic("northQueue");
}
}
以上代码中有几个点:
RedisMessageListenerContainer 是处理消费者和发布者的关系的类 ,使用起来也比较简单。
下面我们做一个小测试:
写一个接口来出发消息发布,使用多个订阅者
@RestController
public class TestController {
@Resource
private MessagePublisher redisMessagePublisher;
@GetMapping("/hello")
public Flux hello(@RequestParam String message) {
redisMessagePublisher.publish(message);
return Flux.*just*("Hello", "Webflux");
}
}
启动SpringBoot项目后我们发送消息测试:
图片
两个消费者都接到了消息:
图片
新闻标题:Redis发布订阅,右手就行!
URL网址:http://www.shufengxianlan.com/qtweb/news17/127417.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联