最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java中thrift 入门详解
时间:2016-08-22 编辑:简简单单 来源:一聚教程网
install
brew install thrift
例子
maven
IDL
编写thrift IDL文件hello.thrift,用于生成java代码
namespace java demo
service HelloService {
string hi()
}
generate
执行名声,生成java代码
thrift -gen java hello.thrift
此时,会在当前目录下生成java-gen文件夹,其中包含所需要的thrift接口文件HelloService.java.
impl
上步中生成了相应的HelloServie.Iface接口,所以需要实现该接口文件,
package demo;
public class HelloImpl implements HelloService.Iface {
public String hi() {
return "Hi!!!";
}
}
server
package demo;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TSimpleServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
public class Server {
public static void main(String[] args) {
HelloImpl hello = new HelloImpl();
final HelloService.Processor
new HelloService.Processor
new Thread(new Runnable() {
public void run() {
try {
TServerTransport st = new TServerSocket(9000);
TServer tServer = new TSimpleServer(new TServer.Args(st).processor(p));
System.out.println("Starting ...");
tServer.serve();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
client
注意server使用的端口9000,
package demo;
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;
public class Client {
public static void main(String[] args) throws TException {
TTransport ts = new TSocket("localhost", 9000);
ts.open();
TProtocol p = new TBinaryProtocol(ts);
HelloService.Client client = new HelloService.Client(p);
System.out.println(client.hi());
ts.close();
}
}
-
上一个: java实现两张图片的合并的例子
-
下一个: Java内存模型和线程安全的使用例子
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24
- SpringBoot自定义bean绑定解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24