pom.xml
file.<!--rabbitmq--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency>
spring:rabbitmq:# The host address can be obtained in the console. You can also use the RabbitMQ service addresshost: amqp-xx.rabbitmq.x.tencenttdmq.comport: 5672# Name of the role to be used, which can be obtained on the **Role Management** page in the consoleusername: admin# Role tokenpassword: eyJrZXlJZ....# Full name of vhost, which can be obtained on the **Vhost** tab in the consolevirtual-host: amqp-xxx|Vhost
Parameter | Description |
host | Cluster access address, which can be obtained from the Client Access section on the Basic Info page of the cluster. |
port | Cluster access address, which can be obtained from the Client Access section on the Basic Info page of the cluster. |
username | Enter the name of the user created in the console. |
password | Enter the password of the user created in the console. |
virtual-host | Vhost name, which can be obtained from the vhost list in the console. |
Fanout exchange
as an example)./*** Fanout exchange configuration*/@Configurationpublic class FanoutRabbitConfig {/*** Exchange*/@Beanpublic FanoutExchange fanoutExchange() {return new FanoutExchange("fanout-logs", true, false);}/*** Message queue*/@Beanpublic Queue fanoutQueueA() {return new Queue("ps_queue", true);}@Beanpublic Queue fanoutQueueB() {// You can use this method to bind a dead letter queueMap<String, Object> requestParam = new HashMap<>();requestParam.put("x-dead-letter-exchange", "my-deadLetter-exchange");// Set the message validity periodrequestParam.put("x-message-ttl", 60000);return new Queue("ps_queue1", true, false,true, requestParam);}/*** Bind the message queue to the exchange*/@Beanpublic Binding bindingFanoutA() {return BindingBuilder.bind(fanoutQueueA()).to(fanoutExchange());}@Beanpublic Binding bindingFanoutB() {return BindingBuilder.bind(fanoutQueueB()).to(fanoutExchange());}}
Parameter | Description |
fanout-logs | Bound exchange name, which can be obtained from the exchange list in the console. |
ps_queue | Name of the first queue bound to the exchange, which can be obtained from the queue list in the console. |
my-deadLetter-exchange | Dead letter exchange name, which can be obtained from the exchange list in the console. |
ps_queue1 | Name of the second queue bound to the exchange, which can be obtained from the queue list in the console. |
DemoApplication.java
and use RabbitTemplate
to send message.@Autowiredprivate RabbitTemplate rabbitTemplate;public String send() {String msg = "This is a new message.";// Send the message// Parameter description: Parameter 1: Exchange name, which can be obtained from the exchange list in the console. Parameter 2: Routing key. Parameter 3: Message contentrabbitTemplate.convertAndSend("direct_logs", "", msg);return "success";}
FanoutReceiver.java
(with Fanout exchange
as an example).@Componentpublic class FanoutReceiver {// Register a listener to listen on the specified message queue@RabbitHandler@RabbitListener(queues = "ps_queue") // Name of the queue bound to the exchange, which can be obtained from the queue list in the consolepublic void listenerPsQueue(String msg) {// Business processing...System.out.println("(ps_queue) receiver message. [" + msg + "]");}}
Was this page helpful?