Remote Procedure Call: The client sends a request to the server. The server processes the request and returns a response. The client terminates after receiving the response.
Here the producer is called as the client, and the consumer as the server receives the request and responds to the producer.
1. Synchronous invocation
1.1. Bind queues
@Configuration
public class RPCRabbitConfig {
@Bean
public Queue RPCQueue(a) {
return new Queue("RPCQueue".true.false.false);
}
@Bean
public DirectExchange RPCExchange(a) {
return new DirectExchange("RPCExchange".true.false);
}
@Bean
public Binding bindingRPC(a) {
return BindingBuilder.bind(RPCQueue()).to(RPCExchange()).with("RPC"); }}Copy the code
1.2 consumers (server)
@Component
@RabbitListener(queues = "RPCQueue")
@Slf4j
public class RPCReceiver {
@RabbitHandler
public String process(String message) {
log.info("Receive remote call request message :[{}]", message);
return "remote procedure call success!"; }}Copy the code
1.3. Producer (client)
@RestController
@Slf4j
public class RPCController {
@Autowired
private RabbitTemplate rabbitTemplate;
@PostConstruct
public void init(a) {
// Synchronous call Sets the remote call response timeout in milliseconds
rabbitTemplate.setReplyTimeout(60000);
}
@PostMapping("/syncRPC")
public String syncRPC(a) {
Object response = rabbitTemplate.convertSendAndReceive("RPCExchange"."RPC"."RPC synchronous call");
String respMsg = response.toString();
log.info("Remote call response :[{}]", respMsg);
returnrespMsg; }}Copy the code
You can set the timeout using the setReplyTimeout(long milliseconds) function.
1.4, the results
Remote call response :[Remote Procedure Call Success!]Copy the code
2. Asynchronous invocation
2.1. Configure beans
/** * AsyncRabbitTemplate SpringBoot does not have a default AsyncRabbitTemplate injection, so you need to configure it yourself@param rabbitTemplate
* @return* /
@Bean
public AsyncRabbitTemplate asyncRabbitTemplate(RabbitTemplate rabbitTemplate) {
return new AsyncRabbitTemplate(rabbitTemplate);
}
Copy the code
2.2 Producer (client)
@RestController
@Slf4j
public class RPCController {
@Autowired
private AsyncRabbitTemplate asyncRabbitTemplate;
@PostMapping("/asyncRPC")
public String asyncRPC(a) {
AsyncRabbitTemplate.RabbitConverterFuture<Object> future = asyncRabbitTemplate.convertSendAndReceive("RPCExchange"."RPC"."RPC asynchronous call");
future.addCallback(new ListenableFutureCallback<Object>() {
@Override
public void onFailure(Throwable throwable) {
log.error("Asynchronous call failed", throwable);
}
@Override
public void onSuccess(Object o) {
log.info("Asynchronous call response :[{}}", o.toString()); }});return "ok"; }}Copy the code
2.3, the results
SimpleConsumer [queue=amq.rabbitmq.reply-to, consumerTag=amq.ctag-nHw71SucAmOUHb6hGVjaZA identity=5fBed23f :[remote Procedure call success!} Bed23F :[remote Procedure call success!Copy the code
Refer to the link
- Use RabbitMQ RPC in SpringBoot
The code address
- Github:github.com/senlinmu100…
- Gitee:gitee.com/ppbin/rabbi…
Personal website
- Gitee Pages
- Github Pages