程序员你是如何使用Nacos作为配置中心的? (2)

测试用例:

场景 期望结果 实际结果 是否符合预期
获取共享配置文件中的配置   r-wz9sp7dhxjnz16bs1jzhutj.redis.rds.aliyuncs.com:6379   r-wz9sp7dhxjnz16bs1jzhutj.redis.rds.aliyuncs.com:6379    
在服务独有app-file.properties配置中重写配置redis.config.servers=r-wz9sp7dhxjnz16bs1jzhutj.redis.rds.aliyuncs.com:637905   r-wz9sp7dhxjnz16bs1jzhutj.redis.rds.aliyuncs.com:637905   r-wz9sp7dhxjnz16bs1jzhutj.redis.rds.aliyuncs.com:637905    

截图如下:

image.png

image.png

image.png

源码分析

掌握用法之后,深入分析源码,知其然而知其所以然;

starter调用封装

使用的starter封装;

https://github.com/alibaba/spring-cloud-alibaba/spring-cloud-alibaba-starters/spring-cloud-starter-alibaba-nacos-config

版本: 2.2.1.RELEASE

启动的时候自动装配的配置如下:

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

分解一下key,看一下用途:

key 说明
org.springframework.cloud.bootstrap.BootstrapConfiguration   A marker interface used as a key in META-INF/spring.factories. Entries in* the factories file are used to create the bootstrap application context.  

翻译:一个标记注解用来作为key 放在META-INF/spring.factories文件中,文件中的条目用来创建启动应用的上下文;

来源:spring-cloud-context-version.jar

value:

com.alibaba.cloud.nacos.NacosConfigBootstrapConfiguration |
| org.springframework.boot.autoconfigure.EnableAutoConfiguration | 注释太长了,不放这里.放到附录中。

来源:spring-boot-autoconfigure-version.jar

com.alibaba.cloud.nacos.NacosConfigAutoConfiguration,\

com.alibaba.cloud.nacos.endpoint.NacosConfigEndpointAutoConfiguration |
| org.springframework.boot.diagnostics.FailureAnalyzer | A {@code FailureAnalyzer} is used to analyze a failure and provide diagnostic* information that can be displayed to the user.

_

翻译: FailureAnalyzer用来分析错误并提供诊断信息展示给到用户

来源: spring-boot-version.jar

com.alibaba.cloud.nacos.diagnostics.analyzer.NacosConnectionFailureAnalyzer |

然后看看都自动装配了什么?以及自动装配的过程。

springboot的方式调用;

1 NacosConfigBootstrapConfiguration

源码:

package com.alibaba.cloud.nacos; import com.alibaba.cloud.nacos.client.NacosPropertySourceLocator; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author xiaojing */ @Configuration(proxyBeanMethods = false) @ConditionalOnProperty(name = "spring.cloud.nacos.config.enabled", matchIfMissing = true) public class NacosConfigBootstrapConfiguration { @Bean @ConditionalOnMissingBean public NacosConfigProperties nacosConfigProperties() { return new NacosConfigProperties(); } @Bean @ConditionalOnMissingBean public NacosConfigManager nacosConfigManager( NacosConfigProperties nacosConfigProperties) { return new NacosConfigManager(nacosConfigProperties); } @Bean public NacosPropertySourceLocator nacosPropertySourceLocator( NacosConfigManager nacosConfigManager) { return new NacosPropertySourceLocator(nacosConfigManager); } }

自动装配流程:

 程序员你是如何使用Nacos作为配置中心的?

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zygsgf.html