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();
}
}
}
}

最新文章

  1. 警惕!高版本VS发布时预编译导致Mono中Razor找不到视图
  2. XSS 防御方法总结
  3. coding.net就这么横空出世
  4. linux默认编辑器 sublime
  5. visio调整画布大小和旋转画布(转)
  6. Memcache学习php完整一例
  7. POJ 2407 Relatives 【欧拉函数】
  8. IDL简介与corba入门案例
  9. Android实现控件动画效果
  10. linux中的解压,压缩命令
  11. UVA 10739 String to Palindrome(动态规划 回文)
  12. Highcharts 异步加载数据曲线图表
  13. JavaScript学习之—prototype
  14. hdu 4472 dp
  15. C#获得时间段
  16. 阿里云负载均衡SSL证书配置
  17. 15_Python模块化编程_Python编程之路
  18. Java链接MySQL数据库的配置文件
  19. 支持向量机(Support Vector Machine):对偶
  20. configEnvironment()源码探究

热门文章

  1. .NET程序调试技巧(一):快速定位异常的一些方法
  2. 4190. Prime Palindromes 一亿以内的质数回文数
  3. 配置管理之PackageProvider接口
  4. Android无线测试之—UiAutomator UiObject API介绍六
  5. Hbase的shell命令学习
  6. poj3411
  7. CoordinatorLayout Behaviors使用说明[翻译]
  8. Get请求-Test版
  9. phpstorm的设置
  10. jquery根据值设置radio和select选中状态