目录:

  • 配置中心简介
  • SpringCloud Config服务端
  • SpringCloud Config客户端
  • 动态配置属性bean
  • 一些补充(源码分析):Spring事件监听、健康检查health()、高可用的分布式配置中心

配置中心简介:

1、什么是配置中心

从字面意思上来说,配置中心就是管理程序配置的一个公共服务;它管理了系统业务相关的配置内容,在系统启动时去加载这些数据。

2、使用配置中心的好处

)统一管理配置的格式,可以更加有效的维护配置

)让私密配置更安全(配置不在项目中,而是在配置中心,使生产配置不可见)

)。。。。。。

SpringCloud Config服务端:

1、添加maven依赖

 <dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>

2、启动类加上@EnableConfigServer注解

3、配置properties

 # 配置中心实例名
spring.application.name=config-server
# 端口
server.port=9090 ## 远程仓库配置
# git仓库地址
spring.cloud.config.server.git.uri=https://github.com/xxx/xxx
# 本地备份
spring.cloud.config.server.git.basedir=git-config ## 本地仓库(也可以不读远端git、svn等,直接读本地配置)
# spring.profiles.active=native
# spring.cloud.config.server.native.search-locations=file:///E:/SpringCloud Config ## Actuator
# 关闭安全校验
management.security.enabled=false

SpringCloud Client:

1、添加maven依赖

1 <dependency>
2 <groupId>org.springframework.cloud</groupId>
3 <artifactId>spring-cloud-config-client</artifactId>
4 </dependency>

2、配置bootstrap.properties、application.properties

1 ## Config Server配置信息
2 ## 配置中心的服务地址
3 spring.cloud.config.uri=http://localhost:9090/
4 ## Environment 的 application 客户端的应用名称
5 spring.cloud.config.name=test
6 ## spring.profiles.active配置
7 spring.cloud.config.profile=dev
8 ## 配置的版本(git/svn 分支)
9 spring.cloud.config.label=master
1 ## 客户端的实例名
2 spring.application.name=config-server
3 ## 客户端提供端口
4 server.port=8080

动态刷新配置:

首先我们知道SpringCloud Config分为服务端和客户端,服务端用于拉取远端的配置,客户端用于拉取服务端配置以供应用使用,那么一次刷新配置的过程应该有以下几点:

1、服务端拉取最新的git配置(只要获取一次数据就会拉取远端的数据)

2、客户端拉取服务端的配置(通过调用public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke()方法让客户端获取服务端最新的配置)

3、刷新bean(此时我们会发现尽管客户端的数据已经刷新了,但为什么我们配置的数据还是旧值呢,原因是spring已经将这个bean注入了,所以我们也需要刷新这个bean:@RefreshScope)

说道这里我们就会有一个疑问,难道我们每次刷新客户端的配置都需要手动curl这个invoke()方法???

答案是否定的,我们可以有很多方法,这里列举一个简单的定时器(那定时器应该执行些什么呢,我们来进一步看看invoke()的源码)。

1、我们看public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint.invoke(),得知invoke调用父类的invoke,我们再看下父类的invoke;

 @RequestMapping(method = RequestMethod.POST)
@ResponseBody
@Override
public Object invoke() {
if (!getDelegate().isEnabled()) {
return new ResponseEntity<>(Collections.singletonMap(
"message", "This endpoint is disabled"), HttpStatus.NOT_FOUND);
}
return super.invoke();
}

2、父类invoke >>> public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()调用其父类invoke;

 @Override
@ActuatorGetMapping
@ResponseBody
public Object invoke() {
return super.invoke();
}

3、protected java.lang.Object org.springframework.boot.actuate.endpoint.mvc.AbstractEndpointMvcAdapter.invoke()调用一个对象的invoke,而这个对象时通过构造方法传进来的;我们在一步步看构造方法的delegate是从哪来的

 private final E delegate;

 public AbstractEndpointMvcAdapter(E delegate) {
Assert.notNull(delegate, "Delegate must not be null");
this.delegate = delegate;
} protected Object invoke() {
if (!this.delegate.isEnabled()) {
// Shouldn't happen - shouldn't be registered when delegate's disabled
return getDisabledResponse();
}
return this.delegate.invoke();
}

4、通过一步步网上看,发现是最开始的public java.lang.Object org.springframework.cloud.endpoint.GenericPostableMvcEndpoint,然后我们看看哪里有调用GenericPostableMvcEndpoint的构造

从图中我们可以看出有三处调用,而因为我们是刷新操作,我们就大胆的猜测是第一个refreshMvcEndpoint,我们在点进去看看。

发现入参是org.springframework.cloud.endpoint.RefreshEndpoint,我们看看RefreshEndpoint是怎样实现的。

 @ConfigurationProperties(prefix = "endpoints.refresh", ignoreUnknownFields = false)
@ManagedResource
public class RefreshEndpoint extends AbstractEndpoint<Collection<String>> { private ContextRefresher contextRefresher; public RefreshEndpoint(ContextRefresher contextRefresher) {
super("refresh");
this.contextRefresher = contextRefresher;
} @ManagedOperation
public String[] refresh() {
Set<String> keys = contextRefresher.refresh();
return keys.toArray(new String[keys.size()]);
} @Override
public Collection<String> invoke() {
return Arrays.asList(refresh());
} }

从中的我们可以看出刷新原来调用的就是RefreshEndpoint的invoke方法,而Set<String> keys = contextRefresher.refresh()便是刷新时所执行的函数!!!!!!∑(゚Д゚ノ)ノ

综上所述:我们的job中调用Set<String> keys = contextRefresher.refresh()这段代码即可刷新配置啦!!!

Spring事件监听模式:

Spring的事件监听模式的实质就是观察者模式,其主要分为两个类

1、ApplicationListener(监听器)>>> public interface ApplicationListener<E extends ApplicationEvent> extends EventListener

2、ApplicationEvent(监听对象)>>> public abstract class ApplicationEvent extends EventObject

3、自定义监听器

 public class SpringEventDemo {

     public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.addApplicationListener(event -> System.err.println("监听到事件" + event.getSource())); context.refresh();
context.publishEvent(new MyApplicationEvent("event1"));
context.publishEvent(new MyApplicationEvent("event2"));
}
} class MyApplicationEvent extends ApplicationEvent { public MyApplicationEvent(Object source) {
super(source);
}
}

健康检查health():

健康检查health,主要作用是对当前系统的运行态进行展示的一个接口,我们可以定制自己系统的健康检查,也可以使用已提供好的。

如果我们要实现自己的健康检查,那应该如何实现呢,我们先看下health()的源码。

1、我们从启动日志中可以看到health()接口是调用:

public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(javax.servlet.http.HttpServletRequest,java.security.Principal)方法

我们跟进去看看

 @ActuatorGetMapping
@ResponseBody
public Object invoke(HttpServletRequest request, Principal principal) {
if (!getDelegate().isEnabled()) {
// Shouldn't happen because the request mapping should not be registered
return getDisabledResponse();
}
Health health = getHealth(request, principal);
HttpStatus status = getStatus(health);
if (status != null) {
return new ResponseEntity<Health>(health, status);
}
return health;
}

2、从上述代码中可以看出invoke的主要逻辑在第8行,我们继续跟进

 private Health getHealth(HttpServletRequest request, Principal principal) {
Health currentHealth = getCurrentHealth();
if (exposeHealthDetails(request, principal)) {
return currentHealth;
}
return Health.status(currentHealth.getStatus()).build();
}

同理主要代码是第2行,继续跟进

 private Health getCurrentHealth() {
long accessTime = System.currentTimeMillis();
CachedHealth cached = this.cachedHealth;
if (cached == null || cached.isStale(accessTime, getDelegate().getTimeToLive())) {
Health health = getDelegate().invoke();
this.cachedHealth = new CachedHealth(health, accessTime);
return health;
}
return cached.getHealth();
}

3、从第5行可以看出,又是代理类org.springframework.boot.actuate.endpoint.mvc.AbstractEndpointMvcAdapter的invoke方法 !!!∑(゚Д゚ノ)ノ

那我们看看AbstractEndpointMvcAdapter的实现

从中可以肯定实现类是第二个HealthMvcEndpoint

4、所以只能看HealthMvcEndpoint的invoke方法咯 ψ(*`ー´)ψ

 @Override
public Health invoke() {
return this.healthIndicator.health();
}

这就很简单了,仅一行代码;继续跟进后可以得知healthIndicator是一个接口,然后我们找到这个接口是在构造函数中初始化的,我们来看看初始化的对象是谁

 public HealthEndpoint(HealthAggregator healthAggregator, Map<String, HealthIndicator> healthIndicators) {
super("health", false);
Assert.notNull(healthAggregator, "HealthAggregator must not be null");
Assert.notNull(healthIndicators, "HealthIndicators must not be null");
CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator(
healthAggregator);
for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) {
healthIndicator.addHealthIndicator(getKey(entry.getKey()), entry.getValue());
}
this.healthIndicator = healthIndicator;
}

哈哈,原来healthIndicator就是CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator(healthAggregator);

所以this.healthIndicator.health()就是CompositeHealthIndicator的health了,来来来我们看看CompositeHealthIndicator的health

 @Override
public Health health() {
Map<String, Health> healths = new LinkedHashMap<String, Health>();
for (Map.Entry<String, HealthIndicator> entry : this.indicators.entrySet()) {
healths.put(entry.getKey(), entry.getValue().health());
}
return this.healthAggregator.aggregate(healths);
}

5、从代码中我们可以看出CompositeHealthIndicator的health就是从this.indicators拿出所有的HealthIndicator,并调用其health()方法

而HealthIndicator是一个接口,所以我们可以通过实现HealthIndicator接口进行自定义的health()健康检查

但实际上我们并不需要再去包装一层,springboot已经实现了一个org.springframework.boot.actuate.health.AbstractHealthIndicator,所以我们实现AbstractHealthIndicator,并重写doHealthCheck方法就可以了

综上所述:如果我们要实现自己的健康检查,只需要重写AbstractHealthIndicator的doHealthCheck方法就可以了

 public class MyHealthIndicator extends AbstractHealthIndicator {
@Override
protected void doHealthCheck(Health.Builder builder) throws Exception {
System.out.println("自定义健康检查 MyHealthIndicator");
builder.down().withDetail("This is MyHealthIndicator", "just so so!");
}
}

注意:需要将MyHealthIndicator注入成一个bean哦 (✪ω✪)

高可用的分布式配置中心:

1、传统模式

传统模式就是多个config server集群,然后通过负载均衡器实现高可用

2、服务模式:将config server注册到eureka

最新文章

  1. c++ 在windows下建立目录
  2. mac上使用终端生成RSA公钥和密钥
  3. 产品原型设计工具 Balsamiq Mockups(转)
  4. python获取外网地址
  5. 被windows“折磨”了一个礼拜
  6. [Unity3d][NGUI]两种思路解决AssetBundle的依赖关系.
  7. (转)cacti无图无数据等常见问题排查
  8. 给Linux RedHat7 设置启动终端的快捷键
  9. windows下安装和使用scrapy
  10. jQuery之事件和批量操作、事件委托示例
  11. Android学好Shape不再依赖美工
  12. FCPX插件54种婚礼调色预设ProWedding Mac
  13. c/c++ 线性表之单向循环链表
  14. 遗传算法selection总结-[Fitness, Tournament, Rank Selection]
  15. IDEA想创建package,却只有directory 解决办法
  16. 腾讯游戏设计(tgideas.qq.com)回复处CSRF任意换马甲
  17. 【CTR】各公司方法
  18. Android开源库集合(控件)
  19. jquery 给iframe里的元素添加事件
  20. 使用IDEA远程部署tomcat和调试

热门文章

  1. Mysql - 高可用方案之MMM(二)
  2. 操作MyBatis引发Error setting null for parameter #X with JdbcType OTHER .无效的列类型
  3. 在centos上安装mysql
  4. 6、netty第五个例子,使用websocket来通讯
  5. 48-创建 overlay 网络
  6. TP验证规则Validate
  7. C# List、Array、Dictionary之间相互转换
  8. CentOS自动化安装LAMP脚本
  9. weblogic解决jar包冲突
  10. dom元素的tabindex属性介绍及在vue项目中的应用