Installation rabbitMq (docker installation)
docker search rabbitmq --- mirrored search rabbitmq docker pull rabbitmq: 3.7.7-management --- mirror pulling docker run -d --name rabbitmq3.7.7 -p 5672: 5672 -p 15672: 15672 2888deb59dfc - starting container
After installation access ip: 15672 into the initial end of the web management guest account password guest (parameter may specify -e RABBITMQ_DEFAULT_USER on startup command = admin -e RABBITMQ_DEFAULT_PASS = admin)
New users and virtual hosts used in the project
Click admin users to add Web Hosting /
Programming test
Add new spring boot project relies rabbitmq
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
Adding mq configuration class
@Configuration public class RabbitMQConfig { /** * Creating a Switch * @return */ @Bean public FanoutExchange fanoutExchange(){ return new FanoutExchange("my_exchange"); } /** * Create swap queue * @return */ @Bean public Queue autoDeleteQueue() { return new Queue("my_queue", true, false, true, null); } /** * Carry out switches and queue bindings * @Param fanoutExchange switch * @Param autoDeleteQueue queue * @return */ @Bean public Binding binding(FanoutExchange fanoutExchange, Queue autoDeleteQueue) { return BindingBuilder.bind(autoDeleteQueue).to(fanoutExchange); } }
The client listens class
@Component public class ConsumerListener { @RabbitListener(queues = "my_queue") public void reciveMessage(Message message){ String msg=new String(message.getBody()); System.out.println(msg); } }
After starting the project view web end
Has completed the switch to create and manage the use of binding the client sends a message to see the result listener
result
The program sends a message
@RestController public class ProviderController { @Autowired private RabbitTemplate rabbitTemplate; @GetMapping("sendMessage") public String sendMessage(){ rabbitTemplate.convertAndSend ( "my_exchange", "", "I am the news provider sends a message"); return "success"; } }
Browser to access the trigger
The client receives the message