2.3rabbitMq整合springboot

rabbitMq整合springboot

github地址:https://github.com/Plumblumpb/messageQueue-.git
rabbitmq(可以先看这两篇文章):
https://blog.csdn.net/c_royi/article/details/86630777
https://blog.csdn.net/c_royi/article/details/86690054

 整体配置

pom.xml文件

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-amqp</artifactId>
	<version>2.0.3.RELEASE</version>
</dependency>
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-test</artifactId>
   <version>2.0.3.RELEASE</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-test</artifactId>
   <version>5.0.8.RELEASE</version>
   <scope>test</scope>
</dependency>
<dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
   <scope>test</scope>
</dependency>

application.properties

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
#spring.rabbitmq.username=admin
#spring.rabbitmq.password=123456

 1.queue模式

config配置文件

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

/**
 * @Auther: cpb
 * @Date: 2019/1/29 09:23
 * @Description:
 */
@Configuration
public class QueueConfig {

    @Bean
    public Queue queue(){
        return new Queue("queue_demo");
    }

}

生产者

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: cpb
 * @Date: 2019/1/29 09:25
 * @Description:
 */
@RestController
public class ProduceController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public String queue(){
        String message = "hello queue_demo";
        String routingKey = "queue_demo";
        for(int i = 0; i<10; i++) {
            rabbitTemplate.convertAndSend(routingKey, message+" "+i);
        }
        return message;
    }
}

消费者

//消费者1
@Component
@RabbitListener(queues = "queue_demo")
public class Consumer1 {

    @RabbitHandler
    public void process(String message){
        System.out.println("consumer1:"+message);
    }

}
//消费者2
@Component
@RabbitListener(queues = "queue_demo")
public class Consumer2 {

    @RabbitHandler
    public void process(String message){
        System.out.println("consumer2:"+message);
    }

}

测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class QueueTest {
    @Autowired
    private ProduceController produceController;

    @Test
    public void sendMessage(){
        produceController.queue();
    }
}

 2.topic模式

config配置文件

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

/**
 * @Auther: cpb
 * @Date: 2019/1/29 10:13
 * @Description:
 */
@Configuration
public class TopicConfig {
    @Bean
    public Queue AMessage() {
        return new Queue("topic_A");
    }
    @Bean
    public Queue BMessage() {
        return new Queue("topic_B");
    }
    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("topic_exchange_demo");
    }
    @Bean
    Binding bindingExchangeA(Queue AMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(AMessage).to(fanoutExchange);
    }
    @Bean
    Binding bindingExchangeB(Queue BMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(BMessage).to(fanoutExchange);
    }
}

生产者

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: cpb
 * @Date: 2019/1/29 09:25
 * @Description:
 */
@RestController
public class TopicProduceController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    public String topic(){
        String message = "hello topic_demo";
        String routingKey = "topic_exchange_demo";
        String exchange = "topic_exchange_demo";
        rabbitTemplate.convertAndSend(exchange,routingKey, message);
        return message;
    }

}

消费者

//消费者1
@Component
@RabbitListener(queues = "topic_A")
public class TopicConsumer1 {

    @RabbitHandler
    public void process(String message){
        System.out.println("consumer1:"+message);
    }

}
//消费者2
@Component
@RabbitListener(queues = "topic_B")
public class TopicConsumer2 {

    @RabbitHandler
    public void process(String message){
        System.out.println("consumer2:"+message);
    }

}

测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class TopicTest {
    @Autowired
    private TopicProduceController topicProduceController;

    @Test
    public void topic (){
        topicProduceController.topic();
    }
}

 3.topics模式

config配置文件

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;

/**
 * @Auther: cpb
 * @Date: 2019/1/29 11:41
 * @Description:
 */
@Configuration
public class TopicsConfig {
    @Bean
    public Queue AMessage() {
        return new Queue("topics_A");
    }
    @Bean
    public Queue BMessage() {
        return new Queue("topics_B");
    }
    @Bean
    TopicExchange exchange() {
        return new TopicExchange("topicsExchange");
    }
    @Bean
    Binding bindingExchangeMessageA(Queue AMessage, TopicExchange exchange) {
        return BindingBuilder.bind(AMessage).to(exchange).with("*.colour");
    }
    @Bean
    Binding bindingExchangeMessageB(Queue BMessage, TopicExchange exchange) {
        return BindingBuilder.bind(BMessage).to(exchange).with("animal");
    }

}

生产者

@RestController
public class TopicsProduceController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public String topics(){
        String message = "hello orange and red";
        String routingKey = "fruit.colour";
        String exchange = "topicsExchange";
        rabbitTemplate.convertAndSend(exchange,routingKey, message);
        return message;
    }

    public String topics1(){
        String message = "hello rabbit and white";
        String routingKey = "animal.colour";
        String exchange = "topicsExchange";
        rabbitTemplate.convertAndSend(exchange,routingKey, message);
        return message;
    }
    public String topics2(){
        String message = "hello cat";
        String routingKey = "animal";
        String exchange = "topicsExchange";
        rabbitTemplate.convertAndSend(exchange,routingKey, message);
        return message;
    }

}

消费者

//消费者1
@Component
@RabbitListener(queues = "topics_A")
public class TopicsConsumer1 {

    @RabbitHandler
    public void process(String message){
        System.out.println("consumer1:"+message);
    }

}
//消费者2
@Component
@RabbitListener(queues = "topics_B")
public class TopicsConsumer2 {

    @RabbitHandler
    public void process(String message){
        System.out.println("consumer2:"+message);
    }

}

测试类

@RunWith(SpringRunner.class)
@SpringBootTest
public class TopicsTest {
    @Autowired
    private TopicsProduceController topics;

    @Test
    public void topics(){
        topics.topics();
        topics.topics1();
        topics.topics2();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值