我们从Spring-Cloud中的配置问题来初识Spring Cloud

文章目录

  • JDK API
    • java的系统属性配置
      • java系统变量的获取:System.getPropertys()
        • System.getPropertys():code
        • System.getPropertys():测试结果
        • System.getProperty("key"):code
        • System.getProperty("key"):测试结果
      • 操作系统变量:path,之类的。
  • Apache Commons API
    • commons-lang API
    • commons-collection API
    • commons-dbcp API
    • commons-configuration API
    • 缺点
  • Netflix Archaius
  • Spring Framework Environment API
    • Environment 抽象接口
    • 读取配置:PropertyResolver
  • Spring Boot 配置实现
  • Spring Cloud 中的配置

JDK API

java的系统属性配置

java系统变量的获取:System.getPropertys()

System.getPropertys():code

@Test
    public void getPropertiesDemo(){
     
        Properties properties=System.getProperties();
        try {
     
            properties.storeToXML(System.out,"测试结果","UTF-8");
        } catch (IOException e) {
     
            e.printStackTrace();
        }
    }

System.getPropertys():测试结果

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>测试结果</comment>
<entry key="java.runtime.name">Java(TM) SE Runtime Environment</entry>
<entry key="sun.boot.library.path">/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib</entry>
<entry key="java.vm.version">25.211-b12</entry>
<entry key="gopherProxySet">false</entry>
<entry key="java.vm.vendor">Oracle Corporation</entry>
<entry key="java.vendor.url">http://java.oracle.com/</entry>
<entry key="path.separator">:</entry>
<entry key="java.vm.name">Java HotSpot(TM) 64-Bit Server VM</entry>
<entry key="file.encoding.pkg">sun.io</entry>
<entry key="user.country">CN</entry>
<entry key="sun.java.launcher">SUN_STANDARD</entry>
<entry key="sun.os.patch.level">unknown</entry>
<entry key="java.vm.specification.name">Java Virtual Machine Specification</entry>
<entry key="user.dir">/Users/gaoxinfu/demo-spring-cloud/demo-spring-cloud-config</entry>
<entry key="java.runtime.version">1.8.0_211-b12</entry>
<entry key="java.awt.graphicsenv">sun.awt.CGraphicsEnvironment</entry>
<entry key="java.endorsed.dirs">/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/endorsed</entry>
<entry key="os.arch">x86_64</entry>
<entry key="java.io.tmpdir">/var/folders/l7/t6tn885s3tx1s1yfjlcs3wx40000gn/T/</entry>
<entry key="line.separator">
</entry>
<entry key="java.vm.specification.vendor">Oracle Corporation</entry>
<entry key="os.name">Mac OS X</entry>
<entry key="sun.jnu.encoding">UTF-8</entry>
<entry key="java.library.path">/Users/gaoxinfu/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.</entry>
<entry key="java.specification.name">Java Platform API Specification</entry>
<entry key="java.class.version">52.0</entry>
<entry key="sun.management.compiler">HotSpot 64-Bit Tiered Compilers</entry>
<entry key="os.version">10.15.1</entry>
<entry key="http.nonProxyHosts">local|*.local|169.254/16|*.169.254/16</entry>
<entry key="user.home">/Users/gaoxinfu</entry>
<entry key="user.timezone"/>
<entry key="java.awt.printerjob">sun.lwawt.macosx.CPrinterJob</entry>
<entry key="file.encoding">UTF-8</entry>
<entry key="java.specification.version">1.8</entry>
<entry key="java.class.path">/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit-rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/junit/lib/junit5-rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/lib/tools.jar:/Users/gaoxinfu/demo-spring-cloud/demo-spring-cloud-config/target/classes:/Users/gaoxinfu/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/gaoxinfu/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar</entry>
<entry key="user.name">gaoxinfu</entry>
<entry key="java.vm.specification.version">1.8</entry>
<entry key="sun.java.command">com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.gaoxinfu.demo.spring.cloud.config.system.SystemDemo,getPropertiesDemo</entry>
<entry key="java.home">/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre</entry>
<entry key="sun.arch.data.model">64</entry>
<entry key="user.language">zh</entry>
<entry key="java.specification.vendor">Oracle Corporation</entry>
<entry key="awt.toolkit">sun.lwawt.macosx.LWCToolkit</entry>
<entry key="java.vm.info">mixed mode</entry>
<entry key="java.version">1.8.0_211</entry>
<entry key="java.ext.dirs">/Users/gaoxinfu/Library/Java/Extensions:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/ext:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java</entry>
<entry key="sun.boot.class.path">/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/sunrsasign.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/jre/classes</entry>
<entry key="java.vendor">Oracle Corporation</entry>
<entry key="file.separator">/</entry>
<entry key="java.vendor.url.bug">http://bugreport.sun.com/bugreport/</entry>
<entry key="idea.test.cyclic.buffer.size">1048576</entry>
<entry key="sun.io.unicode.encoding">UnicodeBig</entry>
<entry key="sun.cpu.endian">little</entry>
<entry key="socksNonProxyHosts">local|*.local|169.254/16|*.169.254/16</entry>
<entry key="ftp.nonProxyHosts">local|*.local|169.254/16|*.169.254/16</entry>
<entry key="sun.cpu.isalist"/>
</properties>

System.getProperty(“key”):code


    @Test
    public void getPropertieDemo(){
     
        System.out.println(System.getProperty("user.name"));
    }

System.getProperty(“key”):测试结果

gaoxinfu

Process finished with exit code 0

操作系统变量:path,之类的。

   @Test
    public void getenvDemo(){
     
        Map<String,String> envMap=System.getenv();
        System.out.println(envMap.toString());
    }

我们从Spring-Cloud中的配置问题来初识Spring Cloud_第1张图片

Apache Commons API

commons-lang API

commons-collection API

commons-dbcp API

commons-configuration API

https://github.com/apache/commons-configuration

commons-configuration 提供了统一配置 API,提供通用数据类型转换、Integer、Long、BigDecimal

我们从Spring-Cloud中的配置问题来初识Spring Cloud_第2张图片

关系型数据库 -
org.apache.commons.configuration.DatabaseConfiguration
Properties 配置文件 -
org.apache.commons.configuration.PropertiesConfiguration
XML 配置文件 -
org.apache.commons.configuration.XMLConfiguration
OS 环境变量 -
org.apache.commons.configuration.EnvironmentConfiguration
Java System Properties -
org.apache.commons.configuration.SystemConfiguration
特殊组合实现 -
org.apache.commons.configuration.CompositeConfiguration
自身不提供数据来源,组合其他子来源,并且控制优先级顺序
以上实现均有外部提供配置的来源

缺点

Apache commons-configuration 提供一套完整配置 API,
并且能够通过组合模式组合多种不同的配置来源,来实现统一的配置读取,不支持动态更新和写入

Netflix Archaius

com.netflix.config.DynamicConfiguration

Spring Framework Environment API

Environment 抽象接口

我们从Spring-Cloud中的配置问题来初识Spring Cloud_第3张图片

读取配置:PropertyResolver

org.springframework.core.env.PropertyResolver

Spring Boot 配置实现

配置来源
核心实现 -
org.springframework.boot.context.config.ConfigFileApplicationListener

Spring Cloud 中的配置

配置来源
核心实现 -
org.springframework.cloud.bootstrap.BootstrapApplicationListener

你可能感兴趣的:(spring-cloud)