Springcloudzookeeper配置-为zookeeper设置ACL

问题描述 投票:0回答:2

根据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配置中。任何可行的例子都会对我有帮助。

spring-boot apache-zookeeper spring-cloud spring-cloud-config
2个回答
0
投票

这个问题有一个 github issues 并且仍处于 open 状态。

只要Spring Cool团队解决了这个问题,您就可以创建自定义的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- 中 org.springframework.cloud.bootstrap.BootstrapConfiguration 属性的值INF/spring.factories 文件

资源/META-INF/spring.factories

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

0
投票

我使用了这段代码,但没有什么区别。它仍然自动为“world”生成/services/{application name},任何人:cdrwa

© www.soinside.com 2019 - 2024. All rights reserved.