Skip to content

Commit c675317

Browse files
committed
第八篇 : SpringBoot 整合 rabbitmq
1 parent b79680a commit c675317

9 files changed

Lines changed: 227 additions & 0 deletions

File tree

springboot-rabbitmq/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
47.2 KB
Binary file not shown.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.5.4/apache-maven-3.5.4-bin.zip

springboot-rabbitmq/pom.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.gf</groupId>
7+
<artifactId>springboot-rabbitmq</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>springboot-rabbitmq</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>2.0.5.RELEASE</version>
18+
<relativePath/> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-amqp</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-web</artifactId>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-test</artifactId>
40+
<scope>test</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<plugins>
46+
<plugin>
47+
<groupId>org.springframework.boot</groupId>
48+
<artifactId>spring-boot-maven-plugin</artifactId>
49+
</plugin>
50+
</plugins>
51+
</build>
52+
53+
54+
</project>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.gf;
2+
3+
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
4+
import org.springframework.boot.SpringApplication;
5+
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
7+
/**
8+
* 自动配置
9+
* 1. RabbitAutoConfiguration
10+
* 2. 自动配置了连接工厂ConnectionFactory
11+
* 3. RabbitProperties 封装了RabbitMQ的配置
12+
* 4. RabbitTemplate : 给RabbitMQ发送和接受消息
13+
* 5. AmqpAdmin : RabbitMQ系统管理功能组件
14+
* 6. @EnableRabbit + @RabbitListener
15+
*/
16+
@EnableRabbit
17+
@SpringBootApplication
18+
public class SpringbootRabbitmqApplication {
19+
20+
public static void main(String[] args) {
21+
SpringApplication.run(SpringbootRabbitmqApplication.class, args);
22+
}
23+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.gf.config;
2+
3+
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.amqp.support.converter.MessageConverter;
7+
8+
/**
9+
* 自定义消息转换器,默认是jdk的序列化转换器,我们自定义为json的
10+
*/
11+
@Configuration
12+
public class MyAMQPConfig {
13+
14+
@Bean
15+
public MessageConverter messageConverter() {
16+
return new Jackson2JsonMessageConverter();
17+
}
18+
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.gf.service;
2+
3+
4+
import org.springframework.amqp.core.Message;
5+
import org.springframework.amqp.rabbit.annotation.RabbitListener;
6+
import org.springframework.stereotype.Service;
7+
8+
@Service
9+
public class MQService {
10+
11+
@RabbitListener(queues = "fanout.queue")
12+
public void receive(Message message) {
13+
System.out.println("收到消息 : " + new String(message.getBody()));
14+
15+
}
16+
17+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spring.rabbitmq.host=127.0.0.1
2+
spring.rabbitmq.username=guest
3+
spring.rabbitmq.password=guest
4+
#spring.rabbitmq.virtual-host=
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.gf;
2+
3+
import org.junit.Test;
4+
import org.junit.runner.RunWith;
5+
import org.springframework.amqp.core.AmqpAdmin;
6+
import org.springframework.amqp.core.Binding;
7+
import org.springframework.amqp.core.DirectExchange;
8+
import org.springframework.amqp.core.FanoutExchange;
9+
import org.springframework.amqp.core.Queue;
10+
import org.springframework.amqp.core.TopicExchange;
11+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
12+
import org.springframework.beans.factory.annotation.Autowired;
13+
import org.springframework.boot.test.context.SpringBootTest;
14+
import org.springframework.test.context.junit4.SpringRunner;
15+
16+
import java.util.Arrays;
17+
import java.util.HashMap;
18+
import java.util.Map;
19+
20+
@RunWith(SpringRunner.class)
21+
@SpringBootTest
22+
public class SpringbootRabbitmqApplicationTests {
23+
24+
@Autowired
25+
RabbitTemplate rabbitTemplate;
26+
27+
@Autowired
28+
AmqpAdmin amqpAdmin;
29+
30+
@Test
31+
public void contextLoads() {
32+
}
33+
34+
@Test
35+
public void create(){
36+
// //创建Exchange
37+
// amqpAdmin.declareExchange( new DirectExchange( "exchange.direct") );
38+
// amqpAdmin.declareExchange( new FanoutExchange( "exchange.fanout") );
39+
// amqpAdmin.declareExchange( new TopicExchange( "exchange.topic") );
40+
41+
//创建Queue
42+
amqpAdmin.declareQueue( new Queue( "direct.queue" , true ) );
43+
amqpAdmin.declareQueue( new Queue( "fanout.queue" , true ) );
44+
45+
//绑定Queue
46+
amqpAdmin.declareBinding( new Binding( "direct.queue" , Binding.DestinationType.QUEUE , "exchange.direct" , "direct.queue" , null ) );
47+
amqpAdmin.declareBinding( new Binding( "fanout.queue" , Binding.DestinationType.QUEUE , "exchange.direct" , "fanout.queue" , null ) );
48+
amqpAdmin.declareBinding( new Binding( "direct.queue" , Binding.DestinationType.QUEUE , "exchange.fanout" , "" , null ) );
49+
amqpAdmin.declareBinding( new Binding( "fanout.queue" , Binding.DestinationType.QUEUE , "exchange.fanout" , "" , null ) );
50+
amqpAdmin.declareBinding( new Binding( "direct.queue" , Binding.DestinationType.QUEUE , "exchange.topic" , "direct.#" , null ) );
51+
amqpAdmin.declareBinding( new Binding( "fanout.queue" , Binding.DestinationType.QUEUE , "exchange.topic" , "direct.*" , null ) );
52+
53+
54+
}
55+
56+
@Test
57+
public void send2Direct() {
58+
Map<String , Object> map = new HashMap<>();
59+
map.put( "msg" , "这是一条点对点消息" );
60+
map.put( "data" , Arrays.asList("helloworld" , 123 , true) );
61+
62+
rabbitTemplate.convertAndSend( "exchange.direct" , "direct.queue" , map );
63+
64+
}
65+
66+
@Test
67+
public void send2Topic() {
68+
Map<String , Object> map = new HashMap<>();
69+
map.put( "msg" , "这是一条广播消息" );
70+
map.put( "data" , Arrays.asList("topic消息" , 123 , true) );
71+
72+
rabbitTemplate.convertAndSend( "exchange.fanout" , "", map );
73+
74+
}
75+
76+
@Test
77+
public void receive() {
78+
Object o = rabbitTemplate.receiveAndConvert( "direct.queue" );
79+
o.getClass();
80+
System.out.println(o.getClass());
81+
System.out.println(o);
82+
}
83+
84+
}

0 commit comments

Comments
 (0)