Use ZooKeeper as a Spring Cloud configuration center (transfer)

This article is reproduced from https://blog.csdn.net/CSDN_Stephen/article/details/78856323

Mainstream implementation of Spring Cloud Configuration Center

Spring cloud config
Spring cloud zookeeper config
The following is an introduction to the two

Srping Cloud Config

Spring cloud config is integrated with git (svn) to achieve configuration center. Spring cloud config is divided into three parts: server, client and git (svn). The server is responsible for publishing the configuration file stored in Git (SVN) into a REST interface. The client can obtain the configuration from the server REST interface (but the client does not Can not actively perceive the configuration change, so as to actively obtain the new configuration, which requires each client to trigger its own /refresh through the POST method. Among them, the purpose of configuring version control can be achieved through the properties of git itself. There is a cache form, first download the config locally to the server and then provide it to the client to improve reliability.

Srping Cloud Zookeeper Config

This project provides Zookeeper integration for Spring Boot applications through automatic configuration and binding to the Spring environment. Zookeeper provides a hierarchical namespace that allows clients to store arbitrary data, such as configuration data. Spring Cloud Zookeeper Config is an alternative to Config Server and Client.

Comparison between the two
Spring Cloud Config is managed through the file system and git/svn warehouse Configuration file. Contains client, server and git/svn repositories. Version control can be achieved through git/svn features

Spring Cloud Zookeeper Config stores configuration item data through Zookeeper hierarchical namespaces, and Zookeeper can monitor node changes and notification mechanisms in real time.

Spring Cloud Zookeeper Config is mainly divided into two parts, the client side and zookeeper, the client side is embedded in spring-cloud-starter-zookeeper-config, using netflix open source CuratorFramework to connect to zookeeper to obtain configuration data. Loaded into the environment when the microservice starts.

How to use Spring Cloud Zookeeper Config

1. Add the following dependencies to the end (caller) pom.xml


org.springframework.cloud
spring-cloud -starter-zookeeper-config

2, add zookeeper connection information in the client configuration file bootstrap.yml / bootstrap.properties< /p>

spring:
application:
name: testApp
cloud:
zookeeper:
enabled: true # true: enable zookeeper external configuration, false: read local configuration ;
connect-string: IP1:port1,IP2:port2,IP3:port3
config:
root: /config/dev
enabled: true
watcher:
enabled: false

Detailed attribute explanation:
connect-string: ZooKeeper address, if it is a cluster, comma separated nodes, format: ip:port[,ip2:port2,…..]
root: Specify the root directory of the attribute in zookeeper
spring.application.name: Define the name of your project, zk will look for the configuration in the directory named after the project name in the root directory you specify
watcher.enabled : The default value is true, to monitor whether the configuration is automatically updated after the configuration is changed, it needs to be used with Spring Boot Actuators

3. Import the configuration file to Zookeeper

3.1. Properties Naming rules (assuming you set the root configuration item above to /config/dev ):

/config/dev/{application-name},{profile}={key}={value}

The service name and dev (profile name) are separated by a comma, you can Set spring.cloud.zookeeper.config.profile-separator to separate with other symbols, such as “-” etc.

3.2. Configuration file example:
testApp-dev.txt

< p>/config/dev/testApp,dev=server.port=8080

3.3. Use ZKUI to visually manage Zookeeper, log in to ZKUI->import and select the corresponding file to import.
Please Baidu for installation and use of ZKUI.

4. Client-side configuration methods
I understand that there are three, and there may be others. You can Baidu by yourself
4.1, through @Value annotation< /p>

@Value(“${db.url}”)
String dbUrl;

4.2. Use directly in the configuration file (xml) (using ${propertyName})

How to make the client automatically Update the changed configuration of zookeeper

1, add the following dependencies to the client pom.xml


org.springframework.boot
spring-boot-starter-actuator

2, in bootstrap. Open the watcher in the yml/bootstrap.properties configuration file

spring.cloud.zookeeper.config.watcher.enabled=true

3. Add @RefreshScope on the Bean Annotation

Introduction to configuration reading

The configuration information is in the service Loaded into the Spring Environment at startup. The configuration information is stored in the /config namespace by default.
For example, if the application name is “testApp” and the environment is “dev”, the following configuration sources will be created:
– config/testApp,dev
– config/testApp
– config/application,dev
– config/application
The most specific configuration source is placed at the top, and the most generalized configuration source is placed at the bottom. The attributes under the config/application namespace will be used for all applications. The attributes under the config/testApp namespace can only be used by instances of the “testApp” service, and cannot be used by other services.

Configuration priority

Remote configuration (zookeeper, etc.)> Command line parameter configuration> application.yml> bootstrap.yml

Leave a Comment

Your email address will not be published.