On the micro-services (VI) from the perspective of actual combat: For basic use of RabbitMQ messaging middleware

1, rabbitMQ mounted at the docker

https://www.rabbitmq.com/download.html

command

docker run  -d --hostname my-rabbit -p 5672:5672 -p 15672:15672 rabbitmq:3.7.15-management

 

Access management interface (default user name and password for the guest)

http://192.168.100.104:15672/

 

2, adding a dependency jar

<!--rocketMQ 开始 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--rocketMQ 结束-->

3, add the configuration

spring:
  rabbitmq:
    host: 192.168.100.104
    port: 5672
    username: guest
    password: guest

4, the received message type is added using


import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/ **
* @ desc: virtual machine configuration docker 104IP start RabbitMQ
* @ Author:.
* @ A Date: 2019/6/17 19:52 in the Created
* /
@ SLF4J
@Component
public class MQReceiver {

//1.0 need to manually create the queue management
// @RabbitListener (Queues = "myQueue")
//2.0 can automatically create queue
// @RabbitListener (queuesToDeclare = @Queue ( "testQueue"))
//3.0 automatically create and Exchange Queue binding
@RabbitListener (@QueueBinding bindings = (
value = @Queue ( "myQueues"),
Exchange @Exchange = ( "myExchanges")
))
public void Process (String messgae) {
log.info ( "== == myQueue IS =: "+ messgae);
}
}

 

5, a test message is sent

import com.gensoft.order.OrderApplicationTests;
import org.junit.Test;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.xml.ws.Action;
import java.util.Date;

/**
* @ desc:
* @ Author .
* @ Date :Created in 19:54 2019/6/17
*/
@Component
public class MQRecieverTest extends OrderApplicationTests {

@Autowired
private AmqpTemplate amqpTemplate;

@Test
public void send(){
amqpTemplate.convertAndSend("myQueues","now is"+new Date());
}

}

Guess you like

Origin www.cnblogs.com/lovechengyu/p/11088406.html