Spring Boot系列(四) Spring Cloud 之 Config Client
Config 是通过 PropertySource 提供. 这节的内容主要是探讨配置, 特别是 PropertySource 的加载机制.
Spring Cloud 技术体系
- 分布式配置
- 服务注册/发现
- 路由
- 服务调用
- 负载均衡
- 短路保护
- 分布式消息
Spring 事件机制
设计模式
- 观察者模式(发布/订阅)
API: java.util.Observable
, java.util.Observer
发布者通过 observable.notifyObservers()
通知, 订阅者是被动感知的. 这种模式是推模式. 而迭代器Iterator 是拉模式, 通过循环主动获取.
- 事件/监听器模式
API: 类 java.util.EventObject
, 接口 java.util.EventListener
, 此接口是一个标识接口, 无方法.
Spring 事件/监听
ApplicationEvent
: 事件. 扩展了 EventObject
.
ApplicationListener
: 事件监听器, 扩展了 EventListener
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
// 注册监听器
context.addApplicationLinstener(new ApplicationListener<MyApplicationEvent>(){
@Override
public void onApplicationEvent(MyApplicationEvent event){
System.out.println("接收到事件: " + event.getSource());
}
});
context.refresh();
// 发布事件
context.publishEvent(new MyApplicationEvent("test event"));
- 应用场景
MyApplicationEvent
可以通过构造器等注入 context
对象, 从而能拿到工厂中的任意 bean 对象.
Spring Boot 的核心事件
ApplicationEnvironmentPreparedEvent
ApplicationPreparedEvent
ApplicationStartingEvent
ApplicationReadyEvent
ApplicationFailedEvent
Spring Boot 事件/监听器
ConfigFileApplicationListener
管理配置文件, 如application-{profile}.properties
或 yaml 格式的文件. 通过下面入口函数找, 可以发现加载配置文件的Loader.load()
方法
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) {
onApplicationEnvironmentPreparedEvent(
(ApplicationEnvironmentPreparedEvent) event);
}
if (event instanceof ApplicationPreparedEvent) {
onApplicationPreparedEvent(event);
}
}
在 /META-INF/spring.factories
文件中配置了需要加载的监听器, 其中包括了 ConfigFileApplicationListener
ConfigFileApplicationListener
实现了Orderd
接口 , 用来控制顺序.
Spring Cloud 事件/监听器
BootstrapApplicationListener
第一, 负责加载bootstrap.properties
或 yaml 格式的文件. 第二, 负责加载ApplicationContext
名为bootstrap
(ConfigurableApplicationContext context = builder.run()
).
自定义配置属性
- 继承
PropertySourceLocator
- 向自定义类注解添加
@Ordered
,@Configuration
- 添加配置到
spring.factories
文件, key为org.springwork.cloud.bootstrap.BootstrapConfiguration
Environment
允许出现同名配置, 优先级高的胜出.
MutablePropertySources
使用了CopyOnWriteArrayList
数据结构
public class MutablePropertySources implements PropertySources {
// 使用了 CopyOnWriteArrayList
private final List<PropertySource<?>> propertySourceList = new CopyOnWriteArrayList<>();
...
}
Spring Boot系列(四) Spring Cloud 之 Config Client的更多相关文章
- Spring Boot系列(四) Spring Boot 之验证
这节没有高深的东西, 但有一些学习思路值得借鉴. JSR 303 (Bean Validation) Maven依赖 <dependency> <groupId>org.spr ...
- 【spring boot 系列】spring data jpa 全面解析(实践 + 源码分析)
前言 本文将从示例.原理.应用3个方面介绍spring data jpa. 以下分析基于spring boot 2.0 + spring 5.0.4版本源码 概述 JPA是什么? JPA (Java ...
- Spring Boot系列(一) Spring Boot准备知识
本文是学习 Spring Boot 的一些准备知识. Spring Web MVC Spring Web MVC 的两个Context 如下图所示, 基于 Servlet 的 Spring Web M ...
- Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作
开发环境延续上一节的开发环境这里不再做介绍 添加mybatis依赖 <dependency> <groupId>org.mybatis.spring.boot</grou ...
- Spring Boot系列(三) Spring Boot 之 JDBC
数据源 类型 javax.sql.DataSource javax.sql.XADataSource org.springframework.jdbc.datasource.embedded,Enbe ...
- Spring Boot系列二 Spring @Async异步线程池用法总结
1. TaskExecutor Spring异步线程池的接口类,其实质是java.util.concurrent.Executor Spring 已经实现的异常线程池: 1. SimpleAsyncT ...
- 二、Spring Boot系列:Spring Initializer快速创建Spring Boot项目
1.点击创建新工程 2.选择Spring Initializer和jdk1.8 注意:项目名称要小写字母 3.现在只需要一个创建一个web,选择一个就好 4.没有用的文件,可以删除 5.pom.xml ...
- Spring Boot系列(二) Spring Boot 之 REST
Rest (Representational Stat Transer) 是一种软件架构风格. 基础理论 架构特性 性能 可伸缩 简化的统一接口 按需修改 组件通信透明 可移植 可靠性 架构约束 C/ ...
- Spring Boot 系列总目录
一.Spring Boot 系列诞生原因 上学那会主要学的是 Java 和 .Net 两种语言,当时对于语言分类这事儿没什么概念,恰好在2009年毕业那会阴差阳错的先找到了 .Net 的工作,此后就开 ...
随机推荐
- 长短时间记忆的中文分词 (LSTM for Chinese Word Segmentation)
翻译学长的一片论文:Long Short-Term Memory Neural Networks for Chinese Word Segmentation 传统的neural Model for C ...
- const定义的并非是常量,而是常量索引
我第一次看const的时候,记忆中对const的定义是,定义常量. 后经过研究,定义的并非常量,而是常量索引. 有时候会遇到使用const定义数组的情况 const arr = [] arr.push ...
- LockSupport详解
concurrent包是基于AQS (AbstractQueuedSynchronizer)框架的,AQS框架借助于两个类: Unsafe(提供CAS操作) LockSupport(提供park/un ...
- 去掉input密码框自动补全功能
<input name="password" autocomplete="off" hidden> <input type="pas ...
- MySQL导出和导入
MySQL的几句脚本 最近做了几次mysql的备份和恢复, 找了一些资料, 写了一些脚本, 记录一下. #导出 mysqldump $_login_info_ $_src_db_name_ --no- ...
- H5 FileReader对象
前言:FileReader是一种异步文件读取机制,结合input:file可以很方便的读取本地文件. input:file 在介绍FileReader之前,先简单介绍input的file类型. < ...
- linux运维、架构之路-CentOS6.9安装Zabbix3.4.1
一.LAMP环境安装 1.环境 [root@m01 ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@m01 ~]# uname ...
- React Native 之SectionList
接上一篇: /pages/SectionListDemo.js import React, {Fragment,Component} from 'react'; import { SafeAreaVi ...
- spring boot不要放在tomcat下启动,因为自身就带了集成tomcat
spring boot不要放在tomcat下启动,因为自身就带了集成tomcat
- Quartus_II官方教程-中文版之SignalTap II
非常实用 187-196 第十二章:调试 Quartus_II官方教程-中文版.pdf