添加RabbitMQ
parent
c2a1d53180
commit
b2bb3930e9
@ -0,0 +1,43 @@
|
|||||||
|
package com.glxp.api.config.rabbit;
|
||||||
|
|
||||||
|
import org.springframework.amqp.core.Binding;
|
||||||
|
import org.springframework.amqp.core.BindingBuilder;
|
||||||
|
import org.springframework.amqp.core.Queue;
|
||||||
|
import org.springframework.amqp.core.TopicExchange;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class TopicRabbitConfig {
|
||||||
|
//绑定键
|
||||||
|
public final static String common = "topic.common";
|
||||||
|
public final static String trace = "topic.trace";
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Queue firstQueue() {
|
||||||
|
return new Queue(TopicRabbitConfig.trace);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Queue secondQueue() {
|
||||||
|
return new Queue(TopicRabbitConfig.common);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
TopicExchange exchange() {
|
||||||
|
return new TopicExchange("topicExchange");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
Binding bindingExchangeMessage() {
|
||||||
|
return BindingBuilder.bind(firstQueue()).to(exchange()).with(trace);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
Binding bindingExchangeMessage2() {
|
||||||
|
return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
package com.glxp.api.controller.trace;
|
||||||
|
|
||||||
|
import cn.hutool.core.lang.UUID;
|
||||||
|
import com.glxp.api.common.res.BaseResponse;
|
||||||
|
import com.glxp.api.common.util.ResultVOUtils;
|
||||||
|
import com.glxp.api.config.rabbit.TopicRabbitConfig;
|
||||||
|
import com.glxp.api.util.DateUtil;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.amqp.rabbit.core.RabbitTemplate;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.time.LocalDateTime;
|
||||||
|
import java.time.format.DateTimeFormatter;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class TestController {
|
||||||
|
@Resource
|
||||||
|
private final RabbitTemplate rabbitTemplate;
|
||||||
|
|
||||||
|
@GetMapping("/sendUdi")
|
||||||
|
public BaseResponse sendUdi(String key) {
|
||||||
|
String messageId = String.valueOf(UUID.randomUUID());
|
||||||
|
Map<String, Object> messageMap = new HashMap<>();
|
||||||
|
messageMap.put("messageId", messageId);
|
||||||
|
messageMap.put("messageData", key);
|
||||||
|
messageMap.put("createTime", DateUtil.formatDate(new Date()));
|
||||||
|
rabbitTemplate.convertAndSend("topicExchange", TopicRabbitConfig.trace, messageMap);
|
||||||
|
return ResultVOUtils.success("发送成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/sendCommon")
|
||||||
|
public BaseResponse sendCommon(String key) {
|
||||||
|
String messageId = String.valueOf(UUID.randomUUID());
|
||||||
|
Map<String, Object> messageMap = new HashMap<>();
|
||||||
|
messageMap.put("messageId", messageId);
|
||||||
|
messageMap.put("messageData", key);
|
||||||
|
messageMap.put("createTime", DateUtil.formatDate(new Date()));
|
||||||
|
rabbitTemplate.convertAndSend("topicExchange", TopicRabbitConfig.common, messageMap);
|
||||||
|
return ResultVOUtils.success("发送成功");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue