Spring Cloud Config Client
阿新 • • 發佈:2018-12-25
Spring cloud config client 技術體系
一:為什麼配置環境跟這兩個類有關ApplicationEnvironmentPreparedEvent和ConfigFileApplicationListener
①當一個spring應用程式啟動時釋出的事件,環境第一個可用來進行檢查和修改。
建立一個環境事件的例項:public ApplicationEnvironmentPreparedEvent(SpringApplication application, String[] args, org.springframework.core.env.ConfigurableEnvironment environment)
②通過從已知的檔案位置載入屬性來配置上下文環境。預設屬性將從“應用程式”載入。屬性“和/或”應用程式。下面是yml的檔案
可以使用setsearchlocation(字串)和setSearchNames(字串)指定可選的搜尋位置和名稱。
另外的檔案也會基於活動概要檔案載入。例如,如果一個“web”配置檔案是活動的“應用程式web”。屬性”和“即web。yml將被考慮。
“spring.config.name”屬性可以用來指定要載入的替代名稱和“spring.config”。位置屬性可用於指定可選的搜尋位置或特定的檔案。
如果看詳細的解釋可以上官方的 API
二: Bootstrap 配置屬性與 Spring Framework / Spring Boot 配置架構的關係
1 // 2 // Source code recreated from a .class file by IntelliJ IDEA 3 // (powered by Fernflower decompiler) 4 // 5 6 package io.netty.bootstrap; 7 8 import io.netty.bootstrap.AbstractBootstrap; 9 import io.netty.channel.Channel;10 import io.netty.channel.ChannelFuture; 11 import io.netty.channel.ChannelFutureListener; 12 import io.netty.channel.ChannelHandler; 13 import io.netty.channel.ChannelOption; 14 import io.netty.channel.ChannelPipeline; 15 import io.netty.channel.ChannelPromise; 16 import io.netty.util.AttributeKey; 17 import io.netty.util.internal.logging.InternalLogger; 18 import io.netty.util.internal.logging.InternalLoggerFactory; 19 import java.net.InetAddress; 20 import java.net.InetSocketAddress; 21 import java.net.SocketAddress; 22 import java.util.Iterator; 23 import java.util.Map; 24 import java.util.Map.Entry; 25 26 public class Bootstrap extends AbstractBootstrap<Bootstrap, Channel> {
// 27 private static final InternalLogger logger = InternalLoggerFactory.getInstance(Bootstrap.class); 28 private volatile SocketAddress remoteAddress; 29 30 public Bootstrap() { 31 } 32 33 private Bootstrap(Bootstrap bootstrap) { 34 super(bootstrap); 35 this.remoteAddress = bootstrap.remoteAddress; 36 } 37 38 public Bootstrap remoteAddress(SocketAddress remoteAddress) {//遠端地址 39 this.remoteAddress = remoteAddress; 40 return this; 41 } 42 43 public Bootstrap remoteAddress(String inetHost, int inetPort) {//主機是字元 44 this.remoteAddress = new InetSocketAddress(inetHost, inetPort); 45 return this; 46 } 47 48 public Bootstrap remoteAddress(InetAddress inetHost, int inetPort) { //主機是類的資料 49 this.remoteAddress = new InetSocketAddress(inetHost, inetPort); 50 return this; 51 } 52 53 public ChannelFuture connect() { 54 this.validate(); 55 SocketAddress remoteAddress = this.remoteAddress; 56 if(remoteAddress == null) { 57 throw new IllegalStateException("remoteAddress not set"); 58 } else { 59 return this.doConnect(remoteAddress, this.localAddress()); 60 } 61 } 62 63 public ChannelFuture connect(String inetHost, int inetPort) { 64 return this.connect(new InetSocketAddress(inetHost, inetPort)); 65 } 66 67 public ChannelFuture connect(InetAddress inetHost, int inetPort) { 68 return this.connect(new InetSocketAddress(inetHost, inetPort)); 69 } 70 71 public ChannelFuture connect(SocketAddress remoteAddress) { 72 if(remoteAddress == null) { 73 throw new NullPointerException("remoteAddress"); 74 } else { 75 this.validate(); 76 return this.doConnect(remoteAddress, this.localAddress()); 77 } 78 } 79 80 public ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress) { 81 if(remoteAddress == null) { 82 throw new NullPointerException("remoteAddress"); 83 } else { 84 this.validate(); 85 return this.doConnect(remoteAddress, localAddress); 86 } 87 } 88 89 private ChannelFuture doConnect(final SocketAddress remoteAddress, final SocketAddress localAddress) { 90 final ChannelFuture regFuture = this.initAndRegister(); 91 final Channel channel = regFuture.channel(); 92 if(regFuture.cause() != null) { 93 return regFuture; 94 } else { 95 final ChannelPromise promise = channel.newPromise(); 96 if(regFuture.isDone()) { 97 doConnect0(regFuture, channel, remoteAddress, localAddress, promise); 98 } else { 99 regFuture.addListener(new ChannelFutureListener() { 100 public void operationComplete(ChannelFuture future) throws Exception { 101 Bootstrap.doConnect0(regFuture, channel, remoteAddress, localAddress, promise); 102 } 103 }); 104 } 105 106 return promise; 107 } 108 } 109 110 private static void doConnect0(final ChannelFuture regFuture, final Channel channel, final SocketAddress remoteAddress, final SocketAddress localAddress, final ChannelPromise promise) { 111 channel.eventLoop().execute(new Runnable() { 112 public void run() { 113 if(regFuture.isSuccess()) { 114 if(localAddress == null) { 115 channel.connect(remoteAddress, promise); 116 } else { 117 channel.connect(remoteAddress, localAddress, promise); 118 } 119 120 promise.addListener(ChannelFutureListener.CLOSE_ON_FAILURE); 121 } else { 122 promise.setFailure(regFuture.cause()); 123 } 124 125 } 126 }); 127 } 128 129 void init(Channel channel) throws Exception { 130 ChannelPipeline p = channel.pipeline(); 131 p.addLast(new ChannelHandler[]{this.handler()}); 132 Map options = this.options(); 133 synchronized(options) { 134 Iterator i$ = options.entrySet().iterator(); 135 136 while(true) { 137 if(!i$.hasNext()) { 138 break; 139 } 140 141 Entry i$1 = (Entry)i$.next(); 142 143 try { 144 if(!channel.config().setOption((ChannelOption)i$1.getKey(), i$1.getValue())) { 145 logger.warn("Unknown channel option: " + i$1); 146 } 147 } catch (Throwable var10) { 148 logger.warn("Failed to set a channel option: " + channel, var10); 149 } 150 } 151 } 152 153 Map attrs = this.attrs(); 154 synchronized(attrs) { 155 Iterator i$2 = attrs.entrySet().iterator(); 156 157 while(i$2.hasNext()) { 158 Entry e = (Entry)i$2.next(); 159 channel.attr((AttributeKey)e.getKey()).set(e.getValue()); 160 } 161 162 } 163 } 164 165 public Bootstrap validate() { 166 super.validate(); 167 if(this.handler() == null) { 168 throw new IllegalStateException("handler not set"); 169 } else { 170 return this; 171 } 172 } 173 174 public Bootstrap clone() { 175 return new Bootstrap(this); 176 } 177 178 public String toString() { 179 if(this.remoteAddress == null) { 180 return super.toString(); 181 } else { 182 StringBuilder buf = new StringBuilder(super.toString()); 183 buf.setLength(buf.length() - 1); 184 return buf.append(", remoteAddress: ").append(this.remoteAddress).append(')').toString(); 185 } 186 } 187 }
Environment 端點:介紹/env 端點的使用場景,並且解讀其原始碼,瞭解其中奧祕
/* * Copyright (c) 1996, 1999, Oracle and/or its affiliates. All rights reserved. * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * */ package org.omg.CORBA; /** * A container (holder) for an exception that is used in <code>Request</code> * operations to make exceptions available to the client. An * <code>Environment</code> object is created with the <code>ORB</code> * method <code>create_environment</code>. * * @since JDK1.2 */ public abstract class Environment { /** * Retrieves the exception in this <code>Environment</code> object. * * @return the exception in this <code>Environment</code> object */ public abstract java.lang.Exception exception(); /** * Inserts the given exception into this <code>Environment</code> object. * * @param except the exception to be set */ public abstract void exception(java.lang.Exception except); /** * Clears this <code>Environment</code> object of its exception. */ public abstract void clear(); }