分享

Thrift安装和应用(C#)

 歆馨 2015-09-01

本文主要介绍两部分内容:

  • C#中使用Thrift简介
  • 用Java创建一个服务端,用C#创建一个客户端通过thrift与其交互。
  • 用纯C#实现Client和Server
  • C#服务端,Java客户端

其中使用到RPC学习----Thrift快速入门和Java简单示例,这篇文章创建的Java服务端。

一、C#中使用Thrift简介

关于rpc的简介,可以参考:RPC学习----Thrift快速入门和Java简单示例

1、下载thrift

1)点击下载:thrift-0.9.1.tar.gz (或者http://thrift./download)

2)Thrift compiler for Windows (thrift-0.9.1.exe) 

两个都要下载。

 

2、引入thrift.dll

这里将下载好的.gz文件解压后,然后找到lib目录

 

用vs打开后,如下图所示,然后右键--》重新生成---》生成thrift.dll

 

 

 

3、生成cs文件

hello.thrift

service  HelloWorldService {
  string sayHello(1:string username)
}

使用命令生成cs文件:

thrift-0.9.1.exe -gen csharp hello.thrift

关于thrift-0.9.1.exe的使用方法可以查看命令: thrift-0.9.1.exe -help

 

将生成的HelloWorldService.cs文件拷入项目中。

 

二、C#客户端发送消息到Java生成的服务端,实现跨平台操作

1、启动Java版的服务端

 

 

2、使用vs新建一个winform程序

 

button点击事件:

复制代码
      private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != null)
            {
              new HelloWorldServiceClient().startClient(textBox1.Text.Trim());
           }
        }            
复制代码

HelloWorldServiceClient.cs

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Thrift.Protocol;
using Thrift.Transport;
using Thrift;

namespace thrfitCsharp
{
    class HelloWorldServiceClient
    {
        public const string SERVERIP = "localhost";
        public static int SERVERPORT = 8090;
        public static int TIMEOUT = 3000;

        public void startClient(String username)
        {
            TTransport transport = null;
            try
            {
                transport = new TSocket(SERVERIP, SERVERPORT, TIMEOUT);
                //协议要和服务端一致
                TProtocol protocol = new TCompactProtocol(transport);
                HelloWorldService.Client client = new HelloWorldService.Client(protocol);
                transport.Open();
                String result = client.sayHello(username);
                Console.WriteLine("Thrift client result =: " + result);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }
            finally
            {
                if (null != transport)
                {
                    //close
                    transport.Close();
                }
            }
        }


    }
}
复制代码

 

HelloWroldImpl.cs

复制代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace thrfitCsharp
{
    class HelloWroldImpl : HelloWorldService.Iface
    {

        public  string sayHello(string username){
            Console.WriteLine("hello"+username);
            return "hello" + username;
        }

    }
}
复制代码

 

效果图:

 

三、纯C#版(C#实现客户端和服务端)

注:下面是改进版的,主要添加了纯C#版的:

 

纯C#版是说用C#实现客户端和服务端,下面是纯c#版的输出:

 

 

四、C#服务端,Java客户端

 

VS 2013终端输出:

复制代码
num1:1 num2:2 num3:3
testCase1  num1+num2 is :3
testCase2 ...
username : amosli
address : shanghai
testCase3 ...........2014/9/1 23:59:19
testCase4 ...........
id:001
IpAddress:192.168.0.11
Content:topic:topic1 is rpc
time:1409587159730
id:002
IpAddress:192.168.0.12
Content:topic:topic2 is rpc too!
time:1409587159730
复制代码

 

Java客户端源码:

生成Java客户端代码:

将生成的Java文件拷到Java项目中:

 

源码:

BlogClient.java

复制代码
package com.amos.thrift;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

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;

/**
 * Created by amosli on 14-8-12.
 */
public class BlogClient {

    public static final String SERVER_IP = "localhost";
    public static final int SERVER_PORT = 7911;
    public static final int TIMEOUT = 3000000;

    /**
     * @param args
     */
    public static void main(String[] args) {
        BlogClient client = new BlogClient();
        client.startClient("amosli");

    }

    /**
     * @param userName
     */
    public void startClient(String userName) {
        TTransport transport = null;
        try {
            transport = new TSocket(SERVER_IP, SERVER_PORT, TIMEOUT);
            // 协议要和服务端一致
            TProtocol protocol = new TBinaryProtocol(transport);
            // TProtocol protocol = new TCompactProtocol(transport);
            // TProtocol protocol = new TJSONProtocol(transport);
            ThriftCase.Client client = new ThriftCase.Client(protocol);

            transport.open();
            
            //case 1
            client.testCase1(1, 2, "3");

            //case 2
            Map<String, String> num1 = new HashMap<String, String>();
            num1.put("username", "amosli");
            num1.put("address", "shanghai");
            client.testCase2(num1);

            //case 3
            client.testCase3();


            //case 4
            List<Blog> list = new ArrayList<Blog>();
            ByteBuffer content = ByteBuffer.allocate(30);
            content.put("this is content java client".getBytes());

            Map<String, String> props = new Hashtable<String, String>();
            props.put("one", "1");
            props.put("two", "2");
            props.put("three", "3");

            list.add(new Blog("topic1 is rpc", content, System.currentTimeMillis(), "001", "192.168.0.11", props));
            list.add(new Blog("topic2 is rpc too!", content, System.currentTimeMillis(), "002", "192.168.0.12", props));

            client.testCase4(list);

            System.out.println("blog client stop ....");
        } catch (TTransportException e) {
            e.printStackTrace();
        } catch (TException e) {
            e.printStackTrace();
        } finally {
            if (null != transport) {
                transport.close();
            }
        }
    }

}
复制代码

C#服务端“开户服务”的事件和纯C#版的代码是一样的,如下:

  Thread thread = new Thread(new ThreadStart(new ThreadStart(new BlogServer().StartServer)));
  thread.Start();//start

 

 

 

本文源码https://github.com/amosli/rpc/tree/thriftCsharp 

纯C#版实现主要参考http://www.cnblogs.com/hanmos/archive/2011/09/15/2177891.html

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多