In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to integrate SpringCloud in Nacos Config. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
The way of system integration
Maven package dependency
Org.springframework.cloud spring-cloud-starter-alibaba-nacos-config 0.2.1.RELEASE
2.bootstrap.properties profile
# nacos configuration center address spring.cloud.nacos.config.server-addr=10.136.15.122:8848# default application name, data-id defaults to name+file-extensionspring.application.name=example without configuration using name as the prefix # spring.cloud.nacos.config.prefix# application file format supports two spring.cloud.nacos.config.file-extension=properties of properties,yml
3.java application code
Package com.nacos;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping ("/ config") @ RefreshScopepublic class ConfigController {@ Value ("${useLocalCache:false}") private boolean useLocalCache / * http://localhost:8080/config/get * / @ RequestMapping ("/ get") public boolean get () {return useLocalCache;}} package com.nacos;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class NacosConfigApplication {public static void main (String [] args) {SpringApplication.run (NacosConfigApplication.class, args);}}
4. Start the application, nacos configure the central configuration parameters, access / config/get result is true, and the configuration is successful
Parameter configuration description
# configure the central address spring.cloud.nacos.config.server-addr=10.136.15.122:8848# application name, which is not required. If prefix is not configured, it defaults to name as the prefix # spring.application.name=example#data-id prefix spring.cloud.nacos.config.prefix=example# file type, supports properties and yml data formats spring.cloud.nacos.config.file-extension=properties# environment, and can isolate configurations between different configuration environments. For example, dev,uat,prospring.profiles.active=dev# namespaces also play a role in isolating different applications. Spring.cloud.nacos.config.namespace=c04b0cdf-91c7-470a-b6a9-423da6cc7a2b# grouping is used to isolate different grouped spring.cloud.nacos.config.group=test# to load additional configurations under the same namespace, except for loading the above main configuration file. Additional loaded public configuration function spring.cloud.nacos.config.ext-config [0] .data-id=test.propertiesspring.cloud.nacos.config.ext-config [0] .refresh = true
Partial source code analysis
The NacosPropertySourceLocator class is the core execution class of Nacos Config, which implements the PropertySourceLocator interface. The executed class is loaded at the start of the SpringCloud project, as follows
PropertySourceBootstrapConfiguration is a configuration class under SpringCloud that implements the ApplicationContextInitializer interface and executes the init method. For this reason, you can query the documentation of ApplicationContextInitializer-related APIs as follows
Public void initialize (ConfigurableApplicationContext applicationContext) {CompositePropertySource composite = new CompositePropertySource (BOOTSTRAP_PROPERTY_SOURCE_NAME); AnnotationAwareOrderComparator.sort (this.propertySourceLocators); boolean empty = true; ConfigurableEnvironment environment = applicationContext.getEnvironment (); for (PropertySourceLocator locator: this.propertySourceLocators) {PropertySource source = null Source = locator.locate (environment); if (source = = null) {continue;} 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 (BOOTSTRAP_PROPERTY_SOURCE_NAME)) {propertySources.remove (BOOTSTRAP_PROPERTY_SOURCE_NAME);} insertPropertySources (propertySources, composite); reinitializeLoggingSystem (environment, logConfig, logFile); setLogLevels (applicationContext, environment) HandleIncludedProfiles (environment);}}
When we find the final executed locate method, let's take a look at the locate implementation of NacosPropertySourceLocator as follows
Public PropertySource locate (Environment env) {ConfigService configService = nacosConfigProperties.configServiceInstance (); if (null = = configService) {LOGGER.warn ("no instance of config service found, can't load config from nacos"); return null } long timeout = nacosConfigProperties.getTimeout (); nacosPropertySourceBuilder = new NacosPropertySourceBuilder (configService, timeout); String name = nacosConfigProperties.getName (); / * configured grouping * / String nacosGroup = nacosConfigProperties.getGroup () / * configured prefix * / String dataIdPrefix = nacosConfigProperties.getPrefix (); if (StringUtils.isEmpty (dataIdPrefix)) {dataIdPrefix = name } / * if there is no prefix, take spring.application.name*/ if (StringUtils.isEmpty (dataIdPrefix)) {dataIdPrefix = env.getProperty ("spring.application.name");} List profiles = Arrays.asList (env.getActiveProfiles ()); nacosConfigProperties.setActiveProfiles (profiles.toArray (new String [0])) String fileExtension = nacosConfigProperties.getFileExtension (); CompositePropertySource composite = new CompositePropertySource (NACOS_PROPERTY_SOURCE_NAME); loadSharedConfiguration (composite); loadExtConfiguration (composite); / * load configuration method * / loadApplicationConfiguration (composite, nacosGroup, dataIdPrefix, fileExtension); return composite;}
Through this method, we can probably understand the role of some of our above configurations. Let's take a look at the method of loading configurations in loadApplicationConfiguration. LoadSharedConfiguration,loadExtConfiguration is the way to load extended configurations, which is described in detail here.
Private void loadApplicationConfiguration (CompositePropertySource compositePropertySource, String nacosGroup, String dataIdPrefix, String fileExtension) {loadNacosDataIfPresent (compositePropertySource, dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension, true); for (String profile: nacosConfigProperties.getActiveProfiles ()) {String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension LoadNacosDataIfPresent (compositePropertySource, dataId, nacosGroup, fileExtension, true);}}
As you can see here, the configuration of dateIdPrefix+DOT+fileExtension is loaded first, and then the configuration of the environment is loaded to overwrite the previous configuration. This is the purpose of configuring dev above.
Private void loadNacosDataIfPresent (final CompositePropertySource composite, final String dataId, final String group, String fileExtension, boolean isRefreshable) {if (NacosContextRefresher.loadCount.get ()! = 0) {NacosPropertySource ps; if (! isRefreshable) {ps = NacosPropertySourceRepository.getNacosPropertySource (dataId) } else {ps = nacosPropertySourceBuilder.build (dataId, group, fileExtension, true);} composite.addFirstPropertySource (ps) } else {NacosPropertySource ps = nacosPropertySourceBuilder.build (dataId, group, fileExtension, isRefreshable); composite.addFirstPropertySource (ps) }} above is how to integrate SpringCloud in Nacos Config. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.