其他分享
首页 > 其他分享> > 微服务使用MQ数据传输

微服务使用MQ数据传输

作者:互联网

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

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

 

如果是使用的 Linux 需要启动和安装 MQ 镜像:

 

导入依赖:

        <!--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 # 密码

 

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

 

定义消费者,创建 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);
    }
}

 

标签:peng,服务,String,队列,Queue,MQ,msg,数据传输,public
来源: https://www.cnblogs.com/pengyuangan/p/16359964.html