如何使用ZooKeeper在Spring Integration中实现围绕轮询器的分布式锁定

时间:2018-12-13 16:31:36

标签: spring-integration apache-zookeeper spring-integration-dsl spring-cloud-zookeeper

https://docs.spring.io/spring-integration/reference/html/zookeeper.html中所述,Spring Integration具有ZooKeeper支持。 但是这份文件太含糊了。

它建议在bean下方添加,但不提供有关在授予节点领导权时如何启动/停止轮询器的详细信息。

@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
    }
}

1 个答案:

答案 0 :(得分:2)

LeaderInitiator成为领导者并撤销其领导权时,会发出OnGrantedEventOnRevokedEvent

有关这些事件处理及其如何影响特定角色的组件的更多信息,请参见https://docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html#endpoint-roles和下一个https://docs.spring.io/spring-integration/reference/html/messaging-endpoints-chapter.html#leadership-event-handling

尽管我同意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();
        }

    }

}

我在日志中有这样的内容:

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