To keep the data highly available, we use Zookeeper as the configuration center to save the data.SpringCloud also has an official description of Zookeeper integration: spring_cloud_zookeeper
Here is a practical explanation of how to use it.
1. Add Dependent Packages
<!-- Operations and Maintenance Monitoring --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Web application program--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- provide zookeeper - config --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zookeeper-config</artifactId> </dependency>
Configuration Instructions:
org.springframework.cloud#spring-cloud-starter-zookeeper-config is a configuration project for zookeeper as the configuration center.
If the web project needs to add org.springframework.boot#spring-boot-starter-web.
The configuration project org.springframework.boot#spring-boot-starter-actuator is used to communicate with zookeeper
2. Add Profile
Add the following configuration under the bootstrap.properties file
spring.application.name=config-demo spring.profiles.active=dev #ZooKeeper's connection string, if clustered, comma-separated nodes, in the format ip:port[,ip2:port2,..... spring.cloud.zookeeper.connect-string = 192.168.0.1:2181 #Specify the root directory of the zookeeper directory spring.cloud.zookeeper.config.root = config #zk enabled configuration spring.cloud.zookeeper.config.enabled = true spring.cloud.zookeeper.config.profileSeparator = :
Configuration Instructions:
Modify 192.168.0.1:2181 to be the Zookeeper address in the project, defaulting to localhost:2181
Based on the above configuration, the address read from zookeeper is: /config/config-demo:dev
Logging in to zookeeper requires manual creation or interface maintenance using zkui
3. Manually create configurations in Zookeeper
Example:
[zk: localhost:2181(CONNECTED) 3] create /config/config-demo:dev Created /config/config-demo:dev [zk: localhost:2181(CONNECTED) 4] create /config/config-demo:dev/user.name yuesf Created /config/config-demo:dev/user.name
4. Use in Programs
1) Create a configuration file
Example: UserProperties.java
package com.example.config; import org.springframework.boot.context.properties.ConfigurationProperties; /* * @auth yuesf * @data 2019/10/29 */ @ConfigurationProperties(prefix = "user") public class UserProperties { private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }
Configuration Instructions:
Mark the class as a configuration file using the @ConfigurationProperties attribute
2) Activate automatic assembly
Activate profile on startup class
package com.example.config; import com.example.config.demo.UserProperties; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; /* * @auth yuesf * @data 2019/11/12 */ @EnableConfigurationProperties(value = {UserProperties.class }) @SpringBootApplication public class ConfigApplication { public static void main(String[] args) { SpringApplication.run(ConfigApplication.class,args); } }
Configuration Instructions:
In the above example code, @EnableConfiguration Properties (UserProperties.class) is the UserProperties.class activation configuration
3) Program Call Configuration
Example: Call the user.name variable of the configuration center
package com.example.config.controller; import com.example.config.demo.UserProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /* * @auth yuesf * @data 2019/11/12 */ @RestController public class UserController { @Autowired private UserProperties userProperties; @GetMapping("/user") public String getUser(){ return userProperties.getName(); } }
5. Interface Maintenance
The zkui deployment used by the interface. For a description of zkui, go to zkui: https://github.com/DeemOpen/zkui
There is also a local zk visualization interface ZooViewer, please move to: https://github.com/HelloKittyNII/ZooViewer for instructions on using ZooViewer
This article is published by blog OpenWrite Release!
Thank you again!!! You have read the full article. Welcome to the WeChat Ape Public Number. Your support is my motivation to keep updating the article!