RPC學習--C#使用Thrift簡介,C#客戶端和Java服務端相互交互

本文主要介紹兩部份內容:html

  • C#中使用Thrift簡介
  • 用Java建立一個服務端,用C#建立一個客戶端經過thrift與其交互。
  • 用純C#實現Client和Server
  • C#服務端,Java客戶端

其中使用到RPC學習----Thrift快速入門和Java簡單示例,這篇文章建立的Java服務端。java

1、C#中使用Thrift簡介

關於rpc的簡介,能夠參考:RPC學習----Thrift快速入門和Java簡單示例git

一、下載thriftgithub

1)點擊下載:thrift-0.9.1.tar.gz (或者http://thrift.apache.org/download)apache

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

兩個都要下載。ide

 

二、引入thrift.dllpost

這裏將下載好的.gz文件解壓後,而後找到lib目錄學習

 

用vs打開後,以下圖所示,而後右鍵--》從新生成---》生成thrift.dllthis

 

 

 

三、生成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文件拷入項目中。

 

2、C#客戶端發送消息到Java生成的服務端,實現跨平臺操做

一、啓動Java版的服務端

 

 

二、使用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;
        }

    }
}

 

效果圖:

 

3、純C#版(C#實現客戶端和服務端)

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

 

純C#版是說用C#實現客戶端和服務端,下面是純c#版的輸出:

 

 

4、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();
            }
        }
    }

}
View Code

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

相關文章
相關標籤/搜索