微服务要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,每一个微服务自己带着一个application.yml,成千上百个配置文件的管理就比较头疼了. 所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题.
2. 是什么官网资料→
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。SpringCloud Config分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器(git或svn)并为客户端提供获取配置信息,加密/解密信息等访问接口.
客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
3. Config服务端配置 3.1 与gitHub整合配置SpringCloud Config功能
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署比如 dev/test/prod/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以REST接口的形式暴露, 使用curl访问刷新均可(post请求)
用你自己的账号在GitHub或码云上新建一个名为springcloud-config的新Repository.
将刚刚创建的远程配置仓库clone到本地. git clone https://gitee.com/wang-qz/springcloud-config.git
注意配置文件的编码, utf-8.
远程配置内容
新增配置中心服务端模块[cloud-config-center-3344].
3.2.1 依赖3.2.2 编写配置atguigu-cloud-2020 com.atguigu.springcloud 1.0-SNAPSHOT 4.0.0 cloud-config-center-3344 org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
application.yml
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://gitee.com/wang-qz/springcloud-config.git # 远程配置仓库
search-paths: springcloud-config #搜索目录
default-label: master # 读取分支
# eureka配置
eureka:
instance:
hostname: localhost
instance-id: cloud-config-center-3344
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://www.eureka01.com:7001/eureka,http://www.eureka02.com:7002/eureka
3.2.3 编写启动类
com.atguigu.springcloud.ConfigCenterApplication
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterApplication.class);
}
}
3.2.4 测试拉取远程配置信息
测试通过Config微服务是否可以从Gitee上获取配置内容, 有以下几种访问规则.
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.yml
- /{application}/{profile}[/{label}
访问 http://localhost:3344/master/config-dev.yml, 表示读取master分支的config-dev.yml文件.
访问 http://localhost:3344/config-dev.yml, 因为配置中心的配置文件中指定了默认读取分支为master, 所以访问时可以
省略label.
访问 http://localhost:3344/config/dev/master
新增Config客户端模块[cloud-config-client-3355]
3.3.1 依赖3.3.2 编写配置atguigu-cloud-2020 com.atguigu.springcloud 1.0-SNAPSHOT ../../pom.xml 4.0.0 cloud-config-client-3355 org.springframework.cloud spring-cloud-starter-config org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-devtools runtime true org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test
bootstrap.yml
server:
port: 3355
spring:
application:
name: config-client
cloud:
#Config客户端配置
config:
uri: http://localhost:3344 #配置中心地址
label: master #分支名称
name: config #配置文件名称
profile: dev #读取后缀名称
# 上述3个综合:master分支上config-dev.yml的配置文件被读取http://localhost:3344/master/config-dev.yml
#eureka配置
eureka:
instance:
hostname: localhost
instance-id: config-client-3355
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://www.eureka01.com:7001/eureka,http://www.eureka02.com:7002/eureka
3.3.3 编写启动类applicaiton.yml 和 bootstrap.yml
- applicaiton.yml是用户级的资源配置项; bootstrap.yml是系统级的,优先级更加高.
- Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的Application Context的父上下文。初始化的时候,
Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment。- Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。 Bootstrap context和Application Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap Context和Application Context配置的分离。
- 将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml
com.atguigu.springcloud.ConfigClientApplication
package com.atguigu.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class);
}
}
3.3.4 编写业务类
com.atguigu.springcloud.controller.ConfigClientController
package com.atguigu.springcloud.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
3.3.5 测试
启动3355作为Client准备访问, http://localhost:3355/configInfo , 成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息.
修改config-dev.yml配置并提交到GitHub中,比如加个变量age或者版本号version.
刷新3344,发现ConfigServer配置中心立刻响应.
刷新3355,发现ConfigClient客户端没有任何响应. 3355没有变化除非自己重启或者重新加载. 难到每次运维修改配置文件,客户端都需要重启?简直是噩梦般的存在. 为了避免每次更新配置都要重启客户端微服务3355. 下面修改Config Client.
引入actuator监控依赖
org.springframework.boot spring-boot-starter-actuator
修改YML,暴露监控端口
# 暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
业务类上添加@RefreshScope
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo() {
return configInfo;
}
}
重启config客户端后, 修改gitHub远程配置, version+1后, 访问 http://localhost:3344/master/config-dev.yml, 正常获取到修改的配置.
再次通过客户端访问获取远程配置 http://localhost:3355/configInfo
发现上面读取的配置信息还是修改之前的, 这是为什么呢?
此时, 还需要运维人员向config客户端发送一个post请求刷新配置. 使用curl或postman发送请求. 刷新缓存后读取正常.
curl -X POST http://localhost:3355/actuator/refresh
上面发送post请求刷新远程配置到config客户端报 404 错误. (curl -X POST http://localhost:3355/actuator/refresh)
网上很多资料说是SpringBoot 1.x和SpringBoot 2.x版本的actuator监控的依赖版本内容改动很大导致. 我是怎么改都不行. 也不知道什么原因. 希望以后学好英语从官方资料中找到解决方案. 另外, 我使用的SpringBoot 2.2.2.RELEASE版本.
监控端点配置暴露全部, 在启动日志中显示的还是只有2个暴露端点. 没有refresh.
spring boot 中使用 actuator 404的问题



