需求:确定两个平台之间能够正常通信,以及业务模块在绑定授权码后像控制平台发送一条消息
代码实现:
application.yml
rocketmq:
name-server: 199.199.199.199:9876;199.199.199.199:9876
#name-server: 127.0.0.1:9876
producer:
group: sveco-project-be-parent-dev
send-message-timeout: 30000
#发送ams开通/关闭服务结果topic
topicServiceSwitchResult: 004_ams_service_switch_result
consumer:
#接收ams申请开通/关闭服务topic
topicServiceSwitch: 004_ams_project_service_switch
xml
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.1.0</version>
</dependency>
ServiceSwitchConsumer
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Optional;
@Slf4j
@Service
@RocketMQMessageListener(topic = "${rocketmq.consumer.topicServiceSwitch}", consumerGroup = "${rocketmq.producer.group}")
public class ServiceSwitchConsumer implements RocketMQListener<MessageExt> {
@Autowired
ServiceSwitchProducer serviceSwitchProducer;
@SneakyThrows
@Override
public void onMessage(MessageExt messageExt) {
String s = new String(messageExt.getBody(), "UTF-8");
log.debug("收到消息:{}", s);
JSONObject messageJson = JSONObject.parseObject(s);
//避免空指针
JSONObject serviceSwitchJson = Optional.ofNullable(messageJson).map(p -> p.getJSONObject("data"))
.orElse(new JSONObject());
serviceSwitchJson.put("respCode", "0");
serviceSwitchJson.put("respMsg", "成功");
serviceSwitchJson.put("respMsgEn", "SUCCESS");
serviceSwitchJson.put("errorCode", "0");
serviceSwitchProducer.sendMsg(serviceSwitchJson);
}
}
ServiceSwitchProducer
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Service;
import java.util.Optional;
import java.util.UUID;
@Slf4j
@Service
public class ServiceSwitchProducer {
@Autowired
private RocketMQTemplate rocketMQTemplate;
/**
* 组名
*/
@Value("${rocketmq.producer.topicServiceSwitchResult}")
String serviceSwitchResultTopic;
/**
* 发送消息
*
* @param serviceSwitchJson
*/
public void sendMsg(JSONObject serviceSwitchJson) {
log.debug("开始发送服务开/关结果消息");
String spCategoryName = Optional.ofNullable(serviceSwitchJson).map(m -> m.getString("spCategoryName"))
.orElse("video");
JSONObject headers = new JSONObject();
headers.put("messageTraceId", UUID.randomUUID());
headers.put("messageSource", "project-service");
headers.put("sourceTimeStamp", System.currentTimeMillis());
headers.put("producerName", "project-service");
headers.put("project_id", "project_id");
JSONObject jsonObject = new JSONObject();
jsonObject.put("data", serviceSwitchJson);
jsonObject.put("headers", headers);
Message<?> message = MessageBuilder.withPayload(jsonObject).build();
log.debug("返回结果报文:{}", jsonObject);
SendResult sendResult = rocketMQTemplate.syncSend(serviceSwitchResultTopic + ":" + spCategoryName, message);
log.debug("发送结果:{}", sendResult);
}
}