文章目录
1. 分布式系统存在的问题—配置问题
分布式系统意味着有好多小服务,这样系统中存在大量的服务,但是每个服务都需要配置信息才能运行,映射到SpringBoot项目中意味中有很多application.yml配置文件,很难管理,这是SpringCloud提供了ConfigServer来解决这个问题,即使用SpringServer这一个服务实现了对所有服务的配置文件进行集中式的,动态的配置
2. Config服务是什么
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同的微服务应用所有环境提供一个中心化的外部配置
3. Config服务能干什么
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置改动时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息已REST接口的形式暴露
4. 项目实战
SpringCloud Config分为服务端和客户端,服务端称为分布式配置中心,是一个独立的服务,用来连接配置服务器并为客户端提供获取配置信息,加密/解密等访问接口;客户端是通过制定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,可以理解为服务提供方provider。配置文件通常放在git上
4.1 gitHub配置
将各个配置文件放置到gitHub上,文件名为:integral-game.yml
spring:
profiles:
active: dev //@profileActive@
---
server:
port: 8090
servlet:
context-path: /game-web
spring:
profiles: dev
application:
name: integral-game-provider
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url:
#客户端注册进eureka服务列表内
defaultZone: http://192.168.22.126:7001/eureka/
instance:
preferIpAddress: true
ipAddress: 192.168.22.126
#注册到eureka后,status的名字(服务在eureka的唯一标志)
instance-id: ${spring.application.name}:${random.int}
#访问路径可以显示IP地址
prefer-ip-address: true
info:
app.name: provider-dept-8001
company.name: www.tfjybj.com
build.artifactId: $project.artifactId$
build.version: $project.version$
---
server:
port: 8090
servlet:
context-path: /game-web
spring:
profiles: test
application:
name: integral-game-provider
main:
allow-bean-definition-overriding: true
eureka:
client:
service-url:
#客户端注册进eureka服务列表内
defaultZone: http://192.168.22.227:7001/eureka/
instance:
#注册到eureka后,status的名字(服务在eureka的唯一标志)
instance-id: ${spring.application.name}:${random.int}
#访问路径可以显示IP地址
prefer-ip-address: true
info:
app.name: provider-dept-8001
company.name: www.tfjybj.com
build.artifactId: $project.artifactId$
build.version: $project.version$
4.2 Config服务端配置
4.2.1 Config服务端连接github
在Config服务端配置如下yml文件:
server:
port: 3344
spring:
application:
name: cloud-config-service
cloud:
config:
server:
git:
uri: [email protected]:zzyybs/microservicecloud-config.git #GitHub上面的git仓库名字
这样服务端就和github建立了连接,可以读取远程git仓库配置了
4.2.2 配置启动项加注解@EnableConfigServer
@EnableConfigServer
@SpringBootApplication
public class CloudConfigApplication {
public static void main(String[] args) {
SpringApplication.run(CloudConfigApplication.class, args);
}
}
4.3 客户端配置
4.3.1 application.yml配置如下:
spring:
application:
name: integral-game-provider
4.3.2 bootstrap.yml 配置如下:
spring:
cloud:
config:
name: integral-game //读取github上的那个文件对应的文件名 integral-game.yml,不带yml后缀名
#正常方式应该从github上更改
profile: dev //@profileActive@ ,拉取dev环境配置
#label: master // 从github的master
label: integral-config
uri: http://192.168.22.126:3344 // Config服务端地址
main:
allow-bean-definition-overriding: true
4.3.3 启动项加注解
@SpringBootApplication
//本服务启动后会自动注册进eureka服务中
@EnableEurekaClient
//服务发现
@EnableDiscoveryClient
public class IntegralGameProviderApplication {
public static void main(String[] args) {
SpringApplication.run(IntegralGameProviderApplication.class, args);
}
}
这注意到,读取外部文件的配置信息写在了bootstrap.yml文件中,为什么要把配置信息写在bootstrap.yml文件中那:application.yml是用户级的资源配置项;而bootstrap.yml是系统级的,优先级更高。SpringCloud会创建一个Bootstrap Context作为Spring应用的Application Context的父上下文。初始化时,Bootstrap Context负责从外部资源加载配置属性并解析配置,这两个上下文共享一个从外部获取的Environment,但Bootstrap有更高优先级,默认情况下,他们不会被本地配置覆盖。