Spring Cloud Config Client原理讲解

# Spring Cloud Config Client

## 预备知识

### 发布/订阅模式

`java.util.Observable` 是一个发布者

`java.util.Observer` 是订阅者

发布者和订阅者:1 : N

发布者和订阅者:N : M

### 事件/监听模式

`java.util.EventObject` :事件对象

? * 事件对象总是关联着事件源(source)

`java.util.EventListener` :事件监听接口(标记)

## Spring 事件/监听

`ApplicationEvent` : 应用事件

`ApplicationListener` : 应用监听器

### Spring Boot 事件/监听器

#### ConfigFileApplicationListener

管理配置文件,比如:`application.properties` 以及 `application.yaml`

`application-{profile}.properties`:

profile = dev 、test

1. `application-{profile}.properties`
2. application.properties

Spring Boot 在相对于 ClassPath : /META-INF/spring.factories

Java SPI : `java.util.ServiceLoader`

Spring SPI:

Spring Boot "/META-INF/spring.factories"

```properties
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener,\
org.springframework.boot.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.logging.LoggingApplicationListener
```

##### 如何控制顺序

实现`Ordered` 以及 标记`@Order`

在 Spring 里面,数值越小,越优先

### Spring Cloud 事件/监听器

#### BootstrapApplicationListener

Spring Cloud "/META-INF/spring.factories":

```properties
# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.cloud.bootstrap.BootstrapApplicationListener,\
org.springframework.cloud.bootstrap.LoggingSystemShutdownListener,\
org.springframework.cloud.context.restart.RestartListener
```

> 加载的优先级 高于 `ConfigFileApplicationListener`,所以 application.properties 文件即使定义也配置不到!
>
> 原因在于:
>
> `BootstrapApplicationListener ` 第6优先
>
> `ConfigFileApplicationListener` 第11优先

1. 负责加载`bootstrap.properties` 或者 `bootstrap.yaml`
2. 负责初始化 Bootstrap ApplicationContext ID = "bootstrap"

```java
ConfigurableApplicationContext context = builder.run();
```

Bootstrap 是一个根 Spring 上下文,parent = null

> 联想 ClassLoader:
>
> ExtClassLoader <- AppClassLoader <- System ClassLoader -> Bootstrap Classloader(null)

#### ConfigurableApplicationContext

标准实现类:`AnnotationConfigApplicationContext`

### Env 端点:`EnvironmentEndpoint`

`Environment` 关联多个带名称的`PropertySource`

可以参考一下Spring Framework 源码:

`AbstractRefreshableWebApplicationContext`

```java
protected void initPropertySources() {
ConfigurableEnvironment env = getEnvironment();
if (env instanceof ConfigurableWebEnvironment) {
((ConfigurableWebEnvironment) env).initPropertySources(this.servletContext, this.servletConfig);
}
}
```

`Environment` 有两种实现方式:

普通类型:`StandardEnvironment`

Web类型:`StandardServletEnvironment`

`Environment`

- `AbstractEnvironment`

? - `StandardEnvironment`

Enviroment 关联着一个`PropertySources` 实例

`PropertySources` 关联着多个`PropertySource`,并且有优先级

其中比较常用的`PropertySource` 实现:

Java System#getProperties 实现: 名称"systemProperties",对应的内容 `System.getProperties()`

Java System#getenv 实现(环境变量): 名称"systemEnvironment",对应的内容 `System.getProperties()`

关于 Spring Boot 优先级顺序,可以参考:https://docs.spring.io/spring-boot/docs/2.0.0.BUILD-SNAPSHOT/reference/htmlsingle/#boot-features-external-config

#### 实现自定义配置

1. 实现`PropertySourceLocator`

2. 暴露该实现作为一个Spring Bean

3. 实现`PropertySource`:

```java
public static class MyPropertySourceLocator implements PropertySourceLocator {

@Override
public PropertySource<?> locate(Environment environment) {
Map<String, Object> source = new HashMap<>();
source.put("server.port","9090");
MapPropertySource propertySource =
new MapPropertySource("my-property-source", source);
return propertySource;
}
}
```

4. 定义并且配置 /META-INF/spring.factories:

```properties
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.gupao.springcloudconfigclient.SpringCloudConfigClientApplication.MyPropertySourceLocator
```

注意事项:

`Environment` 允许出现同名的配置,不过优先级高的胜出

内部实现:`MutablePropertySources` 关联代码:

```java
List<PropertySource<?>> propertySourceList = new CopyOnWriteArrayList<PropertySource<?>>();
```

propertySourceList FIFO,它有顺序

可以通过 MutablePropertySources#addFirst 提高到最优先,相当于调用:

`List#add(0,PropertySource);`

原文地址:https://www.cnblogs.com/dszazhy/p/11509277.html

时间: 2024-08-01 04:12:30

Spring Cloud Config Client原理讲解的相关文章

Spring Cloud Config 入门

1.    简介 Spring Cloud Config 是用来为分布式系统中为微服务应用提供集中化的外部配置支持,主要分为Spring Cloud Config Server(服务器端)和Spring Cloud Config Client(客户端). 2.    Spring Cloud Config Server Spring Cloud Config Server为服务器端,它是一个单独的微服务应用,用来连接配置仓库(本文使用的是git仓库)并为客户端获取配置信息. 1.     首先,

开始Spring Cloud Config

什么是Spring Cloud Config Spring Cloud Config项目提供了一个解决分布式系统的配置管理方案.它包含了Client和Server两个部分. Spring Cloud Config Sever的管理git或svn的外部配置,集中配置到所有客户端. Spring Cloud Config Client根据Spring框架的Environment和PropertySource从Spring Cloud Config Sever获取配置. 所有要开始Spring Clo

Spring Cloud搭建手册(2)——Spring Cloud Config

※在Dalston.SR2版本以后,均不能正常加密,如果必须使用此功能,需要降级到SR1或Camden SR7. 1.首先需要创建一个config-server工程,作为配置中心的服务器,用来与git.svn或者本地仓库连接,从仓库获取配置文件 ① config-server工程的POM文件需要增加以下依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>sprin

Spring Cloud Config 配置中心 自动加解密功能

使用  jasypt-spring-boot-starter  进行加解密功能. 整个流程说明: 配置一个 spring cloud config server ,将要使用的配置文件存放到github上,然后从这个配置源拿配置. 我们使用 jasypt 进行自动加解密,将需要加密的数据,通过jasypt进行加密,然后将该内容放入 github.如下图: 使用 ENC() 将加密后的原文包裹,这样spring cloud config client 客户端拿到这个串之后,会自动解密,拿到原文. 下

Spring cloud config综合整理

Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持.使用Config Server,您可以在所有环境中管理应用程序的外部属性.客户端和服务器上的概念映射与Spring Environment和PropertySource抽象相同,因此它们非常适合Spring应用程序,但可以与任何语言运行的任何应用程序一起使用.当应用程序通过部署管道从开发到测试并进入生产时,您可以管理这些环境之间的配置,并确保应用程序具有迁移时需要运行的所有内容.服务器存储后端的默认实现使用g

Eureka 系列(03)Spring Cloud 自动装配原理

Eureka 系列(03)Spring Cloud 自动装配原理 [TOC] 本文主要是分析 Spring Cloud 是如何整合 Eureka 的,但不会具体分析 Eureka 的源码,之后的文章会对 Eureka 的源码做一个比较具体的分析. 1. Eureka Client 自动装配 org.springframework.boot.autoconfigure.EnableAutoConfiguration=org.springframework.cloud.netflix.eureka.

Spring Cloud Config 分布式配置中心使用教程

一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中.在spring cloud config 组件中,分两个角色,一是config server,二是config client. 二.构建Config Server 创建一个spring-boot项目,取名为config-s

spring cloud 入门系列七:基于Git存储的分布式配置中心--Spring Cloud Config

我们前面接触到的spring cloud组件都是基于Netflix的组件进行实现的,这次我们来看下spring cloud 团队自己创建的一个全新项目:Spring Cloud Config.它用来为分布式系统中的基础设施和微服务提供集中化的外部配置支持,分为服务端和客户端两个部分. 其中服务端也称为分布式配置中心,他是独立的微服务应用,用来连接配置仓库并为客户端提供获取接口(这些接口返回配置信息.加密.解密信息等): 客户端是微服务架构中的各个微服务应用或基础设施,它们通过制定的配置中心来管理

Spring Cloud Config 配置中心

1.构建config-server 创建一个pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://