本文主要介绍两部分内容:
其中使用到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
|
|