zoukankan      html  css  js  c++  java
  • RPC学习--C#使用Thrift简介,C#客户端和Java服务端相互交互

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

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

  • 相关阅读:
    拓扑排序笔记
    最小生成树——垃圾佬抓宠物
    次小生成树
    关于 海平面上升 与 fold的毒瘤题(easy) 的思考
    看正月点灯笼老师的笔记—01背包
    欧拉图的判定欧拉路的求法
    离散实验——关系闭包运算
    Floyd 求最短路
    离散实验——二元关系及其性质
    最小生成树
  • 原文地址:https://www.cnblogs.com/amosli/p/3948342.html
Copyright © 2011-2022 走看看