thrift 实例

        Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 等等编程语言间无缝结合的、高效的服务。Thrift最初由facebook开发,07年四月开放源码,08年5月进入apache孵化器。thrift允许你定义一个简单的定义文件中的数据类型和服务接口。以作为输入文件,编译器生成代码用来方便地生成RPC客户端和服务器通信的无缝跨编程语言。


windows下安装可参考教程:http://www.mamicode.com/info-detail-1570237.html


本实例项目是用maven创建的,所以需要配置pom.xml文件,具体后面会帖出来。

服务端编写的一般步骤:
1. 创建Handler,即实现服务处理接口impl

2. 基于Handler创建Processor

3. 创建Transport(通信方式)

4. 创建Protocol方式(设定传输格式)

5. 基于Processor, Transport和Protocol创建Server

6. 运行Server



客户端编写的一般步骤:

1. 创建Transport

2. 创建Protocol方式

3. 基于Transport和Protocol创建Client

4. 运行Client的方法




Thrift实例编码演示

1、编写服务描述文件test.thrift,定义login服务和register

namespace java com.marho.service

struct User{
1:i64 id,
2:string name,
3:string password
}

service LoginService{
User login(1:string name,2:string psw);
}

service RegisterService{
User createUser(1:string name,2:string psw);
}

2、将该文件放到包含thrift.exe文件(是官网提供的windows下编译工具)的thrift目录下,使用thrift生成java语言代码:

thrift-0.9.2.exe -gen java test.thrift

就会在当前目录下生成一个gen-java文件夹,里面包含目录com/marho/service,目录下生成了三个对应的java文件。见图。

thrift 实例_第1张图片

把这三个文件copy到自己的工程中去,如图。


3、实现服务接口

实现LoginService:

package com.marho.service.impl;

import org.apache.thrift.TException;

import com.marho.service.LoginService.Iface;
import com.marho.service.User;

public class LoginServiceImpl implements Iface {

	public LoginServiceImpl() {
	}

	@Override
	public User login(String name, String psw) throws TException {
	
		User user = null;
		//验证登录用户的用户名和密码
		if(name.equals("marho") && psw.equals("123")){
			user = new User();
			user.setId(1);
			user.setName("marho");
		}
		return user;
	}
}

实现RegisterService:

package com.marho.service.impl;

import org.apache.thrift.TException;

import com.marho.service.RegisterService.Iface;
import com.marho.service.User;

public class RegisterServiceImpl implements Iface {
	

	public RegisterServiceImpl() {
	}

	@Override
	public User createUser(String name, String psw) throws TException {

		User user = new User();
		user.setId(2);
		user.setName(name);
		user.setPassword(psw);
		return user;
	}

}

4、编写服务端代码

package com.marho.service.main;

import org.apache.thrift.TMultiplexedProcessor;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

import com.marho.service.LoginService;
import com.marho.service.RegisterService;
import com.marho.service.impl.LoginServiceImpl;
import com.marho.service.impl.RegisterServiceImpl;

public class Server {

	private void start(){
		
		try {
			//创建用户登录的Processor
			LoginService.Processor loginProcessor = new LoginService.Processor(new LoginServiceImpl());
			//创建用户注册的Processor
			RegisterService.Processor registerProcessor = new RegisterService.Processor(new RegisterServiceImpl());
			//创建Transport
			TServerSocket serverTransport = new TServerSocket(7911);
			//创建Protocol
			/*
			 * 一个接口服务的流程
			Factory protFactory = new TBinaryProtocol.Factory(true, true);
			TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport);
			tArgs.processor(loginProcessor);
			tArgs.protocolFactory(protFactory);
			//基于Processor, Transport和Protocol来创建server
			TServer server = new TThreadPoolServer(tArgs);
			server.serve();
			*/
			//这里使用多接口服务,通过TMultiplexedProcessor类,可以注册多个接口的服务实现类
			TMultiplexedProcessor processor = new TMultiplexedProcessor();
			processor.registerProcessor("LoginService", loginProcessor);
			processor.registerProcessor("RegisterService",registerProcessor);
			//创建Protocol
			Factory protFactory = new TBinaryProtocol.Factory(true, true);			
			//基于Processor, Transport和Protocol来创建server
			TThreadPoolServer.Args tArgs = new TThreadPoolServer.Args(serverTransport);
			tArgs.processor(processor);
			tArgs.protocolFactory(protFactory);
			TServer server = new TThreadPoolServer(tArgs);
			System.out.println("Starting server on port 7911 ...");
			server.serve();			
		} catch (TTransportException  e) {
			e.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		Server serv = new Server();
		serv.start();
	}

}


5、编写客户端代码

package com.marho.client.main;

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

import com.marho.service.LoginService;
import com.marho.service.RegisterService;
import com.marho.service.User;

public class Client {

	public static void main(String[] args) {
		try {
			//创建Transport
			TTransport transport = new TSocket("localhost", 7911);
			//创建创建Protocol
			TProtocol protocol = new TBinaryProtocol(transport);
			//MultiplexedProtocol帮助客户端区别调用哪个接口
			TMultiplexedProtocol mp1 = new TMultiplexedProtocol(protocol, "LoginService");
			LoginService.Client loginCLient = new LoginService.Client(mp1);
			TMultiplexedProtocol mp2 = new TMultiplexedProtocol(protocol, "RegisterService");
			RegisterService.Client registerCLient = new RegisterService.Client(mp2);
			
			transport.open();
			User user1 = loginCLient.login("marho", "123");
			if(user1 != null){
				System.out.println("登录成功:"+user1.getId()+" "+user1.getName());
			}else{
				System.out.println("登录失败");
			}
			
			User user2 = registerCLient.createUser("marho1", "123");
			if(user2 != null){
				System.out.println("创建用户成功:" + user2.getId() + " "+ user2.getName());
			}else{
				System.out.println("创建用户失败");
			}
			transport.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

执行,先启动server端,再启动client端。


整个项目结构:

thrift 实例_第2张图片

其中,pom.xml文件:


  4.0.0
  com.micmiu.thrift
  demo
  0.0.1-SNAPSHOT
  

  
	org.apache.thrift
	libthrift
	0.9.2


	org.slf4j
	slf4j-log4j12
	1.5.8





其中spring和Thrift集成参考https://my.oschina.net/penngo/blog/492253

源码下载

参考:

https://my.oschina.net/penngo/blog/489311
https://my.oschina.net/penngo/blog/492253
http://blog.csdn.net/hivon/article/details/11681977

http://www.micmiu.com/soa/rpc/thrift-sample/

http://www.cnblogs.com/lzq198754/p/5780331.html

你可能感兴趣的:(thrift)