一、配置集群storm.yaml文件,配置drpc.server。
二、开启drpc服务,storm drpc。
三、编写DrpcTopology程序。如下:
<span style="font-size:24px;">import org.apache.storm.Config;
import org.apache.storm.LocalCluster;
import org.apache.storm.LocalDRPC;
import org.apache.storm.StormSubmitter;
import org.apache.storm.drpc.LinearDRPCTopologyBuilder;
import org.apache.storm.topology.BasicOutputCollector;
import org.apache.storm.topology.OutputFieldsDeclarer;
import org.apache.storm.topology.base.BaseBasicBolt;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Tuple;
import org.apache.storm.tuple.Values;
/**
* This topology is a basic example of doing distributed RPC on top of Storm. It implements a function that appends a
* "!" to any string you send the DRPC function.
*
* @see <a href="http://storm.apache.org/documentation/Distributed-RPC.html">Distributed RPC</a>
*/
public class Drpc {
public static class ExclaimBolt extends BaseBasicBolt {
@Override
public void execute(Tuple tuple, BasicOutputCollector collector) {
String input = tuple.getString(1);
collector.emit(new Values(tuple.getValue(0), input + "!"));
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("id", "result"));
}
}
public static void main(String[] args) throws Exception {
LinearDRPCTopologyBuilder builder = new LinearDRPCTopologyBuilder("exclamation");
builder.addBolt(new ExclaimBolt(), 3);
Config conf = new Config();
if (args == null || args.length == 0) {
LocalDRPC drpc = new LocalDRPC();
LocalCluster cluster = new LocalCluster();
cluster.submitTopology("drpc-demo", conf, builder.createLocalTopology(drpc));
for (String word : new String[]{ "hello", "goodbye" }) {
System.out.println("Result for \"" + word + "\": " + drpc.execute("exclamation", word));
}
}
else {
conf.setNumWorkers(3);
StormSubmitter.submitTopologyWithProgressBar(args[0], conf, builder.createRemoteTopology());
}
}
}
</span>
四、提交jar文件至集群后,用storm1.0.1api编写drpcClient程序总是报错,程序如下:
<span style="font-size:24px;">import org.apache.storm.Config;
import org.apache.storm.utils.DRPCClient;
public class DrpcClient {
public static void main(String[] args) throws Exception {
Config conf=new Config();
conf.setDebug(false);
DRPCClient client=new DRPCClient(conf, "storm2", 3772);
System.out.println(client.execute("exclamation", "hello"));
}
}</span>
错误如下:Exception in thread "main" java.lang.RuntimeException: java.lang.NullPointerException
at org.apache.storm.security.auth.AuthUtils.GetTransportPlugin(AuthUtils.java:267)
at org.apache.storm.security.auth.ThriftClient.reconnect(ThriftClient.java:88)
at org.apache.storm.security.auth.ThriftClient.<init>(ThriftClient.java:69)
at org.apache.storm.utils.DRPCClient.<init>(DRPCClient.java:44)
at org.apache.storm.utils.DRPCClient.<init>(DRPCClient.java:39)
at drpc.DrpcClient.main(DrpcClient.java:16)
Caused by: java.lang.NullPointerException
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:190)
at org.apache.storm.security.auth.AuthUtils.GetTransportPlugin(AuthUtils.java:263)
... 5 more
改用storm 0.9.6api则没有问题,可以正常调用server。
<span style="font-size:24px;">import backtype.storm.utils.DRPCClient;
public class DrpcClient {
public static void main(String[] args) throws Exception {
DRPCClient client=new DRPCClient("storm2", 3772);
System.out.println(client.execute("exclamation", "年后"));
}
}
</span>