到了网络时代,大家的电脑都连起来了。以前程序只能调用自己电脑上的进程,能不能调用其他机器上的进程呢?于是就程序员就把IPC扩展到网络上,这就是RPC(远程过程调用)了。现在不仅单机上的进程可以相互通信,多机器中的进程也可以相互通信了.
所用到的演示软件和文本:https://download.csdn.net/download/weixin_43542114/43118953 打开 gen-netstd 文件中生成的C# 类 然后创建 中间通信类
创建服务端 核心代码:
using System;
using Thrift;
using Thrift.Processor;
using Thrift.Server;
using Thrift.Transport;
using Thrift.Transport.Server;
using static Mr.Wang.Contract.UserService;
namespace ConsoleAppThrift_Service
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!" + "thrift——server Start。。。");
TConfiguration config = new TConfiguration();
config.RecursionLimit = 100;
TServerTransport transport = new TServerSocketTransport( 9500,new TConfiguration() { RecursionLimit=10});
ITAsyncProcessor asyncProcessor = new AsyncProcessor(new UserServiceImpl());
TServer server = new TThreadPoolAsyncServer(asyncProcessor, transport);
server.ServeAsync(new System.Threading.CancellationToken());
Console.ReadLine();
server.Stop();
}
}
}
客户端核心代码:
using Mr.Wang.Contract;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using Thrift;
using Thrift.Protocol;
using Thrift.Transport;
using Thrift.Transport.Client;
namespace ConsoleApp_Thrift_Client
{
class Program
{
static void Main(string[] args)
{
using TTransport transport = new TSocketTransport("127.0.0.1",9500, null,10);
using TProtocol protocol = new TBinaryProtocol(transport);
using var client = new UserService.Client(protocol);
client.OpenTransportAsync().Wait();
var u = client.Get(2).Result;
List<User> allUsers = client.GetAll().Result;
allUsers.ForEach(user => { Console.WriteLine($"ID:{user.Id} Name:{user.Name} Age:{user.Age}"); });
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
服务类需要实现,Contract 中间类接口 。
|