storm DRPC指南

storm DRPC指南

@(STORM)[storm]

(一)什么是DRPC

先看一个经典的图:
image

DRPC与一般的RPC没有区别,只不过它是分布式的。
对于用户而言,他是向一个DRPC服务器发送请求,然后等待DRPC的返回结果。整个流程如下:

1、用户向DRPC服务器发送请求,请求内容主要包括请求的方法名以及请求的参数。
2、DRPC服务器接收到这个请求后,将请求发送到拓扑的spout。
3、拓扑计算完成后,将结果返回到DRPC服务器。结果是以请求参数作为key,计算结果作为value的一个tuple(即有2个域)。
4、DRPC服务器将结果返回用户。

简单的说,就是用户的请求作为spout的数据源

对于高计算密度的RPC应用,DRPC可以将请求均匀的分布在整个storm集群进行计算。

在storm0.9以后,drpc都使用了trident的API来创建拓扑,不再使用原有API。

(二)LocalDPRC的例子

下面的例子中将一些字符串发送给DRPC服务器,然后经过拓扑的计算(在字符串后面加上一下“!”)返回到客户端。

public class LocalDRPCDemo {

    public static void main(String[] args) {
        //创建DRPC服务器
        LocalDRPC drpc = new LocalDRPC();

        //1、创建拓扑
        TridentTopology topology = new TridentTopology();
        topology.newDRPCStream("exclaimation", drpc).each(new Fields("args"), new ExclaimBolt(), new Fields("words"))
                .parallelismHint(5);

        //2、提交拓扑
        LocalCluster cluster = new LocalCluster();
        cluster.submitTopology("drpc-demo", new HashMap(), topology.build());

        //3、客户端调用
        String ss = "the cow jumped over the moon";
        for(String s :ss.split(" ")){
            //返回结果是一个KV格式,KEY为请求的id, V为最后一个bolt的返回结果。被封装成一个2个值的tuple。
            System.out.println(drpc.execute("exclaimation", s));
        }

        //4、关闭集群及drpc服务器
        cluster.shutdown();
        drpc.shutdown();

    }

}

 class ExclaimBolt extends BaseFunction{

    @Override
    public void execute(TridentTuple tuple, TridentCollector collector) {
        String input = tuple.getString(0);
        collector.emit(new Values(input+"!"));
    }

}

(三)RemoteDRPC:将DPRC拓扑提交至集群

1、启动DRPC服务器

先在storm集群的配置文件中加上:

 drpc.servers:
     - "192.168.1.1"

然后在上述ip地址的机器上启动DRPC服务器,这台机器上需要有storm集群的配置文件

storm drpc

2、准备storm拓扑代码并提交

public class RemoteDRPCDemo {

    public static void main(String[] args) throws AlreadyAliveException, InvalidTopologyException, AuthorizationException {

        Config conf = new Config();
        conf.setNumWorkers(5);

        //1、创建拓扑
        TridentTopology topology = new TridentTopology();
        topology.newDRPCStream("exclaimation").each(new Fields("args"), new ExclaimBolt2(), new Fields("words"))
                .parallelismHint(5);

        //2、提交拓扑
        StormSubmitter.submitTopology("drpc-demo", conf, topology.build());


    }

}

class ExclaimBolt2 extends BaseFunction{

    @Override
    public void execute(TridentTuple tuple, TridentCollector collector) {
        String input = tuple.getString(0);
        collector.emit(new Values(input+"!"));
    }

}

然后通过storm jar提交拓扑到集群中。

3、使用客户端发起请求

客户端需要指定DRPC的服务器地址与端口,然后发起请求,等待返回。

public class DRPCClientDemo {

    public static void main(String[] args) throws Exception {

        Config conf = new Config();
        conf.setDebug(false);
        conf.put("storm.thrift.transport", "backtype.storm.security.auth.SimpleTransportPlugin");
        conf.put(Config.STORM_NIMBUS_RETRY_TIMES, 3);
        conf.put(Config.STORM_NIMBUS_RETRY_INTERVAL, 10);
        conf.put(Config.STORM_NIMBUS_RETRY_INTERVAL_CEILING, 20);
        conf.put(Config.DRPC_MAX_BUFFER_SIZE, 1048576);

        DRPCClient client = new DRPCClient(conf, "192.168.1.1",3772);

        String ss = "the cow jumped over the moon";
        for(String s :ss.split(" ")){
            //返回结果是一个KV格式,KEY为请求的id, V为最后一个bolt的返回结果。被封装成一个2个值的tuple。
            System.out.println(client.execute("exclaimation", s));
        }


    }

使用DRPC访问hbase有问题,因为storm集群上的hbase jar包还是0.98的,有些类找不到,先解决这个。

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值