|
| 1 | +package com.sohu.tv.mq.cloud.demo; |
| 2 | + |
| 3 | +import com.sohu.index.tv.mq.common.MQMessage; |
| 4 | +import com.sohu.tv.mq.cloud.bo.*; |
| 5 | +import com.sohu.tv.mq.cloud.mq.DefaultCallback; |
| 6 | +import com.sohu.tv.mq.cloud.mq.MQAdminTemplate; |
| 7 | +import com.sohu.tv.mq.cloud.service.*; |
| 8 | +import com.sohu.tv.mq.cloud.util.Result; |
| 9 | +import com.sohu.tv.mq.rocketmq.RocketMQConsumer; |
| 10 | +import com.sohu.tv.mq.rocketmq.RocketMQProducer; |
| 11 | +import org.apache.rocketmq.common.protocol.body.ClusterInfo; |
| 12 | +import org.apache.rocketmq.common.protocol.route.TopicRouteData; |
| 13 | +import org.apache.rocketmq.tools.admin.MQAdminExt; |
| 14 | +import org.slf4j.Logger; |
| 15 | +import org.slf4j.LoggerFactory; |
| 16 | +import org.springframework.beans.factory.annotation.Autowired; |
| 17 | +import org.springframework.boot.CommandLineRunner; |
| 18 | +import org.springframework.stereotype.Component; |
| 19 | + |
| 20 | +import java.util.LinkedList; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Properties; |
| 23 | +import java.util.concurrent.atomic.AtomicLong; |
| 24 | + |
| 25 | +/** |
| 26 | + * demo环境数据初始化 |
| 27 | + * |
| 28 | + * @author: yongfeigao |
| 29 | + * @date: 2022/4/19 10:43 |
| 30 | + */ |
| 31 | +@Component |
| 32 | +public class DemoDataInitializer implements CommandLineRunner { |
| 33 | + private final Logger logger = LoggerFactory.getLogger(getClass()); |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private ServerDataService serverDataService; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private ClusterService clusterService; |
| 40 | + |
| 41 | + @Autowired |
| 42 | + private NameServerService nameServerService; |
| 43 | + |
| 44 | + @Autowired |
| 45 | + private BrokerService brokerService; |
| 46 | + |
| 47 | + @Autowired |
| 48 | + private TopicService topicService; |
| 49 | + |
| 50 | + @Autowired |
| 51 | + private ConsumerService consumerService; |
| 52 | + |
| 53 | + @Autowired |
| 54 | + private UserConsumerService userConsumerService; |
| 55 | + |
| 56 | + @Autowired |
| 57 | + private CommonConfigService commonConfigService; |
| 58 | + |
| 59 | + @Autowired |
| 60 | + private MQAdminTemplate mqAdminTemplate; |
| 61 | + |
| 62 | + @Override |
| 63 | + public void run(String... args) throws Exception { |
| 64 | + String env = System.getProperty("mqcloud.env"); |
| 65 | + if (!"demo".equals(env)) { |
| 66 | + logger.info("mqcloud.env={} return", env); |
| 67 | + return; |
| 68 | + } |
| 69 | + String topicName = "mqcloud-demo-topic"; |
| 70 | + String topicProducer = "mqcloud-demo-topic-producer"; |
| 71 | + String topicConsumer = "mqcloud-demo-consumer"; |
| 72 | + Result<Topic> topicResult = topicService.queryTopic(topicName); |
| 73 | + if (topicResult.isOK()) { |
| 74 | + logger.info("data has initialized!"); |
| 75 | + // 启动生产消费 |
| 76 | + produceAndConsume(topicName, topicProducer, topicConsumer); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + int cid = 1; |
| 81 | + // 1.等待broker注册 |
| 82 | + Cluster cluster = clusterService.getMQClusterById(cid); |
| 83 | + while (true) { |
| 84 | + ClusterInfo clusterInfo = mqAdminTemplate.execute(new DefaultCallback<ClusterInfo>() { |
| 85 | + public ClusterInfo callback(MQAdminExt mqAdmin) throws Exception { |
| 86 | + return mqAdmin.examineBrokerClusterInfo(); |
| 87 | + } |
| 88 | + public ClusterInfo exception(Exception e) { |
| 89 | + logger.error("examineBrokerClusterInfo err", e); |
| 90 | + return null; |
| 91 | + } |
| 92 | + public Cluster mqCluster() { |
| 93 | + return cluster; |
| 94 | + } |
| 95 | + }); |
| 96 | + if (clusterInfo != null && clusterInfo.getBrokerAddrTable() != null && clusterInfo.getBrokerAddrTable().size() > 0) { |
| 97 | + logger.info("brokerClusterInfo:{}", clusterInfo.getBrokerAddrTable()); |
| 98 | + break; |
| 99 | + } |
| 100 | + logger.info("wait broker register to ns, sleep 3000"); |
| 101 | + Thread.sleep(3000); |
| 102 | + } |
| 103 | + |
| 104 | + // 2.添加topic |
| 105 | + Cluster mqCluster = clusterService.getMQClusterById(cid); |
| 106 | + Audit audit = new Audit(); |
| 107 | + audit.setInfo("demo topic"); |
| 108 | + audit.setUid(1); |
| 109 | + AuditTopic auditTopic = new AuditTopic(); |
| 110 | + auditTopic.setName(topicName); |
| 111 | + auditTopic.setQueueNum(8); |
| 112 | + auditTopic.setProducer(topicProducer); |
| 113 | + Result<?> createTopicResult = topicService.createTopic(mqCluster, audit, auditTopic); |
| 114 | + if (createTopicResult.isNotOK()) { |
| 115 | + if (createTopicResult.getException() != null) { |
| 116 | + logger.error("create topic failed", createTopicResult.getException()); |
| 117 | + } else { |
| 118 | + logger.error("create topic failed:{}", createTopicResult.getMessage()); |
| 119 | + } |
| 120 | + // broker直接创建topic |
| 121 | + return; |
| 122 | + } else { |
| 123 | + logger.info("create topic:{} OK!", topicName); |
| 124 | + } |
| 125 | + |
| 126 | + // 3.添加消费者 |
| 127 | + topicResult = topicService.queryTopic(topicName); |
| 128 | + Topic topic = topicResult.getResult(); |
| 129 | + UserConsumer userConsumer = new UserConsumer(); |
| 130 | + userConsumer.setUid(audit.getUid()); |
| 131 | + userConsumer.setTid(topic.getId()); |
| 132 | + Consumer consumer = new Consumer(); |
| 133 | + consumer.setInfo("demo 消费"); |
| 134 | + consumer.setTid(topic.getId()); |
| 135 | + consumer.setName(topicConsumer); |
| 136 | + Result<?> createUserConsumerResult = userConsumerService.saveUserConsumer(cluster, userConsumer, consumer); |
| 137 | + if (createUserConsumerResult.isNotOK()) { |
| 138 | + logger.error("create consumer failed", createUserConsumerResult.getException()); |
| 139 | + return; |
| 140 | + } else { |
| 141 | + logger.info("create consumer:{} OK!", topicConsumer); |
| 142 | + } |
| 143 | + |
| 144 | + // 启动生产消费 |
| 145 | + produceAndConsume(topicName, topicProducer, topicConsumer); |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * 生产和消费 |
| 150 | + * |
| 151 | + * @param topic |
| 152 | + * @param producerGroup |
| 153 | + * @param consumerGroup |
| 154 | + */ |
| 155 | + public void produceAndConsume(String topic, String producerGroup, String consumerGroup) { |
| 156 | + new Thread() { |
| 157 | + public void run() { |
| 158 | + try { |
| 159 | + Result<Topic> topicResult = topicService.queryTopic(topic); |
| 160 | + while (true) { |
| 161 | + TopicRouteData topicRouteData = topicService.route(topicResult.getResult()); |
| 162 | + if (topicRouteData != null && topicRouteData.getBrokerDatas() != null) { |
| 163 | + break; |
| 164 | + } |
| 165 | + logger.info("waiting topic route...{}", topicRouteData); |
| 166 | + Thread.sleep(10000); |
| 167 | + } |
| 168 | + |
| 169 | + // 1.启动消费者 |
| 170 | + RocketMQConsumer rocketMQConsumer = new RocketMQConsumer(consumerGroup, topic); |
| 171 | + rocketMQConsumer.setMqCloudDomain("127.0.0.1:8080"); |
| 172 | + AtomicLong counter = new AtomicLong(); |
| 173 | + rocketMQConsumer.setConsumerCallback((msg, msgExt) -> { |
| 174 | + if (counter.incrementAndGet() % 10 == 0) { |
| 175 | + logger.info("receive msg count:{}", counter.get()); |
| 176 | + } |
| 177 | + }); |
| 178 | + rocketMQConsumer.start(); |
| 179 | + |
| 180 | + // 2.启动生产者 |
| 181 | + RocketMQProducer producer = new RocketMQProducer(producerGroup, topic); |
| 182 | + producer.setMqCloudDomain("127.0.0.1:8080"); |
| 183 | + producer.start(); |
| 184 | + for (int i = 0; i < Long.MAX_VALUE; ++i) { |
| 185 | + try { |
| 186 | + producer.send(MQMessage.build("demo message" + i)); |
| 187 | + Thread.sleep(1000); |
| 188 | + } catch (Exception e) { |
| 189 | + e.printStackTrace(); |
| 190 | + } |
| 191 | + } |
| 192 | + } catch (Exception e) { |
| 193 | + logger.error("start produceAndConsume err", e); |
| 194 | + } |
| 195 | + } |
| 196 | + }.start(); |
| 197 | + } |
| 198 | +} |
0 commit comments