springcloud-zookeeper分布式配置中心
配置信息一般写在application.yml或者bootstrap.yml文件中,bootstrap.yml文件优先级高。但是分布式配置中心需要读取一些公共的配置和根据不同的项目模块读取不同zk节点的配置信息,所以配置信息应该建立在zk节点下。
1.配置中心所需的zookeeper相关配置
server:
port: 9001
spring:
application:
name: Provider
profiles:
active: dev
config:
import: optional:zookeeper:localhost:2181
cloud:
zookeeper:
config:
enabled: true
root: config
profileSeparator: ','
discovery:
enabled: true
connect-string: localhost:2181
这里面,包含了端口、应用名、zookeeper配置中心信息。
spring.application.name是标识了应用名。
spring.profiles.active 是激活的环境,你可以不写对应的配置文件,但这个配置必须有,是zookeeper配置中心路径的一部分。
spring.cloud.zookeeper.connect-string是zookeeper服务器地址。
spring.cloud.zookeeper.config.enabled开启配置中心功能。
spring.cloud.zookeeper.config.root是zookeeper的配置路径根目录。
spring.cloud.zookeeper.config.profileSeparator是profile和应用名直接的分隔符。
比如我这里写了个welcome.value配置。路径是config下的Provider,dev 下的welcome.value。
application.yml:
application.yml中仍可以配置一些常用配置,我这里啥都没写。
2.测试配置管理前初始化zkui
从github上把zkui下载下来,zukui github地址为:https://github.com/DeemOpen/zkui,在下载的zkui资源中,有关于zkui配置的文件config.cfg
默认关键配置如下,基本不需要修改,默认zkui图形页面端口号9090,用户名admin,密码manager
#Server Port
serverPort=9090
#Comma seperated list of all the zookeeper servers
zkServer=localhost:2181,localhost:2181
#Specific roles for ldap authenticated users. Ignore if using file based authentication.
ldapRoleSet={"users": [{ "username":"domain\\user1" , "role": "ADMIN" }]}
userSet = {"users": [{ "username":"admin" , "password":"manager","role": "ADMIN" },{ "username":"appconfig" , "password":"appconfig","role": "USER" }]}
将zkui项目maven clean intall重新编译一下生成target目录
进入target目录的命令行模式执行jdk命令
java -jar .\zkui-2.0-SNAPSHOT-jar-with-dependencies.jar
效果如下图代表zkui初始化成功,此时就可以网页访问zkui
登录localhost:9090,输入admin,manager账号密码即可查看zk节点情况
3.测试配置管理
3.1zookeeper上做配置
新建一个config.txt,内容为/config/Provider,dev=welcome.value=test224233 然后通过zkui的导入Import功能导入配置文件 config.txt。
3.2启动类
启动类就是普通的Springboot启动,无需其他注解。
ConfigApplication :
package com.ls;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
3.3 测试配置变更逻辑
我们使用@Value("${welcome.value}")来注入3.1中配置的welcome.value属性。并在类上加@RefreshScope注解。
- 我们可以将welcom.value属性写入配置文件中,但要注意,配置中心的配置优先级高于本地配置文件
- 如果本地配置文件不存在,配置中心也没有,启动会报错的
- 注意,需要变更配置的类上,要加@RefreshScope注解,否则不会刷新配置。
- 通过接口查询当前的welcome.value值,zookeeper变更配置以后,接口能立即查到变更后的值。
ConfigInfoService :
package com.ls.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;
@RefreshScope
@Service
public class ConfigInfoService {
@Value("${welcome.value}")
private String welcomValue;
public String testValue() {
System.out.println(welcomValue);
return welcomValue;
}
public String getWelcomValue() {
return welcomValue;
}
public void setWelcomValue(String welcomValue) {
this.welcomValue = welcomValue;
}
}
3.4 测试Web
ZkConfigRest :
package com.ls.controller;
import com.ls.entity.ResultModel;
import com.ls.service.ConfigInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/zkConfigApi")
public class ZkConfigRest {
@Autowired
ConfigInfoService configInfoService;
@Value("${spring.application.name}")
private String instanceName;
@Autowired
DiscoveryClient discoveryClient;
@RequestMapping(value = "/value", method = { RequestMethod.GET })
public ResultModel value() {
return ResultModel.ok(configInfoService.testValue());
}
@GetMapping("/services")
public List<String> serviceUrl() {
List<ServiceInstance> list = discoveryClient.getInstances(instanceName);
List<String> services = new ArrayList<>();
if (list != null && list.size() > 0 ) {
list.forEach(serviceInstance -> {
services.add(serviceInstance.getUri().toString());
});
}
return services;
}
}
4、过程中用到的实体
ResultModel:
package com.ls.entity;
public class ResultModel {
private String errorCode;
private String message;
private Object data;
public ResultModel() {
}
public ResultModel(String errorCode, String message) {
this.errorCode = errorCode;
this.message = message;
}
public ResultModel(String errorCode, String message, Object data) {
this.errorCode = errorCode;
this.message = message;
this.data = data;
}
public String geterrorCode() {
return errorCode;
}
public void seterrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public static ResultModel ok() {
return new ResultModel("0000","成功");
}
public static ResultModel ok(Object data) {
return new ResultModel("0000","成功", data);
}
public static ResultModel error() {
return new ResultModel("1111","失败");
}
public static ResultModel error(String msg) {
return new ResultModel("1111","失败", msg);
}
public static ResultModel error(String msg, Object data) {
return new ResultModel("1111", msg, data);
}
}
5.测试请求结果过程
首次请求接口访问配置信息:
zkui修改配置文件:
修改完配置刷新接口url,可以发现配置已经同步刷新了
项目地址:https://github.com/gaykingking/wc.git ,配置中心为config模块。