【问题标题】:Spring cloud zookeeper Config - Set ACL for zookeeperSpring cloud zookeeper Config - 为zookeeper设置ACL
【发布时间】:2019-02-19 04:02:43
【问题描述】:

根据documentation,我可以通过调用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。任何工作示例都会对我有所帮助。

【问题讨论】:

  • 我认为文档需要更新。我看到了一个 setACL() 方法,看起来很可能是替代品。
  • 介意提交问题吗?

标签: spring-boot apache-zookeeper spring-cloud spring-cloud-config


【解决方案1】:

这个问题有一个github issue,它仍然处于打开状态。

只要春酷团队解决了这个问题,就可以创建一个自定义的curator config类,并在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 document

您可以注册配置类以在此阶段运行,方法是使用 @BootstrapConfiguration 注释它们并将它们包含在您设置为资源中 org.springframework.cloud.bootstrap.BootstrapConfiguration 属性值的逗号分隔列表中/ META-INF/spring.factories 文件

resources/META-INF/spring.factories

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-18
    • 2018-10-25
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多