本文给出一个在Windows下,使用thrift一步步完成rpc的Java示例。文章主要从如下几个部分来加以说明:
下面就一步步来完成每一个部分~
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) }
可以访问【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代码部分就完成了。接下来,要做的就是编写服务实现、服务器代码以及客户端调用接口的代码等。
thrift-demo这个Maven工程主要包含四个模块,
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>
直接将上述生成的代码,拷贝到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>
服务接口模块搞定,接下来要完成接口的实现。
<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; }
}
<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>
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(); } }}
<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>
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个模块的代码就简单写完了。 接下来,测试一下。
直接运行GreetingServer.java(其中包含main函数)即可~
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".SLF4J: Defaulting to no-operation (NOP) logger implementationSLF4J: 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...
3.2 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 implementationSLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.五月 28, 2017 3:10:30 下午 com.xxx.tutorial.thrift.client.GreetingClient main信息: 请求参数==>name为EricReceived 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 implementationSLF4J: 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中定义的基础类型,以及多种不同服务类型。