1- package com .alibabacloud .mse .demo ;
1+ package com .alibabacloud .mse .demo . a . mq ;
22
3- import com .alibabacloud .mse .demo .service .MqConsumer ;
43import lombok .RequiredArgsConstructor ;
54import lombok .extern .slf4j .Slf4j ;
65import org .apache .rocketmq .client .consumer .DefaultMQPushConsumer ;
76import org .apache .rocketmq .client .exception .MQClientException ;
87import org .apache .rocketmq .common .consumer .ConsumeFromWhere ;
8+ import org .springframework .beans .factory .annotation .Autowired ;
99import org .springframework .beans .factory .annotation .Value ;
10+ import org .springframework .cloud .commons .util .InetUtils ;
1011import org .springframework .context .annotation .Bean ;
1112import org .springframework .context .annotation .Configuration ;
13+ import org .springframework .web .client .RestTemplate ;
1214
1315@ Slf4j
1416@ Configuration
@@ -24,7 +26,14 @@ public class RocketMqConfiguration {
2426 @ Value ("${rocketmq.consumer.topic}" )
2527 private String topic ;
2628
27- private final MqConsumer mqConsumer ;
29+ @ Autowired
30+ private RestTemplate restTemplate ;
31+
32+ @ Autowired
33+ private InetUtils inetUtils ;
34+
35+ @ Autowired
36+ private String serviceTag ;
2837
2938 static {
3039 System .setProperty ("rocketmq.client.log.loadconfig" , "false" );
@@ -37,6 +46,12 @@ public DefaultMQPushConsumer mqPushConsumer() throws MQClientException {
3746 consumer .setNamesrvAddr (nameSrvAddr );
3847 consumer .subscribe (topic , "*" );
3948 consumer .setConsumeFromWhere (ConsumeFromWhere .CONSUME_FROM_FIRST_OFFSET );
49+
50+ MqConsumer mqConsumer = new MqConsumer (
51+ restTemplate ,
52+ inetUtils ,
53+ serviceTag
54+ );
4055 consumer .registerMessageListener (mqConsumer );
4156 log .info ("完成启动rocketMq的consumer,subscribe:{}" , topic );
4257 return consumer ;
0 commit comments