SpringCloud用Zookeeper搭建配置中心的方法
作者:gbcxy 时间:2022-12-17 18:16:18
本文介绍了SpringCloud +Zookeeper完成配置中心,分享给大家,具有如下:
使用场景
项目配置更改不需要打包,重启
提供配置文件的可视化界面
和springcloud快速整合
为什么使用zookeeper
Zookeeper 作为一个分布式的服务框架,主要用来解决分布式集群中应用系统的一致性问题,它能提供基于类似于文件系统的目录节点树方式的数据存储, Zookeeper 作用主要是用来维护和监控存储的数据的状态变化,通过监控这些数据状态的变化,从而达到基于数据的集群管理。
怎么使用
1.pom文件
<!-- 提供zookeeper整合的包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zookeeper-config</artifactId>
</dependency>
<!-- springboot 提供监听的 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.bootstrap.properties文件
提示: springboot项目启动会优先读取 bootstrap.properties.然后获取zookeeper中的配置内容,才启动其他配置
#指定zookeeper的地址,多个用逗号拼接
spring.cloud.zookeeper.connect-string=192.168.100.0:2181
#指定springcloud 读取zookeeper路径的开始位置
spring.cloud.zookeeper.config.root=springcloud
#开始zk的配置
spring.cloud.zookeeper.config.enabled=true
#zk会在你指定的根目录下寻找以这个项目名命名的目录下的配置
spring.application.name=service_config
3.获取zookeeper中的配置
1.@value方式:
//不能动态更新值,需要重启项目
@Value("${com.xxx.username}")
public String username ;
2.@ConfigurationProperties和@EnableConfigurationProperties方式
//可以动态修改值,不需要重启
@ConfigurationProperties(prefix = "com.xxx")
public class UserInfo {
public String username ;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
application代码:
@SpringBootApplication
//支持多个配置类
@EnableConfigurationProperties({xxx.class,abc.class})
public class ServiceConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceConfigApplication.class, args);
}
}
4.获取配置的规则
假设:
spring.cloud.zookeeper.config.root=xxxx ;
spring.application.name=abc
zk 路径:
/xxxx/abc/com/gabo/username
取值:
@value(${com.gabo.username})
5. 检验:
启动的时候输出: State change: CONNECTED
代表连接zookeeper成功
修改配置中心内容,输出:Refresh keys changed:
代表项目中的值修改成功
来源:https://blog.csdn.net/qq_27336239/article/details/78848088
标签:SpringCloud,配置中心
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java利用移位运算将int型分解成四个byte型的方法
2023-11-09 08:25:00
![](https://img.aspxhome.com/file/2023/4/59334_0s.png)
教你快速搭建sona服务及idea使用sona的方法
2023-11-20 05:22:53
![](https://img.aspxhome.com/file/2023/1/59661_0s.jpg)
SpringBoot 整合 Shiro 密码登录的实现代码
2023-11-10 11:27:59
OpenCV和C++实现图像的翻转(镜像)、平移、旋转、仿射与透视变换
2023-07-14 23:47:22
![](https://img.aspxhome.com/file/2023/3/95523_0s.jpg)
解决Map集合使用get方法返回null抛出空指针异常问题
2023-11-25 00:14:00
![](https://img.aspxhome.com/file/2023/8/59848_0s.png)
使用Spring Security OAuth2实现单点登录
2023-08-13 01:44:34
SpringIOC BeanDefinition的加载流程详解
2023-11-25 18:06:22
![](https://img.aspxhome.com/file/2023/8/59938_0s.png)
Java关键字instanceof的两种用法实例
2023-07-31 06:24:50
MyBatis-Plus联表查询(Mybatis-Plus-Join)的功能实现
2023-11-25 03:24:33
![](https://img.aspxhome.com/file/2023/3/60023_0s.png)
使用SpringBoot+EasyExcel+Vue实现excel表格的导入和导出详解
2023-07-18 18:15:14
java实现航空用户管理系统
2023-11-24 02:42:37
![](https://img.aspxhome.com/file/2023/9/59589_0s.png)
springboot openfeign从JSON文件读取数据问题
2023-11-09 15:55:55
SpringBoot整合第三方技术的详细步骤
2023-11-29 08:22:48
![](https://img.aspxhome.com/file/2023/7/60777_0s.png)
java判断字符串String是否为空问题浅析
2023-08-25 07:06:06
mybatis批量新增、删除、查询和修改方式
2023-11-23 10:13:01
每日六道java新手入门面试题,通往自由的道路
2023-11-13 16:22:29
解析android中的dip,dp,px,sp和屏幕密度
2023-09-26 20:44:38
IntelliJ IDEA 安装教程2019.09.23(最新版)
2023-08-24 23:01:44
![](https://img.aspxhome.com/file/2023/5/58175_0s.jpg)
Java多线程开发工具之CompletableFuture的应用详解
2023-08-30 00:43:00
![](https://img.aspxhome.com/file/2023/9/57709_0s.png)
Maven打包jar包没有主属性问题解决方案
2023-11-25 06:56:32