Redis发布订阅简例

Redis除了可以实现缓存功能以外,还可以用于不同服务之间消息的发布、订阅,可以实现消息提供方处理完成后,及时的将消息返回。例子比较简单就写在一起:

1、安装redis并开启:redis-server.exe redis.windows.conf

2、新建springboot项目,添加maven依赖

<!-- springmvc -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- redis -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
</dependency>
<!-- fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.58</version>
</dependency>

3、appliction.yml添加redis配置

server:
  port: 2019
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    password: 1234
    timeout: 3000ms
    jedis:
      pool:
        max-active: 8
        max-idle: 8
        min-idle: 0
        max-wait: -1ms

4、定义监听器和监听方法

package com.ccl.testst.redis_ps;


import com.alibaba.fastjson.JSONArray;

/**
 * 自定义监听器和监听方法
 */
public class Receiver {
    public void receiveMessage(String message){
        // 接收数据为数组形式的字符串,转换成JSONArray
        System.out.println("收到:"+ JSONArray.parseArray(message));
    }
}

5、订阅配置,

package com.ccl.testst.redis_ps;

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订阅消息
 */
@Configuration
public class MessageListenerConfig {
    /**
     * 注入监听器:绑定消息监听者Receiver()和监听方法receiveMessage
     * @return
     */
    @Bean
    public MessageListenerAdapter listenerAdapter(){
        return new MessageListenerAdapter(new Receiver(),"receiveMessage");
    }

    /**
     * 配置消息监听容器
     * @param redisConnectionFactory
     * @return
     */
    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory redisConnectionFactory){
        // 容器
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        // 连接工厂
        container.setConnectionFactory(redisConnectionFactory);
        // 添加消息监听器
        container.addMessageListener(listenerAdapter(),new PatternTopic("ccl_topic"));
        return container;
    }
}

6、发布消息,此处和订阅写在一起,也可以实现异构服务之间的调用

package com.ccl.testst.redis_ps;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/redis")
public class RedisController {
    @Autowired
    private StringRedisTemplate template;

    @RequestMapping("/publish")
    public String publish(){
        List<String> result = new ArrayList<>();
        for(int i=0;i<10;i++){
            result.add("第"+i+"次发布的消息!");
        }
        // 使用StringRedisTemplate讲消息发布到ccl_topic主题上,并转换成字符串形式传递
        template.convertAndSend("ccl_topic", JSON.toJSONString(result));
        return "SUCCESS";
    }

}

7、浏览器访问:http://localhost:2019/redis/publish即可实现消息订阅。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章