searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

Spring和rabbitmq整合

2024-06-24 09:35:39
36
0

将Spring和RabbitMQ整合在一起,可以利用Spring AMQP(Advanced Message Queuing Protocol)项目提供的功能。Spring AMQP为基于AMQP协议的消息系统(如RabbitMQ)提供了模板和注解支持,使得与RabbitMQ的整合变得简单而高效。

以下是一个基本的Spring Boot应用程序与RabbitMQ整合的示例:

1. 添加依赖

首先,在你的Spring Boot项目的pom.xml文件中添加以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2. 配置RabbitMQ

在你的application.propertiesapplication.yml文件中配置连接RabbitMQ的相关信息:

application.properties

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

application.yml

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

3. 创建配置类和消息处理类

创建一个配置类来定义队列、交换机和绑定:

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    public static final String QUEUE_NAME = "myQueue";
    public static final String EXCHANGE_NAME = "myExchange";
    public static final String ROUTING_KEY = "myRoutingKey";

    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, false);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
}

创建一个消息发送者组件:

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {

    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public MessageSender(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendMessage(String message) {
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME, RabbitConfig.ROUTING_KEY, message);
    }
}

创建一个消息接收者组件:

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MessageReceiver {

    @RabbitListener(queues = RabbitConfig.QUEUE_NAME)
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}

4. 启动类

确保你的Spring Boot应用启动类上有@SpringBootApplication注解:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RabbitMqTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(RabbitMqTestApplication .class, args);
    }
}

5. 运行测试

你可以创建一个Controller或直接在启动类中测试发送和接收消息:

javaCopy Code
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class Tester implements CommandLineRunner {

    private final MessageSender messageSender;

    @Autowired
    public Tester (MessageSender messageSender) {
        this.messageSender = messageSender;
    }

    @Override
    public void run(String... args) throws Exception {
        messageSender.sendMessage("Hello, RabbitMQ!");
    }
}

启动Spring Boot应用程序,你应该会在控制台中看到接收到的消息输出。

这样就完成了一个基本的Spring与RabbitMQ的整合,可以在此基础上根据具体需求进行扩展和优化。

0条评论
0 / 1000
1****m
2文章数
0粉丝数
1****m
2 文章 | 0 粉丝
1****m
2文章数
0粉丝数
1****m
2 文章 | 0 粉丝
原创

Spring和rabbitmq整合

2024-06-24 09:35:39
36
0

将Spring和RabbitMQ整合在一起,可以利用Spring AMQP(Advanced Message Queuing Protocol)项目提供的功能。Spring AMQP为基于AMQP协议的消息系统(如RabbitMQ)提供了模板和注解支持,使得与RabbitMQ的整合变得简单而高效。

以下是一个基本的Spring Boot应用程序与RabbitMQ整合的示例:

1. 添加依赖

首先,在你的Spring Boot项目的pom.xml文件中添加以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2. 配置RabbitMQ

在你的application.propertiesapplication.yml文件中配置连接RabbitMQ的相关信息:

application.properties

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

application.yml

spring:
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

3. 创建配置类和消息处理类

创建一个配置类来定义队列、交换机和绑定:

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    public static final String QUEUE_NAME = "myQueue";
    public static final String EXCHANGE_NAME = "myExchange";
    public static final String ROUTING_KEY = "myRoutingKey";

    @Bean
    public Queue queue() {
        return new Queue(QUEUE_NAME, false);
    }

    @Bean
    public TopicExchange exchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTING_KEY);
    }
}

创建一个消息发送者组件:

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class MessageSender {

    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public MessageSender(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void sendMessage(String message) {
        rabbitTemplate.convertAndSend(RabbitConfig.EXCHANGE_NAME, RabbitConfig.ROUTING_KEY, message);
    }
}

创建一个消息接收者组件:

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class MessageReceiver {

    @RabbitListener(queues = RabbitConfig.QUEUE_NAME)
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}

4. 启动类

确保你的Spring Boot应用启动类上有@SpringBootApplication注解:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class RabbitMqTestApplication {

    public static void main(String[] args) {
        SpringApplication.run(RabbitMqTestApplication .class, args);
    }
}

5. 运行测试

你可以创建一个Controller或直接在启动类中测试发送和接收消息:

javaCopy Code
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class Tester implements CommandLineRunner {

    private final MessageSender messageSender;

    @Autowired
    public Tester (MessageSender messageSender) {
        this.messageSender = messageSender;
    }

    @Override
    public void run(String... args) throws Exception {
        messageSender.sendMessage("Hello, RabbitMQ!");
    }
}

启动Spring Boot应用程序,你应该会在控制台中看到接收到的消息输出。

这样就完成了一个基本的Spring与RabbitMQ的整合,可以在此基础上根据具体需求进行扩展和优化。

文章来自个人专栏
文子的云原生
2 文章 | 1 订阅
0条评论
0 / 1000
请输入你的评论
0
0