簡體   English   中英

Spring cloud zookeeper Config - 為zookeeper設置ACL

[英]Spring cloud zookeeper Config - Set ACL for zookeeper

根據文檔,我可以通過調用 addAuthInfo 為 Zookeeper ACL 添加身份驗證信息。 但是在 Curator Framework bean 中,我沒有找到方法本身。 它引發了編譯問題! .

我的 POM 有

     <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-zookeeper-config</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.apache.zookeeper</groupId>
        <artifactId>zookeeper</artifactId>
        <version>3.4.8</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

如何將 zookeeper 身份驗證信息添加到 Spring Cloud Zookeeper Config。 任何工作示例都會幫助我。

這個問題有github issue ,目前還處於open狀態。

只要springcool團隊解決了這個問題,你就可以創建一個自定義的curator配置類,並將認證信息添加到CuratorFrameworkFactory類的builder方法中:

    @BootstrapConfiguration
    @ConditionalOnZookeeperEnabled
    public class CustomCuratorFrameworkConfig {

        @Autowired(required = false)
        private EnsembleProvider ensembleProvider;

        @Bean
        @ConditionalOnMissingBean
        public ZookeeperProperties zookeeperProperties() {
            return new ZookeeperProperties();
        }

        @Bean
        @ConditionalOnMissingBean
        public CuratorFramework curatorFramework(RetryPolicy retryPolicy, ZookeeperProperties properties) throws Exception{
            // username and password of the ACL digest scheme
            String zkUsername = "user";
            String zkPassword = "password";

            CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder();
            if (this.ensembleProvider != null) {
                builder.ensembleProvider(this.ensembleProvider);
            } else {
                builder.connectString(properties.getConnectString());
            }

            builder.retryPolicy(retryPolicy);

                String authenticationString = zkUsername + ":" + zkPassword;
                builder.authorization("digest", authenticationString.getBytes())
                        .aclProvider(new ACLProvider() {
                            @Override
                            public List<ACL> getDefaultAcl() {
                                return ZooDefs.Ids.CREATOR_ALL_ACL;
                            }

                            @Override
                            public List<ACL> getAclForPath(String path) {
                                return ZooDefs.Ids.CREATOR_ALL_ACL;
                            }
                        });

            CuratorFramework curator =  builder.build();
            curator.start();
curator.blockUntilConnected(properties.getBlockUntilConnectedWait(), properties.getBlockUntilConnectedUnit());
            return curator;
        }

        @Bean
        @ConditionalOnMissingBean
        public RetryPolicy exponentialBackoffRetry(ZookeeperProperties properties) {
            return new ExponentialBackoffRetry(properties.getBaseSleepTimeMs(), properties.getMaxRetries(), properties.getMaxSleepMs());
        }

    }

然后像這個spring 文檔一樣繼續:

您可以注冊配置類以在此階段運行,方法是使用 @BootstrapConfiguration 注釋它們並將它們包含在逗號分隔的列表中,您將其設置為 resources/META-INF 中 org.springframework.cloud.bootstrap.BootstrapConfiguration 屬性的值/spring.factories 文件

資源/META-INF/spring.factories

org.springframework.cloud.bootstrap.BootstrapConfiguration=\
my.project.CustomCuratorFrameworkConfig

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM