Skip to content

Commit dab9c89

Browse files
committed
添加代码示例springboot-rabbitmq
1 parent 41927e2 commit dab9c89

File tree

12 files changed

+547
-2
lines changed

12 files changed

+547
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ app-manage-api | [实现需要认证授权访问的RESTful API接
5757
每个子项目都可以单独运行,都是打包成jar包后,通过使用内置jetty容器执行,有3种方式运行。:point_right:
5858

5959
1. 在IDEA里面直接运行Application.java的main函数。
60-
2. 另一种方式是执行`mvn clean package`命令后传到linux服务器上面,通过命令`java -jar xxx.jar`方式运行
60+
2. 另一种方式是执行`mvn clean package`命令后传到linux服务器上面,通过命令`java -Xms64m -Xmx1024m -jar xxx.jar`方式运行
6161
3. 在linux服务器上面,配置好jdk、maven、git命令后,通过`git clone sb-xxx`拉取工程后,执行`./run.sh start test`命令来执行
6262

6363
注:每个子项目有自己的README.md文件,告诉你该怎么初始化环境,比如准备好数据库SQL文件等。
@@ -66,7 +66,7 @@ app-manage-api | [实现需要认证授权访问的RESTful API接
6666

6767
``` xml
6868
<modelVersion>4.0.0</modelVersion>
69-
<artifactId>sb-cache</artifactId>
69+
<artifactId>springboot-cache</artifactId>
7070
<packaging>war</packaging>
7171
```
7272

springboot-rabbitmq/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 此为注释– 将被Git 忽略
2+
# /结尾表示是目录,忽略目录和目录下的所有件
3+
# /开头表示根目录,否则是.gitignore的相对目录
4+
# !开头表示反选
5+
.idea/
6+
target/
7+
*.iml
8+
*.ipr
9+
*.iws
10+
*.log
11+
.svn/
12+
.project
13+
rebel.xml
14+
.rebel-remote.xml.*

springboot-rabbitmq/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Xiong Neng
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

springboot-rabbitmq/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
## 集成消息队列RabbitMQ
2+
3+
消息队列RabbitMQ的使用例子,演示Direct模式和广播发送模式。
4+
5+
测试用例:`com.xncoding.service.SenderServiceTest.java`
6+
7+
## 许可证
8+
9+
Copyright (c) 2018 Xiong Neng
10+
11+
基于 MIT 协议发布: <http://www.opensource.org/licenses/MIT>

springboot-rabbitmq/pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.xncoding</groupId>
8+
<artifactId>springboot-rabbitmq</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot-rabbitmq</name>
13+
<description>集成消息队列RabbitMQ</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>1.5.9.RELEASE</version>
19+
<relativePath/>
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<java.version>1.8</java.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-amqp</artifactId>
32+
</dependency>
33+
34+
<dependency>
35+
<groupId>org.springframework.boot</groupId>
36+
<artifactId>spring-boot-starter-test</artifactId>
37+
<exclusions>
38+
<exclusion>
39+
<groupId>com.vaadin.external.google</groupId>
40+
<artifactId>android-json</artifactId>
41+
</exclusion>
42+
</exclusions>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
<version>3.6.1</version>
52+
<configuration>
53+
<!--<proc>none</proc>-->
54+
<source>1.8</source>
55+
<target>1.8</target>
56+
</configuration>
57+
</plugin>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-surefire-plugin</artifactId>
61+
<version>2.20</version>
62+
<configuration>
63+
<skip>true</skip>
64+
</configuration>
65+
</plugin>
66+
<plugin>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-maven-plugin</artifactId>
69+
<executions>
70+
</executions>
71+
</plugin>
72+
</plugins>
73+
74+
<resources>
75+
<resource>
76+
<directory>src/main/resources</directory>
77+
</resource>
78+
<resource>
79+
<directory>src/main/java</directory>
80+
<includes>
81+
<include>**/*.xml</include>
82+
</includes>
83+
</resource>
84+
</resources>
85+
</build>
86+
87+
</project>

springboot-rabbitmq/run.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
# 项目自动更新脚本
3+
# 先clone相应的分支下来:
4+
# git clone ssh://[email protected]:7999/xxx.git
5+
# 远程调试启动:
6+
# nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
7+
8+
function start {
9+
profile="$1"
10+
echo "启动环境profile=${profile}"
11+
jarfile=$(ls target/*.jar)
12+
if [[ "$?" == "0" ]]; then
13+
stop $profile $jarfile
14+
fi
15+
branch=$(git branch |awk '{print $2}')
16+
git pull origin ${branch}
17+
echo "更新完代码开始重新打包"
18+
mvn clean && mvn clean && mvn package -DskipTests=true
19+
if [[ "$?" != "0" ]]; then
20+
echo "编译出错,退出!"
21+
exit 1
22+
fi
23+
echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &"
24+
nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
25+
echo "启动应用中,请查看日志文件..."
26+
}
27+
28+
function stop {
29+
profile="$1"
30+
jarfile="$2"
31+
ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null
32+
if [[ "$?" == "0" ]]; then
33+
echo "该应用还在跑,我先停了它"
34+
pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}')
35+
if [[ "$pid" != "" ]]; then
36+
kill -9 $pid
37+
fi
38+
echo "停止应用成功..."
39+
fi
40+
}
41+
42+
if [[ "$1" == "start" ]]; then
43+
if [[ "$#" < 2 ]]; then
44+
echo "请输入正确参数:./epay.sh start {profile}"
45+
exit 1
46+
fi
47+
profile="$2"
48+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
49+
echo "参数错误,请输入正确的profile参数,使用方法:"
50+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
51+
exit 1
52+
fi
53+
start "${profile}"
54+
elif [[ "$1" == "stop" ]]; then
55+
if [[ "$#" < 2 ]]; then
56+
echo "请输入正确参数:./epay.sh stop {profile}"
57+
exit 1
58+
fi
59+
profile="$2"
60+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
61+
echo "参数错误,请输入正确的profile参数,使用方法:"
62+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
63+
exit 1
64+
fi
65+
jarfile=$(ls target/*.jar)
66+
stop $profile $jarfile
67+
else
68+
echo "参数错误,使用方法:{}参数是必填的,[]参数可选"
69+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
70+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
71+
exit 1
72+
fi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.xncoding.pos;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
12+
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package com.xncoding.pos.config;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.amqp.core.*;
6+
import org.springframework.amqp.rabbit.core.RabbitTemplate;
7+
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
8+
import org.springframework.beans.factory.annotation.Qualifier;
9+
import org.springframework.context.annotation.Bean;
10+
import org.springframework.context.annotation.Configuration;
11+
12+
import javax.annotation.Resource;
13+
14+
/**
15+
* RabbitConfig
16+
*
17+
* @author XiongNeng
18+
* @version 1.0
19+
* @since 2018/3/1
20+
*/
21+
@Configuration
22+
public class RabbitConfig {
23+
@Resource
24+
private RabbitTemplate rabbitTemplate;
25+
26+
/**
27+
* 定制化amqp模版 可根据需要定制多个
28+
* <p>
29+
* <p>
30+
* 此处为模版类定义 Jackson消息转换器
31+
* ConfirmCallback接口用于实现消息发送到RabbitMQ交换器后接收ack回调 即消息发送到exchange ack
32+
* ReturnCallback接口用于实现消息发送到RabbitMQ 交换器,但无相应队列与交换器绑定时的回调 即消息发送不到任何一个队列中 ack
33+
*
34+
* @return the amqp template
35+
*/
36+
// @Primary
37+
@Bean
38+
public AmqpTemplate amqpTemplate() {
39+
Logger log = LoggerFactory.getLogger(RabbitTemplate.class);
40+
// 使用jackson 消息转换器
41+
rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
42+
rabbitTemplate.setEncoding("UTF-8");
43+
// 消息发送失败返回到队列中,yml需要配置 publisher-returns: true
44+
rabbitTemplate.setMandatory(true);
45+
rabbitTemplate.setReturnCallback((message, replyCode, replyText, exchange, routingKey) -> {
46+
String correlationId = message.getMessageProperties().getCorrelationIdString();
47+
log.debug("消息:{} 发送失败, 应答码:{} 原因:{} 交换机: {} 路由键: {}", correlationId, replyCode, replyText, exchange, routingKey);
48+
});
49+
// 消息确认,yml需要配置 publisher-confirms: true
50+
rabbitTemplate.setConfirmCallback((correlationData, ack, cause) -> {
51+
if (ack) {
52+
log.debug("消息发送到exchange成功,id: {}", correlationData.getId());
53+
} else {
54+
log.debug("消息发送到exchange失败,原因: {}", cause);
55+
}
56+
});
57+
return rabbitTemplate;
58+
}
59+
60+
/* ----------------------------------------------------------------------------Direct exchange test--------------------------------------------------------------------------- */
61+
62+
/**
63+
* 声明Direct交换机 支持持久化.
64+
*
65+
* @return the exchange
66+
*/
67+
@Bean("directExchange")
68+
public Exchange directExchange() {
69+
return ExchangeBuilder.directExchange("DIRECT_EXCHANGE").durable(true).build();
70+
}
71+
72+
/**
73+
* 声明一个队列 支持持久化.
74+
*
75+
* @return the queue
76+
*/
77+
@Bean("directQueue")
78+
public Queue directQueue() {
79+
return QueueBuilder.durable("DIRECT_QUEUE").build();
80+
}
81+
82+
/**
83+
* 通过绑定键 将指定队列绑定到一个指定的交换机 .
84+
*
85+
* @param queue the queue
86+
* @param exchange the exchange
87+
* @return the binding
88+
*/
89+
@Bean
90+
public Binding directBinding(@Qualifier("directQueue") Queue queue,
91+
@Qualifier("directExchange") Exchange exchange) {
92+
return BindingBuilder.bind(queue).to(exchange).with("DIRECT_ROUTING_KEY").noargs();
93+
}
94+
95+
/* ----------------------------------------------------------------------------Fanout exchange test--------------------------------------------------------------------------- */
96+
97+
/**
98+
* 声明 fanout 交换机.
99+
*
100+
* @return the exchange
101+
*/
102+
@Bean("fanoutExchange")
103+
public FanoutExchange fanoutExchange() {
104+
return (FanoutExchange) ExchangeBuilder.fanoutExchange("FANOUT_EXCHANGE").durable(true).build();
105+
}
106+
107+
/**
108+
* Fanout queue A.
109+
*
110+
* @return the queue
111+
*/
112+
@Bean("fanoutQueueA")
113+
public Queue fanoutQueueA() {
114+
return QueueBuilder.durable("FANOUT_QUEUE_A").build();
115+
}
116+
117+
/**
118+
* Fanout queue B .
119+
*
120+
* @return the queue
121+
*/
122+
@Bean("fanoutQueueB")
123+
public Queue fanoutQueueB() {
124+
return QueueBuilder.durable("FANOUT_QUEUE_B").build();
125+
}
126+
127+
/**
128+
* 绑定队列A 到Fanout 交换机.
129+
*
130+
* @param queue the queue
131+
* @param fanoutExchange the fanout exchange
132+
* @return the binding
133+
*/
134+
@Bean
135+
public Binding bindingA(@Qualifier("fanoutQueueA") Queue queue,
136+
@Qualifier("fanoutExchange") FanoutExchange fanoutExchange) {
137+
return BindingBuilder.bind(queue).to(fanoutExchange);
138+
}
139+
140+
/**
141+
* 绑定队列B 到Fanout 交换机.
142+
*
143+
* @param queue the queue
144+
* @param fanoutExchange the fanout exchange
145+
* @return the binding
146+
*/
147+
@Bean
148+
public Binding bindingB(@Qualifier("fanoutQueueB") Queue queue,
149+
@Qualifier("fanoutExchange") FanoutExchange fanoutExchange) {
150+
return BindingBuilder.bind(queue).to(fanoutExchange);
151+
}
152+
}

0 commit comments

Comments
 (0)