首页 > 代码库 > 两种方式获取Thrift调用的clientIP地址
两种方式获取Thrift调用的clientIP地址
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地址
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。