rabbitMQ学习笔记(七) RPC 远程过程调用

当客户端想要调用服务器的某个方法来完成某项功能时,就可以使用rabbitMQ支持的PRC服务。

其实RPC服务与普通的收发消息的区别不大, RPC的过程其实就是  

客户端向服务端定义好的Queue发送消息,其中携带的消息就应该是服务端将要调用的方法的参数 ,并使用Propertis告诉服务端将结果返回到指定的Queue。

示例:


package com.zf.rabbitmq07;

import java.io.IOException;

import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.ShutdownSignalException;

public class RPCServer {
	
	public static final String RPC_QUEUE_NAME = "rpc_queue";
	
	public static String sayHello(String name){
		return "hello " + name ;
	}
	
	public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {
		
		ConnectionFactory connFac = new ConnectionFactory() ;
		connFac.setHost("localhost");
		
		Connection conn = connFac.newConnection() ;
		
		Channel channel = conn.createChannel() ;
		
		channel.queueDeclare(RPC_QUEUE_NAME, false, false, false, null) ;
		
		QueueingConsumer consumer = new QueueingConsumer(channel);
		
		channel.basicConsume(RPC_QUEUE_NAME, false , consumer) ;
		
		while(true){
			System.out.println("服务端等待接收消息..");  
			Delivery deliver = consumer.nextDelivery() ;
			System.out.println("服务端成功收到消息..");
			BasicProperties props =  deliver.getProperties() ;
			
			String message = new String(deliver.getBody() , "UTF-8") ;
			
			String responseMessage = sayHello(message) ;
			
			BasicProperties responseProps = new BasicProperties.Builder()
			.correlationId(props.getCorrelationId())  
			.build() ;
			
			//将结果返回到客户端Queue
			channel.basicPublish("", props.getReplyTo() , responseProps , responseMessage.getBytes("UTF-8") ) ;
		 	
			//向客户端确认消息
			channel.basicAck(deliver.getEnvelope().getDeliveryTag(), false);
			System.out.println("服务端返回消息完成..");
		}
		
	}

}

package com.zf.rabbitmq07;

import java.io.IOException;
import java.util.UUID;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.ConsumerCancelledException;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.AMQP.BasicProperties;
import com.rabbitmq.client.QueueingConsumer.Delivery;
import com.rabbitmq.client.ShutdownSignalException;

public class RPCClient {

	public static final String RPC_QUEUE_NAME = "rpc_queue";

	public static void main(String[] args) throws IOException, ShutdownSignalException, ConsumerCancelledException, InterruptedException {

		ConnectionFactory connFac = new ConnectionFactory() ;
		connFac.setHost("localhost");
		Connection conn = connFac.newConnection() ;
		Channel channel = conn.createChannel() ;

		//响应QueueName ,服务端将会把要返回的信息发送到该Queue
		String responseQueue = channel.queueDeclare().getQueue() ;

		String correlationId = UUID.randomUUID().toString() ;

		BasicProperties props = new BasicProperties.Builder()
		.replyTo(responseQueue)
		.correlationId(correlationId)
		.build();

		String message = "is_zhoufeng";
		channel.basicPublish( "" , RPC_QUEUE_NAME , props ,  message.getBytes("UTF-8"));

		QueueingConsumer consumer = new QueueingConsumer(channel)	;

		channel.basicConsume( responseQueue , consumer) ;

		while(true){
			
			Delivery delivery = consumer.nextDelivery() ;
			
			if(delivery.getProperties().getCorrelationId().equals(correlationId)){
				String result = new String(delivery.getBody()) ;  
				System.out.println(result);
			}
			
		}
	}

}



  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值