Spring Boot 整合 AMQP

RabbitMQ 安装 https://blog.csdn.net/kangguang/article/details/104551284

一.添加依赖

<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>

二、添加配置文件信息 

spring.rabbitmq.host=localhost
spring.rabbitmq.post=5672
spring.rabbitmq.username=kangxg
spring.rabbitmq.password=123

二、RabbitMQ 提供了4种不同到Exchange 策略 

  • Direct
  • Fanout
  • Topic
  • Header 不常用

 三、.Direct 策略使用

1.DirectChange配置

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

@Configuration
public class RabbitDirectConfig {
    public  final static  String DIRECTNAME = "kxg-direct";
    @Bean
    Queue queue()
    {
        return  new Queue("hello-queue");
    }

    @Bean
    DirectExchange directExchange()
    {
        return  new DirectExchange(DIRECTNAME,true,false);
    }

    @Bean
    Binding binding()
    {
        return BindingBuilder.bind(queue())
                .to(directExchange())
                .with("direct");
    }
}

DirectChange 三个参数是:名字、重启后是否依然有效以及长期未使用是否删除

directExchange和 binding两个方法可以不添加

2.配置消费者

@Component
public class DrectReceiver {
      private String receiveMsg;

    @RabbitListener(queues = "hello-queue")
    public  void handle1(String msg)
    {
        System.out.println("DirectReceiver:"+msg);
        setReceiveMsg("DirectReceiver:<"+msg+">");
    }

    public String getReceiveMsg() {
        return receiveMsg;
    }

    public void setReceiveMsg(String receiveMsg) {
        this.receiveMsg = receiveMsg;
    }
}

3.在 控制器中添加测试方法 

@RestController
public class RabbitMQController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    DrectReceiver drectReceiver;
    @RequestMapping("/direct")
    public String  direct()
    {
        rabbitTemplate.convertAndSend("hello-queue","来自RabbitMQ 的问候");
        return  drectReceiver.getReceiveMsg();
    }
}

4.使用Postman测试

四、Fanout

1.添加配置文件

@Configuration
public class RabbitFanoutConfig {
    public  final static  String FANOUTNAME = "kxg-fanout";
    @Bean
    Queue queueOne()
    {
        return  new Queue("queue-one");
    }
    @Bean
    Queue queueTwo()
    {
        return  new Queue("queue-two");
    }
    @Bean
    FanoutExchange fanoutExchange()
    {
        return  new FanoutExchange(FANOUTNAME,true,false);
    }

    @Bean
    Binding bindingOne()
    {
        return BindingBuilder.bind(queueOne()).to(fanoutExchange());
    }

    @Bean
    Binding bindingTwo()
    {
        return BindingBuilder.bind(queueTwo()).to(fanoutExchange());
    }
}

2.添加接收者

@Component
public class FanoutReceiver {
    @RabbitListener(queues = "queue-one")
    public  void handle1(String msg)
    {
        System.out.println("FanoutReceiver:handle1<"+msg +">");
    }

    @RabbitListener(queues = "queue-two")
    public  void handle2(String msg)
    {
        System.out.println("FanoutReceiver:handle2<"+msg +">");
    }
}

3.配置控制器

@RestController
public class RabbitMQController {
    @Autowired
    RabbitTemplate rabbitTemplate;

    @RequestMapping("/fanout")
    public void   fanout()
    {
        rabbitTemplate.convertAndSend(RabbitFanoutConfig.FANOUTNAME,null,"来自RabbitMQ Fanout 的问候");

    }

}

4.浏览器测试http://localhost:9000/fanout

4.总结:

Fanout 与Direct不同的是 FanoutExchange的数据交换策略是把所有到达FanoutExchange的消息转发到与它绑定的Queue,routingKey将不再起作用

Direct的路由策略是将消息绑定到一个DirectExchange上,当消息到来时会被转发到与该 条消息routing Key相同的Queue 上

五、Topic策略

Queue通过routingkey 绑定到TopicExchange上,当消息到达TopicExchange后,TopicExchange根据消息当routingkey将消息路由到一个或者多个Queue上

1.添加策略配置文件

@Configuration
public class RabbitTopicConfig {
    public  final static  String TOPICNAME = "kxg-topic";
    @Bean
    TopicExchange topicExchange()
    {
        return  new TopicExchange(TOPICNAME,true,false);
    }
    @Bean
    Queue javaQueue()
    {
        return  new Queue("java");
    }
    @Bean
    Queue ocQueue()
    {
        return  new Queue("object-C");
    }

    @Bean
    Queue softQueue()
    {
        return  new Queue("soft");
    }


    @Bean
    Binding bindingJava()
    {
        return BindingBuilder.bind(javaQueue()).to(topicExchange()).with("java.#");
    }

    @Bean
    Binding bindingOc()
    {
        return BindingBuilder.bind(ocQueue()).to(topicExchange()).with("oc.#");
    }

    @Bean
    Binding bindingSwift()
    {
        return BindingBuilder.bind(softQueue()).to(topicExchange()).with("#.soft.#");
    }
}
  • java.# 匹配routingkey 凡是以java开头的 
  • oc.#匹配routingkey 凡是以oc开头的 
  • #.soft.#匹配routingkey 凡是包含 soft的 

2.添加Topic消息接收者

@Component
public class TopicReceiver {
    @RabbitListener(queues = "soft")
    public  void handle1(String msg)
    {
        System.out.println("SoftReceiver:<"+msg+">");
    }

    @RabbitListener(queues = "java")
    public  void handle2(String msg)
    {
        System.out.println("JavaReceiver:<"+msg+">");
    }

    @RabbitListener(queues = "object-C")
    public  void handle3(String msg)
    {
        System.out.println("OCReceiver:<"+msg+">");
    }
}

3.添加消息发送方法

    @RequestMapping("/topic")
    public void   topic()
    {
        rabbitTemplate.convertAndSend(RabbitTopicConfig.TOPICNAME,"java.language","java语言");
        rabbitTemplate.convertAndSend(RabbitTopicConfig.TOPICNAME,"oc.language","Object-c语言");
        rabbitTemplate.convertAndSend(RabbitTopicConfig.TOPICNAME,"java.soft","Java 开发");
        rabbitTemplate.convertAndSend(RabbitTopicConfig.TOPICNAME,"oc.soft","Object-c 开发");
        rabbitTemplate.convertAndSend(RabbitTopicConfig.TOPICNAME,"soft.language","软件 开发");
    }

4.浏览器访问http://localhost:9000/topic

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot与RabbitMQ的整合可以通过使用Spring AMQP实现。下面是一个简单的步骤: 1. 添加依赖:在`pom.xml`文件中添加以下依赖关系: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId> </dependency> ``` 2. 配置RabbitMQ连接:在`application.properties`或`application.yml`文件中配置RabbitMQ的连接信息,例如: ```properties spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest spring.rabbitmq.password=guest ``` 3. 创建生产者:创建一个简单的生产者,用于向RabbitMQ发送消息。你可以使用`RabbitTemplate`类来发送消息,例如: ```java import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Component public class MessageProducer { @Autowired private RabbitTemplate rabbitTemplate; public void sendMessage(String message) { rabbitTemplate.convertAndSend("exchangeName", "routingKey", message); } } ``` 4. 创建消费者:创建一个简单的消费者,用于接收RabbitMQ发送的消息。你可以使用`@RabbitListener`注解来定义一个消息监听器,例如: ```java import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.stereotype.Component; @Component public class MessageConsumer { @RabbitListener(queues = "queueName") public void receiveMessage(String message) { System.out.println("Received message: " + message); } } ``` 5. 启用RabbitMQ:通过在Spring Boot应用程序的主类上添加`@EnableRabbit`注解来启用RabbitMQ功能,例如: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.ComponentScan; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.amqp.rabbit.annotation.EnableRabbit; @SpringBootApplication @EnableRabbit public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 这就是整合Spring Boot和RabbitMQ的基本步骤。你可以根据自己的需求进行更多的高级配置和定制。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值