Thrift 实例 Helloworld

参考资料:
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
service 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
     会生如下代码:
/**
 * 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.   客户端代码如下:
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();
        }
    }
}
  5. 设置C#启动项目
  6. 运行


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Thrift 是一个高效的跨语言 RPC 框架,它支持多种编程语言,包括 C++, Java, Python, Ruby, PHP 等。下面是一个简单的 Thrift 实例,展示了如何使用 Thrift 进行跨语言通信: 1. 编写 Thrift IDL 文件 定义一个 Thrift 接口,使用 Thrift IDL 语言编写一个服务接口文件,例如 Example.thrift: ``` namespace java com.example namespace py example service ExampleService { string ping(1: string message) } ``` 2. 生成代码 使用 Thrift 编译器将 Thrift IDL 文件编译成代码,例如: ``` thrift --gen java Example.thrift thrift --gen py Example.thrift ``` 将会生成 Java 和 Python 的代码文件,可以根据需要进行使用。 3. 实现服务端 使用生成的代码实现服务端,例如 Java 语言: ```java public class ExampleHandler implements ExampleService.Iface { @Override public String ping(String message) throws TException { return "Pong: " + message; } } public static void main(String[] args) throws Exception { TServerTransport serverTransport = new TServerSocket(9090); ExampleService.Processor<ExampleHandler> processor = new ExampleService.Processor<>(new ExampleHandler()); TServer server = new TSimpleServer(new TServer.Args(serverTransport).processor(processor)); System.out.println("Starting the server..."); server.serve(); } ``` 4. 实现客户端 使用生成的代码实现客户端,例如 Python 语言: ```python transport = TSocket.TSocket('localhost', 9090) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = ExampleService.Client(protocol) transport.open() response = client.ping("Hello, world!") print(response) transport.close() ``` 以上就是一个简单的 Thrift 实例,展示了如何使用 Thrift 进行跨语言通信。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值