根据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>如何在Spring Cloud zookeeper Config中添加Zookeeper身份验证信息。任何有效的例子都会对我有所帮助。
发布于 2020-04-19 17:15:15
有一个针对此问题的github issue,它仍处于打开状态。
只要这个问题被spring cool团队解决了,您就可以创建一个自定义的curator配置类,并将认证信息添加到CuratorFrameworkFactory类的构建器方法中:
@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对其进行注释,并将它们包含在逗号分隔的列表中,该列表设置为资源/META-INF/文件中的org.springframework.cloud.bootstrap.BootstrapConfiguration属性的值
resources/META-INF/spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
my.project.CustomCuratorFrameworkConfighttps://stackoverflow.com/questions/52330305
复制相似问题