C# RPC远程方法调用框架thrift

2024-06-10 10:32

本文主要是介绍C# RPC远程方法调用框架thrift,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先项目创建windows控制台程序,项目里面引用
这里写图片描述

写这篇文章时用的是thrift-csharp版本0.10.0
项目结构
这里写图片描述

服务端代码

using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Thrift.Server;namespace ThriftServer
{public class TestServer : ChatService.Iface{public List<string> GetList(string function2_arg1, int function2_arg2, List<string> function2_arg3){List<string> list = new List<string>();Parallel.ForEach(function2_arg3, m =>{list.Add($"{ function2_arg1},年龄 { function2_arg2},正在:{m}");});return list;}public string Say(string thing){return thing + "测试数据";}}
}using Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using Thrift;
using Thrift.Protocol;
using Thrift.Server;
using Thrift.Transport;namespace ThriftServer
{class Program{static void Main(string[] args){//TcpListener tcpListener = new TcpListener(System.Net.IPAddress.Parse("127.0.0.1"),7988);//TServerTransport transport =new TServerSocket(tcpListener);int port = 15789;TServerTransport transport = new TServerSocket(port);TestServer serverIfac = new TestServer();TProcessor processor = new ChatService.Processor(serverIfac);TServer server = new TThreadPoolServer(processor, transport);Task.Run(() =>{                try{Console.WriteLine("服务启动,端口" + port);server.Serve();                   }catch (Exception ex){Console.WriteLine("启动服务异常:" + ex.Message);}});Console.ReadKey();}}
}

客户端代码

using Thrift.Protocol;
using Thrift.Transport;using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace Client
{class ClientDo{public static string GetServerData(string words, Common.ChatService.Client client){//var transport = new TSocket("localhost", 15789);//var protocol = new TBinaryProtocol(transport);//var client = new  Common.ChatService.Client(protocol);//transport.Open();string allBooks = client.Say(words); // Actual Thrift call           return allBooks;}}
}using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Thrift.Protocol;
using Thrift.Transport;namespace Client
{class Program{static void Main(string[] args){var transport = new TSocket("localhost", 15789);var protocol = new TBinaryProtocol(transport);var client = new Common.ChatService.Client(protocol);transport.Open();测试一//Task.Run(() =>//{//    while (true)//    {//        Random m = new Random();//        int ronum = m.Next(10000);//        string words = ronum + "说" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");//        string backMsg = ClientDo.GetServerData(words, client);//        Console.WriteLine(backMsg);//        Task.Delay(1000).Wait();//    }//});//测试二List<string> data = new List<string>();data.Add("吃饭");data.Add("看书");data.Add("跑路");List<string> list=  client.GetList("欧阳修", 77, data);Parallel.ForEach(list,m=> {Console.WriteLine(m);});//transport.Close();Console.ReadKey();}}
}

然后是ChatService类是生成的,需要用到thrift-0.11.0.exe
怎么生成?
首先打开windows的控制台,先进入到thrift-0.11.0.exe所在目录,然后执行命令,这个ChatService.cs是通过命令生成的
命令生成cs文件:
thrift-0.11.0.exe -gen csharp demo-interface.thrift

生成的thrift模板文件里面,需要定义自己所需的方法,参考代码,比如文件:demo-interface.thrift

namespace * Commonservice ChatService
{string Say(1: string thing),list<string> GetList(1: string function2_arg1,2: i32 function2_arg2,3: list<string> function2_arg3),
}

执行命令之后会在当前目录生成一个文件夹gen-csharp,里面包含生成的类文件ChatService.cs,将此文件粘贴到项目Common里面即可
上图:
这里写图片描述

测试效果
这里写图片描述

这里有模板文件参考代码,文件名break1.thrift

/** Licensed to the Apache Software Foundation (ASF) under one* or more contributor license agreements. See the NOTICE file* distributed with this work for additional information* regarding copyright ownership. The ASF licenses this file* to you under the Apache License, Version 2.0 (the* "License"); you may not use this file except in compliance* with the License. You may obtain a copy of the License at**   http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing,* software distributed under the License is distributed on an* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY* KIND, either express or implied. See the License for the* specific language governing permissions and limitations* under the License.*///Thrift Method removed from service base.namespace cpp test//constants
const i32 const1 = 123;
const double const2 = 23.3;
const map<string,string> const3 = {"hello":"world", "thrift":"audit"};//Exception
exception test_exception1 {1: i32 code;2: string json;
}
exception test_exception2 {1: i32 code;2: string json;
}//Enumsenum test_enum1 {enum1_value0 = 0,enum1_value1 = 1,enum1_value2 = 2,enum1_value5 = 5,enum1_value7 = 7,enum1_value8 = 8
}enum test_enum2 {enum2_value0 = 0,enum2_value1 = 1,enum2_value2 = 2,enum2_value3 = 3
}enum test_enum3 {enum3_value1 = 0,enum3_value2 = 1
}struct test_struct1 {1: i16 struct1_member1,2: i32 struct1_member2,3: i64 struct1_member3,4: double struct1_member4 = 2.5,5: string struct1_member5 = "Audit test",6: bool struct1_member6,7: byte struct1_member7,8: binary struct1_member8,9: test_enum1 struct1_member9
}struct test_struct2 {1: list<i16> struct2_member1,2: list<i32> struct2_member2,3: list<i64> struct2_member3= [23, 32],4: list<double> struct2_member4,5: list<string> struct2_member5,6: list<bool> struct2_member6,7: list<byte> struct2_member7,8: list<binary> struct2_member8,9: list<test_enum1> struct2_member9
}struct test_struct3 {1: map<i16, i32> struct3_member1 = {1:2, 3:4},2: map<i64, double> struct3_member2 = {10:1.1, 20:2.1},3: map<string, bool> struct3_member3,4: map<byte, test_enum1> struct3_member4,5: map<test_enum2, test_enum3 > struct3_member5,7: map<double, string> struct3_member7
}struct test_struct4 {1: i32 struct4_member1,2: optional i32 struct4_member2
}struct test_struct5{1: double struct5_member1,2: string struct5_member2 = "Thrift Audit Test"
}
struct test_struct6 {1: i32 struct6_member1,2: required i32 struct6_member2
}service base {oneway void base_oneway(1: i32 arg1),void base_function1(1: i16 function1_arg1,2: i32 function1_arg2,3: i64 function1_arg3,4: double function1_arg4,5: string function1_arg5,6: bool function1_arg6,7: test_enum1 function1_arg7,8: test_struct1 function1_arg8),void base_function2(1: list<i16> function2_arg1,2: list<i32> function2_arg2,3: list<i64> function2_arg3,4: list<double> function2_arg4,5: list<string> function2_arg5,6: list<bool> function2_arg6,7: list<byte> function2_arg7,8: list<test_enum1> function2_arg8,9: list<test_struct1> function2_arg9) throws (1:test_exception2 e),}service derived1 extends base {test_enum1 derived1_function1(1: i64 function1_arg1,2: double function1_arg2,3: test_enum1 function1_arg3) throws (1:test_exception2 e),i64 derived1_function2(1: list<i64> function2_arg1,2: list<double> function2_arg2,3: list<string> function2_arg3,4: list<byte> function2_arg4,5: list<test_enum1> function2_arg5) throws (1:test_exception2 e),double derived1_function3(1: string function3_arg1,2: bool function3_arg2) throws (1:test_exception2 e),string derived1_function4(1: string function4_arg1,2: bool function4_arg2) throws (1:test_exception2 e),bool derived1_function5(1: map<i64, double> function5_arg1,2: map<string, bool> function5_arg2,3: map<test_enum1, test_enum2> function5_arg3) throws (1:test_exception2 e),test_struct1 derived1_function6(1: double function6_arg1) throws (1:test_exception2 e),
}service derived2 extends base {list<i32> derived2_function1(1: i32 function1_arg1) throws (1:test_exception2 e),list<test_enum1> derived2_function2(1:i64 function2_arg2) throws (1:test_exception2 e),list<test_struct1> derived2_function3(1:double function3_arg1) throws(1:test_exception2 e),map<double, string> derived2_function4(1:string function4_arg1) throws(1:test_exception2 e),map<test_enum1, test_enum2> derived2_function5(1:bool function5_arg1) throws(1:test_exception2 e),map<test_struct1, test_struct2> derived2_function6(1:bool function6_arg1) throws(1:test_exception2 e),}

demo下载地址
https://download.csdn.net/download/u011511086/10355584

这篇关于C# RPC远程方法调用框架thrift的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1047911

相关文章

pycharm远程连接服务器运行pytorch的过程详解

《pycharm远程连接服务器运行pytorch的过程详解》:本文主要介绍在Linux环境下使用Anaconda管理不同版本的Python环境,并通过PyCharm远程连接服务器来运行PyTorc... 目录linux部署pytorch背景介绍Anaconda安装Linux安装pytorch虚拟环境安装cu

解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题

《解读为什么@Autowired在属性上被警告,在setter方法上不被警告问题》在Spring开发中,@Autowired注解常用于实现依赖注入,它可以应用于类的属性、构造器或setter方法上,然... 目录1. 为什么 @Autowired 在属性上被警告?1.1 隐式依赖注入1.2 IDE 的警告:

SpringBoot快速接入OpenAI大模型的方法(JDK8)

《SpringBoot快速接入OpenAI大模型的方法(JDK8)》本文介绍了如何使用AI4J快速接入OpenAI大模型,并展示了如何实现流式与非流式的输出,以及对函数调用的使用,AI4J支持JDK8... 目录使用AI4J快速接入OpenAI大模型介绍AI4J-github快速使用创建SpringBoot

Android开发中gradle下载缓慢的问题级解决方法

《Android开发中gradle下载缓慢的问题级解决方法》本文介绍了解决Android开发中Gradle下载缓慢问题的几种方法,本文给大家介绍的非常详细,感兴趣的朋友跟随小编一起看看吧... 目录一、网络环境优化二、Gradle版本与配置优化三、其他优化措施针对android开发中Gradle下载缓慢的问

python 3.8 的anaconda下载方法

《python3.8的anaconda下载方法》本文详细介绍了如何下载和安装带有Python3.8的Anaconda发行版,包括Anaconda简介、下载步骤、安装指南以及验证安装结果,此外,还介... 目录python3.8 版本的 Anaconda 下载与安装指南一、Anaconda 简介二、下载 An

C# string转unicode字符的实现

《C#string转unicode字符的实现》本文主要介绍了C#string转unicode字符的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1. 获取字符串中每个字符的 Unicode 值示例代码:输出:2. 将 Unicode 值格式化

Java中将异步调用转为同步的五种实现方法

《Java中将异步调用转为同步的五种实现方法》本文介绍了将异步调用转为同步阻塞模式的五种方法:wait/notify、ReentrantLock+Condition、Future、CountDownL... 目录异步与同步的核心区别方法一:使用wait/notify + synchronized代码示例关键

MobaXterm远程登录工具功能与应用小结

《MobaXterm远程登录工具功能与应用小结》MobaXterm是一款功能强大的远程终端软件,主要支持SSH登录,拥有多种远程协议,实现跨平台访问,它包括多会话管理、本地命令行执行、图形化界面集成和... 目录1. 远程终端软件概述1.1 远程终端软件的定义与用途1.2 远程终端软件的关键特性2. 支持的

Python使用Pandas对比两列数据取最大值的五种方法

《Python使用Pandas对比两列数据取最大值的五种方法》本文主要介绍使用Pandas对比两列数据取最大值的五种方法,包括使用max方法、apply方法结合lambda函数、函数、clip方法、w... 目录引言一、使用max方法二、使用apply方法结合lambda函数三、使用np.maximum函数

Qt 中集成mqtt协议的使用方法

《Qt中集成mqtt协议的使用方法》文章介绍了如何在工程中引入qmqtt库,并通过声明一个单例类来暴露订阅到的主题数据,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一,引入qmqtt 库二,使用一,引入qmqtt 库我是将整个头文件/源文件都添加到了工程中进行编译,这样 跨平台