是否可以在实际群集环境中使用风暴螺栓/喷口中使用全局静态变量



我有一个风暴项目,拓扑类具有提交拓扑的主要方法,spout1-> bolt1,spout2-> bolt2。我还有另一个具有静态变量的UTIL类 ->字符串列表。现在,Bolt1使用此静态列表并打印出内容,如Bolt2所述,列表中添加了字符串。Spout1和Spout2都以1000ms和500ms的间隔向相应的螺栓发出消息。

我将工人的数量设置为4,即config.setnumworkers(4)。

并行性提示:Spout1-1,Bolt1-100,Spout2-1,Bolt2-100。

此代码设置正在我的窗口计算机中以本地收集器模式工作。

但我不确定这是否可以在Linux的实际群集环境中起作用,并在多个服务器中餐饮主管Deamon。在实际的集群环境中,我想工人将在具有不同JVM流程的不同机器中运行。然后,螺栓是否可以访问全局静态变量,即字符串列表,因为它们可以在本地群集中进行操作?

下面有一些代码以供参考:

public static void main(String[] args) {
    System.out.println("Starting Topology....");
    TopologyBuilder builder = new TopologyBuilder();
    builder.setSpout("spout12", new TestSpout2(), 1);
    builder.setBolt("bolt12", new TestBolt2(), 100).shuffleGrouping("spout12", "spout12Stream");
    builder.setSpout("spout11", new TestSpout1(), 1);
    builder.setBolt("bolt11", new TestBolt1(), 100).shuffleGrouping("spout11", "spout11Stream");
    Config conf = new Config();
    conf.setDebug(false);
    conf.setNumWorkers(4);
    LocalCluster cluster = new LocalCluster();
    cluster.submitTopology("TestTopology3", conf, builder.createTopology());
}

Spout1 :
public void nextTuple() {
    int a = (int) (((Math.random() * 10)+1)*((Math.random() * 10)+1));
    String str = String.valueOf(a);
    Utils.sleep(1000);
    collector.emit("spout11Stream", new Values(str), str);
}

Bolt1 : 
public void execute(Tuple tuple) {
    System.out.println("########## In execute of TestBolt11....t Value 0 : " + tuple.getString(0) + "tt List : "
            + CommonUtils.list);
    _collector.ack(tuple);
}

Spout2 : 
public void nextTuple() {
    int a = (int) (((Math.random() * 10)+1)*((Math.random() * 10)+1));
    String str = String.valueOf(a);
    Utils.sleep(500);
    collector.emit("spout12Stream", new Values(str), str);
}

Bolt2 : 
public void execute(Tuple tuple) {
    System.out.println("!!!!!!!!!!!!! In execute of TestBolt12....t Value 0 : " + tuple.getString(0));
    CommonUtils.list.add(tuple.getString(0)+"gb");
    _collector.ack(tuple);
}

CommonUtilis class : 
public final class CommonUtils {
public static List<String> list = new ArrayList<String>();
}

成功运行中的sysout:

 !!!!!!!!!!!!! In execute of TestBolt12....  Value 0 : 31
 !!!!!!!!!!!!! In execute of TestBolt12....  Value 0 : 9
 !!!!!!!!!!!!! In execute of TestBolt12....  Value 0 : 68
 !!!!!!!!!!!!! In execute of TestBolt12....  Value 0 : 24
 ########## In execute of TestBolt11....     Value 0 : 39        List : [31gb, 9gb, 68gb, 24gb]
 !!!!!!!!!!!!! In execute of TestBolt12....  Value 0 : 60
 !!!!!!!!!!!!! In execute of TestBolt12....  Value 0 : 30
 ########## In execute of TestBolt11....     Value 0 : 26        List : [31gb, 9gb, 68gb, 24gb, 60gb, 30gb]
 !!!!!!!!!!!!! In execute of TestBolt12....  Value 0 : 9
 !!!!!!!!!!!!! In execute of TestBolt12....  Value 0 : 15
 ########## In execute of TestBolt11....     Value 0 : 11        List : [31gb, 9gb, 68gb, 24gb, 60gb, 30gb, 9gb, 15gb]

简短的答案是否,静态变量访问被jvm范围范围,您指出的是几个JVM。

有一些工作,

  1. 您可以具有带有1个平行性提示1的螺栓,该螺栓访问静态变量。由于只有一个螺栓,因此仅在一个JVM中进行访问,但是再说一次,您不妨在该螺栓上使用一个实例变量。
  2. (1)上的一个变化是在Storm中使用调度程序实现来具有多个螺栓的实例,但全部分配给了同一工人。这种方法不是我尝试过的,但我以为我还是把它扔到那里。
  3. 您可以使用数据库,关系或NOSQL作为读/写商店的数据,用于遍布群集
  4. 您可以将列表存储在Zookeeper中。我将Zookeeper访问包装在单例类中,以使您的螺栓实现更简单。

最新更新