Thrift实现C#调用Java开发步骤详解
概述
详细
Apache Thrift 是 Facebook 实现的一种高效的、支持多种语言的远程服务调用的框架。
类似的跨语言RPC框架还有ICE、Hessian、Protocol Buffer、Avro等。
一、下载thrift
参考博客 http://blog.csdn.net/gaowenhui2008/article/details/70694560
CentOS安装Thrift
官方文档地址:
http://thrift.apache.org/docs/install/centos
Apache Thrift 官方JAVA教程
官方教程的代码地址:
https://git1-us-west.apache.org/repos/asf?p=thrift.git;a=tree;f=tutorial;hb=HEAD
下载地址:http://thrift.apache.org/download
thrift-0.10.0.exe 用于编译Thrift中间文件生成对应语言代码的工具 (ps:我是在Linux环境里边安装的 Thrift)
thrift-0.10.0.tar.gz 包含Thrift各个语言的源码库,以及一些测试程序代码等
二、编译生产 .NET库(DLL)和JAVA库(jar)
解压thrift-0.10.0.tar.gz文件。
(1) 生成.NET库
打开工程:E:\thrift\thrift-0.10.0\lib\csharp\src\Thrift.sln 编译,即可生成Thrift.dll
我的环境是VS2017以及.NET 4.5
(2) 生成Java库
Java库是通过Ant构建的,需要安装Ant,安装步骤这里就不赘述了。
打开命令行CD到java库代码所在的路径:E:\thrift\thrift-0.10.0\lib\java(包含build.xml)
然后直接执行ant命令即可发现build目录下生成对应的jar文件
三、编写thrift中间文件hello.thrift
namespace java com.cnblogs.yjmyzz.demo.service.api.thrift
service ThriftHelloService{
string ping()
}
四、生成java和c#各自的接口文件
将接口文件放到thrift目录:
在thrift目录运行 thrift --gen java hello.thrift
可以看到在当前目录下会出现生成的对应代码。
同样生成C#的代码
thrift --gen csharp hello.thrift
五、编写java服务端代码
新建Maven项目,然后还要将生成的ThriftHelloService.java也加入到工程中,注意包名。
(1)编写接口实现类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | package com.cnblogs.yjmyzz.demo.service.api.thrift; import org.apache.thrift.TException; /** *Created by on 2017/8/4 */ public class ThriftHelloServiceImpl implements ThriftHelloService.Iface { @Override public String ping() throws TException { return "service call done: ThriftHelloService.ping()123456" ; } } |
(2)编写寄宿代码,启动并监听在指定端口:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | package com.cnblogs.yjmyzz.demo.service.api.thrift; /** * Created by on 2017/8/4 */ import org.apache.thrift.TProcessor; import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TThreadPoolServer; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TTransportException; import com.cnblogs.yjmyzz.demo.service.api.thrift.ThriftHelloService; import com.cnblogs.yjmyzz.demo.service.api.thrift.ThriftHelloServiceImpl; public class HelloServiceServer { /** * 启动 Thrift 服务器 * @param args */ public static void main(String[] args) { try { // 设置服务端口为 7911 TServerSocket serverTransport = new TServerSocket( 7911 ); // 设置协议工厂为 TBinaryProtocol.Factory TBinaryProtocol.Factory proFactory = new TBinaryProtocol.Factory(); // 关联处理器与 Hello 服务的实现 TProcessor processor = new ThriftHelloService.Processor( new ThriftHelloServiceImpl()); TThreadPoolServer.Args args1 = new TThreadPoolServer.Args(serverTransport); args1.processor(processor); args1.protocolFactory(proFactory); TServer server = new TThreadPoolServer(args1); System.out.println( "Start server on port 7911..." ); server.serve(); } catch (TTransportException e) { e.printStackTrace(); } } } |
六、编写C#客户端代码
新建普通控制台项目,引入Thrift.dll;然后还要将生成的ThriftHelloService.cs也加入到工程中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Thrift.Transport; using Thrift.Protocol; namespace ConsoleApp1.Properties { class ClientTest { static void Main( string [] args) { TTransport transport = new TSocket( "localhost" , 7911); transport.Open(); TProtocol protocol= new TBinaryProtocol(transport); ThriftHelloService.Client client = new ThriftHelloService.Client(protocol); Console.WriteLine( "ThriftHelloService client.ping()....." ); Console.WriteLine(client.ping()); System.Console.WriteLine( "call done : Hello World! --》" + client.ping()); client.Dispose(); transport.Close(); } } } |
七、运行
运行java服务端,HelloServiceServer.java:
运行C#客户端代码ClientTest.cs Ctrl+F5
请按任意键继续…
调用成功!
可以看到Thrift和ICE等跨语言RPC框架开发步骤非常相似,几乎相同,生成的文件也都差不多,但是和基于Servlet的Hessian这种跨语言RPC框架差别较大。
注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· 使用C#创建一个MCP客户端
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· Windows编程----内核对象竟然如此简单?