22
33import lombok .RequiredArgsConstructor ;
44import lombok .extern .slf4j .Slf4j ;
5+ import org .apache .commons .lang3 .StringUtils ;
6+ import org .apache .rocketmq .acl .common .AclClientRPCHook ;
7+ import org .apache .rocketmq .acl .common .SessionCredentials ;
58import org .apache .rocketmq .client .consumer .DefaultMQPushConsumer ;
9+ import org .apache .rocketmq .client .consumer .rebalance .AllocateMessageQueueAveragely ;
610import org .apache .rocketmq .client .exception .MQClientException ;
711import org .apache .rocketmq .common .consumer .ConsumeFromWhere ;
12+ import org .apache .rocketmq .remoting .RPCHook ;
813import org .springframework .beans .factory .annotation .Autowired ;
9- import org .springframework .beans .factory .annotation .Qualifier ;
1014import org .springframework .beans .factory .annotation .Value ;
1115import org .springframework .cloud .commons .util .InetUtils ;
1216import org .springframework .context .annotation .Bean ;
@@ -27,6 +31,11 @@ public class RocketMqConfiguration {
2731 @ Value ("${rocketmq.consumer.topic}" )
2832 private String topic ;
2933
34+ @ Value ("${middleware.mq.ak:}" )
35+ private String ak ;
36+ @ Value ("${middleware.mq.sk:}" )
37+ private String sk ;
38+
3039 @ Autowired
3140 private RestTemplate restTemplate ;
3241
@@ -43,16 +52,21 @@ public class RocketMqConfiguration {
4352 @ Bean (initMethod = "start" , destroyMethod = "shutdown" )
4453 public DefaultMQPushConsumer mqPushConsumer () throws MQClientException {
4554 log .info ("正在启动rocketMq的consumer" );
46- DefaultMQPushConsumer consumer = new DefaultMQPushConsumer (groupName );
55+
56+ DefaultMQPushConsumer consumer ;
57+ if (StringUtils .isNotEmpty (this .ak ) && StringUtils .isNotEmpty (this .sk )) {
58+ log .info ("middleware.mq.ak 不为空,MQ ACL信息已填充" );
59+ RPCHook rpcHook = new AclClientRPCHook (new SessionCredentials (this .ak , this .sk ));
60+ consumer = new DefaultMQPushConsumer (this .groupName , rpcHook , new AllocateMessageQueueAveragely ());
61+ } else {
62+ log .info ("middleware.mq.ak 为空" );
63+ consumer = new DefaultMQPushConsumer (groupName );
64+ }
4765 consumer .setNamesrvAddr (nameSrvAddr );
4866 consumer .subscribe (topic , "*" );
4967 consumer .setConsumeFromWhere (ConsumeFromWhere .CONSUME_FROM_FIRST_OFFSET );
5068
51- MqConsumer mqConsumer = new MqConsumer (
52- restTemplate ,
53- inetUtils ,
54- serviceTag
55- );
69+ MqConsumer mqConsumer = new MqConsumer (restTemplate , inetUtils , serviceTag );
5670 consumer .registerMessageListener (mqConsumer );
5771 log .info ("完成启动rocketMq的consumer,subscribe:{}" , topic );
5872 return consumer ;
0 commit comments