1. 程式人生 > >兩種方式獲取Thrift調用的clientIP地址

兩種方式獲取Thrift調用的clientIP地址

thread delet rpc服務 clas artifact 觸發 void new sta

thrift依賴

<dependency>
    <groupId>org.apache.thrift</groupId>
    <artifactId>libthrift</artifactId>
    <version>0.9.3</version>
</dependency>


方法一:

package com.lala.server;

import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.ServerContext;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TServerEventHandler;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import com.pp.calc.Calculator;
import com.pp.calc.impl.CalculatorImpl;

/**
 * 使用事件監聽的方式 
 */
public class Server
{
	public static final int PORT = 9988;
	
	public static void main(String[] args)throws Exception
	{
		TServerSocket serverSocket = new TServerSocket(PORT); 
		
		TProtocolFactory protocolFactory = new TCompactProtocol.Factory();  

		TProcessor processor = new Calculator.Processor<>(new CalculatorImpl());
		
		final TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverSocket).protocolFactory(protocolFactory).processor(processor));  
		server.setServerEventHandler(new MyTServerEventHandler());
		
		server.serve();
	}
}

class MyTServerEventHandler implements TServerEventHandler
{
	/**
	 * 服務成功啟動後運行
	 */
	public void preServe()
	{
		System.out.println("Start server on port " + Server.PORT + " ...");  
	}

	/**
	 * 創建Context的時候。觸發
	 * 在server啟動後。僅僅會運行一次
	 */
	public ServerContext createContext(TProtocol input, TProtocol output)
	{
		System.out.println("createContext ... ");
		return null;
	}

	/**
	 * 刪除Context的時候。觸發
	 * 在server啟動後。僅僅會運行一次
	 */
	public void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output)
	{
		System.out.println("deleteContext ... ");
	}

	/**
	 * 調用RPC服務的時候觸發
	 * 每調用一次方法。就會觸發一次
	 */
	public void processContext(ServerContext serverContext, TTransport inputTransport, TTransport outputTransport)
	{
		/**
		 * 把TTransport對象轉換成TSocket。然後在TSocket裏面獲取Socket,就能夠拿到clientIP
		 */
		TSocket socket = (TSocket)inputTransport;
		System.out.println(socket.getSocket().getRemoteSocketAddress());
		
		System.out.println("method invoke ... ");
	}
}

方法二:

package com.lala.server;

import org.apache.thrift.TException;
import org.apache.thrift.TProcessor;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.protocol.TProtocolFactory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TSocket;

import com.pp.calc.Calculator;
import com.pp.calc.impl.CalculatorImpl;

/**
 * 使用代理的TProcessor,獲取clientIP
 */
public class Server2
{
	public static final int PORT = 9988;
	
	public static void main(String[] args)throws Exception
	{
		TServerSocket serverSocket = new TServerSocket(PORT); 
		
		TProtocolFactory protocolFactory = new TCompactProtocol.Factory();  

		//註意這裏,把真實的processor包裝成LogProcessor
		TProcessor processor = new LogProcessor(new Calculator.Processor<>(new CalculatorImpl()));
		
		final TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverSocket).protocolFactory(protocolFactory).processor(processor));  
		
		server.serve();
	}
}

/**
 * 代理對象
 */
class LogProcessor implements TProcessor
{
	private TProcessor processor;
	
	public LogProcessor(TProcessor processor)
	{
		this.processor = processor;
	}
	
	/**
	 * 該方法,client每調用一次,就會觸發一次
	 */
	public boolean process(TProtocol in, TProtocol out) throws TException
	{
		/**
		 * 從TProtocol裏面獲取TTransport對象
		 * 把TTransport對象轉換成TSocket,然後在TSocket裏面獲取Socket,就能夠拿到clientIP
		 */
		TSocket socket = (TSocket)in.getTransport();
		System.out.println(socket.getSocket().getRemoteSocketAddress());
		
		return processor.process(in, out);
	}
}



以上

import com.pp.calc.Calculator;
import com.pp.calc.impl.CalculatorImpl;

這兩個類,見我上一篇博客

http://blog.csdn.net/mn960mn/article/details/50476759

兩種方式獲取Thrift調用的clientIP地址