1,drpc principle
The client sends the name of the method to be executed to the DRPC server, as well as the parameters of this method. The topology of this function is implemented using Drpcspout to receive the function call stream from the DRPC server. Each function call is marked with a unique ID by the DRPC server. The topology then calculates that the last bolt in topology, called Returnresults, connects to the DRPC server and sends the result of the call to the DRPC server (identified by that unique ID). The DRPC server uses that unique ID to match the waiting client, wakes up the client and sends the result to it.
2, example
Package Storm.starter;import Backtype.storm.config;import Backtype.storm.localcluster;import Backtype.storm.localdrpc;import Backtype.storm.drpc.drpcspout;import Backtype.storm.drpc.returnresults;import Backtype.storm.topology.basicoutputcollector;import Backtype.storm.topology.outputfieldsdeclarer;import Backtype.storm.topology.topologybuilder;import Backtype.storm.topology.base.basebasicbolt;import Backtype.storm.tuple.fields;import Backtype.storm.tuple.tuple;import Backtype.storm.tuple.values;public Class MANUALDRPC {public static class Exclamationbolt extends Basebasicbolt {@Override public void Declareoutputfields ( Outputfieldsdeclarer declarer) {Declarer.declare (New fields ("Result", "return-info")); } @Override public void execute (tuple tuple, basicoutputcollector collector) {String arg = tuple.getstring (0); Object retinfo = tuple.getvalue (1); Collector.emit (New Values (ARG + "!!!", retinfo)); }} public static void Main (string[] args) { Topologybuilder builder = new Topologybuilder (); Localdrpc drpc = new Localdrpc (); drpcspout spout = new Drpcspout ("exclamation", DRPC); Builder.setspout ("Drpc", spout); Builder.setbolt ("Exclaim", New Exclamationbolt (), 3). shufflegrouping ("Drpc"); Builder.setbolt ("Return", New Returnresults (), 3). shufflegrouping ("exclaim"); Localcluster cluster = new Localcluster (); Config conf = new config (); Cluster.submittopology ("Exclaim", Conf, Builder.createtopology ()); System.out.println (Drpc.execute ("exclamation", "AAA")); System.out.println (Drpc.execute ("exclamation", "BBB")); }}
Note: Drpcspout's name matches the name specified by the Drpc.execute.
Storm Drpc Example