一、基本步骤
首先,在 pom.xml 文件中添加 RabbitMQ 的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
然后,在 application.yml 中添加 RabbitMQ 的配置:
spring:
rabbitmq:
host: localhost # RabbitMQ服务器地址
port: 5672 # RabbitMQ服务器端口
username: guest # RabbitMQ用户名
password: guest # RabbitMQ密码
其中,spring.rabbitmq 是 RabbitMQ 的相关配置。
接下来,创建一个发送者和一个接收者:
发送者 Sender.java:
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class Sender {
@Autowired
private AmqpTemplate amqpTemplate;
public void send(String message) {
amqpTemplate.convertAndSend("myQueue", message);
}
}
接收者 Receiver.java:
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@RabbitListener(queues = "myQueue")
public class Receiver {
@RabbitHandler
public void process(String message) {
System.out.println("Received message: " + message);
}
}
其中,Sender 类使用 AmqpTemplate 发送消息,Receiver 类使用 @RabbitListener 注解监听消息队列。
最后,可以在任意一个 Controller 中调用 Sender 发送消息:
@RestController
public class TestController {
@Autowired
private Sender sender;
@RequestMapping("/send")
public void send() {
sender.send("Hello, World!");
}
}
二、常用搭配配置
# 1. 服务端口号
server:
port: 8080
# 2. 应用上下文路径
server:
servlet:
context-path: /
# 3. 应用名称
spring:
application:
name: my-application
# 4. 数据源配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/mydatabase
username: myusername
password: mypassword
driver-class-name: com.mysql.jdbc.Driver
# 5. MyBatis 配置
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.example.entity
# 6. Redis 配置
spring:
redis:
host: localhost
port: 6379
password: mypassword
database: 0
# 7. 日志级别
logging:
level:
root: info
com.example: debug
# 8. 调试端口
spring:
devtools:
remote:
port: 8000
# 9. 静态资源配置
spring:
resources:
static-locations: classpath:/static/
# 10. Thymeleaf 配置
spring:
thymeleaf:
prefix: classpath:templates/
suffix: .html
# 11. 自定义属性配置
my:
custom:
property-value: abc123
# 12. 多环境配置
spring:
profiles:
active: dev
---
spring:
profiles: dev
datasource:
url: jdbc:mysql://localhost:3306/dev_database
---
spring:
profiles: prod
datasource:
url: jdbc:mysql://localhost:3306/prod_database
# 13. 安全配置
spring:
security:
user:
name: admin
password: secret
basic:
enabled: true
# 14. Actuator 配置
management:
endpoints:
web:
exposure:
include: '*'
# 15. 监控配置
spring:
actuator:
metrics:
export:
prometheus:
enabled: true
# 16. 消息转换器配置
spring:
http:
converters:
preferred-json-mapper: jackson
# 17. RestTemplate 配置
my:
rest:
timeout: 5000
max-connections: 50
# 18. Feign 配置
feign:
hystrix:
enabled: true
# 19. Ribbon 配置
ribbon:
eureka:
enabled: true
# 20. Hystrix 配置
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
# 21. Spring Cloud Config 配置
spring:
cloud:
config:
uri: http://localhost:8888
profile: myprofile
label: mybranch
# 22. Eureka 配置
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
# 23. Zuul 配置
zuul:
routes:
api:
path: /api/**
url: http://localhost:8080
# 24. Zipkin 配置
spring:
zipkin:
base-url: http://localhost:9411
# 25. Sleuth 配置
spring:
sleuth:
sampler:
probability: 0.5
# 26. Spring Cloud Bus 配置
spring:
cloud:
bus:
enabled: true
# 27. Spring Cloud Stream 配置
spring:
cloud:
stream:
default:
consumer:
concurrency: 3
producer:
partition-count: 3
required-groups: group1,group2
# 28. Kafka 配置
spring:
kafka:
bootstrap-servers: localhost:9092
consumer:
group-id: my-group
producer:
acks: all
# 29. Hazelcast 配置
hazelcast:
network:
port:
auto-increment: true
tcp-ip:
enabled: true
members: 127.0.0.1
# 30. MongoDB 配置
spring:
data:
mongodb:
uri: mongodb://myusername:mypassword@localhost:27017/mydatabase
其他的整合例子可以参考本合集的其他内容,获取更多整合例子。