当创建了一个StandServletEnvironment对象时,会触发事件,分析下做了哪些事情。
---
@Override public void environmentPrepared(ConfigurableEnvironment environment) { this.initialMulticaster.multicastEvent(new ApplicationEnvironmentPreparedEvent( this.application, this.args, environment)); }
可以看到又做了一次分发事件。
---
1)ConfigFileApplicationListener
private void onApplicationEnvironmentPreparedEvent( ApplicationEnvironmentPreparedEvent event) { //接收事件 ListpostProcessors = loadPostProcessors(); //加上自己 postProcessors.add(this); // AnnotationAwareOrderComparator.sort(postProcessors); // for (EnvironmentPostProcessor postProcessor : postProcessors) { postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication()); } }
具体的postProcessors有
main[1] print postProcessors postProcessors = "[org.springframework.boot.env.SpringApplicationJsonEnvironmentPostProcessor@2767e23c, org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor@710c2b53, org.springframework.boot.context.config.ConfigFileApplicationListener@498d318c]"
下面一个一个分析
1.1)SpringApplicationJsonEnvironmentPostProcessor.postProcessEnvironment
什么都不做
1.2)CloudFoundryVcapEnvironmentPostProcessor.postProcessEnvironment
Step completed: "thread=main", org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor.postProcessEnvironment(), line=119 bci=0119 if (CloudPlatform.CLOUD_FOUNDRY.isActive(environment)) {main[1] next> Step completed: "thread=main", org.springframework.boot.cloud.CloudFoundryVcapEnvironmentPostProcessor.postProcessEnvironment(), line=137 bci=95137 }
仍然什么都不做!
1.3)ConfigFileApplicationListener自己的处理
这个里面会加载配置文件
protected void addPropertySources(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { //这里 RandomValuePropertySource.addToEnvironment(environment); //这里加载配置文件 try { new Loader(environment, resourceLoader).load(); } catch (IOException ex) { throw new IllegalStateException("Unable to load configuration files", ex); } }
加载的配置文件路径有
main[1] print locations locations = "[file:./config/, file:./, classpath:/config/, classpath:/]"
---第一组
1)首先尝试加载location = "file:./config/zipkin-server.properties"
2)然后尝试加载 resource = "URL [file:./config/zipkin-server.xml]"
3)接着尝试加载 location = "file:./config/zipkin-server.yml"
4)最后尝试加载 location = "file:./config/zipkin-server.yaml"
---第二组
1)首先尝试加载 location = "file:./zipkin-server.properties"
2)然后尝试加载location = "file:./zipkin-server.xml"
3)接着尝试加载location = "file:./zipkin-server.yml"
4)最后尝试加载~location = "file:./zipkin-server.yaml"
其中第3步的代码为
@Override public PropertySource load(String name, Resource resource, String profile) throws IOException { if (ClassUtils.isPresent("org.yaml.snakeyaml.Yaml", null)) { Processor processor = new Processor(resource, profile); Mapsource = processor.process(); if (!source.isEmpty()) { return new MapPropertySource(name, source); } } return null; }
---第3组
1)location = "classpath:/config/zipkin-server.properties"
2)location = "classpath:/config/zipkin-server.xml"
3)location = "classpath:/config/zipkin-server.yml"
4)location = "classpath:/config/zipkin-server.yaml"
---第4组
1)location = "classpath:/zipkin-server.properties"
2)"classpath:/zipkin-server.xml"
3)location = "classpath:/zipkin-server.yml"
4)location = "classpath:/zipkin-server.yaml"
===因为配置文件里配置的是shared,所以开始了新的一轮配置文件读取
---第1组
1)location = "file:./config/zipkin-server-shared.properties"
剩下的就不用多说了,反正最终加载了zipkin-server-shared.yml
那么得到的结果就是:
Step completed: "thread=main", org.springframework.boot.env.YamlPropertySourceLoader.load(), line=59 bci=2759 if (!source.isEmpty()) {main[1] print source source = "{zipkin.self-tracing.enabled=${SELF_TRACING_ENABLED:false}, zipkin.self-tracing.sample-rate=${SELF_TRACING_SAMPLE_RATE:1.0}, zipkin.self-tracing.flush-interval=${SELF_TRACING_FLUSH_INTERVAL:1}, zipkin.collector.sample-rate=${COLLECTOR_SAMPLE_RATE:1.0}, zipkin.collector.kafka.zookeeper=${KAFKA_ZOOKEEPER:}, zipkin.collector.kafka.topic=${KAFKA_TOPIC:zipkin}, zipkin.collector.kafka.group-id=${KAFKA_GROUP_ID:zipkin}, zipkin.collector.kafka.streams=${KAFKA_STREAMS:1}, zipkin.collector.kafka.max-message-size=${KAFKA_MAX_MESSAGE_SIZE:1048576}, zipkin.collector.scribe.enabled=${SCRIBE_ENABLED:true}, zipkin.collector.scribe.category=zipkin, zipkin.collector.scribe.port=${COLLECTOR_PORT:9410}, zipkin.query.lookback=${QUERY_LOOKBACK:86400000}, zipkin.query.names-max-age=300, zipkin.query.allowed-origins=*, zipkin.storage.strict-trace-id=${STRICT_TRACE_ID:true}, zipkin.storage.type=${STORAGE_TYPE:mem}, zipkin.storage.cassandra.contact-points=${CASSANDRA_CONTACT_POINTS:localhost}, zipkin.storage.cassandra.local-dc=${CASSANDRA_LOCAL_DC:}, zipkin.storage.cassandra.username=${CASSANDRA_USERNAME:}, zipkin.storage.cassandra.password=${CASSANDRA_PASSWORD:}, zipkin.storage.cassandra.keyspace=${CASSANDRA_KEYSPACE:zipkin}, zipkin.storage.cassandra.max-connections=${CASSANDRA_MAX_CONNECTIONS:8}, zipkin.storage.cassandra.ensure-schema=${CASSANDRA_ENSURE_SCHEMA:true}, zipkin.storage.cassandra.span-ttl=${CASSANDRA_SPAN_TTL:604800}, zipkin.storage.cassandra.index-ttl=${CASSANDRA_INDEX_TTL:259200}, zipkin.storage.cassandra.index-cache-max=${CASSANDRA_INDEX_CACHE_MAX:100000}, zipkin.storage.cassandra.index-cache-ttl=${CASSANDRA_INDEX_CACHE_TTL:60}, zipkin.storage.cassandra.index-fetch-multiplier=${CASSANDRA_INDEX_FETCH_MULTIPLIER:3}, zipkin.storage.cassandra.use-ssl=${CASSANDRA_USE_SSL:false}, zipkin.storage.cassandra3.contact-points=${CASSANDRA3_CONTACT_POINTS:localhost}, zipkin.storage.cassandra3.local-dc=${CASSANDRA3_LOCAL_DC:}, zipkin.storage.cassandra3.username=${CASSANDRA3_USERNAME:}, zipkin.storage.cassandra3.password=${CASSANDRA3_PASSWORD:}, zipkin.storage.cassandra3.keyspace=${CASSANDRA3_KEYSPACE:zipkin3}, zipkin.storage.cassandra3.max-connections=${CASSANDRA3_MAX_CONNECTIONS:8}, zipkin.storage.cassandra3.ensure-schema=${CASSANDRA3_ENSURE_SCHEMA:true}, zipkin.storage.cassandra3.index-fetch-multiplier=${CASSANDRA3_INDEX_FETCH_MULTIPLIER:3}, zipkin.storage.cassandra3.use-ssl=${CASSANDRA3_USE_SSL:false}, zipkin.storage.elasticsearch.cluster=${ES_CLUSTER:elasticsearch}, zipkin.storage.elasticsearch.hosts=${ES_HOSTS:}, zipkin.storage.elasticsearch.pipeline=${ES_PIPELINE:}, zipkin.storage.elasticsearch.max-requests=${ES_MAX_REQUESTS:64}, zipkin.storage.elasticsearch.aws.domain=${ES_AWS_DOMAIN:}, zipkin.storage.elasticsearch.aws.region=${ES_AWS_REGION:}, zipkin.storage.elasticsearch.index=${ES_INDEX:zipkin}, zipkin.storage.elasticsearch.index-shards=${ES_INDEX_SHARDS:5}, zipkin.storage.elasticsearch.index-replicas=${ES_INDEX_REPLICAS:1}, zipkin.storage.mysql.host=${MYSQL_HOST:localhost}, zipkin.storage.mysql.port=${MYSQL_TCP_PORT:3306}, zipkin.storage.mysql.username=${MYSQL_USER:}, zipkin.storage.mysql.password=${MYSQL_PASS:}, zipkin.storage.mysql.db=${MYSQL_DB:zipkin}, zipkin.storage.mysql.max-active=${MYSQL_MAX_CONNECTIONS:10}, zipkin.storage.mysql.use-ssl=${MYSQL_USE_SSL:false}, zipkin.ui.query-limit=10, zipkin.ui.environment=, zipkin.ui.default-lookback=3600000, zipkin.ui.instrumented=.*, server.port=${QUERY_PORT:9411}, server.compression.enabled=true, server.compression.mime-types=application/json,application/javascript,text/css,image/svg, spring.mvc.favicon.enabled=false, spring.autoconfigure.exclude[0]=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration, spring.autoconfigure.exclude[1]=org.springframework.boot.autoconfigure.elasticsearch.jest.JestAutoConfiguration, info.zipkin.version=1.19.0, logging.level.com.facebook.swift.service.ThriftServiceProcessor=OFF}"
---
然后接下来的一个listener就是
org.springframework.boot.autoconfigure.BackgroundPreinitializer.onApplicationEvent
主要做一些后台初始化工作,
---