`
superlxw1234
  • 浏览: 542441 次
  • 性别: Icon_minigender_1
  • 来自: 西安
博客专栏
Bd1c0a0c-379a-31a8-a3b1-e6401e2f1523
Hive入门
浏览量:43242
社区版块
存档分类
最新评论

java Thrift example

    博客分类:
  • java
阅读更多

记录备用。。

 

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

 

0
0
分享到:
评论
1 楼 josico 2014-04-02  
用是这么用没错
想问一下 LZ公司现在是用Thrift做RPC架构吗?
用Thrift的话 服务端性能如何 目前版本只是0.9.1,稳定性如何,与应用程序耦合程度等

相关推荐

Global site tag (gtag.js) - Google Analytics