概述
分布式系统面临的-配置文件的问题?
微服务意味着要将单体应用中的业务拆分成一个个子服务, 每个服务的粒度相对较小,因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,所以一套集中式的,动态的配置管理设施是必不可少的。 SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务 自己带着一个application.yml, 那上百的的配置文件要修改起来,岂不是要发疯!
什么是SpringCloud config分布式配置中心?
Spring Cloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环节提供了一个中心化的外部配置。 Spring Cloud Config 分为服务端和客户端两部分:
服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息。配置服务器默认采用git来存储配置信息,有助于对环境配置进行版本管理。并且可以通过git客户端工具来方便管理和访问配置中心
SpringCloud config分布式配置中心能干嘛
集中管理配置文件 不同环境,不同配置,动态化的配置更新,分环境部署,比如/dev /test/ /prod /beta /release - 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉配置自己的信息。 当配置发生变动时,服务不需要重启,即可感知到配置的变化,并应用新的配置
把所以服务(项目)的配置文件放到GitHub上,通过ConfigServer连接GitHub 实现配置与编码解耦 项目不需要编写配置文件,只需要连接ConfigServer,ConfigServer连接的是GitHub的远程仓库
测试
已知:客户端、服务端、远程仓库
1. 远程仓库配置
新建仓库,拉取到本地,并在本地编辑application.yml配置文件上传到仓库
spring :
profiles :
active : dev
---
spring :
profiles : dev
application :
name : springcloud- config- dev
---
spring :
profiles : test
application :
name : springcloud- config- test
2. 服务端配置
新建服务端springboot项目,导入服务端需要的jar包
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-config-server</ artifactId>
< version> 2.2.7.RELEASE</ version>
</ dependency>
</ dependencies>
在配置文件中连接远程仓库
server :
port : 3344
spring :
application :
name : springcloud- config- server
cloud :
config :
server :
git :
uri : https: //gitee.com/onmycode/hand- springcloud- config.git
编写启动类,开启ConfigServer
@SpringBootApplication
@EnableConfigServer
public class Config_Server_3344 {
public static void main ( String[ ] args) {
SpringApplication. run ( Config_Server_3344. class , args) ;
}
}
启动程序,访问配置文件(不需要controller层)
3. 客户端配置
本地新建application.yml文件,并推送到远程仓库
spring :
profiles :
active : dev
---
server :
port : 8201
spring :
profiles : dev
application :
name : springcloud- dev
eureka :
client :
service-url :
defaultZone : http: //eureka7001.com: 7001/eureka/
---
server :
port : 8202
spring :
profiles : test
application :
name : springcloud- test
eureka :
client :
service-url :
defaultZone : http: //eureka7001.com: 7001/eureka/
新建客户端springboot项目,导入jar包
< dependencies>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-config</ artifactId>
< version> 2.2.7.RELEASE</ version>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
</ dependencies>
编写配置类,为了使本地配置的优先级大于远程,增加系统级配置文件bootstrap.yml文件
spring :
cloud :
config :
uri : http: //localhost: 3344
name : config- client
profile : dev
label : master
spring :
application :
name : springcloud- config- client
编写controller层,通过controller接口,获取需要读取的几个重要信息,通过注解绑定远程配置文件的属性值
@RestController
public class ConfigClientController {
@Value ( "${spring.application.name}" )
private String applicationName;
@Value ( "${eureka.client.service-url.defaultZone}" )
private String eurekaServer;
@Value ( "${server.port}" )
private String port;
@RequestMapping ( "/config" )
public String getConfig ( ) {
return "applicationName" + applicationName+
"eurekaServer" + eurekaServer+
"port" + port;
}
}
启动配置类,会发现端口号会根据bootstrap.yml中的profile属性所指定的环境的端口号来启动,本地端口号会失效,尝试访问接口