Spring integration 如何使用ZooKeeper在Spring集成中实现分布式轮询锁
Spring集成具有ZooKeeper支持,如中所述 然而,这份文件是如此模糊 它建议在bean下面添加,但没有给出当节点被授予领导权时如何启动/停止轮询器的详细信息Spring integration 如何使用ZooKeeper在Spring集成中实现分布式轮询锁,spring-integration,apache-zookeeper,spring-integration-dsl,spring-cloud-zookeeper,Spring Integration,Apache Zookeeper,Spring Integration Dsl,Spring Cloud Zookeeper,Spring集成具有ZooKeeper支持,如中所述 然而,这份文件是如此模糊 它建议在bean下面添加,但没有给出当节点被授予领导权时如何启动/停止轮询器的详细信息 @Bean public LeaderInitiatorFactoryBean leaderInitiator(CuratorFramework client) { return new LeaderInitiatorFactoryBean() .setClient(client)
@Bean
public LeaderInitiatorFactoryBean leaderInitiator(CuratorFramework client) {
return new LeaderInitiatorFactoryBean()
.setClient(client)
.setPath("/siTest/")
.setRole("cluster");
}
我们有没有关于如何使用zookeeper确保在集群中任何时候只运行一次以下轮询器的示例
@Component
public class EventsPoller {
public void pullEvents() {
//pull events should be run by only one node in the cluster at any time
}
}
LeaderInitiator
在成为leader且其领导权被撤销时发出OnGrantedEvent
和OnRevokedEvent
有关这些事件处理以及它如何影响特定角色中的组件的更多信息,请参阅和下一页
虽然我同意Zookkeper章节必须有一些链接到SmartLifecycleRoleController
章节。请随时就此事提出JIRA,欢迎您的贡献
更新
这就是我在测试中所做的:
@RunWith(SpringRunner.class)
@DirtiesContext
public class LeaderInitiatorFactoryBeanTests extends ZookeeperTestSupport {
private static CuratorFramework client;
@Autowired
private PollableChannel stringsChannel;
@BeforeClass
public static void getClient() throws Exception {
client = createNewClient();
}
@AfterClass
public static void closeClient() {
if (client != null) {
client.close();
}
}
@Test
public void test() {
assertNotNull(this.stringsChannel.receive(10_000));
}
@Configuration
@EnableIntegration
public static class Config {
@Bean
public LeaderInitiatorFactoryBean leaderInitiator(CuratorFramework client) {
return new LeaderInitiatorFactoryBean()
.setClient(client)
.setPath("/siTest/")
.setRole("foo");
}
@Bean
public CuratorFramework client() {
return LeaderInitiatorFactoryBeanTests.client;
}
@Bean
@InboundChannelAdapter(channel = "stringsChannel", autoStartup = "false", poller = @Poller(fixedDelay = "100"))
@Role("foo")
public Supplier<String> inboundChannelAdapter() {
return () -> "foo";
}
@Bean
public PollableChannel stringsChannel() {
return new QueueChannel();
}
}
}
你有没有任何简单的民意调查者被动物园管理员选举控制的例子?我尝试实现SmartLifecycle,向leaderInitiator bean添加一个DefaultCandidate。但我仍然没有看到zookeeper调用或控制生命周期方法。想知道是否有简单民意测验的例子?请看我答案中的更新。
2018-12-14 10:12:33,542 DEBUG [Curator-LeaderSelector-0] [org.springframework.integration.support.SmartLifecycleRoleController] - Starting [leaderInitiatorFactoryBeanTests.Config.inboundChannelAdapter.inboundChannelAdapter] in role foo
2018-12-14 10:12:33,578 DEBUG [Curator-LeaderSelector-0] [org.springframework.integration.support.SmartLifecycleRoleController] - Stopping [leaderInitiatorFactoryBeanTests.Config.inboundChannelAdapter.inboundChannelAdapter] in role foo