飞道的博客

Spring Boot 2020 官方基础68课程第九个 Messaging with Redis

388人阅读  评论(0)

本章花点时间来看下 developed a publish-and-subscribe application with Spring and Redis.

本章要从Redis开始说起:

不管怎样先下载一个吧,Redis有windows版本的。

Windows中redis的下载及安装、设置

下载地址: https://github.com/MicrosoftArchive/redis/releases

打开cmd命令窗口,使用命令进行安装和注册redis到window服务

安装命令:redis-server.exe --service-install redis.windows.conf --loglevel verbose

启动服务命令:redis-server.exe  --service-start

关闭服务命令:redis-server.exe  --service-stop

接下来我们重新开一个cmd,测试下 :

步骤一:开着Redis.

步骤二:用你熟悉的IDE,创建一个SpringBoot项目,依赖spring Data Redis Access Driver.项目结构如下:我用的springBoot版本是2.2.2

新建一个包和class:


  
  1. package com.dongyu.springbootguids09_redis.messagingredis;
  2. import java.util.concurrent.atomic.AtomicInteger;
  3. import org.slf4j.Logger;
  4. import org.slf4j.LoggerFactory;
  5. public class Receiver {
  6. private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);
  7. private AtomicInteger counter = new AtomicInteger();
  8. public void receiveMessage(String message) {
  9. LOGGER.info( "Received <" + message + ">");
  10. counter.incrementAndGet();
  11. }
  12. public int getCount() {
  13. return counter.get();
  14. }
  15. }

再来一个main。启动项


  
  1. package com.dongyu.springbootguids09_redis.messagingredis;
  2. import org.slf4j.Logger;
  3. import org.slf4j.LoggerFactory;
  4. import org.springframework.boot.SpringApplication;
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;
  6. import org.springframework.context.ApplicationContext;
  7. import org.springframework.context.annotation.Bean;
  8. import org.springframework.data.redis.connection.RedisConnectionFactory;
  9. import org.springframework.data.redis.core.StringRedisTemplate;
  10. import org.springframework.data.redis.listener.PatternTopic;
  11. import org.springframework.data.redis.listener.RedisMessageListenerContainer;
  12. import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
  13. @SpringBootApplication
  14. public class MessagingRedisApplication {
  15. private static final Logger LOGGER = LoggerFactory.getLogger(MessagingRedisApplication.class);
  16. @Bean
  17. RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
  18. MessageListenerAdapter listenerAdapter) {
  19. RedisMessageListenerContainer container = new RedisMessageListenerContainer();
  20. container.setConnectionFactory(connectionFactory);
  21. container.addMessageListener(listenerAdapter, new PatternTopic( "chat"));
  22. return container;
  23. }
  24. @Bean
  25. MessageListenerAdapter listenerAdapter(Receiver receiver) {
  26. return new MessageListenerAdapter(receiver, "receiveMessage");
  27. }
  28. @Bean
  29. Receiver receiver() {
  30. return new Receiver();
  31. }
  32. @Bean
  33. StringRedisTemplate template(RedisConnectionFactory connectionFactory) {
  34. return new StringRedisTemplate(connectionFactory);
  35. }
  36. public static void main(String[] args) throws InterruptedException {
  37. ApplicationContext ctx = SpringApplication.run(MessagingRedisApplication.class, args);
  38. StringRedisTemplate template = ctx.getBean(StringRedisTemplate.class);
  39. Receiver receiver = ctx.getBean(Receiver.class);
  40. while (receiver.getCount() == 0) {
  41. LOGGER.info( "Sending message...");
  42. template.convertAndSend( "chat", "Hello from Redis!");
  43. Thread.sleep( 500L);
  44. }
  45. System.exit( 0);
  46. }
  47. }

 

可以看到我们已经收到了发出的消息。

 

 

 

 

 

 

 


转载:https://blog.csdn.net/ldy889/article/details/104962196
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场