springboot整合rabbitmq

本文介绍了如何将SpringBoot与RabbitMQ进行整合,包括在pom.xml中添加RabbitMQ依赖,设置RabbitMQ服务器配置,创建发送者和接收者类,以及在Controller中调用发送消息的方法。同时,文章还列举了常用的应用配置如服务器端口、数据源、MyBatis、Redis、安全、监控等。
摘要由CSDN通过智能技术生成

一、基本步骤

首先,在 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

其他的整合例子可以参考本合集的其他内容,获取更多整合例子。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

源城编程哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值