记录备用。。
test.thrift
namespace java com.lxw.data.aggregation.test struct PhysicalDeviceModel{ 1:i32 id ; 2:string name; 3:string status; 4:string ip; 5:string pool; } service PhysicalDeviceServices { void addPhysicalDevice(1:PhysicalDeviceModel device), PhysicalDeviceModel getDeviceById(1:i32 id ) }
thrift --gen java test.thrift
编译生成PhysicalDeviceModel.java和PhysicalDeviceServices.java
实现PhysicalDeviceServices:
package com.lxw.data.aggregation.test; import java.util.HashMap; import java.util.Map; import org.apache.thrift.TException; public class PhysicalDeviceServicesImpl implements PhysicalDeviceServices.Iface { private Map<String,PhysicalDeviceModel> devices = new HashMap<String,PhysicalDeviceModel>(); public PhysicalDeviceServicesImpl() { PhysicalDeviceModel p1 = new PhysicalDeviceModel(1,"p1","running","10.10.10.1","pool1"); PhysicalDeviceModel p2 = new PhysicalDeviceModel(2,"p2","running","10.10.10.2","pool1"); PhysicalDeviceModel p3 = new PhysicalDeviceModel(3,"p3","shutdown","10.10.10.3","pool2"); PhysicalDeviceModel p4 = new PhysicalDeviceModel(4,"p4","shutdown","10.10.10.4","pool2"); devices.put(p1.getId() + "", p1); devices.put(p2.getId() + "", p2); devices.put(p3.getId() + "", p3); devices.put(p4.getId() + "", p4); } @Override public void addPhysicalDevice(PhysicalDeviceModel device) throws TException { String k = device.getId() + ""; devices.put(k, device); } @Override public PhysicalDeviceModel getDeviceById(int id) throws TException { return devices.get(id + ""); } }
RCPServer.java
package com.lxw.data.aggregation.test; import java.net.InetSocketAddress; 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.TServerTransport; import org.apache.thrift.transport.TTransportException; import com.lxw.data.aggregation.test.PhysicalDeviceServices.Iface; public class RPCServer { public static void main(String[] args) { PhysicalDeviceServices.Processor<Iface> processor = new PhysicalDeviceServices.Processor<PhysicalDeviceServices.Iface>(new PhysicalDeviceServicesImpl()); try { TServerTransport serverTransport = new TServerSocket( new InetSocketAddress("localhost",9813)); TServer.Args tArgs = new TServer.Args(serverTransport); tArgs.processor(processor); tArgs.protocolFactory(new TBinaryProtocol.Factory()); TServer server = new TSimpleServer(tArgs); server.serve(); } catch (TTransportException e) { e.printStackTrace(); } } }
ThriftClient.java
package com.lxw.data.aggregation.test; 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 ThriftClient { public static void main(String[] args) throws Exception { TTransport transport = new TSocket("localhost", 9813); TProtocol protocol = new TBinaryProtocol(transport); PhysicalDeviceServices.Client client = new PhysicalDeviceServices.Client(protocol); transport.open(); PhysicalDeviceModel p1 = client.getDeviceById(1); System.out.println(p1.getId() + ":" + p1.getName()); System.out.println(p1); PhysicalDeviceModel p2 = new PhysicalDeviceModel(1,"p1","ddddddd","10.10.10.1","pool1"); client.addPhysicalDevice(p2); p1 = client.getDeviceById(1); System.out.println(p1.getId() + ":" + p1.getName()); System.out.println(p1); } }
相关推荐
Thrift java demo实例,通过简单的例子,帮助初学者理解Thrift
java代码使用thrift2操作hbase示例,thrift2连接hbase添加数据,单条查找,删除数据,根据扫描器查找,修改数据等测试实例
采用java操作thrift代码示例
thrift server. use client connection pool. singleton conf , async logger and utility classes
一个java和python的thrift例子,其中java作为服务端,python作为客户端
Thrift双向通讯java代码
NULL 博文链接:https://onlyor.iteye.com/blog/1700777
thrift java build jar
通过java api 接口 利用thrift读取hbase表中的数据 解压后包含两个文件 一个是实验步骤文档 一个是java 代码
Thrift0.9.1 示例代码 Java版本 Thrift0.9.1 示例代码 Java版本
Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。接下来通过本文给大家介绍Java thrift服务器和客户端创建实例代码,需要的朋友参考下吧
基于Thrift、zookeeper的rpc实现 基于注解配置ThriftService、ThriftReference 基于权重的简单负载均衡 使用TMultiplexedProcessor发布多个服务
thrift-0.9.2 for java 的依赖包,libthrift-0.9.2.jar
此文件为自己在Mac电脑上写的thrift的demo,使用maven 管理了小程序,并用thrift生成了源码,其中对thrift生成的源码,把override注销了,其他的没处理,如果感兴趣参考博客:...
Apache Thrift Java实战源码,包含了客户端和服务端源码,客户端和服务端是分开的,如果需要放到一个工程,直接把Client.java文件复制到服务端运行即可。
节俭的模拟 用于模拟节俭服务的轻量级Java单元测试库特征初始化节俭模拟服务器,无需任何预定义节俭IDL,仅需一个端口动态绑定节俭接口对其响应可配置的响应延迟支持将不同的节俭服务中的不同接口绑定到一台服务器...
* 方便的使Java、Python、C++三种程序可以相互通信 * 负载均衡和容灾处理 * 方便的实现任务的分布式处理 * 支持服务的水平扩展,自动发现新的服务节点 * 能够兼容各种异常情况,如节点的异常down机 * 可视化管理...
thrift安装包以及JAVA的一个测试项目
Thrift Java 服务器 客户端通信,网上看到的,觉得不错就分享出来供大家一起共同分享学习。
Thrift作为可伸缩的跨语言服务开发框架,网上的资源较少,这里是一个简单的入门小程序,文件中的mylib下包含了依赖的jar包,并且在file目录下放了一个简单的thrift...gen java Hello.thrift 命令就能生成对应的工具类