Thrift简单调用
2024-09-03 16:05:03
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>
<groupId>io.guangsoft</groupId>
<artifactId>thrift</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
在官网下载thrift工具 http://thrift.apache.org/download
编写Thrift.thrift
namespace java io.guangsoft.thrift
service Thrift{
string TransmitStr(1:string para)
}
执行命令 thrift -r -gen java Thrift.thrift
ThriftImpl.java
package io.guangsoft.thrift; import org.apache.thrift.TException; public class ThriftImpl implements Thrift.Iface{ @Override
public String TransmitStr(String para) throws TException {
return "GuangSoft Thrift : " + para;
} }
ThriftServer.java
package io.guangsoft.thrift.Server; 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 io.guangsoft.thrift.Thrift;
import io.guangsoft.thrift.ThriftImpl; public class ThriftServer {
/**
* 启动thrift服务器
*
* @param args
*/
public static void main(String[] args) {
try {
System.out.println("服务端开启....");
TProcessor tprocessor = new Thrift.Processor<Thrift.Iface>(new ThriftImpl());
// 简单的单线程服务模型
TServerSocket serverTransport = new TServerSocket(9898);
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) {
e.printStackTrace();
}
}
}
ThriftClient.java
package io.guangsoft.thrift.client; 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 io.guangsoft.thrift.Thrift; public class ThriftClient {
public static void main(String[] args) {
System.out.println("客户端启动....");
TTransport transport = null;
try {
transport = new TSocket("localhost", 9898, 30000);
// 协议要和服务端一致
TProtocol protocol = new TBinaryProtocol(transport);
Thrift.Client client = new Thrift.Client(protocol);
transport.open();
String result = client.TransmitStr("你好呀,Thrift!");
System.out.println(result);
} catch (TTransportException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} finally {
if (null != transport) {
transport.close();
}
}
}
}
最新文章
- 警惕!高版本VS发布时预编译导致Mono中Razor找不到视图
- XSS 防御方法总结
- coding.net就这么横空出世
- linux默认编辑器 sublime
- visio调整画布大小和旋转画布(转)
- Memcache学习php完整一例
- POJ 2407 Relatives 【欧拉函数】
- IDL简介与corba入门案例
- Android实现控件动画效果
- linux中的解压,压缩命令
- UVA 10739 String to Palindrome(动态规划 回文)
- Highcharts 异步加载数据曲线图表
- JavaScript学习之—prototype
- hdu 4472 dp
- C#获得时间段
- 阿里云负载均衡SSL证书配置
- 15_Python模块化编程_Python编程之路
- Java链接MySQL数据库的配置文件
- 支持向量机(Support Vector Machine):对偶
- configEnvironment()源码探究