Thrift项目实现
参考 https://my.oschina.net/wangmengjun/blog/910817
Maven工程结构
一共四个模块
thrift-rpc-interface
用来存放上一节thrift文件生成的代码
thrift-rpc-service
用来实现接口
thrift-rpc-server
简单的服务器
thrift-rpc-client
简单的客户端
service代码实现
package top.lilixin.thrift.service;
import org.apache.thrift.TException;
import java.util.logging.Logger;
/**
* @Description TODO
* @Author lilixin
* @Date 2021/1/20 11:37 上午
**/
public class GreetingServiceImpl implements GreetingService.Iface{
private static final Logger logger = Logger.getLogger(GreetingServiceImpl.class.getName());
public String sayHello(String name) throws TException {
logger.info(String.format("welcome to my world! name = {%s}", name));
return "Hello, " + name;
}
}
server代码实现
package top.lilixin.thrift.server;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;
import top.lilixin.thrift.service.GreetingService;
import top.lilixin.thrift.service.GreetingServiceImpl;
import java.util.logging.Logger;
/**
* @Description TODO
* @Author lilixin
* @Date 2021/1/20 11:42 上午
**/
public class GreetingServer {
private static final Logger logger = Logger.getLogger(GreetingServer.class.getName());
public static void main(String[] args) {
try {
TServerSocket serverTransport = new TServerSocket(9999);
TBinaryProtocol.Factory factory = new TBinaryProtocol.Factory();
/**
* 关联处理器与GreetingService服务实现
*/
GreetingService.Processor processor = new GreetingService.Processor(new GreetingServiceImpl());
TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport);
serverArgs.processor(processor);
serverArgs.protocolFactory(factory);
TServer server = new TThreadPoolServer(serverArgs);
logger.info("Start server on port 9999...");
server.serve();
} catch (TTransportException e) {
e.printStackTrace();
}
}
}
client代码实现
package top.lilixin.thrift.client;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import top.lilixin.thrift.service.GreetingService;
import java.util.logging.Logger;
/**
* @Description TODO
* @Author lilixin
* @Date 2021/1/20 11:48 上午
**/
public class GreetingClient {
private static final Logger logger = Logger.getLogger(GreetingClient.class.getName());
public static void main(String[] args) {
try {
TTransport transport = new TSocket("127.0.0.1",9999);
transport.open();
TBinaryProtocol tBinaryProtocol = new TBinaryProtocol(transport);
GreetingService.Client client = new GreetingService.Client(tBinaryProtocol);
String name = "lilixin";
logger.info("client 请求参数name="+name);
String result = client.sayHello(name);
logger.info("server 返回结果result="+result);
transport.close();
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
}
}
}
完整代码
Last updated
Was this helpful?