内容简介:每日一博 | 一步步完成 thrift Java 示例
本文给出一个在Windows下,使用thrift一步步完成rpc的 Java 示例~
本文将从如下几个部分来加以说明,
- 根据thift自动生成代码 - 编写thrif文件,并根据 工具 在window下自动生成thrif相关代码
- 代码组成 - 给出Maven工程的模块化结构组成,并在每个模块中一步步实现代码
- 测试 - 对编写的代码进行测试,包括Server启动并绑定服务、Client连接并调用服务
下面就一步步来完成每一个部分~
根据thrift自动生成代码
定义服务接口
thrift是一种可伸缩的跨语言服务的发展软件框架。它结合了功能强大的软件堆栈的代码生成引擎,以建设服务,工作效率和无缝地与C + +,C#,Java,Python和 PHP 和 Ruby 结合。
thrift 采用IDL(Interface Definition Language)来定义通用的服务接口,并通过生成不同的语言代理实现来达到跨语言、平台的功能。
hello.thrift
namespace java com.xxx.tutorial.thrift.service service GreetingService { string sayHello(1:string name) }
产生thrift对应的代码
可以访问【 http://thrift.apache.org/download 】下载Thrift Compiler for Windows。
本文下载的版本为thrift-0.10.0.exe, 可以通过如下地址下载。
【 http://www.apache.org/dyn/closer.cgi?path=/thrift/0.10.0/thrift-0.10.0.exe 】
将编写的xxx.thrift文件,放置在和下载好的thrift-0.10.0.exe同一个目录之下,
然后执行如下操作即可产生相关代码~
thrift-0.10.0.exe -gen java hello.thrift
进入gen-java目录,我们可以看到根据hello.thrift文件,生成的服务接口文件~
至此,根据thrift文件产生Java代码部分就完成了。接下来,要做的就是编写服务实现、服务器代码以及客户端调用接口的代码等~
代码组成
Maven工程结构
thrift-demo 这个Maven工程主要包含四个模块,
- thrift-demo-interface - 存放thrift文件产生的代码
- thrift-demo-service - 实现服务
- thrift-demo-serve r - 简单服务器
- thrift-demo-client - 简单的客户端
pom文件如下,
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.xxx.tutorial</groupId> <artifactId>thrift-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>pom</packaging> <modules> <module>thrift-demo-interface</module> <module>thrift-demo-service</module> <module>thrift-demo-server</module> <module>thrift-demo-client</module> </modules> </project>
thrift-demo-interface模块
直接将上述生成的代码,拷贝到 src/main/java 中,如
遇到“ 红叉叉 ”是因为,没有导入相关的jar包~ 在该模块下的pom.xml文件中添加相关的依赖包即可,
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.xxx.tutorial</groupId> <artifactId>thrift-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>thrift-demo-interface</artifactId> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.thrift/libthrift --> <dependency> <groupId>org.apache.thrift</groupId> <artifactId>libthrift</artifactId> <version>0.10.0</version> </dependency> </dependencies> </project>
服务接口模块搞定,接下来要完成接口的实现~
thrift-demo-service模块
- 添加thrift-demo-interface依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.xxx.tutorial</groupId> <artifactId>thrift-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>thrift-demo-service</artifactId> <dependencies> <dependency> <groupId>com.xxx.tutorial</groupId> <artifactId>thrift-demo-interface</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
- 编写服务接口实现类
package com.xxx.tutorial.thrift.service.impl; import java.util.logging.Logger; import org.apache.thrift.TException; import com.xxx.tutorial.thrift.service.GreetingService; 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("调用sayHello方法的参数name = {%s}", name)); return "Hello, " + name; } }
thrift-demo-server模块
- 添加thrift-demo-service依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.xxx.tutorial</groupId> <artifactId>thrift-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>thrift-demo-server</artifactId> <dependencies> <dependency> <groupId>com.xxx.tutorial</groupId> <artifactId>thrift-demo-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
- 编写Server类
package com.xxx.tutorial.thrift.server; import java.util.logging.Logger; import org.apache.thrift.TProcessor; 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 com.xxx.tutorial.thrift.service.GreetingService; import com.xxx.tutorial.thrift.service.impl.GreetingServiceImpl; public class GreetingServer { private static final Logger logger = Logger.getLogger(GreetingServer.class.getName()); public static void main(String[] args) { try { TServerSocket serverTransport = new TServerSocket(9090); TBinaryProtocol.Factory proFactory = new TBinaryProtocol.Factory(); /** * 关联处理器与GreetingService服务实现 */ TProcessor processor = new GreetingService.Processor(new GreetingServiceImpl()); TThreadPoolServer.Args serverArgs = new TThreadPoolServer.Args(serverTransport); serverArgs.processor(processor); serverArgs.protocolFactory(proFactory); TServer server = new TThreadPoolServer(serverArgs); logger.info("Start server on port 9090..."); server.serve(); } catch (TTransportException e) { e.printStackTrace(); } } }
thrift-demo-client模块
- 添加thrift-demo-interface依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.xxx.tutorial</groupId> <artifactId>thrift-demo</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <artifactId>thrift-demo-client</artifactId> <dependencies> <dependency> <groupId>com.xxx.tutorial</groupId> <artifactId>thrift-demo-service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </project>
- 编写客户端调用rpc服务类
package com.xxx.tutorial.thrift.client; import java.util.logging.Logger; 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 com.xxx.tutorial.thrift.service.GreetingService; 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", 9091); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); GreetingService.Client client = new GreetingService.Client(protocol); String name = "Eric"; logger.info("请求参数==>name为" + name); String result = client.sayHello("Eric"); logger.info("返回结果==>为" + result); transport.close(); } catch (TTransportException e) { e.printStackTrace(); } catch (TException e) { e.printStackTrace(); } } }
至此,Maven工程中,4个模块的代码就简单写完了~ 接下来,测试一下~
测试
Server启动
直接运行GreetingServer.java(其中包含main函数)即可~
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 五月 28, 2017 3:03:27 下午 com.xxx.tutorial.thrift.server.GreetingServer main 信息: Start server on port 9090...
Client运行
直接运行GreetingClient.java(其中包含main函数)即可~
package com.xxx.tutorial.thrift.client; import java.util.logging.Logger; 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 com.xxx.tutorial.thrift.service.GreetingService; 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", 9091); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); GreetingService.Client client = new GreetingService.Client(protocol); String name = "Eric"; logger.info("请求参数==>name为" + name); String result = client.sayHello("Eric"); logger.info("返回结果==>为" + result); transport.close(); } catch (TTransportException e) { e.printStackTrace(); } catch (TException e) { e.printStackTrace(); } } }
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 五月 28, 2017 3:10:30 下午 com.xxx.tutorial.thrift.client.GreetingClient main 信息: 请求参数==>name为Eric Received 1 五月 28, 2017 3:10:30 下午 com.xxx.tutorial.thrift.client.GreetingClient main 信息: 返回结果==>为Hello, Eric
结果查看
我们可以看到,客户端调用接口,传值name为Eric,得到服务端的相应为Hello, Eric。
五月 28, 2017 3:10:30 下午 com.xxx.tutorial.thrift.client.GreetingClient main 信息: 返回结果==>为Hello, Eric
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". SLF4J: Defaulting to no-operation (NOP) logger implementation SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 五月 28, 2017 3:03:27 下午 com.xxx.tutorial.thrift.server.GreetingServer main 信息: Start server on port 9090... 五月 28, 2017 3:10:30 下午 com.xxx.tutorial.thrift.service.impl.GreetingServiceImpl sayHello 信息: 调用sayHello方法的参数name = {Eric}
至此,一个使用thrift完成的简单rpc示例就完成了~
后续会对示例代码去完善,并使用更多的IDL中定义的基础类型~
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- 每日一博 | 一步步完成 thrift Java 示例
- 利用python完成大学刷课(从0到完成的思路)
- 粒子滤波Matlab示例
- transformers示例
- 粒子滤波Matlab示例
- DuckDB:接口与示例
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Unix/Linux编程实践教程
Bruce Molay、杨宗源、黄海涛 / 杨宗源、黄海涛 / 清华大学出版社 / 2004-10-1 / 56.00元
操作系统是计算机最重要的系统软件。Unix操作系统历经了几十年,至今仍是主流的操作系统。本书通过解释Unix的工作原理,循序渐进地讲解实现Unix中系统命令的方法,让读者理解并逐步精通Unix系统编程,进而具有编制Unix应用程序的能力。书中采用启发式、举一反三、图示讲解等多种方法讲授,语言生动、结构合理、易于理解。每一章后均附有大量的习题和编程练习,以供参考。 本书适合作为高等院校计算机及......一起来看看 《Unix/Linux编程实践教程》 这本书的介绍吧!