欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 金融 > Eureka 学习笔记(2)加载eureka-server.properties中的配置

Eureka 学习笔记(2)加载eureka-server.properties中的配置

2024/10/25 8:19:18 来源:https://blog.csdn.net/swanzhu/article/details/139786435  浏览:    关键词:Eureka 学习笔记(2)加载eureka-server.properties中的配置

一 两种配置文件的方式

我们点开 EurekaServerConfig 可以看到

public interface EurekaServerConfig {/*** Gets the <em>AWS Access Id</em>. This is primarily used for* <em>Elastic IP Biding</em>. The access id should be provided with* appropriate AWS permissions to bind the EIP.** @return*/String getAWSAccessId();/*** Gets the <em>AWS Secret Key</em>. This is primarily used for* <em>Elastic IP Biding</em>. The access id should be provided with* appropriate AWS permissions to bind the EIP.** @return*/String getAWSSecretKey();/*** Gets the number of times the server should try to bind to the candidate* EIP.** <p>* <em>The changes are effective at runtime.</em>* </p>** @return the number of times the server should try to bind to the*         candidate EIP.*/int getEIPBindRebindRetries();

EurekaServerConfig,这是个接口,这里面有一堆getXXX()的方法,包含了eureka server需要使用的所有的配置,都可以通过这个接口来获取

针对配置定义了一个接口,接口里通过方法暴露了大量的配置项获取的方法,我们可以通过这个接口来获取你需要的配置项。

很多时候,我们会把配置文件加载到Properties就结束了,然后获取配置的时候,使用get方法获取就行。

二 加载配置文件

 EurekaServerConfig eurekaServerConfig = new DefaultEurekaServerConfig();

DefaultEurekaServerConfig,是EurekaServerConfig 的实现类,创建实例的时候,会执行一个init()方法,在这个方法中,就会完成eureka-server.properties文件中的配置项的加载。

private void init() {String env = ConfigurationManager.getConfigInstance().getString(EUREKA_ENVIRONMENT, TEST);ConfigurationManager.getConfigInstance().setProperty(ARCHAIUS_DEPLOYMENT_ENVIRONMENT, env);String eurekaPropsFile = EUREKA_PROPS_FILE.get();try {// ConfigurationManager// .loadPropertiesFromResources(eurekaPropsFile);ConfigurationManager.loadCascadedPropertiesFromResources(eurekaPropsFile);} catch (IOException e) {logger.warn("Cannot find the properties specified : {}. This may be okay if there are other environment "+ "specific properties or the configuration is installed with a different mechanism.",eurekaPropsFile);}}

点击EUREKA_PROPS_FILE.get(); 可以看到如下

private static final DynamicStringProperty EUREKA_PROPS_FILE = DynamicPropertyFactory.getInstance().getStringProperty("eureka.server.props","eureka-server");

String eurekaPropsFile = EUREKA_PROPS_FILE.get(); 就是获取了eureka-server。

然后点击ConfigurationManager中的 loadCascadedPropertiesFromResources方法,可以看到如下内容

public static void loadCascadedPropertiesFromResources(String configName) throws IOException {Properties props = loadCascadedProperties(configName);if (instance instanceof AggregatedConfiguration) {ConcurrentMapConfiguration config = new ConcurrentMapConfiguration();config.loadProperties(props);((AggregatedConfiguration) instance).addConfiguration(config, configName);} else {ConfigurationUtils.loadProperties(props, instance);}}

紧接着点击loadCascadedProperties可以看到,defaultConfigFileName 的值就是eureka-server.properties.

private static Properties loadCascadedProperties(String configName) throws IOException {String defaultConfigFileName = configName + ".properties";if (instance == null) {instance = getConfigInstance();}ClassLoader loader = Thread.currentThread().getContextClassLoader();URL url = loader.getResource(defaultConfigFileName);if (url == null) {throw new IOException("Cannot locate " + defaultConfigFileName + " as a classpath resource.");}Properties props = getPropertiesFromFile(url);String environment = getDeploymentContext().getDeploymentEnvironment();if (environment != null && environment.length() > 0) {String envConfigFileName = configName + "-" + environment + ".properties";url = loader.getResource(envConfigFileName);if (url != null) {Properties envProps = getPropertiesFromFile(url);if (envProps != null) {props.putAll(envProps);}}}return props;}

上面就是eureka-sesrver.properties中的配置,加载到了Properties对象中去;然后会加载eureka-server-环境.properties中的配置,加载到另外一个Properties中,覆盖之前那个老的Properties中的属性。

public static void loadCascadedPropertiesFromResources(String configName) throws IOException {Properties props = loadCascadedProperties(configName);if (instance instanceof AggregatedConfiguration) {ConcurrentMapConfiguration config = new ConcurrentMapConfiguration();config.loadProperties(props);((AggregatedConfiguration) instance).addConfiguration(config, configName);} else {ConfigurationUtils.loadProperties(props, instance);}}

将加载出来的Properties中的配置项都放到ConfigurationManager中去,由这个ConfigurationManager来管理

三 各种方法的实现

比如下面这个获取EIPBindingRetryIntervalMs的例子,就是

DefaultEurekaServerConfig调用getEIPBindingRetryIntervalMs()方法
  @Overridepublic int getEIPBindingRetryIntervalMs() {return configInstance.getIntProperty(namespace + "eipBindRebindRetryIntervalMs", (5 * 60 * 1000)).get();}

四 总结

DefaultEurekaServerConfig.init()方法中,会将eureka-server.properties文件中的配置加载出来,都放到ConfdigurationManager中去,然后在DefaultEurekaServerConfig的各种获取配置项的方法中,配置项的名字是在各个方法中硬编码的,是从一个DynamicPropertyFactory里面去获取的,你可以认为DynamicPropertyFactory是从ConfigurationManager那儿来的,因为ConfigurationManager中都包含了加载出来的配置了,所以DynamicPropertyFactory里,也可以获取到所有的配置项

在从DynamicPropertyFactory中获取配置项的时候,如果你没配置,那么就用默认值,全部都给你弄好了各个配置项的默认值,相当于所有的配置项的默认值,在DefaultEurekaServerConfig的各个方法中,都可以看到,如果你没配置,那么就用这里的默认值就可以了

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com