Java 杂谈springboot SpringCloud

Spring Cloud Config 流程源码详解

2018-08-30  本文已影响0人  黄大海

Server

public String refresh(String label) {
       Git git = createGitClient();
       ...
       checkout(git, label);
       ...
       merge(git, label);
       ...
       resetHard(...)
       ...
}

模式 应用
{spring.application.name}-{profile}.properties/yml 应用-环境-配置
{spring.application.name}.properties/yml 应用-全局-配置
application-{profile}.properties/yml 公众-环境-配置
application.properties/yml 公众-全局-配置

Client

spring:
  application:
    name: user
  cloud:
    config:
      fail-fast: true
      profile: dev
      label: master
      uri: http://localhost:8761/config, http://localhost:8762/config, http://localhost:8763/config
spring:
  application:
    name: user
  cloud:
    config:
      fail-fast: true
      profile: dev
      label: master
      discovery:
        enabled: true
        serviceId: dashboard
      
eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/,http://localhost2:8762/eureka,http://localhost3:8763/eureka
public PropertySource<?> locate(Environment environment) {
    CompositePropertySource composite = new CompositePropertySource("configService");
    ...
    RestTemplate restTemplate = getSecureRestTemplate(properties);
    ...
    Environment result = getRemoteEnvironment(restTemplate...);
    ...
    for (PropertySource source : result.getPropertySources()) {
        ...
        composite.addPropertySource(source);
    }
    ...
}
@ConditionalOnProperty(value = "spring.cloud.config.discovery.enabled", matchIfMissing = false)
@Configuration
@EnableDiscoveryClient
public class DiscoveryClientConfigServiceBootstrapConfiguration {
    void refresh() {
        String serviceId = this.config.getDiscovery().getServiceId();
        List<ServiceInstance> serviceInstances = this.instanceProvider
                    .getConfigServerInstances(serviceId);
        for (int i = 0; i < serviceInstances.size(); i++) {
                ServiceInstance server = serviceInstances.get(i);
                String url = getHomePage(server);
                listOfUrls.add(url);
        }
        ...
        configClientProperties.setUri(listOfUrls);
    }
}
上一篇下一篇

猜你喜欢

热点阅读