最近幫老師做專案,需要用C#作為客戶端做傳輸大量資料給Java服務端,之前的代碼是用原生Socket寫的,Java端這邊非常的冗余復雜,難以維護,因此想到使用rpc框架來實作,方便后續的擴展和維護,
查閱資料發現thrift框架提供了完整的序列化和傳輸,(考慮效率也可使用protobuf+grpc,檔案更加豐富)
關于rpc的框架對比,見:https://tech.meituan.com/2015/02/26/serialization-vs-deserialization.html
https://blog.csdn.net/z69183787/article/details/52605653
thrift這個框架的檔案太少了,想真的把框架落地使用太難了5555......網上資料參差不齊,但是整了很久也算搞出來了,因此記錄一下整個程序和走過的坑,
我是在本地測驗的C#和Java都在windows端,首先安裝thrift,
各個版本的鏈接如下:http://archive.apache.org/dist/thrift/
dll
我這里下的是 thrift-0.10.0.tar.gz 版本,環境是VS2019以及.NET 4.5,打開工程:thrift-0.10.0\lib\csharp\src\Thrift.sln,重新生成,編譯,即可生成Thrift.dll(位置在thrift-0.10.0\lib\csharp\src\bin\Debug\Thrift.dll),后面撰寫代碼的時候需要參考這個dll,
dat.drift
新建dat.drift檔案,使用IDL對介面進行描述,用于生成.cs和.java代碼,這一步需要用到Linux系統環境(沒有的話建議使用虛擬機裝Linux環境),因為我在Win10環境下按照網上的方法是無法生成對應的代碼檔案的,
IDL描述語言可以參考:https://diwakergupta.github.io/thrift-missing-guide/#_types
https://gitbox.apache.org/repos/asf?p=thrift.git;a=blob;hb=HEAD;f=tutorial/tutorial.thrift
示例如下:
定義了一個結構體和一個service方法
struct Dat{
1: bool isColDat
2: list<byte> data
}
service SendDatService{
oneway void send(1:Dat dat)
}
這里的oneway表示方法呼叫不需要回傳值,這樣的話提高了呼叫的效率,但是可靠性不能保證,
在linux環境下安裝thrift,參考:https://blog.csdn.net/huangbaokang/article/details/80065606
安裝成功后,使用thrift生成.cs檔案,在Linux命令列中執行
thrift -r --gen csharp dat.thrift

當前目錄下會生成gen-csharp目錄,里面是.cs檔案,在編碼時需要拷貝到C#工程目錄下,
在Linux命令列中執行
thrift -r --gen java dat.thrift

當前目錄下會生成gen-java目錄,里面是.java檔案,在編碼時需要拷貝到Java工程目錄下,
客戶端代碼
C#新建專案,引入前面生成Thrift.dll,復制前面生成的.cs檔案到專案中,主程式示例代碼如下,
using System;
using System.Collections.Generic;
using Thrift.Protocol;
using Thrift.Transport;
namespace IBAClientLT
{
static class Program
{
public const string SERVERIP = "localhost";
public static int SERVERPORT = 9000;
public static int TIMEOUT = 5000;
[STAThread]
static void Main()
{
TTransport transport = null;
try
{
//transport = new TSocket(SERVERIP, SERVERPORT, TIMEOUT);
transport = new TFramedTransport(new TSocket(SERVERIP, SERVERPORT, TIMEOUT));
//協議要和服務端一致
TProtocol protocol = new TCompactProtocol(transport);
SendDatService.Client client = new SendDatService.Client(protocol);
transport.Open();
Dat d = new Dat();
d.IsColDat = false;
List<sbyte> bt = new List<sbyte>();
bt.Add(1);
bt.Add(2);
bt.Add(3);
d.Data = bt;
client.send(d);//遠程呼叫
client.Dispose();
Console.ReadKey();//保持命令列視窗不關閉
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
}
finally
{
if (null != transport)
{
//close
transport.Close();
}
}
}
}
}
服務端代碼
Java新建Maven專案,pom.xml檔案添加依賴
<!-- https://mvnrepository.com/artifact/org.apache.thrift/libthrift -->
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.10.0</version>
<!--<type>pom</type>-->
</dependency>
繼承我們定義的service介面,并且實作介面中的方法,這里我們列印出來內容,
package thrift;
import org.apache.thrift.TException;
public class SendDatServiceImpl implements SendDatService.Iface {
@Override
public void send(Dat dat) throws TException {
System.out.println("iscolDat: "+dat.isColDat);
System.out.println("data: "+dat.data);
}
}
服務端主程式,
package thrift;
import org.apache.thrift.TNonblockingMultiFetchClient;
import org.apache.thrift.TProcessorFactory;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.server.THsHaServer;
import org.apache.thrift.server.TServer;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TNonblockingServerSocket;
import org.apache.thrift.transport.TTransportException;
public class ThriftServer {
public static void main(String[] args) throws TTransportException {
TNonblockingServerSocket socket =new TNonblockingServerSocket(9000);
THsHaServer.Args arg =new THsHaServer.Args(socket).minWorkerThreads(1).maxWorkerThreads(4);
SendDatService.Processor<SendDatServiceImpl> processor=
new SendDatService.Processor<SendDatServiceImpl>(new SendDatServiceImpl());
arg.protocolFactory(new TCompactProtocol.Factory());
arg.transportFactory(new TFramedTransport.Factory());
arg.processorFactory(new TProcessorFactory(processor));
TServer server =new THsHaServer(arg);
System.out.println("server start");
server.serve();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/199483.html
標籤:java
上一篇:Grpc&&protocol buffer結合提供grpc服務
下一篇:羞羞電量插件v1.0安卓版
