本文主要介紹兩部分內容:
- 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.apache.org/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