Redis发布式订阅

前言

1、redis配置文件

    <!--配置监听队列-->
    <bean id="requestMessageListener" class="com.hlj.redis.listener.RequestMessageListener"/>

    <redis:listener-container>
        <redis:listener ref="requestMessageListener"  topic="request" />
    </redis:listener-container>



</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:redis="http://www.springframework.org/schema/redis"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/redis http://www.springframework.org/schema/redis/spring-redis.xsd">



<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" >
        <property name="maxTotal" value="${hlj.redis.max-total}"/>
        <property name="maxIdle" value="${hlj.redis.max-idle}"/>
        <property name="maxWaitMillis" value="${hlj.redis.pool.max-wait}"/>
    </bean>

    <bean id="redisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" destroy-method="destroy">
        <property name="password" value="${hlj.redis.password}"/>
        <property name="hostName" value="${hlj.redis.host-name}"/>
        <property name="port" value="${hlj.redis.port}"/>
        <property name="usePool" value="true"/>
        <property name="poolConfig" ref="jedisPoolConfig"/>
    </bean>

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate" scope="prototype">
        <property name="connectionFactory" ref="redisConnectionFactory"/>
        <property name="keySerializer">
            <bean class="com.hlj.redis.cacheSerializer.CustomStringRedisSerializer"/>
        </property>
        <property name="valueSerializer">
            <bean class="com.hlj.redis.cacheSerializer.CustomJSONStringRedisSerializer"/>
        </property>
    </bean>


    <bean id="stringRedisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate" scope="prototype">
        <property name="connectionFactory" ref="redisConnectionFactory"/>
    </bean>

    <!--配置监听队列-->
    <bean id="requestMessageListener" class="com.hlj.redis.listener.RequestMessageListener"/>

    <redis:listener-container>
        <redis:listener ref="requestMessageListener"  topic="request" />
    </redis:listener-container>



</beans>

2、配置监听消息

package com.hlj.redis.listener;

import com.hlj.redis.cacheSerializer.CustomJSONStringRedisSerializer;
import com.hlj.redis.cacheSerializer.CustomStringRedisSerializer;
import com.hlj.redis.listener.data.ConvertBean;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;

/**
 * 通过监听redistemplate进行发送消息
 */
public class RequestMessageListener implements MessageListener {

    private CustomStringRedisSerializer stringRedisSerializer = new CustomStringRedisSerializer();

    private CustomJSONStringRedisSerializer jsonStringRedisSerializer = new CustomJSONStringRedisSerializer();

    @Override
    public void onMessage(Message message, byte[] bytes) {
        System.out.println("message监听");
        ConvertBean convertBean = (ConvertBean) jsonStringRedisSerializer.deserialize(message.getBody());

//        System.out.println(convertBean.toString());

    }
}

3、测试

package com.hlj.redis.listener.controller;

import com.hlj.redis.listener.data.ConvertBean;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

/**
 * @Desc:
 * @Author HealerJean
 * @Date 2018/9/13  下午1:30.
 */
@RequestMapping("redis/listener")
@Controller
public class ListenerController {

    private  RedisTemplate redisTemplate;

    @GetMapping("test")
    @ResponseBody
    public void lockRedis(){
        ConvertBean convertBean = new ConvertBean();
        convertBean.setContent("content");
        convertBean.setToUid("uuid");

        redisTemplate.convertAndSend("request",convertBean);

    }

}





如果满意,请打赏博主任意金额,感兴趣的在微信转账的时候,添加博主微信哦, 请下方留言吧。可与博主自由讨论哦

支付包 微信 微信公众号
支付宝 微信 微信公众号
posted @ 2018-09-13 13:57  HealerJean  阅读(70)  评论(0)    收藏  举报