赞
踩
配置信息一般写在application.yml或者bootstrap.yml文件中,bootstrap.yml文件优先级高。但是分布式配置中心需要读取一些公共的配置和根据不同的项目模块读取不同zk节点的配置信息,所以配置信息应该建立在zk节点下。
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中仍可以配置一些常用配置,我这里啥都没写。
从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节点情况
新建一个config.txt,内容为/config/Provider,dev=welcome.value=test224233 然后通过zkui的导入Import功能导入配置文件 config.txt。
启动类就是普通的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);
}
}
我们使用@Value("${welcome.value}")来注入3.1中配置的welcome.value属性。并在类上加@RefreshScope注解。
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; } }
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; } }
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); } }
首次请求接口访问配置信息:
zkui修改配置文件:
修改完配置刷新接口url,可以发现配置已经同步刷新了
项目地址:https://github.com/gaykingking/wc.git ,配置中心为config模块。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。