我们点开 EurekaServerConfig 可以看到
- public interface EurekaServerConfig {
-
- /**
- * Gets the AWS Access Id. This is primarily used for
- * Elastic IP Biding. The access id should be provided with
- * appropriate AWS permissions to bind the EIP.
- *
- * @return
- */
- String getAWSAccessId();
-
- /**
- * Gets the AWS Secret Key. This is primarily used for
- * Elastic IP Biding. 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.
- *
- *
- * The changes are effective at runtime.
- *
- *
- * @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()方法
- @Override
- public 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的各个方法中,都可以看到,如果你没配置,那么就用这里的默认值就可以了