参考资料:
thrift.apache.org
http://mingxiao2010.blog.163.com/blog/static/8619048120114273843772/
做一个HelloWorld的例子,基本上跑通了。
过程如下:
环境准备:
开始我选择的是最新的版本:Apache Thrift v0.9.0 ,
1.在linux下有一些lib库没有安装成功,问题: configure: error: "Error: libcrypto required."
2.在windows下了一个windows版本, 运行 thrift-0.9.0.exe -r --gen java Hello.thrift ,发现不是win32版本。
3.重新下载了0.7.0 windows版本,此版本OK,可以在win 32使用
4.下载相关jar包:org.apache.servicemix.bundles.libthrift-0.7.0_1.jar , libfb303-0.7.0.jar
5.接下来就在windows环境下开发
例子:
1. thrift接口定义:Hello.thrift
- namespace java service.demo
- ervice Hello{
- string helloString(1:string para)
- i32 helloInt(1:i32 para)
- bool helloBoolean(1:bool para)
- void helloVoid()
- string helloNull()
2. 生成java接口
thrift-0.7.0.exe -r --gen java Hello.thrift
会生如下代码:
thrift-0.7.0.exe -r --gen java Hello.thrift
会生如下代码:
- /**
- * Autogenerated by Thrift Compiler (0.7.0)
- *
- * DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
- */
- package service.demo;
- import java.util.List;
- import java.util.ArrayList;
- import java.util.Map;
- import java.util.HashMap;
- import java.util.EnumMap;
- import java.util.Set;
- import java.util.HashSet;
- import java.util.EnumSet;
- import java.util.Collections;
- import java.util.BitSet;
- import java.nio.ByteBuffer;
- import java.util.Arrays;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- public class Hello {
- public interface Iface {
- public String helloString(String para) throws org.apache.thrift.TException;
- public int helloInt(int para) throws org.apache.thrift.TException;
- public boolean helloBoolean(boolean para) throws org.apache.thrift.TException;
- public void helloVoid() throws org.apache.thrift.TException;
- public String helloNull() throws org.apache.thrift.TException;
- }
- .....此处省略,详细请看自己生成文件
- }
3. 将生成的service.demo.Hello.java的导入eclipse中
4. 编写Server端实现类HelloServiceImpl: 即 Hello.Iface 的实现
- package service.demo;
- import org.apache.thrift.TException;
- public class HelloServiceImpl implements Hello.Iface {
- @Override
- public boolean helloBoolean(boolean para) throws TException {
- return para;
- }
- @Override
- public int helloInt(int para) throws TException {
- try {
- Thread.sleep(1000);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- return para+1;
- }
- @Override
- public String helloNull() throws TException {
- return null;
- }
- @Override
- public String helloString(String para) throws TException {
- return para;
- }
- @Override
- public void helloVoid() throws TException {
- System.out.println("Hello World");
- }
- }
5. Server启动类: HelloServiceServer
- package service.server;
- import service.demo.Hello.Processor;
- 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.server.TThreadPoolServer.Args;
- import org.apache.thrift.transport.TServerSocket;
- import org.apache.thrift.transport.TTransportException;
- import service.demo.Hello;
- import service.demo.HelloServiceImpl;
- public class HelloServiceServer {
- public void startServer() {
- try {
- TServerSocket serverTransport = new TServerSocket(1234);
- Hello.Processor process = new Processor(new HelloServiceImpl());
- Factory portFactory = new TBinaryProtocol.Factory(true, true);
- Args args = new Args(serverTransport);
- args.processor(process);
- args.protocolFactory(portFactory);
- TServer server = new TThreadPoolServer(args);
- server.serve();
- } catch (TTransportException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- HelloServiceServer server = new HelloServiceServer();
- server.startServer();
- }
- }
6. 客户端类:HelloClient
- package service.client;
- import org.apache.thrift.TException;
- import org.apache.thrift.protocol.TBinaryProtocol;
- import org.apache.thrift.protocol.TProtocol;
- import org.apache.thrift.transport.TSocket;
- import org.apache.thrift.transport.TTransport;
- import org.apache.thrift.transport.TTransportException;
- import service.demo.Hello;
- public class HelloClient {
- public void startClient() {
- TTransport transport;
- try {
- transport = new TSocket("localhost", 1234);
- TProtocol protocol = new TBinaryProtocol(transport);
- Hello.Client client = new Hello.Client(protocol);
- transport.open();
- System.out.println(client.helloInt(12));
- transport.close();
- } catch (TTransportException e) {
- e.printStackTrace();
- } catch (TException e) {
- e.printStackTrace();
- }
- }
- public static void main(String[] args) {
- HelloClient client = new HelloClient();
- client.startClient();
- }
- }
7.运行:
a. 启动HelloServiceServer
b. 启动HelloClient
运行结果:client端会显示13 即:helloInt是进行+1操作。
附:c#客户端代码(参考http://blog.csdn.net/kc87654321/article/details/7226675)
1. thrift -gen csharp Hello.thrift
2. 导入thrift的C#库工程:/thrift/lib/
3. 将生成的Hello.cs加入到项目
4. 客户端代码如下:
5. 设置C#启动项目
6. 运行
运行结果:client端会显示13 即:helloInt是进行+1操作。
附:c#客户端代码(参考http://blog.csdn.net/kc87654321/article/details/7226675)
1. thrift -gen csharp Hello.thrift
2. 导入thrift的C#库工程:/thrift/lib/
3. 将生成的Hello.cs加入到项目
4. 客户端代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Thrift.Transport;
- using Thrift.Protocol;
- namespace ConsoleApplication1
- {
- class Program
- {
- static void Main(string[] args)
- {
- TTransport transport = new TSocket("localhost", 1234);
- TProtocol protocol = new TBinaryProtocol(transport);
- Hello.Client client = new Hello.Client(protocol);
- transport.Open();
- Console.WriteLine("Client calls .....");
- while (true)
- {
- String str=Console.ReadLine();
- Console.WriteLine(client.helloInt(Convert.ToInt32(str)));
- }
- transport.Close();
- Console.ReadLine();
- }
- }
- }
6. 运行