springboot使用rabbitmq记录 1 加入ma

  1. 加入maven依赖

1
2
3
4
xml复制代码<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
  1. 配置rabbitmq

1
2
3
4
5
6
7
yaml复制代码spring:
rabbitmq:
host: localhost
port: 5672
virtual-host: /users
username: users
password: 1234
  1. 生产者测试类

注:单独调用生产者测试类,如果没有消费者监听,并不会生产消息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
typescript复制代码import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@SpringBootTest
@RunWith(SpringRunner.class)
public class RabbitMqProviderTest {

@Autowired
private RabbitTemplate rabbitTemplate;

@Test
//点对点和工作队列模式,发送普通消息
public void sendMsg() {
for (int i=0; i<10; i++) {
rabbitTemplate.convertAndSend("hello", i+" hello world");
}
}

@Test
//交换机模式/广播模式发送消息,引入交换机,路由为空
public void sendExchangeMsg() {
rabbitTemplate.convertAndSend("logs", "", "this is a log");
}

@Test
//路由模式发送消息,相比交换机模式,发送路由key
public void sendRoutingMsg() {
rabbitTemplate.convertAndSend("directs", "info", "this is a info log");
rabbitTemplate.convertAndSend("directs", "error", "this is a error log");
}

@Test
//动态路由模式发送消息,相比路由模式,路由key可以是多个单词,通过分隔符.分割
public void sendTopicMsg() {
rabbitTemplate.convertAndSend("topics", "user.save", "topic test");
rabbitTemplate.convertAndSend("topics", "user.dept.save", "topic test");
}

}
  1. 点对点和工作队列模式 消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
less复制代码import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class HelloConsumer {

@RabbitHandler
@RabbitListener(queuesToDeclare = @Queue("hello"))
public void receive1(String msg) {
System.out.println("msg1=" + msg);
}

@RabbitHandler
@RabbitListener(queuesToDeclare = @Queue("hello"))
public void receive2(String msg) {
System.out.println("msg2=" + msg);
}
}
  1. 交换机模式/广播模式 消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
less复制代码import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

@Component
public class ExchangeConsumer {

@RabbitHandler
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,
exchange = @Exchange(value = "logs", type = "fanout"))
})
public void exchangeMsg1(String msg) {
System.out.println("exchangeMsg1=" + msg);
}

@RabbitHandler
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,
exchange = @Exchange(value = "logs", type = "fanout"))
})
public void exchangeMsg2(String msg) {
System.out.println("exchangeMsg2=" + msg);
}
}
  1. 路由模式 消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
less复制代码import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

@Component
public class RoutingConsumer {

@RabbitHandler
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,
exchange = @Exchange(value = "directs"),
key = {"info", "error"})
})
public void exchangeMsg1(String msg) {
System.out.println("directMsg1=" + msg);
}

@RabbitHandler
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,
exchange = @Exchange(value = "directs"),
key = {"error"})
})
public void exchangeMsg2(String msg) {
System.out.println("directMsg2=" + msg);
}
}
  1. 动态路由模式 消费者

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
less复制代码import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

@Component
public class TopicConsumer {

@RabbitHandler
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,
exchange = @Exchange(value = "topics", type = "topic"),
key = {"user.*"})
})
public void exchangeMsg1(String msg) {
System.out.println("topics1=" + msg);
}

@RabbitHandler
@RabbitListener(bindings = {
@QueueBinding(value = @Queue,
exchange = @Exchange(value = "topics", type = "topic"),
key = {"user.#"})
})
public void exchangeMsg2(String msg) {
System.out.println("topics2=" + msg);
}
}

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%