依赖
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>2.3.12.RELEASE</version>
发布端
stringRedisTemplate.convertAndSend(channel, JSON.toJSONString(event));
# 参数说明 channel 通道key;JSON.toJSONString(event) 发布的内容,json格式解析方便,较为通用
消费端
配置
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
/**
*/
@Component
public class RedisPubSubConfig {
private static RedisMessageListenerContainer listenerContainer;
@Primary
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
RedisPubSubConfig.listenerContainer = container;
return container;
}
@Component
public static class RedisListenerConfig{
@Autowired
public RedisListenerConfig(RedisMessageListenerContainer redisMessageListenerContainer,
ConfigReadMessageRedisSubListener configReadMessageRedisSubListener,
ConfigWriteMessageRedisSubListener configWriteMessageRedisSubListener,
SerialChangeStatusRedisSubListener serialChangeStatusRedisSubListener,
SerialPortMessageRedisSubListener serialPortMessageRedisSubListener,
VpnMessageRedisSubListener vpnMessageRedisSubListener,
VpnStatusChangeRedisSubListener vpnStatusChangeRedisSubListener) {
// 监听的通道及对应处理类
redisMessageListenerContainer.addMessageListener(configReadMessageRedisSubListener,
new ChannelTopic(RedisKey.DEVICE_CONFIGURE_PANEL_READ_REDIS_CHANNEL));
redisMessageListenerContainer.addMessageListener(configWriteMessageRedisSubListener,
new ChannelTopic(RedisKey.DEVICE_CONFIGURE_PANEL_WRITE_REDIS_CHANNEL));
redisMessageListenerContainer.addMessageListener(serialChangeStatusRedisSubListener,
new ChannelTopic(RedisKey.CLIENT_SERIAL_CHANGE_REDIS_CHANNEL));
redisMessageListenerContainer.addMessageListener(serialPortMessageRedisSubListener,
new ChannelTopic(RedisKey.CLIENT_SERIAL_MESSAGE_REDIS_CHANNEL));
redisMessageListenerContainer.addMessageListener(vpnMessageRedisSubListener,
new ChannelTopic(RedisKey.CLIENT_VPN_MESSAGE_REDIS_CHANNEL));
redisMessageListenerContainer.addMessageListener(vpnStatusChangeRedisSubListener,
new ChannelTopic(RedisKey.CLIENT_VPN_STATUS_CHANGE_REDIS_CHANNEL));
}
}
}
处理类
import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.stereotype.Component;
import java.nio.charset.StandardCharsets;
@Component
public class ConfigReadMessageRedisSubListener implements MessageListener {
@Override
public void onMessage(Message message, byte[] pattern) {
byte[] messageBody = message.getBody();
// 消息体
String body = new String(messageBody, StandardCharsets.UTF_8);
ObjectAAA event = JSON.parseObject(body, ObjectAAA.class);
}
}