记录备用。。
?
test.thrift
class="java">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);
}
}
?