SpringCloud - 08配置中心Config
(1)Config介绍
- 微服务意味着将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统出现大量的服务。
- 由于每个服务都需要配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
- SpringCloud提供了ConfigServer来解决这个问题。
- SpringCloud Config 为微服务架构中的微服务提供集中化的外部配置支持,
- 配置服务器为每个不同微服务应用的所有环境提供了一个中心化的外部配置。
- SpringCloud Config 分为 服务端 和 客户端两部分。
(1.1)配置中心作用
- 集中管理配置文件
- 不同环境不同配置,动态化的配置更新,分环境部署,如dev/test/prod/release
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
- 当配置发生变化时,服务不需要重启即可感知到配置的变化并应用新的配置
- 将配置信息以REST接口的形式暴露
(2)GitHub仓库放置配置文件
spring:
profiles:
active:
- dev
---
spring:
profiles: dev
application:
name: microservicecloud-config-dev
---
spring:
profiles: test
application:
name: microservicecloud-config-test
(2.1)IDEA配置GitHub, 或者使用Token
(3)构建Config Server
(3.1)POM文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 避免Config的Git插件报错: org/eclipse/jgit/api/TransportConfigCallBack -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.8.0.201706111038-r</version>
</dependency>
(3.2)application.yml 配置文件
server:
port: 3001
spring:
application:
name: microservicecloud-config
cloud:
config:
server:
git:
uri: https://github.com/XXX/microservicecloud-config.git
(3.3)主启动类
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterApplication { }
(3.4)测试: 实现用SpringCloud Config通过GitHub获取配置信息
(4)配置Config Client
(4.1)POM文件
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.8.0.201706111038-r</version>
</dependency>
(4.2)配置文件
microservicecloud-config-client.xml: 上传至GitHub仓库中
spring:
profiles:
active:
- dev
---
server:
port: 8201
spring:
profiles: dev
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka7001:7001/eureka,http://eureka7002:7002/eureka,http://eureka7003:7003/eureka
---
server:
port: 8202
spring:
profiles: test
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka7001:7001/eureka
bootstrap.yml
spring:
cloud:
config:
name: microservicecloud-config-client // 从GitHub上读取的资源名称,没有yml后缀名
profile: test // 本次访问的配置项
label: master
uri: http://config3001.com:3001/ // 本微服务启动后先去查找Config Server服务
application.yml
spring:
application:
name: microservicecloud-config-client
- application.yml是用户级的资源配置项; bootstrap.yml是系统级的,优先级更高。
- SpringCloud会创建一个"Bootstrap Context",作为Spring应用的"Application Context"的父上下文。
- 初始化时,"Bootstrap Context"负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的"Enviroment"。
- "Bootstrap"属性有高优先级,默认情况下,他们不会被本地配置覆盖。
- "Bootstrap Context"和"Application Context"有着不同的约定,新增一个"bootstrap.yml"文件,保证配置的分离。
(4.3)获取配置文件中的信息
@RestController public class ConfigClientController { @Value("${spring.application.name}") private String applicationName; @Value("${eureka.client.service-url.defaultZone}") private String eurekaServers; @Value("${server.port}") private String port; @RequestMapping("/config") public String getConfig(){ String str = "applicationNam: "+ applicationName +"\t" +"eurekaServers: "+ eurekaServers +"\t" +"port: "+ port; System.out.println("str------->" + str); return str; } }
(4.4)测试
2021-01-17 22:07:56.016 INFO 8188 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://config3001.com:3001/ 2021-01-17 22:08:01.398 INFO 8188 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Located environment: name=microservicecloud-config-client, profiles=[test], label=master, version=0332acc8bf460230c9019400d77fdb46f4d8eb47, state=null 2021-01-17 22:08:01.399 INFO 8188 --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-configClient'},
BootstrapPropertySource {name='bootstrapProperties-https://github.com/xxx/microservicecloud-config.git/microservicecloud-config-client.yml (document #2)'},
BootstrapPropertySource {name='bootstrapProperties-https://github.com/xxx/microservicecloud-config.git/application.yml (document #2)'},
BootstrapPropertySource {name='bootstrapProperties-https://github.com/xxx/microservicecloud-config.git/microservicecloud-config-client.yml (document #0)'},
BootstrapPropertySource {name='bootstrapProperties-https://github.com/xxx/microservicecloud-config.git/application.yml (document #0)'}] 2021-01-17 22:08:01.403 INFO 8188 --- [ main] c.b.s.c.client.ConfigClientApplication : No active profile set, falling back to default profiles: default 2021-01-17 22:08:01.942 INFO 8188 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=6bbbee29-c346-3f3c-87eb-45fe3a04bf74 2021-01-17 22:08:02.099 INFO 8188 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8202 (http) 2021-01-17 22:08:02.106 INFO 8188 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-01-17 22:08:02.106 INFO 8188 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.39] 2021-01-17 22:08:02.193 INFO 8188 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-01-17 22:08:02.193 INFO 8188 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 781 ms 2021-01-17 22:08:02.384 INFO 8188 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021-01-17 22:08:02.743 INFO 8188 --- [ main] o.s.b.a.e.web.EndpointLinksResolver : Exposing 2 endpoint(s) beneath base path '/actuator' 2021-01-17 22:08:02.776 INFO 8188 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8202 (http) with context path '' 2021-01-17 22:08:02.977 INFO 8188 --- [ main] c.b.s.c.client.ConfigClientApplication : Started ConfigClientApplication in 8.132 seconds (JVM running for 8.784)
(5)统一管理微服务配置
server: port: 8005 mybatis: config-location: classpath:mybatis/mybatis-config.xml type-aliases-package: com.bearpx.springcloud.entity mapper-locations: - classpath:mapper/*.xml spring:
profiles: dev application: name: microservicecloud-dept-provider datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/clouddb03?useUnicode=true&characterEncoding=UTF-8&useSSL=false username: root password: Mysql2020 dbcp2: min-idle: 10 initial-size: 10 max-total: 20 max-wait-millis: 100 eureka: client: service-url: defaultZone: http://localhost:7001/eureka,http://localhost:7002/eureka,http://localhost:7003/eureka instance: instance-id: microservice-dept-provider prefer-ip-address: true info: app.name: microservicecloud company.name: www.bearpx.com build.artifactId: ${project.artifactId} build.version: ${project.version} hystrix: metrics: enabled: true
bootstrap.yml
spring:
cloud:
config:
name: microservicecloud-config-dept-client
label: master
profile: dev
uri: http://config3001.com:3001
application.yml
info:
app.name: microservicecloud
company.name: www.bearpx.com
build.artifactId: ${project.artifactId}
build.version: ${project.version}
hystrix:
metrics:
enabled: true