springboot整合redis實現消息發布和訂閱
https://blog.csdn.net/weixin_43587472/article/details/112296403
springboot整合redis實現消息發布和訂閱
先了解一下redis消息發布訂閱的機制:
發布者將消息發布在一個channel(可認為是頻道)上,可以供多個訂閱者訂閱查看信息,所以說channel是連接發布者和訂閱者之間的橋梁。
1.實現一個用於接聽消息的實體類
@Component
public class MessageReceiver implements MessageListener {
@Autowired
private RedisTemplate redisTemplate;
private static Logger logger = LoggerFactory.getLogger(MessageReceiver.class);
@Override
public void onMessage(Message message, byte[] bytes) {
RedisSerializer<String> redisSerializer = redisTemplate.getStringSerializer();
String msg= redisSerializer.deserialize(message.getBody());
System.out.println("接收到的消息是:"+ msg);
logger.info("Received <" + msg + ">");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
此處可用@Component注解標識 ,也可以在配置文件即@Configration注解文件中用@Bean注入(這個是標准的springboot注入方式)
2.進行redis消息監聽器和適配器的配置
package com.remoteTest.cache.config;
import com.remoteTest.cache.bean.Employee;
import com.remoteTest.cache.bean.MessageReceiver;
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.core.RedisTemplate;
import org.springframework.data.redis.listener.PatternTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import java.net.UnknownHostException;
@Configuration
@AutoConfigureAfter({MessageReceiver.class})
public class MyRedisConfig {
/* * Redis消息監聽器容器 * 這個容器加載了RedisConnectionFactory和消息監聽器 * 可以添加多個監聽不同話題的redis監聽器,只需要把消息監聽器和相應的消息訂閱處理器綁定,該消息監聽器 * 通過反射技術調用消息訂閱處理器的相關方法進行一些業務處理 */
@Bean
public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory, MessageListenerAdapter adapter) {
RedisMessageListenerContainer container = new RedisMessageListenerContainer();
container.setConnectionFactory(connectionFactory);
//可以添加多個 messageListener
container.addMessageListener(adapter,new PatternTopic("topic"));
return container;
}
/* * 消息監聽器適配器,綁定消息處理器,利用反射技術調用消息處理器的業務方法 * 將MessageReceiver注冊為一個消息監聽器,可以自定義消息接收的方法(handleMessage) * 如果不指定消息接收的方法,消息監聽器會默認的尋找MessageReceiver中的onMessage這個方法作為消息接收的方法 */
@Bean
public MessageListenerAdapter adapter(MessageReceiver messageReceiver) {
return new MessageListenerAdapter(messageReceiver, "onMessage");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
container.addMessageListener(adapter,new PatternTopic(“topic”));
adapter()函數返回值是一個MessageListenerAdapter的對象。
new MessageListenerAdapter(messageReceiver, “onMessage”);第一個參數為接受消息的實體類對象(既上面定義的實體類對象,由adapter()作為參數傳進來,第二個參數為實體類中定義的接收消息的方法)該語句中的兩個參數一個是消息適配器,下面為配置,第二個參數是channel(既為頻道),該參數由發布者提供。
3.下面實現發布者
Controller層
package com.remoteTest.cache.Controller;
import com.remoteTest.cache.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/redis")
@RestController
public class RedisController {
@Autowired
RedisTemplate<String,String> redisTemplate;
@Autowired
RedisService redisService;
@RequestMapping("/publish")
public String sendMessage (String msg) {
return redisService.sendMessage(msg);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
Service層
package com.remoteTest.cache.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
@Service
public class RedisService {
@Autowired
RedisTemplate<String,String> redisTemplate;
public String sendMessage (String msg) {
try {
redisTemplate.convertAndSend("topic", msg);
System.out.println(msg);
return "消息發送成功";
}catch (Exception e) {
e.printStackTrace();
return "消息發送失敗";
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
redisTemplate.convertAndSend(“topic”,msg)
該方法用於發布消息,內置函數,可以直接調用,其中兩個參數第一個是channle(頻道,需和訂閱者保持一致),第二個參數是發布的消息。
4.redis配置
#redis連接
server.port=8010
spring.redis.host=127.0.0.1
spring.redis.port=6379
- 1
- 2
- 3
- 4
密碼默認為空