• 通过get方法的方式获取配置项信息


    这种写法比其他的方法好的一点是,当你需要修改参数名或者参数值的时候,只需要改一个地方就可以了,其他地方根本不用动,面向接口编程。

    eureka-server.properties 


    archaius.dynamicPropertyFactory.Default_getInstanceId=inistanceid
    archaius.dynamicPropertyFactory.DEFAULT_GETAPPNAME=getname
    archaius.dynamicPropertyFactory.DEFAULT_APPGROUPNAME


    package com.liwei.leshop.eureka.config;

    /**
    * 常量类
    */
    public class Content {
    public static final String URL_CONFIG_NAME = "archaius.dynamicPropertyFactory.URL_CONFIG";
    public static final String DEFAULT_GETINSTANCEID = "archaius.dynamicPropertyFactory.Default_getInstanceId";
    public static final String DEFAULT_GETAPPNAME = "archaius.dynamicPropertyFactory.DEFAULT_GETAPPNAME";
    public static final String SYS_CONFIG_NAME = "archaius.dynamicPropertyFactory.SYS_CONFIG";
    public static final String ENV_CONFIG_NAME = "archaius.dynamicPropertyFactory.ENV_CONFIG";
    public static final String DEFAULT_APPGROUPNAME = "archaius.dynamicPropertyFactory.DEFAULT_APPGROUPNAME";
    }


    /**
    * server编码
    */
    public interface EurekaServiceConfig {

    String getInstanceId();


    String getAppname();

    String getAppGroupName();

    }

    package com.liwei.leshop.eureka.config;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Optional;
    import java.util.Properties;
    
    public class PropertiesInstanceConfig implements EurekaServiceConfig {
        public static void main(String[] args) {
            PropertiesInstanceConfig config = PropertiesInstanceConfig.getPropertiesInstanceConfig();
            System.out.println( config.getAppGroupName());
        }
        public static Properties properties = new Properties();
    
        static volatile PropertiesInstanceConfig instance = null;
    
        public static PropertiesInstanceConfig getPropertiesInstanceConfig() {
            if (instance == null) {
                synchronized (PropertiesInstanceConfig.class) {
                    if (instance == null) {
                        instance = new PropertiesInstanceConfig();
                    }
                }
            }
            return instance;
        }
    
        private PropertiesInstanceConfig() {
            InputStream in = PropertiesInstanceConfig.class.getClassLoader().getResourceAsStream("eureka-server.properties");
            try {
                properties.load(in);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
    
        @Override
        public String getInstanceId() {
            return String.valueOf(Optional.ofNullable(properties.get(Content.DEFAULT_GETINSTANCEID)).orElse("Default_getInstanceId"));
        }
    
        @Override
        public String getAppname() {
            return String.valueOf(Optional.ofNullable( properties.get(Content.DEFAULT_GETAPPNAME)).orElse("Default_getAppname"));
    
        }
    
        @Override
        public String getAppGroupName() {
            return String.valueOf(Optional.ofNullable(properties.get(Content.DEFAULT_GETAPPNAME)).orElse("deflut"));
        }
    }





  • 相关阅读:
    亚马逊的客户服务和承诺
    亚马逊云主管:我们有可能成为全世界最大的企业公司
    阿里市值超越亚马逊 马云开启下半场技术理想
    股价飙升500倍,市值超过4700亿美元,从网络书店起家的亚马逊凭什么一飞冲天?
    美国女子不断收到中国神秘快递,背后原因竟是刷单
    亚马逊称网络星期一成公司史上“最大的购物日
    万众创业葬送了多少人的前程
    日本企业遭遇严重用工短缺
    培生同意以3亿美元出售华尔街英语
    亚马逊500多名欧洲员工宣布在“黑色星期五”罢工
  • 原文地址:https://www.cnblogs.com/q1359720840/p/14815105.html
Copyright © 2020-2023  润新知