今天就跟大家聊聊有關怎么進行spring boot rabbitMQ RPC實現,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結了以下內容,希望大家根據這篇文章可以有所收獲。
成都創新互聯公司公司2013年成立,先為莎車等服務建站,莎車等地企業,進行企業商務咨詢服務。為莎車企業網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。
環境配置
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 中與消息一起發送。
服務端:
處理理消息后,將應答消息發送至默認交換機即(/)。
看完上述內容,你們對怎么進行spring boot rabbitMQ RPC實現有進一步的了解嗎?如果還想了解更多知識或者相關內容,請關注創新互聯行業資訊頻道,感謝大家的支持。
文章名稱:怎么進行springbootrabbitMQRPC實現
分享URL:http://www.2m8n56k.cn/article44/iechhe.html
成都網站建設公司_創新互聯,為您提供網站制作、微信公眾號、靜態網站、企業建站、網站營銷、網站導航
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯