SpringCloud Config分布式配置中心
微服务意味着要将单体应用中的业务拆分成一个个自服务,每个服务的粒度相对较小,因此系统中会出现大量服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
SpringCloud提供了ConfigServer来解决这个问题。
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。
分为服务端和客户端两部分。
服务端也称为分布式配置中心,它是一个独立的微服务应用
作用:
- 集中管理配置文件
- 不同环境不同配置,同台华的配置更新,分环境部署比如dev/test/prod/beta/release
- 运行期期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会将配置中心统一拉取配置自己的信息
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以REST接口的形式暴露
与GitHub整合配置,由于SpringCloud Config默认使用Git来存储配置文件(也有其他方式,比如支持SVN和本地文件),但最推荐用Git,而且是月hi用的是http/https访问的形式
新建cloud-config-center-3344
(提前在github上建好仓库和文件)
写pom
<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.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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--自定义的api包-->
<dependency>
<groupId>com.huangyy.cloud2021</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
写yml
周阳老师这里写的是uri: git@github.com:xxxxx/springcloud-config.git
但我不行,找了一下,说改成https://,试了可以,但不知道对不对
server:
port: 3344
spring:
application:
name: cloud-config-center
cloud:
config:
server:
git:
# uri: git@github.com:xxxxxxx/springcloud-config.git
uri: https://github.com/xxxxxxxx/springcloud-config.git
search-paths:
- springcloud-config
label: main
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/
主启动
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
public static void main(String[] args) {
SpringApplication.run(ConfigCenterMain3344.class,args);
}
}
windows下修改hosts增加映射,127.0.0.1 config-3344.com
客户端cloud-config-client-3355
写pom
<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.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>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--自定义的api包-->
<dependency>
<groupId>com.huangyy.cloud2021</groupId>
<artifactId>cloud-api-commons</artifactId>
<version>${project.version}</version>
</dependency>
<!--eureka-client-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
写yml
- bootstrap.yml是系统级的,优先级更高
- application.yml是用户级的资源配置项
这里要将Client模块下的application.yml改为bootstrap.yml
server:
port: 3355
spring:
application:
name: config-client
cloud:
config:
label: main #分支名称
name: config #配置文件名称,如config-dev.yml的config
profile: dev #配置文件后缀名称,如config-dev.yml的dev
uri: http://localhost:3344 #配置中心地址
#综上http://config-3344.com:3344/main/config-dev.yml
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka
主启动:
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientMain3355 {
public static void main(String[] args) {
SpringApplication.run(ConfigClientMain3355.class,args);
}
}
controller
@RestController
public class ConfigClientController {
@Value("${config.info}")
private String configInfo;
@GetMapping("/configInfo")
public String getConfigInfo(){
return configInfo;
}
}
客户端值动态刷新
假设运维修改了GitHub上的配置文件内容做调整
之后产生了问题,3344访问,数据已变,但3355怎么刷新都不会变,除非其重启
修改3355,pom中引入(之前我就引了):
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
改yml
#暴露监控端点
management:
endpoints:
web:
exposure:
include: "*"
在controller类上加@RefreshScope
还需要发个POST请求刷新
又有问题,若有多个微服务客户端,需多次手动刷新。可否广播,一次通知,处处生效。
所以引入下一节,消息总线。