一步步完成thrift rpc示例
根据thift自动生成代码 - 编写thrif文件,并根据工具在window下自动生成thrif相关代码
代码组成 - 给出Maven工程的模块化结构组成,并在每个模块中一步步实现代码
测试 - 对编写的代码进行测试,包括Server启动并绑定服务、Client连接并调用服务
一、根据thrift自动生成代码
1.1 定义服务接口
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)
}
1.2 产生thrift对应的代码
【http://www.apache.org/dyn/closer.cgi?path=/thrift/0.10.0/thrift-0.10.0.exe】
thrift-0.10.0.exe -gen java hello.thrift
二、代码组成
2.1 Maven工程结构
thrift-demo这个Maven工程主要包含四个模块,
thrift-demo-interface - 存放thrift文件产生的代码
thrift-demo-service - 实现服务
thrift-demo-server - 简单服务器
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>
2.2 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-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>
2.3 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;
}
}
2.4 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();
}
}
}
2.5 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个模块的代码就简单写完了。 接下来,测试一下。
三、测试
3.1 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...
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 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
3.3 结果查看
五月 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}