zoukankan      html  css  js  c++  java
  • Thrift实践

     

    Thrift实践:(一)安装 -- 未完待续

     

    1. 新建一个目录,C: est hrift-test,里面建2个子文件夹,client-node和sever-csharp,然后把Thrift官方的thrift定义文件也拷贝进去。

    2. 官方的thrift定义文件,会去引用一个shared的thrift文件,但是这个文件貌似没地方下载,导致生成接口时报错,索性我就把他从定义里面删掉,干净起见,注释也删了。

    1. namespace cpp tutorial  
    2. namespace d tutorial  
    3. namespace java tutorial  
    4. namespace php tutorial  
    5. namespace perl tutorial  
    6.   
    7. typedef i32 MyInteger  
    8.   
    9. const i32 INT32CONSTANT = 9853  
    10. const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'}  
    11.   
    12. enum Operation {  
    13.   ADD = 1,  
    14.   SUBTRACT = 2,  
    15.   MULTIPLY = 3,  
    16.   DIVIDE = 4  
    17. }  
    18.   
    19. struct Work {  
    20.   1: i32 num1 = 0,  
    21.   2: i32 num2,  
    22.   3: Operation op,  
    23.   4: optional string comment,  
    24. }  
    25.   
    26. exception InvalidOperation {  
    27.   1: i32 what,  
    28.   2: string why  
    29. }  
    30.   
    31. service Calculator{  
    32.   
    33.    void ping(),  
    34.   
    35.    i32 add(1:i32 num1, 2:i32 num2),  
    36.   
    37.    i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch),  
    38.   
    39.    oneway void zip()  
    40.   
    41. }  

    3. 打开Visual Studio 2012,新建一个项目,名为thrift-1stApp-server,项目放在刚才建立的子目录server-csharp下。

    4. 右键项目,选择Manage Nuget Packages,选择Online, 然后搜索thrift,第一个应该就是Apache官方提供的thrift C#库了,注意版本号和CreateBy属性,CreateBy一定要是The Apache Software Foundation,别下载山寨版。版本号要和接下来下载的thrift compiler对应。我这里显示的是2012年11月20日发布的0.9.0.0版本。点击安装,thrift.dll就被自动加入到项目引用里面了。

    5. 现在thrift的windows版compiler(已经帮你编译好的exe),官方提供的下载链接是:http://thrift.apache.org/download/,只有最新0.91版的,找了半天也没找到0.90的,网上搜了下,总算下载到了:http://archive.apache.org/dist/thrift/0.9.0/

    6. 把下载下来的thrift-0.9.0.exe,放到D:Tools hrift目录下。

    7. 执行: C: est hrift-testserver-csharp>D:Tools hrift hrift-0.9.0 --gen csharp .. utorial-tutorial.thrift

    8. 你应该能看到,多出来一个子目录gen-csharp,把该目录下的所有文件都Include到Visual Studio的工程里面去。

    9. 编译一下,应该是没有编译错误的。

    【开始编写Server端代码】:

    1. 添加CalculatorHandler.cs:

    [csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
     
    1. using System;  
    2.   
    3. namespace thrift_1stApp_server  
    4. {  
    5.     public class CalculatorHandler : Calculator.Iface  
    6.     {  
    7.         public CalculatorHandler()  
    8.         {  
    9.         }  
    10.   
    11.         public void ping()  
    12.         {  
    13.             Console.WriteLine("ping()");  
    14.         }  
    15.   
    16.         public int add(int n1, int n2)  
    17.         {  
    18.             Console.WriteLine("add({0},{1})", n1, n2);  
    19.             return n1 + n2;  
    20.         }  
    21.   
    22.         public int calculate(int logid, Work work)  
    23.         {  
    24.             Console.WriteLine("calculate({0}, [{1},{2},{3}])", logid, work.Op, work.Num1, work.Num2);  
    25.             int val = 0;  
    26.             switch (work.Op)  
    27.             {  
    28.                 case Operation.ADD:  
    29.                     val = work.Num1 + work.Num2;  
    30.                     break;  
    31.   
    32.                 case Operation.SUBTRACT:  
    33.                     val = work.Num1 - work.Num2;  
    34.                     break;  
    35.   
    36.                 case Operation.MULTIPLY:  
    37.                     val = work.Num1 * work.Num2;  
    38.                     break;  
    39.   
    40.                 case Operation.DIVIDE:  
    41.                     if (work.Num2 == 0)  
    42.                     {  
    43.                         InvalidOperation io = new InvalidOperation();  
    44.                         io.What = (int)work.Op;  
    45.                         io.Why = "Cannot divide by 0";  
    46.                         throw io;  
    47.                     }  
    48.                     val = work.Num1 / work.Num2;  
    49.                     break;  
    50.   
    51.                 default:  
    52.                     {  
    53.                         InvalidOperation io = new InvalidOperation();  
    54.                         io.What = (int)work.Op;  
    55.                         io.Why = "Unknown operation";  
    56.                         throw io;  
    57.                     }  
    58.             }  
    59.   
    60.             return val;  
    61.         }  
    62.   
    63.   
    64.         public void zip()  
    65.         {  
    66.             Console.WriteLine("zip()");  
    67.         }  
    68.     }  
    69. }  

    2. 实现Main方法:
    [csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
     
    1. using System;  
    2. using Thrift.Server;  
    3. using Thrift.Transport;  
    4.   
    5. namespace thrift_1stApp_server  
    6. {  
    7.     class Program  
    8.     {  
    9.         public static void Main()  
    10.         {  
    11.             try  
    12.             {  
    13.                 CalculatorHandler handler = new CalculatorHandler();  
    14.                 Calculator.Processor processor = new Calculator.Processor(handler);  
    15.                 TServerTransport serverTransport = new TServerSocket(9090);  
    16.                 TServer server = new TSimpleServer(processor, serverTransport);  
    17.                 Console.WriteLine("Starting the server...");  
    18.                 server.Serve();  
    19.             }  
    20.             catch (Exception x)  
    21.             {  
    22.                 Console.WriteLine(x.StackTrace);  
    23.             }  
    24.             Console.WriteLine("done.");  
    25.         }  
    26.     }  
    27. }  

    3. 编译运行,控制台会显示:Starting the Server...,然后就等待。

    【开始编写Client端代码】:

    1. 执行: C: est hrift-testclient-node>npm init,一路回车直到终结。
    2. 安装thrift库,执行: C: est hrift-testclient-node>npm install thrift --save。
    3. 生成Nodejs接口,执行:c: est hrift-testclient-node>D:Tools hrift hrift-0.9.0.exe --gen js:node .
    . utorial-tutorial.thrift

    4. 应该可以看到,多了一个gen-nodejs的目录

    5. TBD...

  • 相关阅读:
    PostgreSQL的数据类型
    博客园背景页面动态特效
    css ie7中overflow:hidden失效问题及解决方法
    win10的安装、win10启动盘制作
    windows win7 win10 多系统启动菜单 多系统引导设置
    微博加关注按钮
    {转}一位北京差生9年的北京生活
    最全的CSS浏览器兼容问题
    网站开发命名详细规范
    <meta http-equiv = "X-UA-Compatible" cotent = "IE=edge,chrome=1"/>
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/4224002.html
Copyright © 2011-2022 走看看