深入理解SpringCloud之配置刷新

  我们知道在SpringCloud中,当配置变更时,我们通过访问,可以在不启动服务的情况下获取最新的配置,那么它是如何做到的呢,当我们更改数据库配置并刷新后,如何能获取最新的数据源对象呢?下面我们看SpringCloud如何做到的。

一、环境变化 1.1、关于ContextRefresher

  当我们访问/refresh时,会被RefreshEndpoint类所处理。我们来看源代码:

深入理解SpringCloud之配置刷新

深入理解SpringCloud之配置刷新

/* * Copyright 2013-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.springframework.cloud.endpoint; import java.util.Arrays; import java.util.Collection; import java.util.Set; import org.springframework.boot.actuate.endpoint.AbstractEndpoint; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedResource; /** * @author Dave Syer * @author Venil Noronha */ @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()); } }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wppfgp.html