新闻资讯



Axon 框架扩展 - JGroups
时间:2022-07-05
是用于分发命令总线(命令)的 Axon 的替代方案。
使用(顾名思义)作为底层的发现和分发机制。所描述的功能超出了本文的范围。有关详细信息,请参阅用户指南。
要使用 Axon 的组件,请确保 axon- 通过依赖管理系统在类路径上可用。与 Boot 结合使用时,可以包含 axon---boot- 依赖项以启用自动配置。
由于它处理节点的发现和它们之间的通信,因此它同时充当两者。
笔记
您可以在 axon--- 中找到 s 的特定组件。
有四个必需的配置元素:
笔记
使用缓存时,应清除更改以避免潜在的数据损坏(例如,当命令未指定 @on 并且新成员快速加入和离开时,修改聚合时仍会缓存在其他地方。)
最终,需要实际连接才能将消息发送到其他段。为此,请调用 () 方法。
JChannel channel = new JChannel("path/to/channel/config.xml");
CommandBus localSegment = SimpleCommandBus.builder().build();
Serializer serializer = XStreamSerializer.builder().build();
JGroupsConnector connector = JGroupsConnector.builder()
.channel(channel)
.clusterName("myCommandBus")
.localSegment(localSegment)
.serializer(serializer)
.build();
DistributedCommandBus commandBus = DistributedCommandBus.builder()
.connector(connector)
.commandRouter(connector)
.build();
// on one node:
commandBus.subscribe(CommandType.class.getName(), handler);
connector.connect();
// on another node, with more CPU:
commandBus.subscribe(CommandType.class.getName(), handler);
commandBus.subscribe(AnotherCommandType.class.getName(), handler2);
commandBus.updateLoadFactor(150); // defaults to 100
connector.connect();
// from now on, just deal with commandBus as if it is local...
笔记
请注意,并不要求所有段都具有相同类型命令的命令处理程序。您可以完全将不同的部分用于不同的命令类型。分布式命令总线将始终选择一个节点来分发命令,该节点支持该特定类型的命令。
配置(引导)
如果你使用楼宇自控扩展模块,你可能要考虑使用。它在启动时自动连接连接器,并在关机时正确断开连接。此外,它对测试环境(但不应被视为生产就绪)和配置的自动装配使用合理的默认值。
如果使用 Boot,可以通过包含 axon---boot- 依赖项来进一步简化配置。
连接器设置以轴突为前缀...
# enables Axon to construct the DistributedCommandBus
axon.distributed.enabled=true
# defines the load factor used for this segment. Defaults to 100
axon.distributed.load-factor=100
# the address to bind this instance to. By default, it attempts to find the Global IP address
axon.distributed.jgroups.bind-addr=GLOBAL
# the port to bind the local instance to
axon.distributed.jgroups.bind-port=7800
# the name of the JGroups Cluster to connect to
axon.distributed.jgroups.cluster-name=Axon
# the JGroups Configuration file to configure JGroups with
axon.distributed.jgroups.configuration-file=default_tcp_gossip.xml
# The IP and port of the Gossip Servers (comma separated) to connect to
axon.distributed.jgroups.gossip.hosts=localhost[12001]
# when true, will start an embedded Gossip Server on bound to the port of the first mentioned gossip host.
axon.distributed.jgroups.gossip.auto-start=false