10 Spring Cloud微服务 异步消息RabbitMQ

1一 异步概念
客户端请求不会阻塞进程,服务端的相应可以是非即时的。

异步的常见形态
。通知
。请求/异步相应(默认响应不会同时到达)
。消息(1对多发布)

二MQ应用场景
。异步处理(短信服务,积分服务)
。流量消峰(消息队列控制,超过请求跳转到异常页面)
。日志处理(kafka)
。应用解耦(订单调用商品服务)
三实现(git reset --hard bfa82e7)
打开工程order服务
1.1pom.xml增加依赖

    org.springframework.boot
    spring-boot-starter-amqp

1.2修改bootstrap.yml文件

rabbitmq:
  host: localhost
  port: 5672
  username: guest
  password: guest

目前我启动的虚拟机容器rabbitmq
rabbitmq:
  host: 192.168.229.191
  port: 5672
  username: guest
  password: guest


spring.rabbitmq.host= 192.168.229.191
spring.rabbitmq.port= 5672
spring.rabbitmq.username= guest
spring.rabbitmq.password= guest

可以把这些配置文件放到Git上,之后就可以删掉代码bootstrap.yml中关于rabbitmq的配置了
config-repo/order-test.yml
config-repo/order-dev.yml

四 接收消息的方法
order->server->order->message->MqReceiver.java
package com.imooc.order.message;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 * 接收mq消息
 * Created by 廖师兄
 * 2018-02-04 22:02
 */
@Slf4j
@Component
public class MqReceiver {

    //1. @RabbitListener(queues = "myQueue")
    //2. 自动创建队列 @RabbitListener(queuesToDeclare = @Queue("myQueue"))
    //3. 自动创建, Exchange和Queue绑定
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue("myQueue"),
            exchange = @Exchange("myExchange")
    ))
    public void process(String message) {
        log.info("MqReceiver: {}", message);
    }

    /**
     * 数码供应商服务 接收消息
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("myOrder"),
            key = "computer",
            value = @Queue("computerOrder")
    ))
    public void processComputer(String message) {
        log.info("computer MqReceiver: {}", message);
    }


    /**
     * 水果供应商服务 接收消息
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("myOrder"),
            key = "fruit",
            value = @Queue("fruitOrder")
    ))
    public void processFruit(String message) {
        log.info("fruit MqReceiver: {}", message);
    }
}

五发送方
直接写order test中
test->order->repository->MySenderTest.java
package com.imooc.order;

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

import java.util.Date;

/**
 * 发送mq消息测试
 */
@Component
public class MqSenderTest extends OrderApplicationTests {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Test
    public void send() {
        amqpTemplate.convertAndSend("myQueue", "now " + new Date());
    }

    @Test
    public void sendOrder() {
        amqpTemplate.convertAndSend("myOrder", "computer", "now " + new Date());
    }
}

六RabbitMQ新建队列
Name: myQueue ->添加
七新建程序自动创建队列
@RabbitListener(queues="myQueue")
@RabbitListener(queuesToDeclare= @ Queue("myQueue"))
自动创建, Exchange和Queue绑定
@RabbitListener(bindings= @QueueBinding(
    value= @Queue("myQueue"),
exchange =@Exchange("myExchange")
))

八消息分组
关键点在key字段
  /**
     * 数码供应商服务 接收消息
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("myOrder"),
            key = "computer",
            value = @Queue("computerOrder")
    ))
    public void processComputer(String message) {
        log.info("computer MqReceiver: {}", message);
    }

    /**
     * 水果供应商服务 接收消息
     * @param message
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange("myOrder"),
            key = "fruit",
            value = @Queue("fruitOrder")
    ))
    public void processFruit(String message) {
        log.info("fruit MqReceiver: {}", message);
    }
}

九测试
@Component
public class MqSenderTest extends OrderApplicationTests {

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Test
    public void send() {
        amqpTemplate.convertAndSend("myQueue", "now " + new Date());
    }

    @Test
    public void sendOrder() {
        amqpTemplate.convertAndSend("myOrder", "computer", "now " + new Date());
    }
}

compute会发往这个队列

你可能感兴趣的:(SpringCloudMS)