Android knock tired, play with Java

The installation

This is where I used the Docker to install RabbitMQ. Note that the docker login must use the docker ID login to download the image, the email cannot use the pull command.

Docker search for RabbitMQ

docker search rabbitmq:management

Download the rabbitmq

docker pull rabbitmq:management

Run the rabbitmq

docker run -d --name rabbitmq -p 5672:5672 -p 15672:15672 docker.io/rabbitmq:3-management


Can run the installation is complete, then enter http://http://localhost:15672/, the browser to jump to the RabbitMQ login interface, enter the default user name and password, the user name and password, as are all guest.

Guest’s account can only restrict localhost access, so we’ll have to create a new account

Add user

Log in to the Guest account and select the Admin box

Then Add Username, password, and Tags, which give the highest permission administrator, and finally click “Add user” to Add the user

At this time, the added user is No access, which is not accessible. We still need to set permission for him

Then click the root user to manage the root user, click “Set Permission” to add permission, the yellow line above will disappear

use

Project directory structure diagram

Adding Maven dependencies

<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> . < version > 2.0.0 RELEASE < / version > < / parent > < properties > < project. Build. SourceEncoding > utf-8 < / project. Build. SourceEncoding > < Java version > 1.8 < / Java version > < / properties > <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId>  </dependency> <! --rabbitMQ--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId>  </dependency> </dependencies>Copy the code

The Main start class

@SpringBootApplication public class Main { public static void main(String[] args) { SpringApplication.run(Main.class, args); }}Copy the code

RabbitMQConfig configuration class

@configuration public class RabbitMQConfig {/** * create a queue named Hello * @return
     */
    @Bean
    public Queue queue() {
        return new Queue("hello"); }}Copy the code

RabbitProducerController Producer controller

@RestController public class RabbitProducerController { @Autowired private AmqpTemplate rabbitTemplate; /** * The producer sends a message to the Hello message queue * @param name * @return
     */
    @RequestMapping("/produce")
    public String produce(@RequestParam String name) {
        rabbitTemplate.convertAndSend("hello"."send msg is " + name);
        return "Message sent successfully"; }}Copy the code

RabbitConsumer consumers

@Component
@RabbitListener(queues = "hello")
public class RabbitConsumer {

    @RabbitHandler
    public void process(String msg) {
        System.out.println("-- -- -- -- -- -- -- -- -- -- -- --"+ msg); }}Copy the code

Application. The properties configuration

# configuration the rabbitMQSpring. The rabbitmq. Host = 127.0.0.1 spring. The rabbitmq. Port = 5672 spring. The rabbitmq. Username = root spring. The rabbitmq. Password = rootCopy the code

We then run the Main method of the Main class to launch SpringBoot

Type http://localhost:8080/produce? in your browser name=lisi

Then see that the consumer has received the message and output it