学习的旅程,就像蜗牛爬山,一点点的往上爬,一点点的欣赏旅途的风景

某天,小猿向小编讲了个很好笑的笑话,第二天,全世界都知道了!这是为啥呢?请看下图:

redis消息订阅与发布Java redis消息订阅与发布 ajax_spring


现在大家大概知道什么是订阅和发布的概念(就是上图的消息发布,消息订阅的一个过程)

订阅者可以是消息产生者,也可以是消息的消费者。那么自产自销就可以了,为啥还要小编呢?
小编是来干嘛的?大家都知道,小编嗓门大,用来传八卦最合适了(瞎编的),小编是用来做消息推广滴!!!!刚好redis就适合小编这个身份了

从上面这个瞎编故事中引出redis的订阅和发布功能!

假设你是分布式系统,A系统和B系统都是独立的。A系统突然想向B系统表白?怎么办?怎么通信?
A系统害羞,不能直接对B系统进行表白通信。这时候A系统就把表白消息以爱情的主题发送给redis。
redis接收到表白消息后,就把消息推送给也订阅了爱情主题的B系统。(此处注意,C系统也订阅爱情主题,C也收的到表白消息)

好了,扯大了,说正文了,怎么实现上述功能。

redis 订阅发布实现

  • 第一步:编写观察器(监听器),用来监听有没有主题消息的
import com.example.hxzboot.Dome.Sys.Core.Service.CoreService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * 监听
 */
@Component("hxzReceiver")
public class HxzReceiver {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private CoreService coreService;//可以注入自己的业务层实现

    public void receiveMessage(String message) {//主题接收
        System.out.println("我监听到了主题消息:消息内容:"+message);
    }
}
  • 第二步:编写Redis配置文件
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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;

@Configuration
public class RedisConfiguration {

    @Value("${spring.redis.msg.title}")
    private String title;//此处是主题--我写进了springboot的配置文件

    /**
     * 消息适配器
     *
     * 绑定消息监听者和接收监听的方法,必须要注入这个监听器,不然会报错
     * @return MessageListenerAdapter
     */
    @Bean
    public MessageListenerAdapter listenerAdapter(HxzReceiver hxzReceiver) {//此处注入观察器(监听器)
        return new MessageListenerAdapter(hxzReceiver, "receiveMessage");
    }

    /**
     * 定义消息监听者容器
     * @param connectionFactory 连接工厂
     * @param listenerAdapter 消息处理器
     * @return RedisMessageListenerContainer
     */
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                                   MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer listenerContainer = new RedisMessageListenerContainer();
        listenerContainer.setConnectionFactory(connectionFactory);
        listenerContainer.addMessageListener(listenerAdapter, new PatternTopic(title));//设置监听的主题
        return listenerContainer;
    }
}
  • 第三步:测试发布主题消息
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/redisdome")
public class RedisDome {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Value("${spring.redis.msg.title}")
    private String title;//此处是主题--我写进了springboot的配置文件

    @RequestMapping("/rsdome")
    public void rsdome(String ags1, String ags2, HttpServletRequest request) {
        stringRedisTemplate.convertAndSend(title, "消息aaaaaaaaaaa");//发布主题消息
    }
}