Thrift第一个示例

xiaoxiao2021-02-27  348

第一步:引入thrift依赖包

compile group: 'org.apache.thrift', name: 'libthrift', version: '0.9.3'

第二步:创建配置文件HelloService.thrift

// 定义命名空间 namespace java com.mhy.thrift.gen // 定义Service service HelloService { //定义方法 string sayHello(1: string username) }

第三步:生成代码

在命令行下执行如下命令

➜ thrift ls Hello.thrift ➜ thrift thrift --gen java HelloService.thrift ➜ thrift ls HelloService.thrift gen-java

生成完之后的项目结构如下

其中gen-java下生成的结果一般是一种API的描述,这个东西就是提供给服务调用方来使用,而我们还需要针对于这个API来做实现

接下来把生成的代码文件拷贝到项目源码目录下面

其中gen包下就是通过Thrift生成的IDL(Interface Defination Language),client包下存放客户端代码,server包下存放服务端代码。

第四步:服务接口实现

package com.mhy.thrift.server; import com.mhy.thrift.gen.HelloService; import org.apache.thrift.TException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author mahaiyuan * @ClassName: HelloServiceHandler * @date 2017-05-04 下午2:29 */ public class HelloServiceHandler implements HelloService.Iface { private static final Logger logger = LoggerFactory.getLogger(HelloServiceHandler.class); @Override public String sayHello(String username) throws TException { logger.info("HelloServiceHandler.sayHello username={}", username); return "Hello " + username; } }

编写服务端启动类

package com.mhy.thrift.server; import com.mhy.thrift.gen.HelloService; import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TSimpleServer; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TTransportException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author mahaiyuan * @ClassName: HelloServer * @date 2017-05-04 下午2:38 */ public class HelloServer { private static final int SERVER_PORT = 8888; private static final Logger logger = LoggerFactory.getLogger(HelloServer.class); public static void main(String[] args) { logger.info("HelloServer start server ......."); try { TProcessor tprocessor = new HelloService.Processor<HelloService.Iface>(new HelloServiceHandler()); // 简单的单线程服务模型,一般用于测试 TServerSocket serverTransport = new TServerSocket(SERVER_PORT); TServer.Args tArgs = new TServer.Args(serverTransport); tArgs.processor(tprocessor); tArgs.protocolFactory(new TBinaryProtocol.Factory()); TServer server = new TSimpleServer(tArgs); server.serve(); } catch (TTransportException e) { logger.warn("HelloServer exception.", e); } } }

编写客户端实现

package com.mhy.thrift.client; import com.mhy.thrift.gen.HelloService; import org.apache.thrift.TException; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TProtocol; import org.apache.thrift.transport.TSocket; import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author mahaiyuan * @ClassName: HelloClient * @date 2017-05-04 下午2:48 */ public class HelloClient { private static final Logger logger = LoggerFactory.getLogger(HelloClient.class); public static final String SERVER_IP = "localhost"; //服务端地址 public static final int SERVER_PORT = 8888; //服务端端口 public static final int TIMEOUT = 30000; //超时时间 public static void main(String[] args) { String username = "张三"; TTransport transport = null; try { transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT); // 协议要和服务端一致 TProtocol protocol = new TBinaryProtocol(transport); HelloService.Client client = new HelloService.Client( protocol); transport.open(); logger.info("call sayHello username={}", username); String result = client.sayHello(username); logger.info("call sayHello result={}", result); } catch (TTransportException e) { e.printStackTrace(); } catch (TException e) { e.printStackTrace(); } finally { if (null != transport) { transport.close(); } } } }

接下来先启动服务端,然后再运行客户端。在控制台下得到如下结果 服务端控制台下输出的内容

[main] INFO com.mhy.thrift.server.HelloServer - HelloServer start server ....... [main] INFO com.mhy.thrift.server.HelloServiceHandler - HelloServiceHandler.sayHello username=张三

客户端控制台下输出的内容

[main] INFO com.mhy.thrift.client.HelloClient - call sayHello username=张三 [main] INFO com.mhy.thrift.client.HelloClient - call sayHello result=Hello 张三

代码地址:https://github.com/mhy2011/thrift-examples

转载请注明原文地址: https://www.6miu.com/read-4276.html

最新回复(0)