Leadership Event Handling
可以根据是否授予或撤销领导权而启动和停止端点组。这在群集场景中很有用,其中共享资源只能由单个实例使用。一个示例是轮询共享目录的文件入站通道适配器。(参见 Reading Files)。
Groups of endpoints can be started and stopped based on leadership being granted or revoked, respectively. This is useful in clustered scenarios where shared resources must be consumed by only a single instance. An example of this is a file inbound channel adapter that is polling a shared directory. (See Reading Files).
要参与领导者选举,并在当选领导者、领导权被撤销或因获取领导者资源失败时收到通知,应用程序在应用程序上下文中创建一个被称作“领导者发起程序
”的组件。通常,领导者发起程序是一个 SmartLifecycle
,因此它在上下文启动时(可选地)启动,然后在领导权更改时发布通知。您还可以通过将 publishFailedEvents
设置为 true
(从版本 5.0 开始)接收故障通知,当故障发生时您想要执行特定操作时。根据传统,您应提供一个接收回调的 Candidate
。您还可以通过框架提供的 Context
对象撤销领导权。您的代码还可以侦听 o.s.i.leader.event.AbstractLeaderEvent
实例(OnGrantedEvent
和 OnRevokedEvent
的超类)并做出相应响应(例如,使用 SmartLifecycleRoleController
)。这些事件包含对 Context
对象的引用。以下列表显示了 Context
接口的定义:
To participate in a leader election and be notified when elected leader, when leadership is revoked, or on failure to acquire the resources to become leader, an application creates a component in the application context called a “leader initiator”.
Normally, a leader initiator is a SmartLifecycle
, so it starts (optionally) when the context starts and then publishes notifications when leadership changes.
You can also receive failure notifications by setting the publishFailedEvents
to true
(starting with version 5.0), for cases when you want to take a specific action if a failure occurs.
By convention, you should provide a Candidate
that receives the callbacks.
You can also revoke the leadership through a Context
object provided by the framework.
Your code can also listen for o.s.i.leader.event.AbstractLeaderEvent
instances (the super class of OnGrantedEvent
and OnRevokedEvent
) and respond accordingly (for instance, by using a SmartLifecycleRoleController
).
The events contain a reference to the Context
object.
The following listing shows the definition of the Context
interface:
public interface Context {
boolean isLeader();
void yield();
String getRole();
}
从 5.0.6 版本开始,上下文提供对候选者角色的引用。
Starting with version 5.0.6, the context provides a reference to the candidate’s role.
Spring Integration 提供了基于 LockRegistry
抽象的领导者发起程序的基本实现。要使用它,您需要创建一个实例作为 Bean,如下例所示:
Spring Integration provides a basic implementation of a leader initiator that is based on the LockRegistry
abstraction.
To use it, you need to create an instance as a bean, as the following example shows:
@Bean
public LockRegistryLeaderInitiator leaderInitiator(LockRegistry locks) {
return new LockRegistryLeaderInitiator(locks);
}
如果已正确实现锁定注册表,那么最多只有一个领导者。如果锁定注册表还提供了在过期或中断时引发异常的锁定(理想情况下为 InterruptedException
),则无领导者时期的持续时间可以与锁定实现中的固有延迟允许的时间一样短。busyWaitMillis
属性的默认值,是在锁定不完善的(更为常见的情况)下增加一些额外的延迟,以防止 CPU 耗尽,并且您只有在尝试重新获得锁定时才知道它们已过期。
If the lock registry is implemented correctly, there is only ever at most one leader.
If the lock registry also provides locks that throw exceptions (ideally, InterruptedException
) when they expire or are broken, the duration of the leaderless periods can be as short as is allowed by the inherent latency in the lock implementation.
By default, the busyWaitMillis
property adds some additional latency to prevent CPU starvation in the (more usual) case that the locks are imperfect, and you only know they expired when you try to obtain one again.
有关使用 Zookeeper 的领导者选举和事件的详细信息,请参见 Zookeeper Leadership Event Handling。有关使用 Hazelcast 的领导者选举和事件的详细信息,请参见 Hazelcast Leadership Event Handling。
See Zookeeper Leadership Event Handling for more information about leadership election and events that use Zookeeper. See Hazelcast Leadership Event Handling for more information about leadership election and events that use Hazelcast.