内容简介:每日一博 | 一步步完成 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:接口与示例
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
ACM图灵奖演讲集
阿申豪斯特 / 苏运霖 / 电子工业出版社 / 2005-4 / 55.0
本书完整地收录了这些演讲,并配之以部分获奖者撰写的后记,旨在反映过去数年来这一领域中发生的变化。对任何一位计算机科学的历史与发展有兴趣的人来说,本书都极具收藏价值。 本文收录了自图灵奖开始颁发的1966年起到1985年这20年间图灵奖获得者在授奖大会上所做演讲的全文。由于在此期间有三次是把奖项同时授予两个人的,而其中有两次两位获奖者分别做了演讲,因此一共收录了22篇演讲稿。本书把这些演讲分为两大......一起来看看 《ACM图灵奖演讲集》 这本书的介绍吧!