微服务使用MQ数据传输

MQ传输的方法有7种,常用的有5种。

  简单、工作、广播、路由、通配符。

 

如果是使用的 Linux 需要启动和安装 MQ 镜像:https://www.cnblogs.com/pengyuangan/p/16360704.html

 

导入依赖:

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

 

配置 application.yml 配置:

  消费者配置一次拿一条,如果是多线程那么效率高的可以多处理,效率低的少处理。

生产者:

spring: rabbitmq: host:
192.168.30.134 # 主机名 port: 5672 # 端口 virtual-host: / # 虚拟主机 username: peng # 用户名 password: 123456 # 密码


消费者:

spring:
rabbitmq:
host: 192.168.30.134 # 主机名
port: 5672 # 端口
virtual-host: / # 虚拟主机
username: peng # 用户名
password: 123456 # 密码
listener:
simple:
prefetch: 1 # 每次只能获取一条消息,处理完成才能获取下一个消息

 

生产者,也就是消息发送者,消费者也就是消息接收者。

 

定义消费者,创建 SpringRabbitFanout 类,里面配置交换机和队列:

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;

/**
 * @ClassName 定义交换机
 **/
@Configuration
public class SpringRabbitFanout {
    /**
     * @Description:交换机
     **/
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("peng.fanout");
    }

    /**
     * @Description:队列1
     **/
    @Bean
    public Queue fanoutQueue1(){
        return new Queue("fanout.queue1");
    }

    /**
     * @Description:队列和交换机绑定
     **/
    @Bean
    public Binding bindingQueue(Queue fanoutQueue1,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(fanoutQueue1).to(fanoutExchange);
    }

    /**
     * @Description:队列2
     **/
    @Bean
    public Queue fanoutQueue2(){
        return new Queue("fanout.queue2");
    }

    /**
     * @Description:队列和交换机绑定
     **/
    @Bean
    public Binding bindingQueue2(Queue fanoutQueue2,FanoutExchange fanoutExchange){
        return BindingBuilder.bind(fanoutQueue2).to(fanoutExchange);
    }
}

 

定义 SpringFanout 类接收数据,这里我定义了 3种:广播 、路由也叫定向、通配符

/**
 * @ClassName 广播方式接收数据
 **/
@Component
public class SpringFanout {
    //广播==========================

    @RabbitListener(queues = "fanout.queue1")
    public void listFanoutQueue1(String msg){
        System.out.println("1--广播队列接收到:"+msg);
    }

    @RabbitListener(queues = "fanout.queue2")
    public void listFanoutQueue2(String msg){
        System.out.println("2--广播队列接收到:"+msg);
    }


    //定向=====================

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange =@Exchange(name = "peng.direct",type = ExchangeTypes.DIRECT),
            key = {"bey","bylist"}
    ))
    public void listDirectQueue1(String msg){
        System.out.println("1--定向队列接收到:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name ="peng.direct",type = ExchangeTypes.DIRECT),
            key = {"bey"}
    ))
    public void listDirectQueue2(String msg){
        System.out.println("2--定向队列接收到:"+msg);
    }


    //通配符==========================

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange =@Exchange(name = "peng.topic",type = ExchangeTypes.TOPIC),
            key = {"bey.#","#.bylist"}
    ))
    public void listTopicQueue1(String msg){
        System.out.println("1--通配符队列接收到:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name ="peng.topic",type = ExchangeTypes.TOPIC),
            key = {"#.bey"}
    ))
    public void listTopicQueue2(String msg){
        System.out.println("2--通配符队列接收到:"+msg);
    }

}

 

定义测试类发送数据:

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAmqpTest {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Test
    public void testSimpleQueue() {
        //定义队列
        String dui="peng.queue";
        //数据
        String shu="java,heoll~";

        //发送信息
        rabbitTemplate.convertAndSend(dui,shu);
    }

    @Test
    public void testFanoutQueue() {
        //定义队列
        String dui="peng.fanout1";
        //数据
        String shu="java,heoll~";

            //发送信息
            rabbitTemplate.convertAndSend(dui,"bylist",shu);
    }

    @Test
    public void testTopicQueue() {
        //定义队列
        String dui="peng.topic";
        //数据
        String shu="java,heoll~";

        //发送信息
        rabbitTemplate.convertAndSend(dui,"bey.bylist",shu);
    }
}

 

posted @ 2022-06-09 16:59  追星月?问酒缘。  阅读(245)  评论(0)    收藏  举报