Spring cloud zookeeper配置-为zookeeper设置ACL

u0njafvf  于 8个月前  发布在  Apache
关注(0)|答案(1)|浏览(94)

根据文档,我可以通过调用addAuthInfo为Zookeeper ACL添加身份验证信息。但是在CuratorFrameworkbean中,我没有找到该方法本身。它抛出编译问题!!!
我的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 auth信息添加到Spring Cloud Zookeeper Config。任何工作的例子都可以帮助我。

ruoxqz4g

ruoxqz4g1#

有一个github issue解决此问题,它仍然处于打开状态。
只要spring cool团队解决了这个问题,你就可以创建一个自定义的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文档:
您可以注册要在此阶段运行的配置类,方法是使用@BootstrapConfiguration注解它们,并将它们包含在一个逗号分隔的列表中,该列表设置为resources/META-INF/spring.factories文件中的org.springframework.cloud.bootstrap.BootstrapConfiguration属性的值

resources/META-INF/spring.factories

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

相关问题