Thrift学习(1)C#调用Java开发步骤详解
来源:互联网 发布:西部域名怎么解析 编辑:程序博客网 时间:2024/05/01 16:27
转载请注明出处:jiq•钦's technical Blog
Apache Thrift 是 Facebook 实现的一种高效的、支持多种编程语言的远程服务调用的框架。
1 下载Thrift
下载地址:http://thrift.apache.org/download
thrift-0.9.3.exe 用于编译Thrift中间文件生成对应语言代码的工具
thrift-0.9.3.tar.gz 包含Thrift各个语言的源码库,以及一些测试程序代码等
2 编译生成.NET库(DLL)和Java库(Jar)
解压thrift-0.9.3.tar.gz文件。
(1) 生成.NET库
打开工程:E:\Thrift\thrift-0.9.3\lib\csharp\src\Thrift.sln 编译,即可生成Thrift.dll
我的环境是VS2010以及.NET 4.0
(2) 生成Java库
Java库是通过Ant构建的,需要安装Ant,安装步骤这里就不赘述了。
打开命令行CD到java库代码所在的路径:E:\Thrift\thrift-0.9.3\lib\java(包含build.xml)
然后直接执行ant命令即可发现build目录下生成对应的jar文件。
3 编写thrift中间文件
namespace java testnamespace csharp testservice Hello { string helloString(1:string word)}
4 生成Java和C#各自的接口文件
Thrift-0.9.3.exe –gen java test.thrift
Thrift-0.9.3.exe –gen csharp test.thrift
可以看到在当前目录下会出现生成的对应代码。
5 编写Java服务端代码
新建普通Java项目,将之前ant编译生成的相关的jar文件(libthrift-0.9.3.jar以及E:\Thrift\thrift-0.9.3\lib\java\build\lib目录下所有jar文件)add tobuild path;然后还要将生成的Hello.java也加入到工程中,注意包名。
(1)首先编写接口实现类:
package test;import org.apache.thrift.TException;import test.Hello.Iface; public classHelloImpl implementsIface{ privatestaticintcount= 0; @Override publicString helloString(String word)throwsTException { count += 1; System.out.println("get " + word + " " +count); return "hello " + word + " " + count; } }
(2)编写寄宿代码,启动并监听在指定端口:
package test;import org.apache.thrift.protocol.TBinaryProtocol; import org.apache.thrift.protocol.TBinaryProtocol.Factory; import org.apache.thrift.server.TServer; import org.apache.thrift.server.TThreadPoolServer; import org.apache.thrift.server.TThreadPoolServer.Args; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TTransportException; import test.Hello.Processor; public classServer { @SuppressWarnings({"rawtypes", "unchecked" }) public void startServer() { try { System.out.println("thrift server host on port 8899"); TServerSocket serverTransport = new TServerSocket(8899); Hello.Processorprocess = newProcessor(newHelloImpl()); Factory portFactory = newTBinaryProtocol.Factory(true, true); Args args = newArgs(serverTransport); args.processor(process); args.protocolFactory(portFactory); TServer server = newTThreadPoolServer(args); server.serve(); } catch (TTransportException e) { e.printStackTrace(); } } publicstaticvoidmain(String[] args) { System.out.println("thrift server init"); Server server = new Server(); System.out.println("thrift server start"); server.startServer(); System.out.println("thrift server end"); } }
6 编写C#客户端代码
新建普通控制台项目,引入Thrift.dll;然后还要将生成的Hello.cs也加入到工程中。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using Thrift.Transport;using Thrift.Protocol;namespace ThriftTest{ class ClientTest { static void Main(string[]args) { TTransporttransport = new TSocket("localhost", 8899); TProtocolprotocol = new TBinaryProtocol(transport); test.Hello.Client client = newtest.Hello.Client(protocol); transport.Open(); Console.WriteLine("Client calls client.helloString()....."); Console.WriteLine(client.helloString("jiyiqin")); client.Dispose(); } }}
7 运行
运行java服务端Server.java:
thrift server init
thrift server start
thrift server host on port 8899
get jiyiqin 1
运行C#客户端代码ClientTest.cs
Client calls client.helloString().....
Hello jiyiqin 1
请按任意键继续…
可以看到Thrift和ICE等跨语言RPC框架开发步骤非常相似,几乎相同,生成的文件也都差不多,但是和基于Servlet的Hessian这种跨语言RPC框架差别较大。
- Thrift学习(1)C#调用Java开发步骤详解
- Thrift实现C#调用Java开发步骤详解
- Apache Thrift - java开发详解
- Apache Thrift - java开发详解
- Apache Thrift - java开发详解
- Apache Thrift - java开发详解
- Thrift学习(1)-Thrift简介
- Thrift的一些总结(Java开发)
- java thrift开发姿势
- C#通过thrift连接hbase操作步骤
- Thrift学习(OC的调用)
- NDK开发学习笔记(1):JNI开发步骤及遇到的问题详解
- Struts2学习总结(1)--基本原理,开发步骤,配置详解
- Hibernate4学习总结(1)--基本原理,开发步骤,配置详解
- Struts2学习总结(1)--基本原理,开发步骤,配置详解
- 详解VB调用C#类库的方法步骤(转载)
- Java调用dll步骤1
- 详解VB调用C#类库的方法步骤
- 卷积的意义
- 合并表记录
- 系统中文件映射常用的函数及映射方法
- mybatis源码之MapperMethod
- batman相关配置修改
- Thrift学习(1)C#调用Java开发步骤详解
- JSP入门
- 负载均衡之起始篇
- Java 数组的声明方式
- Android:三星指纹解锁SDK
- batman-adv协议相关概念
- 提取不重复的整数
- batman_adv主要数据包格式与作用
- iOS-UIImageView对象