Spring Cloud Config Client

 

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();

}

猜你喜欢

转载自www.cnblogs.com/zhikou/p/7712184.html