服务异步通讯——springcloud

这篇具有很好参考价值的文章主要介绍了服务异步通讯——springcloud。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

服务异步通讯——springcloud


服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

初始MQ

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

RabbitMQ快速入门

官网https://www.rabbitmq.com/

单机部署

我们在Centos7虚拟机中使用Docker来安装。

1.1.下载镜像

方式一:在线拉取

docker pull rabbitmq:3-management

方式二:从本地加载

在课前资料已经提供了镜像包:
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

上传到虚拟机中后,使用命令加载镜像即可:

docker load -i mq.tar
安装MQ

执行下面的命令来运行MQ容器:

docker run \
 -e RABBITMQ_DEFAULT_USER=itcast \
 -e RABBITMQ_DEFAULT_PASS=123321 \
 --name mq \
 --hostname mq1 \
 -p 15672:15672 \
 -p 5672:5672 \
 -d \
 rabbitmq:3-management

SpringAMQP

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
https://spring.io/projects/spring-amqp/
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

 <!--AMQP依赖,包含RabbitMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
        </dependency>

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

spring:
  rabbitmq:
    host: 192.168.10.88 # rabbitMQ的ip地址
    port: 5672 # 端口
    username: itcast
    password: 123321
    virtual-host: /
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAmqpTest {


    @Autowired
    private RabbitTemplate rabbitTemplate;


    @Test
    public void testSendMessage2SimpleQueue(){
        String queueName = "simple.queue";
        String message = "hello, spring amqp!";
        rabbitTemplate.convertAndSend(queueName,message);
    }
}

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

package cn.itcast.mq.listener;

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

@Component
public class SpringRabbitListener {

    @RabbitListener(queues = "simple.queue")
    public void listenSimpleQueue(String msg){
        System.out.println("消费者接受到消息:[" + msg + "]");
    }
}

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

work Queue 工作队列

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

Fanout Exchange广播模式

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

@Configuration
public class FanoutConfig {

    // itcast.fanout
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("itcast.fanout");
    }

    // fanout.queue1
    @Bean
    public Queue fanoutQueue1(){
        return new Queue("fanout.queue1");
    }


    // 绑定队列1到交换机
    @Bean
    public Binding fanoutBinding1(Queue fanoutQueue1, FanoutExchange fanoutExchange){
        return BindingBuilder
                .bind(fanoutQueue1)
                .to(fanoutExchange);
    }

    // fanout.queue2
    @Bean
    public Queue fanoutQueue2(){
        return new Queue("fanout.queue2");
    }
    // 绑定队列2到交换机
    @Bean
    public Binding fanoutBinding2(Queue fanoutQueue2, FanoutExchange fanoutExchange){
        return BindingBuilder
                .bind(fanoutQueue2)
                .to(fanoutExchange);
    }


}

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

   @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String msg){
        System.out.println("消费者接受到fanoutQueue1 消息:[" + msg + "]");
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String msg){
        System.out.println("消费者接受到fanoutQueue2 消息:[" + msg + "]");
    }
    

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

    @Test
    public void testSendFanoutExchange(){
        //交换机名称
        String exchanneName = "itcast.fanout";
        //消息
        String message = "hello every one!";
        // 发送消息
        rabbitTemplate.convertAndSend(exchanneName,"",message);
    }

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

DirectExchange路由模式

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","blue"}
    ))
    public void LinstenDirectQueue1(String msg){
        System.out.println("消费者接受到directQueue1 消息:[" + msg + "]");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"red","yellow"}
    ))
    public void LinstenDirectQueue2(String msg){
        System.out.println("消费者接受到directQueue2 消息:[" + msg + "]");
    }

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

    @Test
    public void testSendDirectExchange(){
        //交换机名称
        String exchanneName = "itcast.direct";
        //消息
        String message = "hello,red";
        // 发送消息
        rabbitTemplate.convertAndSend(exchanneName,"red",message);
    }

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

TopicExchange话题模式

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq
服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

 @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "china.#"
    ))
    public void LinstenTopicQueue1(String msg){
        System.out.println("消费者接受到topicQueue1消息:[" + msg + "]");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = "#.news"
    ))
    public void LinstenTopicQueue2(String msg){
        System.out.println("消费者接受到topicQueue2消息:[" + msg + "]");
    }

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

 @Test
    public void testSendTopicExchange(){
        //交换机名称
        String exchanneName = "itcast.topic";
        //消息
        String message = "小米汽车倒闭了";
        // 发送消息
        rabbitTemplate.convertAndSend(exchanneName,"china.news",message);
    }

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

消息转换器

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

    @Test
    public void testSendOnjectQueue() {
        Map<String, Object> msg = new HashMap<>();
        msg.put("name","柳岩");
        msg.put("age",18);
        rabbitTemplate.convertAndSend("object.queue",msg);
    }

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
 @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq

   @RabbitListener(queues = "object.queue")
    public void ListenObjectQueue(Map<String,Object> msg){
        System.out.println("接收到object.queue的消息:" + msg);
    }

服务异步通讯——springcloud,spring cloud,java,spring,rabbitmq文章来源地址https://www.toymoban.com/news/detail-813967.html

到了这里,关于服务异步通讯——springcloud的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包