SpringBoot2.0 Advanced Case (02): Integration RocketMQ, implement asynchronous request processing

  A, RocketMQ
  
  1, architecture picture
  
  2, the role of classification
  
  (1), Broker
  
  core RocketMQ receiving Producer sent me a message, the message processing Consumer spending request, persistent storage of messages, the server filter functions.
  
  (2), NameServer
  
  status of individual components of the server message queue, the cluster through which to understand global information. Similar micro-services in the service registry registry, discovery, off the assembly line, the concept of on-line.
  
  Hot Backup: NamServer can deploy multiple, independent of each other, while other roles NameServer report status information to multiple machines.
  
  Heartbeat mechanism: NameServer in Broker, Topic and other status information is not persistent store, and regularly report are stored in memory by the various roles, overtime not to report the case, NameServer would think that a machine malfunction is not available.
  
  (3), Producer
  
  generation of the message, is the most commonly used class of producer DefaultMQProducer.
  
  (4), Consumer
  
  consumer messages, commonly Consumer class DefaultMQPushConsumer automatically call after receiving an incoming message processing method to deal with high real-time DefaultMQPullConsumer user self-control, greater flexibility.
  
  3, the communication mechanism
  
  (1), after completing a Broker needs to start the operation of NameServer to register himself; then every 30s time to regularly update the routing information NameServer Topic.
  
  (2), Producer sends a message, the need for routing information acquisition message from the local cache Topic based. If you do not update the routing information will pull back from NameServer, while the Producer would default to every 30s NameServer pull a routing information.
  
  (3), Consumer consume messages when the routing information obtained from NameServer, and then complete the client load balancing, listening on the specified message queue and get messages consumption.
  
  Second, the code implementation Case
  
  1, the structure of FIG item
  
  version descriptor
  
  <Spring-boot.version> 2.1.3.RELEASE </spring-boot.version>
  
  <rocketmq.version> 4.3.0 </rocketmq.version>
  
  2, profile
  
  rocketmq:
  
  # Manufacturer arranged
  
  producer:
  
  isOnOff: ON
  
  # message transmission setting of the same type as a group with a guaranteed unique
  
  groupName: FeePlatGroup
  
  # service address
  
  namesrvAddr: 10.1.1.207:9876
  
  # default maximum length of the message. 4 * 1024 (4M)
  
  MaxMessageSize : 4096
  
  # send timeout, default 3000
  
  sendMsgTimeout: 3000
  
  # failure retry sending the message, the default 2
  
  retryTimesWhenSendFailed: 2
  
  # Configure Consumer
  
  Consumer:
  
  isOnOff: ON
  
  # official Suggestions: Make sure each of the same group of consumers subscribe to the same theme.
  
  groupName: FeePlatGroup
  
  # service address
  
  namesrvAddr: 10.1.1.207:9876
  
  # Tag receives all this Topic
  
  Topics: FeePlatTopic ~ *;
  
  consumeThreadMin: 20 is
  
  consumeThreadMax: 64
  
  number of message # Set a consumer, the default is a
  
  consumeMessageBatchMaxSize:. 1
  
  # Configure Topic the Tag Group
  
  Cicada-Plat:
  
  Cicada-Plat-Group: CicadaPlatGroup
  
  Cicada-Plat-Topic: CicadaPlatTopic
  
  Cicada-Account-Tag: CicadaAccountTag
  
  . 3, the producer arranged
  
  import org.apache.rocketmq.client.exception.MQClientException;
  
  import org.apache.rocketmq.client.producer.DefaultMQProducer;
  
  import org.slf4j.Logger;
  
  import org.slf4j.LoggerFactory;
  
  import org.springframework.beans.factory.annotation.Value;
  
  import org.springframework.context.annotation.Bean;
  
  import org.springframework.context.annotation.Configuration;
  
  /**
  
  * RocketMQ 生产者配置
  
  */
  
  @Configuration
  
  public class ProducerConfig www.baikayule.cn{
  
  private static final Logger LOG = LoggerFactory.getLogger(ProducerConfig.class) ;
  
  @Value("${rocketmq.producer.groupName}"www.qunfLtie.com)
  
  private String groupName;
  
  @Value("${rocketmq.producer.namesrvAddr}")
  
  private String namesrvAddr;
  
  @Value ( "rocketmq.producer.maxMessageSize $ {}")
  
  Private Integer MaxMessageSize;
  
  @value ( "rocketmq.producer.sendMsgTimeout $ {}")
  
  Private Integer sendMsgTimeout;
  
  @value ( "rocketmq.producer.retryTimesWhenSendFailed $ {}")
  
  Integer retryTimesWhenSendFailed Private;
  
  @Bean
  
  public DefaultMQProducer getRocketMQProducer (www.chengsyl.cn) {
  
  DefaultMQProducer producer;
  
  producer = new new DefaultMQProducer (this.groupName);
  
  producer.setNamesrvAddr (this.namesrvAddr);
  
  // if different producer in the same jvm send a message to the different clusters mq necessary to provide different instanceName
  
  IF (this.maxMessageSize = null!) {
  
  producer.setMaxMessageSize (this.maxMessageSize);
  
  }
  
  IF (! this.sendMsgTimeout = null) {
  
  producer.setSendMsgTimeout (this.sendMsgTimeout);
  
  }
  
  // if message transmission failed, set the number of retries, the default is 2
  
  IF (this.retryTimesWhenSendFailed = null!) {
  
  producer.setRetryTimesWhenSendFailed (this.retryTimesWhenSendFailed);
  
  }
  
  the try {
  
  Producer. Start (www.tdcqpt.cn);
  
  } the catch (MQClientException E) {
  
  e.printStackTrace ();
  
  }
  
  return Producer;
  
  }
  
  }
  
  . 4, consumers arranged
  
  Import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
  
  Import the org.apache .rocketmq.client.exception.MQClientException;
  
  Import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
  
  Import org.slf4j.Logger;
  
  Import org.slf4j.LoggerFactory;
  
  import org.springframework.beans.factory.annotation.Value;
  
  import org.springframework.www.dfyLdL2019.com context.annotation.Bean;
  
  import org.springframework.context.annotation.Configuration;
  
  import javax.annotation.Resource;
  
  /**
  
  * RocketMQ 消费者配置
  
  */
  
  @Configuration
  
  public class ConsumerConfig {
  
  private static final Logger LOG = LoggerFactory.getLogger(ConsumerConfig.class) ;
  
  @Value("${rocketmq.consumer.namesrvAddr}")
  
  private String namesrvAddr;
  
  @Value("${rocketmq.consumer.www.zbyL2019.com groupName}")
  
  private String groupName;
  
  @Value("${rocketmq.consumer.consumeThreadMin}")
  
  private int consumeThreadMin;
  
  @Value("${rocketmq.consumer.consumeThreadMax}")
  
  private int consumeThreadMax;
  
  @Value("${rocketmq.consumer.topics}")
  
  private String topics;
  
  @Value("${rocketmq.consumer.consumeMessageBatchMaxSize}")
  
  private int consumeMessageBatchMaxSize;
  
  @Resource
  
  private RocketMsgListener msgListener;
  
  @Bean
  
  public DefaultMQPushConsumer getRocketMQConsumer(www.fusyLwg.com){
  
  DefaultMQPushConsumer consumer = new DefaultMQPushConsumer(groupName);
  
  consumer.setNamesrvAddr(namesrvAddr);
  
  consumer.setConsumeThreadMin(consumeThreadMin);
  
  consumer.setConsumeThreadMax(consumeThreadMax);
  
  consumer.registerMessageListener(msgListener);
  
  consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_LAST_OFFSET);
  
  consumer.setConsumeMessageBatchMaxSize(consumeMessageBatchMaxSize);
  
  try {
  
  String[] topicTagsArr = topics.split(";");
  
  for (String topicTags : topicTagsArr) {
  
  String[] topicTag = topicTags.split("~");
  
  consumer.subscribe(topicTag[0],topicTag[1]);
  
  }
  
  consumer.start();
  
  }catch (MQClientException e){
  
  e.printStackTrace();
  
  }
  
  return consumer;
  
  }
  
  }
  
  5、消息监听配置
  
  import com.rocket.queue.service.impl.ParamConfigService;
  
  import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
  
  import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
  
  import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
  
  import org.apache.rocketmq.common.message.MessageExt;
  
  import org.slf4j.Logger;
  
  import org.slf4j.LoggerFactory;
  
  import org.springframework.stereotype.Component;
  
  import org.springframework.util.CollectionUtils;
  
  import javax.annotation.Resource;
  
  import java.util.List;
  
  /**
  
  * 消息消费监听
  
  */
  
  @Component
  
  public class RocketMsgListener implements MessageListenerConcurrently {
  
  private static final Logger LOG = LoggerFactory.getLogger(RocketMsgListener.class) ;
  
  @Resource
  
  ParamConfigService paramConfigService Private;
  
  @Override
  
  public ConsumeConcurrentlyStatus consumeMessage (List <messageext> List, ConsumeConcurrentlyContext context) {
  
  IF (CollectionUtils.isEmpty (List)) {
  
  return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
  
  }
  
  MessageExt messageExt = List.get (0);
  
  log.info ( "the message is received:" + new new String (messageExt.getBody ()));
  
  int reConsume = messageExt.getReconsumeTimes ();
  
  // message has been retried three times, if you do not consume again, success is returned
  
  if ( ==. 3 reConsume) {
  
  return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
  
  }
  
  IF (messageExt.getTopic () the equals (paramConfigService.feePlatTopic)) {.
  
  String Tags messageExt.getTags = ();
  
  Switch (Tags) {
  
  case "FeeAccountTag":
  
  LOG.info("开户 tag == &gt;&gt;"+tags);
  
  break ;
  
  default:
  
  LOG.info("未匹配到Tag == &gt;&gt;"+tags);
  
  break;
  
  }
  
  }
  
  // 消息消费成功
  
  return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
  
  }
  
  }
  
  6、配置参数绑定
  
  import org.springframework.beans.factory.annotation.Value;
  
  import org.springframework.stereotype.Service;
  
  @Service
  
  public class ParamConfigService {
  
  @Value("${cicada-plat.cicada-plat-group}")
  
  public String feePlatGroup ;
  
  @Value("${cicada-plat.cicada-plat-topic}")
  
  public String feePlatTopic ;
  
  @Value("${cicada-plat.cicada-account-tag}")
  
  public String feeAccountTag ;
  
  }
  
  7、消息发送测试
  
  import com.rocket.queue.service.FeePlatMqService;
  
  import org.apache.rocketmq.client.producer.DefaultMQProducer;
  
  import org.apache.rocketmq.client.producer.SendResult;
  
  import org.apache.rocketmq.common.message.Message;
  
  import org.springframework.stereotype.Service;
  
  import javax.annotation.Resource;
  
  @Service
  
  public class FeePlatMqServiceImpl implements FeePlatMqService {
  
  @Resource
  
  private DefaultMQProducer defaultMQProducer;
  
  @Resource
  
  private ParamConfigService paramConfigService ;
  
  @Override
  
  public SendResult openAccountMsg(String msgInfo) {
  
  // 可以不使用Config中的Group
  
  defaultMQProducer.setProducerGroup(paramConfigService.feePlatGroup);
  
  SendResult sendResult = null;
  
  try {
  
  Message sendMsg = new Message(paramConfigService.feePlatTopic,
  
  paramConfigService.feeAccountTag,
  
  "cicada_open_account_key", msgInfo.getBytes());
  
  sendResult = defaultMQProducer.send(sendMsg);
  
  } catch (Exception e) {
  
  e.printStackTrace();
  
  }
  
  return sendResult ;
  
  }
  
  }
  
  三、源代码地址
  

Guess you like

Origin www.cnblogs.com/qwangxiao/p/11200396.html