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文件。见图。


把这三个文件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端。


整个项目结构:


其中,pom.xml文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.micmiu.thrift</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
<dependencies>
  <dependency>
	<groupId>org.apache.thrift</groupId>
	<artifactId>libthrift</artifactId>
	<version>0.9.2</version>
</dependency>
<dependency>
	<groupId>org.slf4j</groupId>
	<artifactId>slf4j-log4j12</artifactId>
	<version>1.5.8</version>
</dependency>

</dependencies>
</project>


其中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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值