微服务意味着要将单体应用中的业务拆分成一个个自服务,每个服务的粒度相对较小,因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,所以一套集中式的动态的配置管理设施是必不可少的。
springcloud提供了configserver来解决这个问题,我们每一个微服务自己带一个application.yml,上百个配置的管理太繁琐了
是什么
springcloud config为微服务架构中的微服务提供集中化外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化外部配置。
springcloud config分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口
客户端则是通过指定的配置中心来管理应用资源,一级与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器来默认采用git来存储配置信息,这样有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
能干什么
集中管理配置文件
不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
运行期间动态调整配置,不再需要在每个服务器器部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
降配置信息以rest接口的形式暴露
与github整合配置
config服务端配置
添加依赖
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
yml配置文件
application.yml
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
uri: https://github.com/gwh117/cloud-config.git #github 上的仓库名称
search-paths:
- cloud-config #仓库目录(存放配置文件的目录)
label: main #读取分支
eureka:
client:
service-url:
defaultZone: http://server1:7001/eureka
主启动类开启注解
@SpringBootApplication
@EnableConfigServer
public class Cloud_config {
public static void main(String[] args) {
SpringApplication.run(Cloud_config.class,args);
}
}
config客户端配置
bootstrap.yml与application.yml
bootstrap.yml是系统级,优先度更高
application.yml是用户级的资源配置项
springcloud会创建一个“bootstrap context”,作为spring应用的“application context”的父上下文,初始化的时候,‘bootstrap context’负责从外部源加载配置属性并解析配置,这两个上下文共享一个外部回去的‘evironment’。
‘bootstrap’属性右高优先级,默认情况下,不会被本地配置覆盖,‘bootstrap context’和‘application context’有着不同的约定,所以新增了一个‘bootstrap.yml’,保证‘bootstrap context’和‘application context’和‘application context’配置分离。
要将客户端模块下的application.yml文件改为bootstrap.yml是非常关键的,因为bootstrap.yml是比application.yml先加载,bootstrap.yml优先级高于application.yml
pom.xml文件
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.gwh</groupId>
<artifactId>common-api-cloud</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
bootstrap.yml
server:
port: 3355
spring:
application:
name: cloud-config-client
cloud:
config:
label: main #分支名称
name: application #配置文件名称-前面的
profile: dev #配置文件名称-后边的 githu上main分支下application-dev.yml配置文件
uri: http://localhost:3344 #配置中心地址
eureka:
client:
service-url:
defaultZone: http://server1:7001/eureka
主启动类
@SpringBootApplication
@EnableEurekaClient
public class CloudConfig {
public static void main(String[] args) {
SpringApplication.run(CloudConfig.class,args);
}
}
无法动态实时更新获取github配置文件
github修改配置文件微服务客户端需要重新启动修改后的配置文件才能生效
解决方案
客户端的动态刷新
添加新的配置
bootstrap.yml
server:
port: 3355
spring:
application:
name: cloud-config-client
cloud:
config:
label: main #分支名称
name: application #配置文件名称-前面的
profile: dev #配置文件名称-后边的 githu上main分支下application-dev.yml配置文件
uri: http://localhost:3344 #配置中心地址
eureka:
client:
service-url:
defaultZone: http://server1:7001/eureka
management:
endpoints:
web:
exposure:
include: "*"
在业务类添加注解@RefreshScope
@RestController
@RefreshScope
public class TestController {
@Value("${config}")
private String info;
/**
* 测试配置文件是否引入
* @return
*/
@GetMapping("getInfo")
public String getInfo(){
return info;
}
}
需要在命令处post请求刷新客户端
curl -X POST “http:localhost:3355/actuator/refresh”
C:\Users\Administrator>curl -X POST “http://localhost:3355/actuator/refresh”
[“config.client.version”,“config”]