SpringBoot+RabbitMQ的简单示例

在SpringBoot中配置RabbitMQ

添加maven坐标:

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

配置application.properties,设置连接地址,端口,用户名,密码:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
实现发送和接收对象

定义一个简单的对象,要实现Serializable:

public class User implements Serializable {

    private String id;
    private String name;
    private String address;
    ..省略构造函数 get set
}

在配置类中定义队列:

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {
	//定义一个名为queue.a,持久化,非排他,非自动删除的队列
    @Bean
    public Queue queueA()
    {
        return new Queue("queue.a",true,false,false);
    }
}

发送对象,使用RabbitTemplate:

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SenderService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public void sendByRabbitTemplate()
    {	//queue.a为之前定义的队列
        rabbitTemplate.convertAndSend("queue.a",new User("swe111","jack","福建省"));
    }
}

接收对象:

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
//接收来自队列queue.a
@Service
@RabbitListener(queues = {"queue.a"})
public class ReceiverService {
//接收消息处理函数
    @RabbitHandler
    public void receiver(User user)
    {
        System.out.println(user);
    }
}
实现发送接收多个主题(topic)
topic交换器模式下根据routing key来确定要投递的队列,
routing key 为一个“.”号分隔的字符串,如com.rabbit.client。
routing key 中也会出现“*”和“#”,“*”匹配一个字母,“#”匹配多个字母

在配置类中配置队列和交换器:

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {
    @Bean
    public Queue queueA()
    {
        return new Queue("queue.a",true,false,false);
    }
    @Bean
    public Queue queueB()
    {
        return new Queue("queue.b",true,false,false);
    }
    //定义topic交换器
    @Bean
    public TopicExchange topicExchange()
    {
        return new TopicExchange("topicExchange",true,false,null);
    }
    @Bean
    public Binding queueBindingA(Queue queueA,TopicExchange topicExchange)
    {	//将队列绑定至交换器,并指定routing key
        return BindingBuilder.bind(queueA).to(topicExchange).with("queue.a");
    }
    @Bean
    public Binding queueBindingB(Queue queueB,TopicExchange topicExchange)
    {	//将队列绑定至交换器,并指定routing key
        return BindingBuilder.bind(queueB).to(topicExchange).with("queue.#");
    }
}

发送消息

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

@Service
public class SenderService {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public void sendByRabbitTemplate()
    {	//发送时要指定交换器和routing key,否则会将消息发送至默认交换器,routing key将不起作用
        rabbitTemplate.convertAndSend("topicExchange","queue.a",
                new User("swe111","zxy","福建省"));
    }
}
Spring Boot是一个非常流行的Java Web框架,它简化了Java应用程序的开发和部署过程。 RabbitMQ是一个开源的消息代理,它支持多种协议,包括AMQP、STOMP和MQTT等。TLS协议则是一种加密传输协议,它可以保证数据在传输过程中的安全性。 在Spring Boot应用程序中使用RabbitMQ需要引入相应的依赖,可以使用Maven或Gradle来进行管理。同时,为了保证消息的安全传输,我们可以使用TLS协议对消息进行加密传输。 以下是使用Spring BootRabbitMQ进行消息传输并加密的简单示例: 1. 引入依赖 在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-rsa</artifactId> </dependency> ``` 2. 配置RabbitMQ和TLS 在application.properties文件中添加以下配置: ```properties spring.rabbitmq.host=localhost spring.rabbitmq.port=5671 spring.rabbitmq.username=user spring.rabbitmq.password=password spring.rabbitmq.ssl.enabled=true spring.rabbitmq.ssl.key-store=file:/path/to/keystore spring.rabbitmq.ssl.key-store-password=changeit spring.rabbitmq.ssl.trust-store=file:/path/to/truststore spring.rabbitmq.ssl.trust-store-password=changeit ``` 其中,key-store和trust-store分别为用于TLS加密的密钥库和信任库文件路径,需要根据实际情况进行配置。 3. 发送和接收消息 在Spring Boot应用程序中使用RabbitTemplate来发送和接收消息,示例代码如下: ```java @Service public class RabbitMQService { @Autowired private RabbitTemplate rabbitTemplate; public void send(String message) { rabbitTemplate.convertAndSend("exchange", "routingKey", message); } @RabbitListener(queues = "queue") public void receive(String message) { System.out.println("Received message: " + message); } } ``` 其中,send方法用于发送消息,receive方法用于接收消息。在这个例子中,我们将消息发送到名为exchange的交换机,使用名为routingKey的路由键进行路由,然后将消息发送到名为queue的队列中进行消费。 以上就是在Spring Boot应用程序中使用RabbitMQ和TLS进行消息传输的简单示例。需要注意的是,这只是一个基本的示例,实际应用中还需要进行更多的配置和处理,以确保消息传输的安全和可靠性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值