设为首页 收藏本站
查看: 2156|回复: 0

[经验分享] 使用thrift进行跨语言调用(php c# java)

[复制链接]

尚未签到

发表于 2015-8-23 11:36:07 | 显示全部楼层 |阅读模式
  1:前言
  实际上本文说的是跨进程的异构语言调用,举个简单的例子就是利用PHP写的代码去调C#或是java写的服务端。其实除了本文提供的办法还有其他办法,例如http+xml(json)等等都能做到。
  本文的意义是介绍thrift,记录我在调试thrift时遇到的问题和相应的解决方案,避免大家走弯路。
  2:大概的流程
  thrift是通过socket+序列化协议来完成跨语言调用的。类似的方案有protocolbuffer(http://code.google.com/p/protobuf/这个性能出众,thrift性能我回头再做测试。
  使用的流程是
  A:定义自己的通信接口,接口可以使用的数据类型有string,int32等,当然你也可以自己定义枚举和结构体
  B:使用thrift.exe生成相应的代码
  C:调用
  3:c#,PHP,Java在调试thrift时遇到的问题
  首先我们去下载thrift,地址是http://thrift.apache.org/
  解压缩后会看到
DSC0000.png
  lib下就是执行的各种语言的代码。
  A:调试PHP注意事项
  一定要注册各个php代码也就是这句。



require_once __DIR__.'/lib/Thrift/ClassLoader/ThriftClassLoader.php';
$loader = new ThriftClassLoader();
$loader->registerNamespace('Thrift', __DIR__ . '/lib');
$loader->register();

  B:调试java注意事项
  下载没有提供的包
DSC0001.png
  地址是:
  http://commons.apache.org/proper/commons-lang/download_lang.cgi
  http://hc.apache.org/downloads.cgi
  http://www.slf4j.org/
  3:demo
  定义接口(方法名不能一样)


DSC0002.gif DSC0003.gif View Code


enum ParameterValueType
{
AnsiString = 1,
Byte,
Boolean,
Currency = 4,
Date,
DateTime,
Decimal,
Double,
Guid,
Int16,
Int32,
Int64,
String,
Time,
Xml
}
enum ParameterValueDirection
{
Input,
Output,
InputOutput,
ReturnValue
}
enum AdoCommandType
{
Text,
StoredProcedure
}
struct DBParameter {
1: string DbParameterName,
2: ParameterValueType DbType,
3: string DbParameterValue,
4: ParameterValueDirection DbDirection,
5: i32 Size
}
struct ResultMessage {
1: i32 IsSuccess,
2: string ErrorMessage,
3: string ReturnValue,
4: list<DBParameter> ReturnParameter,
5: list<string> DataSetColumnName,
6: list<list<string>> DataSetRowValue
}
service DataAccessComponent {
ResultMessage ExecuteDataset(1:string ConnectionConfigKey,2:AdoCommandType commandType,3:string commandText,4:list<DBParameter> DBParameter),
ResultMessage ExecuteNonQuery(1:string ConnectionConfigKey,2:AdoCommandType commandType,3:string commandText,4:list<DBParameter> DBParameter),
ResultMessage ExecuteScalar(1:string ConnectionConfigKey,2:AdoCommandType commandType,3:string commandText,4:list<DBParameter> DBParameter),
}
  生成代码


View Code


thrift -gen java ado.thrift
thrift -gen php ado.thrift
thrift -gen csharp ado.thrift
  根据语言把生成的语言放到相应的文件夹下。
  下面简单列一下调用代码



1:PHP
//设置IP,端口建立连接
$socket = new TSocket('localhost', '9090');
$transport = new TBufferedTransport($socket);
//选定通信协议
$protocol = new TBinaryProtocol($transport);
$client = new DataAccessComponentClient($protocol); //生成的类
2:c#
server
int port = 9090;
DataAccess da = new DataAccess();
// Processor
DataAccessComponent.Processor pc = new DataAccessComponent.Processor(da);
// Transport
TServerSocket tServerSocket =
new TServerSocket(port);
// Protocol factory
TProtocolFactory tProtocolFactory =
new TBinaryProtocol.Factory();
TServer serverEngine;
// Simple Server
serverEngine = new TSimpleServer(pc, tServerSocket);
// ThreadPool Server
//serverEngine = new TThreadPoolServer(pc,tServerSocket);
// Run it
serverEngine.Serve();
client
TTransport transport = new TSocket("localhost", 9090);
TProtocol protocol = new TBinaryProtocol(transport);
ThriftTest.Client client = new ThriftTest.Client(protocol);
transport.Open();
int val = client.test("1213");
client.work();
transport.Close();
Console.WriteLine(val);
Console.ReadLine();
3:java
server
try {
//private static final Logger LOGGER = LoggerFactory.getLogger(Processor.class.getName());
//System.setProperty("log4j.configuration", "log4j.properties");
int port = 9090;
// Processor
ThriftServer testHandler =
new ThriftServer();
ThriftTest.Processor testProcessor =
new ThriftTest.Processor(testHandler);
// Transport
TServerSocket tServerSocket =
new TServerSocket(port);
// Protocol factory
TProtocolFactory tProtocolFactory =
new TBinaryProtocol.Factory();
TServer serverEngine;
// Simple Server
//serverEngine = new TSimpleServer(new Args(tServerSocket).processor(testProcessor));
// ThreadPool Server
serverEngine = new TThreadPoolServer(new TThreadPoolServer.Args(tServerSocket).processor(testProcessor).protocolFactory(tProtocolFactory));
//Set server event handler
serverEngine.setServerEventHandler(new TestServerEventHandler());
// Run it
System.out.println("Starting the server on port " + port + "...");
serverEngine.serve();
} catch (Exception x) {
x.printStackTrace();
}
System.out.println("done.");

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-102931-1-1.html 上篇帖子: [开心学php100天]第四天:完爆php魔力函数 下篇帖子: PHP常用库函数介绍+常见疑难问题解答
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表