chkconfig --list|grep thrift
或者通戚禅过进程查看
ps -ef|grep thrift
最近公司有个需求,需要在后端应用服务器上实时获取STORM集群的运行信息和topology相关的提交和控制,经过几天对STORM UI和CMD源码的分析,得出可以通过其thrift接口调用实现这些功能。先下载一个thrift库进行编码和安卖悉装。关于thrift可以参见这个地方。安装完成后,从STORM源码中将storm.thrift拷贝到thrift目录下。输入:hrift -gen cpp storm.thrift
会得到一个gen-cpp目录,里面就是thrift先关脚本的C++实现。我们先看storm.thrift文件接口:
view sourceprint?
01.service Nimbus
02.{
03.//TOPOLOGY上传接口
04.void submitTopology(1: string name, 2: string uploadedJarLocation, 3: string jsonConf,4: StormTopology topology)
05.void submitTopologyWithOpts(1: string name, 2: string uploadedJarLocation, 3: string jsonConf, 4: StormTopology topology, 5: SubmitOptions options)
06.void killTopology(1: string name)
07.void killTopologyWithOpts(1: string name, 2: KillOptions options) throws (1: NotAliveException e)
08.void activate(1: string name)
09.void deactivate(1: string name)
10.void rebalance(1: string name, 2: RebalanceOptions options)
11.
12.//TOPOLOGY JAR包上传接口
13.string beginFileUpload()
14.void uploadChunk(1: string location, 2: binary chunk)
15.void finishFileUpload(1: string location)
16.string beginFileDownload(1: string file)
17.binary downloadChunk(1: string id)
18.
19.//获取NIMBUS的配置信息
20.string getNimbusConf()
21.//获取STORM集群运行信息
22.ClusterSummary getClusterInfo()
23.//获取TOPOLOGY的运行状态信息
24.TopologyInfo getTopologyInfo(1: string id)
25.//获取TOPOLOGY对象信息
26.string getTopologyConf(1: string id)
27.StormTopology getTopology(1: string id)
28.StormTopology getUserTopology(1: string id)
29.}
中宴乎生成C++文件后,我们就可以对其接口进行调用,由于thrift c++框祥团架是使用boost库实现的,必须安装boost库依赖。实现的代码如下:
view sourceprint?
01.#define HAVE_NETDB_H //使用网络模块的宏必须打开
02.#include "Nimbus.h"
03.#include "storm_types.h"
04.
05.#include <string>
06.#include <iostream>
07.#include <set>
08.
09.#include <transport/TSocket.h>
10.#include <transport/TBufferTransports.h>
11.#include <protocol/TBinaryProtocol.h>
12.int test_storm_thrift()
13.{
14.boost::shared_ptr<TSocket>tsocket(new TSocket("storm-nimbus-server", 6627))
15.boost::shared_ptr<TTransport>ttransport(new TFramedTransport(tsocket, 1024 * 512))//此处必须使用TFramedTransport
16.boost::shared_ptr<TProtocol>tprotocol(new TBinaryProtocol(ttransport))
17.try{
18.//创建一个nimbus客户端对象
19.NimbusClient client(tprotocol)
20.//打开通道
21.ttransport->open()
22.
23.ClusterSummary summ
24.std::string conf
25.//对STORM的RPC调用,直接获取信息,同步进行的。
26.client.getNimbusConf(conf)
27.client.getClusterInfo(summ)
28.//关闭通道
29.ttransport->close()
30.}catch(TException &tx){
31.printf("InvalidOperation: %s
32.", tx.what())
33.}
34.}
以上代码就可以直接获取nimbus的配置和集群信息,其他接口以此类推。值得注意的是storm.thrift to C++生成的storm_types.h文件里其中operator <函数都未实现,所以必须手动进行添加实现,否则编译会有问题。
不仅仅C++可以实现STORM的控制,PHP和其他的语言也可以实现,只要thrift支持就OK。有兴趣可以实现一下试试看。
转载
Apache Thrift 跨语言服务框架,本质RPC我发service需要放候,遇跨语言调用问题,JAVA语言发UserService用提供获取用户信息服务,服务消费端PHP/Python/C++等,我能所语言都适配相应调用式,候我奈使用Http作访问协议服务消费端能使用HTTP,且更加倾向于 *** 作本API式 使用服务,我需要Thrift提供支持.本文UserService例,描述使用thrift式,及其原理..
. service.thrift
struct User{
1:i64 id,
2:string name,
3:i64 timestamp,
4:bool vip
}
service UserService{
User getById(1:i64 id)
}
二. API文件
首先载安装thrift客户端,比码则windows平台,载thrift.exe,处需要提醒,同thrift客户端迟首棚版本API能兼容.本例使用thrift-0.9.0.exe通"--gen"指定API所适配语言.本实例java客户端API.
//windows平台,API文件输service目录(目录需要存)
>thrift.exe --gen java -o service service.thrift
三. UserService实现类
public class UserServiceImpl implements UserService.Iface {
@Override
public User getById(long id){
System.out.println("invoke...id:" + id)
return new User()//for test
}
}
四.原理简析
1. User.java : thriftAPI能力非限,比struct能使用简单数据类型(支持Date,Collection等),我能User看,类实现"Serializable"接口"TBase"接口.
其Serializable接口表明类实例需要序列化网络传输,干扰JAVA本身序列化反序列化机制,重写readObjectwriteObject.thrift本身并没帮助.
TBase接口thrift序列化反序列化使用,两核:readwrite.述thrift文件,struct定义每属性都序号,比:1:id,thrift序列化,根据序号顺序依属性"名称 + 值"写入inputStream,反序列化.(具体参见readwrite实现).
thrift序列化反序列化实例数据,根据"属性序号"进行,保证数据inputstreamoutputstream顺序严格.点要求API发者,更改thrift文件struct定义,需要重新客户端API,否则服务继续使用(能报错,能数据错误).thrift序列化/反序列化程JAVA自带序列化机制同,携带额外class结构,外thrift种序列化机制更加适合网络传输,且性能更加高效.
2. UserService.Client: UserService,Client静态类,类典型代理类,类芹手已经实现UserService所.发者需要使用Client类API与Thrift server端交互,负责与Thrift serverSocket链接,发送请求接收响应.
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)