spring boot rabbitMQ RPC 实现

环境配置

package com.example.demo;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;


@Configuration
@EnableRabbit
public class RabbitMQConfigurer {

    @Autowired
    private ConnectionFactory connectionFactory;

    @Bean
    @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
    public RabbitTemplate rabbitTemplate() {
        //必须是prototype类型
        //Reply received after timeout
        RabbitTemplate rabbitTemplate =  new RabbitTemplate(this.connectionFactory);
        rabbitTemplate.setReceiveTimeout(9000);
        return rabbitTemplate;
    }

   
    @Bean
    @Qualifier("rpcTestExchange")
    public DirectExchange rpcTestExchange() {
        return new DirectExchange("rpcTest");
    }


    @Bean
    public Queue rpcTestQueue() {
        return new Queue("rpcTestQueue");
    }


    @Bean
    public Binding rpcTestBind() {
        return BindingBuilder.bind(rpcTestQueue()).to(rpcTestExchange()).with("addUser");
    }


}

 

server 端

 

package com.example.demo;

import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;


@Component
@RabbitListener(queues = "rpcTestQueue")
public class UserServer {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserServer.class);

    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public UserServer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @RabbitHandler
    public void process(@Payload String payload, Channel channel, @Header(AmqpHeaders.REPLY_TO) String replyTo,
                        @Header(AmqpHeaders.CORRELATION_ID) String correlationId) throws Exception {
        LOGGER.info("====== server receive data 【{}】  ====== ", payload);

        this.rabbitTemplate.convertAndSend(replyTo, "then " + payload + " is create", message -> {
            message.getMessageProperties().setCorrelationId(correlationId);
            return message;
        });

        LOGGER.info("====== server response queue 【{}】 ======", replyTo);
    }


}

 

client 端

package com.example.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component
public class Client {
    private static final Logger LOGGER = LoggerFactory.getLogger(Client.class);

    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public Client(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;

    }

    public void doRequest() {
        for (String name : Arrays.asList("张三", "李四", "王五")) {
            LOGGER.info("---- client send user name is 【{}】", name);

            Object response = this.rabbitTemplate.convertSendAndReceive("rpcTest", "addUser", name);
            LOGGER.info("---- and response is : {} -------", response);
        }

    }


}

 

客户端:

在请求发送消息之前,创建一个【匿名队列】绑定至默认的交换机(即 /)。将队【匿名队列】名称放在 reply_to 中与消息一起发送。

服务端:

处理理消息后,将应答消息发送至默认交换机即(/)。并路由键为 reply_to 即【客户端】所创建的的【匿名队列】

 

 

 

 

转载于:https://my.oschina.net/u/2552286/blog/3086296

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值