配置中心请求过程

@SpringBootApplication
@EnableDiscoveryClient
public class XmfenbeiConfigClientApplication {

    public static void main(String[] args) {
        //T1
        SpringApplication.run(XmfenbeiConfigClientApplication.class, args);
    }

}

SpringApplication

    public static ConfigurableApplicationContext run(Object source, String... args) {
        //T2
        return run(new Object[]{source}, args);
    }
    public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
        //T3
        return (new SpringApplication(sources)).run(args);
    }
    public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        FailureAnalyzers analyzers = null;
        this.configureHeadlessProperty();
        SpringApplicationRunListeners listeners = this.getRunListeners(args);
        listeners.starting();

        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
            ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
            Banner printedBanner = this.printBanner(environment);
            context = this.createApplicationContext();
            new FailureAnalyzers(context);
            //T4
            this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
            this.refreshContext(context);
            this.afterRefresh(context, applicationArguments);
            listeners.finished(context, (Throwable)null);
            stopWatch.stop();
            if (this.logStartupInfo) {
                (new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
            }

            return context;
        } catch (Throwable var9) {
            this.handleRunFailure(context, listeners, (FailureAnalyzers)analyzers, var9);
            throw new IllegalStateException(var9);
        }
    }

    private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment, SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
        context.setEnvironment(environment);
        this.postProcessApplicationContext(context);
        //T5
        this.applyInitializers(context);
        listeners.contextPrepared(context);
        if (this.logStartupInfo) {
            this.logStartupInfo(context.getParent() == null);
            this.logStartupProfileInfo(context);
        }

        context.getBeanFactory().registerSingleton("springApplicationArguments", applicationArguments);
        if (printedBanner != null) {
            context.getBeanFactory().registerSingleton("springBootBanner", printedBanner);
        }

        Set<Object> sources = this.getSources();
        Assert.notEmpty(sources, "Sources must not be empty");
        this.load(context, sources.toArray(new Object[sources.size()]));
        listeners.contextLoaded(context);
    }

    protected void applyInitializers(ConfigurableApplicationContext context) {
        Iterator var2 = this.getInitializers().iterator();

        while(var2.hasNext()) {
            ApplicationContextInitializer initializer = (ApplicationContextInitializer)var2.next();
            Class<?> requiredType = GenericTypeResolver.resolveTypeArgument(initializer.getClass(), ApplicationContextInitializer.class);
            Assert.isInstanceOf(requiredType, context, "Unable to call initializer.");
            //T6
            initializer.initialize(context);
        }

    }

PropertySourceBootstrapConfiguration


    public void initialize(ConfigurableApplicationContext applicationContext) {
        CompositePropertySource composite = new CompositePropertySource("bootstrapProperties");
        AnnotationAwareOrderComparator.sort(this.propertySourceLocators);
        boolean empty = true;
        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        Iterator var5 = this.propertySourceLocators.iterator();

        while(var5.hasNext()) {
            PropertySourceLocator locator = (PropertySourceLocator)var5.next();
            PropertySource<?> source = null;
            //T7
            source = locator.locate(environment);
            if (source != null) {
                logger.info("Located property source: " + source);
                composite.addPropertySource(source);
                empty = false;
            }
        }

        if (!empty) {
            MutablePropertySources propertySources = environment.getPropertySources();
            String logConfig = environment.resolvePlaceholders("${logging.config:}");
            LogFile logFile = LogFile.get(environment);
            if (propertySources.contains("bootstrapProperties")) {
                propertySources.remove("bootstrapProperties");
            }

            this.insertPropertySources(propertySources, composite);
            this.reinitializeLoggingSystem(environment, logConfig, logFile);
            this.setLogLevels(applicationContext, environment);
            this.handleIncludedProfiles(environment);
        }

    }

ConfigServicePropertySourceLocator


    @Override
    @Retryable(interceptor = "configServerRetryInterceptor")
    public org.springframework.core.env.PropertySource<?> locate(
            org.springframework.core.env.Environment environment) {
        ConfigClientProperties properties = this.defaultProperties.override(environment);
        CompositePropertySource composite = new CompositePropertySource("configService");
        RestTemplate restTemplate = this.restTemplate == null ? getSecureRestTemplate(properties)
                : this.restTemplate;
        Exception error = null;
        String errorBody = null;
        logger.info("Fetching config from server at: " + properties.getRawUri());
        try {
            String[] labels = new String[] { "" };
            if (StringUtils.hasText(properties.getLabel())) {
                labels = StringUtils.commaDelimitedListToStringArray(properties.getLabel());
            }

            String state = ConfigClientStateHolder.getState();

            // Try all the labels until one works
            for (String label : labels) {
                                //T8
                Environment result = getRemoteEnvironment(restTemplate,
                        properties, label.trim(), state);
                if (result != null) {
                    logger.info(String.format("Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s",
                            result.getName(),
                            result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()),
                            result.getLabel(), result.getVersion(), result.getState()));

                    if (result.getPropertySources() != null) { // result.getPropertySources() can be null if using xml
                        for (PropertySource source : result.getPropertySources()) {
                            @SuppressWarnings("unchecked")
                            Map<String, Object> map = (Map<String, Object>) source
                                    .getSource();
                            composite.addPropertySource(new MapPropertySource(source
                                    .getName(), map));
                        }
                    }

                    if (StringUtils.hasText(result.getState()) || StringUtils.hasText(result.getVersion())) {
                        HashMap<String, Object> map = new HashMap<>();
                        putValue(map, "config.client.state", result.getState());
                        putValue(map, "config.client.version", result.getVersion());
                        composite.addFirstPropertySource(new MapPropertySource("configClient", map));
                    }
                    return composite;
                }
            }
        }
        catch (HttpServerErrorException e) {
            error = e;
            if (MediaType.APPLICATION_JSON.includes(e.getResponseHeaders()
                    .getContentType())) {
                errorBody = e.getResponseBodyAsString();
            }
        }
        catch (Exception e) {
            error = e;
        }
        if (properties.isFailFast()) {
            throw new IllegalStateException(
                    "Could not locate PropertySource and the fail fast property is set, failing",
                    error);
        }
        logger.warn("Could not locate PropertySource: "
                + (errorBody == null ? error==null ? "label not found" : error.getMessage() : errorBody));
        return null;

    }

    private Environment getRemoteEnvironment(RestTemplate restTemplate, ConfigClientProperties properties,
                                             String label, String state) {
        String path = "/{name}/{profile}";
        String name = properties.getName();
        String profile = properties.getProfile();
        String token = properties.getToken();
        String uri = properties.getRawUri();

        Object[] args = new String[] { name, profile };
        if (StringUtils.hasText(label)) {
            args = new String[] { name, profile, label };
            path = path + "/{label}";
        }
        ResponseEntity<Environment> response = null;

        try {
            HttpHeaders headers = new HttpHeaders();
            if (StringUtils.hasText(token)) {
                headers.add(TOKEN_HEADER, token);
            }
            if (StringUtils.hasText(state)) { //TODO: opt in to sending state?
                headers.add(STATE_HEADER, state);
            }
            final HttpEntity<Void> entity = new HttpEntity<>((Void) null, headers);
                        //T9
            response = restTemplate.exchange(uri + path, HttpMethod.GET,
                    entity, Environment.class, args);
        }
        catch (HttpClientErrorException e) {
            if (e.getStatusCode() != HttpStatus.NOT_FOUND) {
                throw e;
            }
        }

        if (response == null || response.getStatusCode() != HttpStatus.OK) {
            return null;
        }
        Environment result = response.getBody();
        return result;
    }

Server

EnvironmentController


    @RequestMapping({"/{name}/{profiles}/{label:.*}"})
    public Environment labelled(@PathVariable String name, @PathVariable String profiles, @PathVariable String label) {
        if (name != null && name.contains("(_)")) {
            name = name.replace("(_)", "/");
        }

        if (label != null && label.contains("(_)")) {
            label = label.replace("(_)", "/");
        }
        //T10
        Environment environment = this.repository.findOne(name, profiles, label);
        return environment;
    }

EnvironmentEncryptorEnvironmentRepository


    public Environment findOne(String name, String profiles, String label) {
        //T11
        Environment environment = this.delegate.findOne(name, profiles, label);
        if (this.environmentEncryptor != null) {
            environment = this.environmentEncryptor.decrypt(environment);
        }

        if (!this.overrides.isEmpty()) {
            environment.addFirst(new PropertySource("overrides", this.overrides));
        }

        return environment;
    }

CompositeEnvironmentRepository


    public Environment findOne(String application, String profile, String label) {
        Environment env = new Environment(application, new String[]{profile}, label, (String)null, (String)null);
        if (this.environmentRepositories.size() == 1) {
            //T12
            Environment envRepo = ((EnvironmentRepository)this.environmentRepositories.get(0)).findOne(application, profile, label);
            env.addAll(envRepo.getPropertySources());
            env.setVersion(envRepo.getVersion());
            env.setState(envRepo.getState());
        } else {
            Iterator var7 = this.environmentRepositories.iterator();

            while(var7.hasNext()) {
                EnvironmentRepository repo = (EnvironmentRepository)var7.next();
                env.addAll(repo.findOne(application, profile, label).getPropertySources());
            }
        }

        return env;
    }
}
public class DatabasesEnvironmentRepository implements EnvironmentRepository {
    @Override
    public Environment findOne(String s, String s1, String s2) {
        return null;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。