释放双眼,带上耳机,听听看~!
使用
生产者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 1@Component
2@Slf4j
3public class MqProducer {
4
5 @Autowired
6 private RedisTemplate redisTemplate;
7
8 public void send(String topic, String msg) {
9 log.info("topic ={}, msg = {}", topic, msg);
10 redisTemplate.convertAndSend(topic, msg);
11 }
12
13 public void send(String topic, User user) {
14 log.info("topic ={}, user = {}", topic, user);
15 send(topic, JSON.toJSONString(user));
16 }
17}
18
19
20
消费者
配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 1 /**
2 * 订阅
3 * @author lipo
4 * @date 2019-11-13 11:57
5 */
6 @Bean
7 public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
8 RedisMessageListenerContainer container = new RedisMessageListenerContainer();
9 container.setConnectionFactory(connectionFactory);
10 //添加多个消费者
11 container.addMessageListener(new MessageListenerAdapter(new MqConsumer(), "onMessage"), new PatternTopic(RedisConstant.TOPIC));
12 container.addMessageListener(new MessageListenerAdapter(new MqConsumer2(), "onMessage"), new PatternTopic(RedisConstant.TOPIC));
13 return container;
14 }
15
16
17
两个消费者
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 1@Component("consumer")
2@Slf4j
3public class MqConsumer implements MessageListener {
4
5 @Override
6 public void onMessage(Message message, byte[] pattern) {
7 String msg = new String(message.getBody());
8 String topic = new String(message.getChannel());
9 String p = new String(pattern);
10 log.info("topic ={}, msg = {}, pattern = {}", topic, msg, p);
11 }
12}
13
14@Component("consumer2")
15@Slf4j
16public class MqConsumer2 implements MessageListener {
17
18 @Override
19 public void onMessage(Message message, byte[] pattern) {
20 String msg = new String(message.getBody());
21 String topic = new String(message.getChannel());
22 String p = new String(pattern);
23 log.info("topic ={}, msg = {}, pattern = {}", topic, msg, p);
24
25 User user = JSON.parseObject(msg, User.class);
26 log.info("user = {}", user);
27 }
28}
29
30
31
测试
1
2
3
4
5
6
7
8
9
10
11
12
13
14 1@RestController
2@RequestMapping("mq")
3public class MqController {
4 @Autowired
5 private MqProducer mqProducer;
6
7 @GetMapping("publish")
8 public Object publish() {
9 mqProducer.send(RedisConstant.TOPIC, new User(1, "lipo"));
10 return "ok";
11 }
12}
13
14
结果

分布式环境
manage服务配置消费者
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 1@Configuration
2public class RedisConsumerConfig {
3
4 /**
5 * 订阅
6 * @author lipo
7 * @date 2019-11-13 11:57
8 */
9 @Bean
10 public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory) {
11 RedisMessageListenerContainer container = new RedisMessageListenerContainer();
12 container.setConnectionFactory(connectionFactory);
13 //添加多个消费者
14 container.addMessageListener(new MessageListenerAdapter(new MqConsumer(), "onMessage"), new PatternTopic(RedisConstant.TOPIC));
15 return container;
16 }
17
18}
19
20
21
merchant服务也配置相同的消费者
order服务配置生产者
order发布消息时,两个消费者都能收到消费消息。
但是,如果manange重启了,消息错过了,就收不到消息了。
消息容易丢失,还是mq更好