当前位置:网站首页>Environment preparation of Nacos configuration center
Environment preparation of Nacos configuration center
2022-07-24 19:15:00 【InfoQ】
Nacos Environment preparation of the configuration center
@SpringBootApplication
public class SpringCloudNacosConfigApplication {
public static void main(String[] args) {
ConfigurableApplicationContext context=
SpringApplication.run(SpringCloudNacosConfigApplication.class, args);
String info=context.getEnvironment().getProperty("info");
System.out.println(info);
}
}
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
// Environmental preparation
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
configureIgnoreBeanInfo(environment);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
exceptionReporters = getSpringFactoriesInstances(
SpringBootExceptionReporter.class,
new Class[] { ConfigurableApplicationContext.class }, context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
refreshContext(context);
afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
listeners.started(context);
callRunners(context, applicationArguments);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, listeners);
throw new IllegalStateException(ex);
}
try {
listeners.running(context);
}
catch (Throwable ex) {
handleRunFailure(context, ex, exceptionReporters, null);
throw new IllegalStateException(ex);
}
return context;
}
- Get monitoring and start monitoring
- Create according to the passed in parameters applicationArguments object
- call prepareEnvironment Method , Environmental preparation
- Print banner, Prepare context , Refresh context , Then perform the operation after refreshing, etc
- Return after rent context Context
prepareEnvironment() Method
private ConfigurableEnvironment prepareEnvironment(
SpringApplicationRunListeners listeners,
ApplicationArguments applicationArguments) {
// Create and configure the environment
ConfigurableEnvironment environment = getOrCreateEnvironment();
configureEnvironment(environment, applicationArguments.getSourceArgs());
listeners.environmentPrepared(environment);
bindToSpringApplication(environment);
if (!this.isCustomEnvironment) {
environment = new EnvironmentConverter(getClassLoader())
.convertEnvironmentIfNecessary(environment, deduceEnvironmentClass());
}
ConfigurationPropertySources.attach(environment);
return environment;
}
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
if (!environment.getProperty("spring.cloud.bootstrap.enabled", Boolean.class,
true)) {
return;
}
// don't listen to events in a bootstrap context
if (environment.getPropertySources().contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) {
return;
}
ConfigurableApplicationContext context = null;
String configName = environment
.resolvePlaceholders("${spring.cloud.bootstrap.name:bootstrap}");
for (ApplicationContextInitializer<?> initializer : event.getSpringApplication()
.getInitializers()) {
if (initializer instanceof ParentContextApplicationContextInitializer) {
context = findBootstrapContext(
(ParentContextApplicationContextInitializer) initializer,
configName);
}
}
if (context == null) {
context = bootstrapServiceContext(environment, event.getSpringApplication(),
configName);
event.getSpringApplication()
.addListeners(new CloseContextOnFailureApplicationListener(context));
}
apply(context, event.getSpringApplication(), environment);
}
BootstrapImportSelectorConfiguration class
@Configuration
@Import(BootstrapImportSelector.class)
public class BootstrapImportSelectorConfiguration {
}
@Override
public String[] selectImports(AnnotationMetadata annotationMetadata) {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// Use names and ensure unique to protect against duplicates
List<String> names = new ArrayList<>(SpringFactoriesLoader
.loadFactoryNames(BootstrapConfiguration.class, classLoader));
names.addAll(Arrays.asList(StringUtils.commaDelimitedListToStringArray(
this.environment.getProperty("spring.cloud.bootstrap.sources", ""))));
List<OrderedAnnotatedElement> elements = new ArrayList<>();
for (String name : names) {
try {
elements.add(
new OrderedAnnotatedElement(this.metadataReaderFactory, name));
}
catch (IOException e) {
continue;
}
}
AnnotationAwareOrderComparator.sort(elements);
String[] classNames = elements.stream().map(e -> e.name).toArray(String[]::new);
return classNames;
}
# Bootstrap components
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
org.springframework.cloud.bootstrap.config.PropertySourceBootstrapConfiguration,\
org.springframework.cloud.bootstrap.encrypt.EncryptionBootstrapConfiguration,\
org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration,\
org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration
org.springframework.cloud.bootstrap.BootstrapConfiguration=\
com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.cloud.nacos.NacosConfigAutoConfiguration,\
com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration
org.springframework.boot.diagnostics.FailureAnalyzer=\
com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer
summary
边栏推荐
- 32-bit stack overflow advanced
- 卷积神经网络感受野计算指南
- matplotlib
- High speed ASIC packaging trends: integration, SKU and 25g+
- Meshlab&PCL ISS关键点
- Hangdian multi School Game 1 question 3 backpack (XOR dp+bitset)
- Principle and application of database
- [Huawei lyevk-3861a intelligent IOT development board evaluation] unpacking experience and Hisilicon hi3861v100 chip learning experience
- 2022暑期杭电多校第一场1012Alice and Bob(博弈论)
- Installation and use of lsky Pro lancong drawing bed: a drawing bed program for online uploading and managing pictures
猜你喜欢

Meshlab&PCL ISS关键点

Crazy God redis notes 11

Create parent-child projects in clion (cmake tool) and introduce the method of third-party libraries

PWN learning

文献阅读:GoPose 3D Human Pose Estimation Using WiFi

今日睡眠质量记录79分

Why are there loopholes in the website to be repaired

Reading notes of XXL job source code

Convolutional Neural Networks in TensorFlow quizs on Coursera

卷积神经网络感受野计算指南
随机推荐
Get module handle / base address
MySQL index principle and query optimization "suggestions collection"
[JVM learning 03] class loading and bytecode Technology
Summer Niuke multi school 1:i chiitoitsu (expectation DP, inverse yuan)
Those gods on Zhihu reply
Today's sleep quality record 79 points
Data model subclassing reference
PostgreSQL weekly news - July 13, 2022
JS part
Go Xiaobai implements a simple go mock server
profile环境切换
Sqoop
Why are there loopholes in the website to be repaired
On dynamic application of binary array
2022 Hangzhou Electric Multi school first Dragon Slayer (dfs+ state compression)
MySQL version 5.7.9 SQL_ mode=only_ full_ group_ By question
Convolution neural network receptive field calculation Guide
杭电多校第一场第三题 Backpack(异或dp+bitset)
知乎上的那些神回复
2022暑期杭电多校第一场1012Alice and Bob(博弈论)