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 的核心事件
ApplicationEnvironmentPreparedEventApplicationPreparedEventApplicationStartingEventApplicationReadyEventApplicationFailedEvent
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 的工作,此后就开 ...
随机推荐
- Use of Function Arctan
Use of Function Arctan Time Limit:10000MS Memory Limit:0KB 64bit IO Format:%lld & %llu S ...
- 微信 PHP - SDK 包
下载 个人公众号谢谢各位老铁支持
- css使既有浮动又有左右margin的多个元素两端对其
两端对齐效果 如上图中红色的9个div它们中间有间距,而最左边和最右边是没有间距的,这种布局如果使用css3的flex来实现是非常简单的,而如果要使用float布局就需要一些特殊的技巧了. 实现原理 ...
- MySQL简版(一)
第一章 数据库的基本概念 1.1 数据库的英文单词 Database,简称DB. 1.2 什么是数据库? 用于存储和管理数据的仓库. 1.3 数据库的特点 持久化存储数据的.其实数据库就是一个文件系统 ...
- python+selenium实现发送一封带附件的邮件
163邮件登录首页 登录成功断言是否有退出按钮 点击退出退出登录 from selenium import webdriver import unittest import time class Vi ...
- CSS3实现三角形和对话框
这是最终实现的效果,类似于微信对话框的样式. 分析一下这个对话框的结构,由一个小三角形和一个长方形构成.长方形很容易就可以实现,重点是如何用CSS3做出一个小三角形. 一.如何生成一个三角形 总结: ...
- React Native 之react-native-sqlite-storage
npm 官网指导: https://www.npmjs.com/package/react-native-sqlite-storage 1. 执行: npm install react-native- ...
- 如何在ASP.NET Core中上传超大文件
HTML部分 <%@PageLanguage="C#"AutoEventWireup="true"CodeBehind="index.aspx. ...
- Comet OJ - Contest #4 D求和 思维题
Code: #include <bits/stdc++.h> #define setIO(s) freopen(s".in","r",stdin) ...
- [USACO08FEB]连线游戏Game of Lines
题目背景 Farmer John最近发明了一个游戏,来考验自命不凡的贝茜. 题目描述 Farmer John has challenged Bessie to the following game: ...