spring cloud中redis的订阅与发布

项目A作为发布者,项目B作为订阅者

先在项目A中,注册redisTemplate,作为消息队列的发布者

package com.bbg.domainManager.common.redis;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * @ClassName PublisherConfig  注册redisTemplate,作为消息队列的发布者
 * @Description TODO
 * @Author wyc
 * @Date 2019/7/29 9:28
 * @Version 1.0.1
 **/
@Configuration
public class PublisherConfig {

    @Bean
    public StringRedisTemplate getRedisTemplate(RedisConnectionFactory redisConnectionFactory){
        return new StringRedisTemplate(redisConnectionFactory);
    }
}

然后,在service中,新建发布的方法RedisService

package com.bbg.domainManager.service;

/**
 * @ClassName RedisServer
 * @Description TODO
 * @Author wyc
 * @Date 2019/7/29 11:13
 * @Version 1.0.1
 **/
public interface RedisServer {

    /**
     * 数据推送
     * @return
     */
    public String sendMsg(Object o);
}


和RedisServerImpl

package com.bbg.domainManager.service.impl;

import com.bbg.domainManager.common.ConstantConfiguration;
import com.bbg.domainManager.service.RedisServer;
import org.apache.poi.ss.formula.functions.T;
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.Service;

/**
 * @ClassName RedisServerImpl
 * @Description TODO
 * @Author wyc
 * @Date 2019/7/29 11:14
 * @Version 1.0.1
 **/
@Service
public class RedisServerImpl implements RedisServer {

    private static final Logger logger = LoggerFactory.getLogger(RedisServerImpl.class);

    private static final String SUCCESS = "SUCCESS";
    private static final String FAILURE = "FAILURE";

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public String sendMsg(Object o) {
        try {
            stringRedisTemplate.convertAndSend(ConstantConfiguration.TOPIC_USERNAME,o);
            return SUCCESS;
        }catch (Exception e){
            logger.error("redis队列消息发送失败-----------------");
            return FAILURE;
        }
    }
}

枚举,发布与订阅的频道

package com.bbg.domainManager.common;

/**
 * @ClassName ConstantConfiguration
 * @Description TODO
 * @Author wang
 * @Date 2019/7/26 16:30
 * @Version 1.0.1
 **/
public class ConstantConfiguration {


    public static final String TOPIC_USERNAME = "TOPIC_USERNAME";
 
}

在controller中,使用

//在更新缓存之后,需要去通知
		String str = redisServer.sendMsg((new JSONArray(listDomain)).toString());

以上,发布者代码就绪 下面是订阅者B项目

消息订阅者配置类

package com.cloud.domain.common.redis;    /**
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @author xwq
 * @date 2019/8/7 000715:30
 */

import com.cloud.domain.common.ConstantConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
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;

/**
 *   消息订阅者配置类
 * @author xwq
 * @create 2019-08-07 15:30
 **/
@Configuration
@AutoConfigureAfter({Receiver.class})
public class SubscriberConfig {
    /**
     * 消息监听适配器,注入接受消息方法,输入方法名字 反射方法
     *
     * @param receiver
     * @return
     */
    @Bean
    public MessageListenerAdapter getMessageListenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "onMessage"); //当没有继承MessageListener时需要写方法名字
    }

    /**
     * 创建消息监听容器
     *
     * @param redisConnectionFactory
     * @param messageListenerAdapter
     * @return
     */
    @Bean
    public RedisMessageListenerContainer getRedisMessageListenerContainer(RedisConnectionFactory redisConnectionFactory, MessageListenerAdapter messageListenerAdapter) {
        RedisMessageListenerContainer redisMessageListenerContainer = new RedisMessageListenerContainer();
        redisMessageListenerContainer.setConnectionFactory(redisConnectionFactory);
        redisMessageListenerContainer.addMessageListener(messageListenerAdapter, new PatternTopic(ConstantConfiguration.TOPIC_USERNAME));
        return redisMessageListenerContainer;
    }


}


ConstantConfiguration.TOPIC_USERNAME就是枚举,和发布者要一样的频道,然后@AutoConfigureAfter({Receiver.class})中的Receiver.class就是订阅者在收到消息后的处理方法
Receiver.class

package com.cloud.domain.common.redis;    /**
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: ${todo}
 * @author xwq
 * @date 2019/8/7 000715:28
 */

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Component;

/**
 *    reids接受   订阅
 * @author xwq
 * @create 2019-08-07 15:28
 **/

@Component
public class Receiver implements MessageListener {
    private static Logger logger = LoggerFactory.getLogger(Receiver.class);

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void onMessage(Message message, byte[] pattern) {
        RedisSerializer<String> valueSerializer = stringRedisTemplate.getStringSerializer();
        String deserialize = valueSerializer.deserialize(message.getBody());
        logger.info("收到的mq消息" + deserialize);
    }

}

这个只是个简单的使用,更加复杂的还在研究中

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值