Spring Cloud Config
解决了在分布式场景下多环境配置文件的管理和维护。
好处:
• 集中管理配置文件(码云,github)
• 不同环境不同配置,动态化的配置更新
• 配置信息改变时,不需要重启即可更新配置信息到服务


配置中心本质上也是一个微服务,同样需要注册到Eureka服务注册中心!
1. config-快速入门-gitee搭建远程仓库
搭建gitee远程仓库,最好设置私有
##04-config-快速入门-config server搭建 7:35
config server:
-
使用gitee创建远程仓库,上传配置文件
配置文件的路径规则: /{label}/{name}-{profiles}.{type} label 分支名称 如:master dev ,不写就是master。 name 配置文件名称 profiles 环境名称,不可省略,假如我们的仓库中配置文件命名没有环境名称,可以profile可以写为-a -
搭建 config server 模块并导入 config-server 依赖
<!-- config-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> -
编写配置,设置 gitee 远程仓库地址
server: port: 9527 spring: application: name: config-server # spring cloud config cloud: config: server: # git 的 远程仓库地址 git: uri: https://gitee.com/kd/ittest-config-test.git username: kd # 账号 password: kong # 密码 , 公有仓库可以不写账号和密码 #search-paths: user #对应码云上目录名字 将配置文件通过目录分类 label: master # 分支配置 -
引导类加@EnableConfigServer 注解// 启用config server功能
package com.ittest.config; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer // 启用config server功能 public class ConfigServerApp { public static void main(String[] args) { SpringApplication.run(ConfigServerApp.class,args); } } -
测试访问远程配置文件
访问 http://localhost:9527/master/config-dev.yml
2. config-快速入门-config client搭建
config client:
-
导入 starter-config 依赖
<!--config client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> -
配置config server 地址,读取配置文件名称等信息
新建bootstrap.yml配置文件
# 配置config-server地址 # 配置获得配置文件的名称等信息 spring: cloud: config: # 配置config-server地址 uri: http://localhost:9527 # 配置获得配置文件的名称等信息 name: config # 文件名 profile: dev # profile指定, config-dev.yml label: master # 分支 -
获取配置值
@Value("${ittest}") private String ittest; -
启动测试
访问http://localhost:8001/goods/findOne/2
bootstrap 和 application.yml区别
bootstrap.yml文件也是Spring Boot的默认配置文件,而且其加载的时间相比于application.yml更早。
bootstrap.yml可以理解成系统级别的一些参数配置,这些参数一般是不会变动的。application.yml 可以用来定义应用级别的参数,如果搭配 spring cloud config 使用,application.yml 里面定义的文件可以实现动态替换。总结就是,bootstrap.yml文件相当于项目启动时的引导文件,内容相对固定。application.yml文件是微服务
的一些常规配置参数,变化比较频繁。
3. config-快速入门-config client刷新
-
在 config 客户端引入 actuator 依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> -
获取配置信息类上,添加 @RefreshScope 注解
@RestController @RequestMapping("/goods") @RefreshScope // 开启刷新功能 public class GoodsController { ...... } -
添加配置
management.endpoints.web.exposure.include: refresh
management:
endpoints:
web:
exposure:
include: '*'
- 使用curl工具发送post请求
curl -X POST http://localhost:8001/actuator/refresh
4. config-集成Eureka
config-server配置:
①导入eureka client坐标
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
②eureka配置
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka
③引导类加上@EnableEurekaClient注解
package com.ittest.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableConfigServer // 启用config server功能
//@EnableDiscoveryClient
@EnableEurekaClient
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class,args);
}
}
config-client配置
①使用服务名字从配置中心获取
# 配置config-server地址
# 配置获得配置文件的名称等信息
spring:
cloud:
config:
# 配置config-server地址
# uri: http://localhost:9527
# 配置获得配置文件的名称等信息
name: config # 文件名
profile: dev # profile指定, config-dev.yml
label: master # 分支
#使用服务名字 从配置中心获取
discovery:
enabled: true
service-id: CONFIG-SERVER
management:
endpoints:
web:
exposure:
include: '*'
同时也支持动态刷新
curl -X POST http://localhost:8001/actuator/refresh
本文介绍SpringCloud Config在分布式环境下如何集中管理和维护多环境配置文件,包括快速入门、远程仓库搭建、客户端配置和集成Eureka的过程。
985

被折叠的 条评论
为什么被折叠?



