Spring cloud zookeeper提供了分布式配置中心以及注册中心的功能,可以使用其配置中心功能替代Spring cloud config。

由于是Spring出品,所以与Spring Boot应用集成非常简单,下面就让我们来了解一下其简单的使用方式。

Spring Cloud Zookeeper provides Apache Zookeeper integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms. With a few simple annotations you can quickly enable and configure the common patterns inside your application and build large distributed systems with Zookeeper. The patterns provided include Service Discovery and Distributed Configuration.

系统架构图

系统架构

依赖引入

Maven

1
2
3
4
5
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-config</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>

Gradle

1
compile('org.springframework.cloud:spring-cloud-starter-zookeeper-config:2.0.0.RELEASE')

系统配置

bootstarp.xml

1
2
3
4
5
6
7
8
9
spring:
cloud:
zookeeper:
enabled: true #Is Zookeeper enabled
connect-string: localhost:2181 #Connection string to the Zookeeper cluster
max-retries: 3 #Max number of times to retry
config:
root: config/root #Root folder where the configuration for Zookeeper is kept
defaultContext: application #The name of the default context

代码演示

启动时加载配置

1
2
3
4
5
6
7
8
9
public class Example {
//Server level switch
@Value("${server}")
private String serverEnabled;
//Feature level switch
@Value("${feature.log}")
private String featureLogEnabled;
}

配置更新时使用@RefreshScope自动刷新

1
2
3
4
5
6
7
8
9
10
@RefreshScope
public class Example {
//Server level switch
@Value("${server}")
private String serverEnabled;
//Feature level switch
@Value("${feature.log}")
private String featureLogEnabled;
}

变量的节点需要事先在Zookeeper上配置好。

Zookeeper树

zk结构

注意事项

由于应用启动需要从ZK Server上去拉取配置,所以数据节点要预先在ZK上设置好,或者通过application.yml文件给定一个默认值,否则启动会报错。

关于Zookeeper版本

3.5.x

Spring-cloud-zookeeper目前最新版本依赖的zookeeper-client依赖树如下:

1
2
3
4
5
6
7
8
9
10
11
spring-cloud-starter-zookeeper-config:2.0.0.RELEASE
|
|--curator-recipes:4.0.1
|
|--curator-framework:4.0.1
| |
| |--zookeeper:3.5.3-beta
|
|--curator-test:4.0.1
| |
| |--zookeeper:3.5.3-beta

如果Zookeeper Server版本为3.5.x,则直接使用就可以。

3.4.x

如果Zookeeper Server版本为3.4.X,则需要手动进行Client断兼容。方法如下:

Maven

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!-- Curator Recipes -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.0.1</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Curator Test -->
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<version>2.12.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</exclusion>
</exclusions>
<scope>test</scope>
</dependency>
<!-- Zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.x</version>
</dependency>

Gradle

1
2
3
4
5
6
7
compile('org.apache.curator:curator-recipes:4.0.1) {
exclude group: 'org.apache.zookeeper', module: 'zookeeper'
}
testCompile('org.apache.curator:curator-test:2.12.0') {
exclude group: 'org.apache.zookeeper', module: 'zookeeper'
}
compile('org.apache.zookeeper:zookeeper:3.4.x')

特别注意

如果要使用3.4.x版本的Zookeeper Client, 那么在使用TestingServer时只能依赖org.apache.curator:curator-test:2.12.0

代码地址

GitHub: https://github.com/CharleyWuCL/spring-config-sample