从零学 spring cloud第5-2课:中间件之消息队列RabbitMQ

    项目的DEMO代码:https://github.com/heyu52/-spring-cloud
    RabbitMQ的官网:https://www.rabbitmq.com/
    RabbitMQ是一套开源(MPL)的消息队列服务软件,是由 LShift 提供的一个 Advanced Message Queuing Protocol (AMQP) 的开源实现,由以高性能、健壮以及可伸缩性出名的 Erlang 写成。目测业界现在使用率,能排第一。有以下两个特点:

  • 可伸缩性:集群服务
  • 消息持久化:从内存持久化消息到硬盘,再从硬盘加载到内

    其他的介绍这里就不说了,网上一查一大堆,这里直接使用。

安装
1、拉取RabbitMQ镜像文件,这里加上了management,表明是带web管理界面的,便于管理。

docker pull rabbitmq:management

2、运行

docker run -d --hostname my-rabbit3 -p 5671:5671 -p 5672:5672 -p 4369:4369 -p 25672:25672 -p 15671:15671 -p 15672:15672  --name my-rabbit3 rabbitmq:management

3、测试
http://192.168.0.123:15672/,用户名密码:guest/guest
在这里插入图片描述
登录后界面如下:
在这里插入图片描述
新建项目
在这里插入图片描述
pom中会增加引用

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

配置文件中增加连接配置

spring.application.name=Spring-boot-rabbitmq
spring.rabbitmq.host=192.168.0.123
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=admin

队列

package com.csdn.demo.Config;

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

@Configuration
public class RabbitConfig {
    @Bean
    public Queue helloQueue() {
        return new Queue("csdn");
    }

}

定义发送者

package com.csdn.demo.Sender;

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

@Component
public class MySender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "这是我发送的消息";
        this.rabbitTemplate.convertAndSend("csdn", context);
        System.out.println("我是发送者 : " + context);
    }
}

定义接收者

package com.csdn.demo.Receiver;

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

@Component
@RabbitListener(queues = "csdn")
public class MyReceiver {
    @RabbitHandler
    public void process(String hello) {
        System.out.println("我是接收者  : " + hello);
    }

}

测试Controller

package com.csdn.demo.web;

import com.csdn.demo.Sender.MySender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MySender mySender;

    @RequestMapping("/MySendMethod")
    public void MySendMethod() throws Exception {
        mySender.send();
    }

}

运行测试项目 http://127.0.0.1:8080/MySendMethod
在这里插入图片描述
多个接收者
我们新定义一个接收者

package com.csdn.demo.Receiver;

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

@Component
@RabbitListener(queues = "csdn")
public class MyReceiverA {
    @RabbitHandler
    public void process(String hello) {
        System.out.println("我是接收者A  : " + hello);
    }
}

多次发消息

 @RequestMapping("/MySend5")
    public void MySend5 () throws Exception {
        for (int i=0;i<5;i++) {
            mySender.send();
        };
    }

测试
浏览器输入:http://127.0.0.1:8080/MySend5
在这里插入图片描述从上面的结果可以看出,一个发送者,多 个接收者,经过测试接收端均匀接收到消息,接收端自动进行了均衡负载,这个特性可以做流量分发。

定义对象
RabbitMQ不只是支持字符串,发送接收对象也是可以的。

package com.csdn.demo.model;

import java.io.Serializable;

public class User implements Serializable {

    private String name;

    private String sex;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

发送对象

   public void send(User user) {
        System.out.println("我是对象发送者: " + user.toString());
        this.rabbitTemplate.convertAndSend("csdn", user);
    }

接收对象

 @RabbitHandler
    public void process(User user) {
        System.out.println("我是对象接收者 : " + user);
    }

测试对象

 @RequestMapping("/SendUser")
    public void SendUser() throws Exception {
        User user=new User();
        user.setName("csdn");
        user.setSex("男");
        mySender.send(user);
        Thread.sleep(1000l);
    }

在这里插入图片描述
Topic模式

新建配置

package com.csdn.demo.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;

@Configuration
public class RabbitConfig {

    final static String TopicSendMessage = "Topic.SendMessage";
    final static String TopicSendMessageTest = "Topic.SendMessage.Test";

    @Bean
    public Queue TopicSendMessage() {
        return new Queue(RabbitConfig.TopicSendMessage);
    }

    @Bean
    public Queue TopicSendMessageTest() {
        return new Queue(RabbitConfig.TopicSendMessageTest);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("TopicExchange");
    }

    @Bean
    Binding bindingExchangeMessage(Queue TopicSendMessage, TopicExchange exchange) {
        return BindingBuilder.bind(TopicSendMessage).to(exchange).with("Topic.SendMessage");
    }

    @Bean
    Binding bindingExchangeMessages(Queue TopicSendMessageTest, TopicExchange exchange) {
        return BindingBuilder.bind(TopicSendMessageTest).to(exchange).with("Topic.#");
    }
}


新建Sendor

package com.csdn.demo.Sender;

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

@Component
public class MySender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "我是发送者Send,队列是Topic.A";
        System.out.println("发送者 : " + context);
        this.rabbitTemplate.convertAndSend("TopicExchange", "Topic.A", context);
    }

    public void TopicSendMessage() {
        String context = "我是发送者TopicSendMessage,Topic.SendMessage";
        System.out.println("发送者 : " + context);
        this.rabbitTemplate.convertAndSend("TopicExchange", "Topic.SendMessage", context);
    }

    public void TopicSendMessageTest() {
        String context = "我是发送者TopicSendMessageTest,Topic.SendMessage.Test";
        System.out.println("发送者 : " + context);
        this.rabbitTemplate.convertAndSend("TopicExchange", "Topic.SendMessage.Test", context);
    }
}

新建接收者A

package com.csdn.demo.Receiver;

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

@Component
@RabbitListener(queues = "Topic.SendMessage")
public class MyReceiverA {
    @RabbitHandler
    public void process(String message) {
        System.out.println("接收者A : " + message);
    }
}

新建接收者B

package com.csdn.demo.Receiver;

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

@Component
@RabbitListener(queues = "Topic.SendMessage.Test")
public class MyReceiverB {

    @RabbitHandler
    public void process(String message) {
        System.out.println("接收者B : " + message);
    }

}

新建测试Controller

package com.csdn.demo.web;

import com.csdn.demo.Sender.MySender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {

    @Autowired
    private MySender sender;

    @RequestMapping("mySend")
    public void mySend() throws Exception {
        sender.send();
    }


    @RequestMapping("TopicSendMessage")
    public void TopicSendMessage() throws Exception {
        sender.TopicSendMessage();
    }


    @RequestMapping("TopicSendMessageTest")
    public void TopicSendMessageTest() throws Exception {
        sender.TopicSendMessageTest();
    }
}

测试结果
http://127.0.0.1:8080/mySend
在这里插入图片描述
http://127.0.0.1:8080/TopicSendMessage
在这里插入图片描述
http://127.0.0.1:8080/TopicSendMessageTest
在这里插入图片描述
这种方式,非常灵活,可以按规则直接匹配消息。

广播消息
配置

package com.csdn.demo.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;


@Configuration
public class RabbitConfig {

    @Bean
    public Queue AMessage() {
        return new Queue("队列A");
    }

    @Bean
    public Queue BMessage() {
        return new Queue("队列B");
    }

    @Bean
    public Queue CMessage() {
        return new Queue("队列C");
    }

    @Bean
    FanoutExchange fanoutExchange() {
        return new FanoutExchange("fanoutExchange");
    }

    @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);
    }

    @Bean
    Binding bindingExchangeC(Queue CMessage, FanoutExchange fanoutExchange) {
        return BindingBuilder.bind(CMessage).to(fanoutExchange);
    }

}

发送者

package com.csdn.demo.Sender;

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

@Component
public class MySender {
    @Autowired
    private AmqpTemplate rabbitTemplate;

    public void send() {
        String context = "广播消息来了";
        System.out.println("发送者 : " + context);
        this.rabbitTemplate.convertAndSend("fanoutExchange","", context);
    }
}

接收者A

package com.csdn.demo.Receiver;

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

@Component
@RabbitListener(queues = "队列A")
public class MyReceiverA {

    @RabbitHandler
    public void process(String message) {
        System.out.println("接收者 A: " + message);
    }

}

接收者B

package com.csdn.demo.Receiver;

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

@Component
@RabbitListener(queues = "队列B")
public class MyReceiverB {

    @RabbitHandler
    public void process(String message) {
        System.out.println("接收者 B: " + message);
    }

}

接收者C

package com.neo.rabbit.fanout;

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

@Component
@RabbitListener(queues = "fanout.C")
public class FanoutReceiverC {

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

}

测试Controller

package com.csdn.demo.web;

import com.csdn.demo.Sender.MySender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    @Autowired
    private MySender sender;

    @RequestMapping("mySend")
    public void mySend() throws Exception {
        sender.send();
    }
}

测试结果 http://127.0.0.1:8080/mySend
在这里插入图片描述
三者都接收到了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值