前言
微服务将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,由于每个服务都需要配置信息才能运行,所以一套集中式的、动态的配置管理设施必不可少。
Spring Cloud提供了ConfigServer来解决这个问题。
Spring Cloud Config 是什么
一、概念
Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持,它分为Config Server和Config Client,Config Server为所有环境中的应用程序管理其外部属性,非常适合spring应用,也可以使用在其他语言的应用上。
二、服务端和客户端
1.服务端也称为分布式配置中心,是一个独立的微服务应用,用来连接配置服务器并未客户端提供获取配置信息,加密/解密信息等访问接口。
2.客户端是通过制定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动时从配置中心获取和加载配置信息。
3.配置服务器默认采用git来存储配置信息,这样有助于对环境配置进行版本管理,可以通过git客户端工具来方便管理和访问配置内容。
Config做什么
- 集中管理配置文件;
- 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release;
- 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置信息;
- 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置;
- 将配置信息以REST接口形式暴露。
Config服务端配置
一、GitHub网站创建仓库-microservicecloud-config
1.创建仓库名--microservicecloud-config
2. 复制一下项目的地址,方便clone到本地磁盘
我选择的是HTTPS,这种方式不用配置密钥
3.本地磁盘克隆仓库代码
git clone https://github.com/FirstComeHere/microservicecloud-config.git
二、在克隆后的代码文件夹下创建配置文件--application.yml
注意:(缩进使用Tab,冒号后使用空格,否则后面无法获取git服务器配置文件)否则会报错
spring:
profiles:
active:
- dev
---
spring:
profiles: dev #开发环境
application:
name: microservicecloud-config-atguigu-dev
---
spring:
profiles: test #测试环境
application:
name: microservicecloud-config-atguigu-test
三、配置文件上传gitHub服务器
git add .
git commit -m 'init'
git push origin master
四、创建Config服务-microservicecloud-config-3344
1.pom.xml文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-config-3344</artifactId>
<dependencies>
<!-- springCloud Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</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-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</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-test</artifactId>
</dependency>
<!-- 热部署插件 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
2.配置文件application.yml
uri就是我们配置的GitHub仓库的地址,我使用的是https。
server:
port: 3344
spring:
application:
name: microservicecloud-config
cloud:
config:
server:
git:
uri: https://github.com/FirstComeHere/microservicecloud-config.git #GitHub上面的git仓库名字
3.主启动类注解
@EnableConfgiServer
@SpringBootApplication
@EnableConfigServer
public class Config_3344_StartSpringCloudApp
{
public static void main(String[] args)
{
SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
}
}
4.启动服务,查看效果
输入地址多种格式:
http://localhost:3344/application-dev.yml
http://localhost:3344/application/dev/master
http://localhost:3344/master/application-dev.yml
动态切换环境dev/test
Config客户端配置
一、在本地仓库代码新建配置文件--microsevicecloud-config-client.yml
代码如下,使用git命令上传到服务器
spring:
profiles:
active:
- dev
---
server:
port: 8203
spring:
profiles: dev #开发环境
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-dev.com:7001/eureka/
---
server:
port: 8205
spring:
profiles: test
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka-test.com:7001/eureka/
二、新建客户端服务--microservicecloud-config-client-3355
1.pom文件
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-config-client-3355</artifactId>
<dependencies>
<!-- SpringCloud Config客户端 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</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-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
2.两个配置文件-application.yml 和bootstrap.yml
application.yml是用户级的资源配置项,bootstrap.yml是系统级的,优先级更高。
Spring Cloud会创建一个Boostrap Context,作为Spring应用的Application Context的父上下文,初始化的时候,Boostrap Context负责从外部源加载配置属性并解析配置,这两个上下文共享一个从外部获取的Environment,BootStrap属性有高优先级,默认情况下,不会被本地配置覆盖。
一个boostrap.yml文件,可以保证Boostrap Context和Application Context配置的分离。
(1)application.yml
spring:
application:
name: microservicecloud-config-client
(2)bootstrap.yml
服务启动时,就会从Config 服务端去获取配置信息。具体环境文件,是根据profile属性值决定的。
spring:
cloud:
config:
name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名
profile: test #本次访问的配置项
label: master
uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
3.添加一个测试接口,验证获取配置信息
package com.atguigu.springcloud.rest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientRest
{
@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 = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
System.out.println("******str: " + str);
return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
}
}
三、 测试客户端从Config服务端拉取配置信息
测试地址:http://localhost:8205/config
开发地址:http://localhost:8203/config
感谢您的访问!