Zookeeper Support
版本 4.2 在 4.2 版本中为该框架添加了 Zookeeper 支持,该支持包括:
Version 4.2 added Zookeeper support to the framework in version 4.2, which consists of:
你需要将此依赖项包含在你的项目中:
You need to include this dependency into your project:
-
Maven
-
Gradle
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-zookeeper</artifactId>
<version>{project-version}</version>
</dependency>
compile "org.springframework.integration:spring-integration-zookeeper:{project-version}"
Zookeeper Metadata Store
您可以在需要任何 MetadataStore
的任何地方使用 ZookeeperMetadataStore
,例如持久文件列表过滤器。有关更多信息,请参阅 Metadata Store。以下示例使用 XML 配置 Zookeeper 元数据存储:
You can use the ZookeeperMetadataStore
where any MetadataStore
is needed, such as for persistent file list filters.
See Metadata Store for more information.
The following example configures a Zookeeper metadata store with XML:
<bean id="client" class="org.springframework.integration.zookeeper.config.CuratorFrameworkFactoryBean">
<constructor-arg value="${connect.string}" />
</bean>
<bean id="meta" class="org.springframework.integration.zookeeper.metadata.ZookeeperMetadataStore">
<constructor-arg ref="client" />
</bean>
以下示例展示如何通过 Java 配置 Zookeeper 元数据存储:
The following example shows how to configure a Zookeeper metadata store with Java:
@Bean
public MetadataStore zkStore(CuratorFramework client) {
return new ZookeeperMetadataStore(client);
}
Zookeeper Lock Registry
可以在任何需要 LockRegistry
的地方使用 ZookeeperLockRegistry
,例如在群集环境中使用聚合器和共享 MessageStore
时。
The ZookeeperLockRegistry
can be used where any LockRegistry
is needed, such as when using an aggregator in a clustered environment with a shared MessageStore
.
LockRegistry
用于基于键(聚合器使用 correlationId
)“查找”锁。默认情况下,ZookeeperLockRegistry
中的锁保存在 Zookeeper 中的以下路径下:` /SpringIntegration-LockRegistry/。您可以通过提供 `ZookeeperLockRegistry.KeyToPathStrategy
的实现来自定义路径,如下例所示:
A LockRegistry
is used to “look up” a lock based on a key (the aggregator uses correlationId
).
By default, locks in the ZookeeperLockRegistry
are maintained in zookeeper under the following path: /SpringIntegration-LockRegistry/
.
You can customize the path by providing an implementation of ZookeeperLockRegistry.KeyToPathStrategy
, as the following example shows:
public interface KeyToPathStrategy {
String pathFor(String key);
boolean bounded();
}
如果该策略从 isBounded
返回 true
,则不再需要收集未使用的锁。对于无界限策略(例如默认策略),您需要定期调用 expireUnusedOlderThan(long age)
以从内存中删除未使用的旧锁。
If the strategy returns true
from isBounded
, unused locks do not need to be harvested.
For unbounded strategies (such as the default), you need to periodically invoke expireUnusedOlderThan(long age)
to remove old unused locks from memory.
从版本 5.5.6 开始,ZookeeperLockRegistry
支持通过 ZookeeperLockRegistry.setCacheCapacity()
自动清除 ZookeeperLockRegistry.locks
中 ZkLock 的缓存。有关更多信息,请参阅其 JavaDocs。
Starting with version 5.5.6, the ZookeeperLockRegistry
is support automatically clean up cache for ZkLock in ZookeeperLockRegistry.locks
via ZookeeperLockRegistry.setCacheCapacity()
.
See its JavaDocs for more information.
Zookeeper Leadership Event Handling
以下示例使用 XML 为 Zookeeper 中的领导选举配置应用程序:
The following example uses XML to configure an application for leader election in Zookeeper:
<int-zk:leader-listener client="client" path="/siNamespace" role="cluster" />
client
用于引用 CuratorFramework
bean。CuratorFrameworkFactoryBean
是可用的。当选出领导者时,将为角色 cluster
发布 OnGrantedEvent
。该角色中的所有端点都将启动。当取消领导地位时,将为角色 cluster
发布 OnRevokedEvent
。该角色中的所有端点都将停止。有关更多信息,请参阅 Endpoint Roles。
client
is a reference to a CuratorFramework
bean.
A CuratorFrameworkFactoryBean
is available.
When a leader is elected, an OnGrantedEvent
is published for the role cluster
.
Any endpoints in that role are started.
When leadership is revoked, an OnRevokedEvent
is published for the role cluster
.
Any endpoints in that role are stopped.
See Endpoint Roles for more information.
您可以使用 Java 配置创建リーダー发起者实例,如下例所示:
You can use Java configuration to create an instance of the leader initiator, as the following example shows:
@Bean
public LeaderInitiatorFactoryBean leaderInitiator(CuratorFramework client) {
return new LeaderInitiatorFactoryBean()
.setClient(client)
.setPath("/siTest/")
.setRole("cluster");
}
从版本 5.3 开始,candidate
选项显示在 LeaderInitiatorFactoryBean
上,用于更精细地配置外部提供的 Candidate
实例。只能提供 candidate
或 role
选项之一,但不能同时提供;role
选项使用 UUID
创建内部 DefaultCandidate
实例,用作 id
选项。
Starting with version 5.3, a candidate
option is exposed on the LeaderInitiatorFactoryBean
for more configuration control of the externally provided Candidate
instance.
Only one of the candidate
or role
options has to be provided, but not both; the role
options creates internally a DefaultCandidate
instance with an UUID
for id
option.