Alibaba Nacos 学习(二):Spring Cloud Nacos Config
Alibaba Nacos 学习(一):Nacos介绍与安装
Alibaba Nacos 学习(二):Spring Cloud Nacos Config
Alibaba Nacos 学习(三):Spring Cloud Nacos Discovery - FeignClient,Nacos 服务注册与发现
Alibaba Nacos 学习(四):Nacos Docker
Alibaba Nacos 学习(五):K8S Nacos搭建,使用nfs
Nacos 分布式配置中心
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件
接入配置中心
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0..RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>nacos.server</groupId>
<artifactId>nacos.server</artifactId>
<name>nacos.server</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-boot.version>2.0..RELEASE</spring-boot.version>
<nacos-config-spring-boot.version>0.2.</nacos-config-spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>0.2..RELEASE</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.5.</version>
</dependency>
<!-- swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.5.</version>
</dependency>
</dependencies>
</project>
新增控制器
@Value 注解来将对应的配置注入到ConfigController中的字段,并添加 @RefreshScope 打开动态刷新功能@RestController
@RequestMapping("/config")
@RefreshScope
public class ConfigController { @Value("${useLocalCache:false}")
private boolean useLocalCache;
@Value("${user.name}")
private String userName;
@Value("${user.age}")
private Integer userAge; /**
* http://localhost:8080/config/get
*/
@RequestMapping("/get")
@ResponseBody
public String get() {
return "useLocalCache:" + useLocalCache + " userName:" + userName + " userAge:" + userAge;
}
}
新增注册配置
bootstrap.properties增加以下配置
spring.cloud.nacos.config.server-addr=192.168.50.31:8848 --配置地址
spring.application.name=service-provider --dataid
spring.profiles.active=test --运行环境
server.port=
登录Nacos控制台
设置相关配置信息


启动项目
-- ::14.025 INFO --- [ main] o.s.c.a.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'service-provider-test.properties', group: 'DEFAULT_GROUP'
-- ::14.025 INFO --- [ main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource {name='NACOS', propertySources=[NacosPropertySource {name='service-provider-test.properties'}, NacosPropertySource {name='service-provider.properties'}]}
-- ::14.029 INFO --- [ main] e.d.example.dockertest.Application : The following profiles are active: test
-- ::14.043 INFO --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@22c86919: startup date [Mon Nov :: CST ]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6aa61224
-- ::14.699 INFO --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=0fd1ef74-7ad3-37d4-b602-1ed486ff9aa7
Loading nacos data, dataId: 'service-provider-test.properties'
在Nacos-Server中新建配置,其中Data ID它的定义规则是:${prefix}-${spring.profile.active}.${file-extension}
- prefix 默认为
spring.application.name的值,也可以通过配置项spring.cloud.nacos.config.prefix来配置。 - spring.profile.active 即为当前环境对应的
profile,可以通过配置项spring.profile.active来配置。 - file-exetension 为配置内容的数据格式,可以通过配置项
spring.cloud.nacos.config.file-extension来配置。
测试内容:

修改内容
-- ::24.525 INFO --- [.168.50.31_8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4275635b: startup date [Mon Nov :: CST ]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@12381b77
-- ::24.556 INFO --- [.168.50.31_8848] o.s.boot.SpringApplication : Started application in 2.611 seconds (JVM running for 855.251)
-- ::24.556 INFO --- [.168.50.31_8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@4275635b: startup date [Mon Nov :: CST ]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@12381b77
-- ::24.556 INFO --- [.168.50.31_8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@12381b77: startup date [Mon Nov :: CST ]; root of context hierarchy
-- ::24.562 INFO --- [.168.50.31_8848] o.s.c.e.event.RefreshEventListener : Refresh keys changed: [user.age, user.name]
Refresh keys changed: [user.age, user.name],没有修改useLocalCache字段,所以不会有刷新纪录。
Alibaba Nacos 学习(二):Spring Cloud Nacos Config的更多相关文章
- Alibaba Nacos 学习(三):Spring Cloud Nacos Discovery - FeignClient,Nacos 服务注册与发现
Alibaba Nacos 学习(一):Nacos介绍与安装 Alibaba Nacos 学习(二):Spring Cloud Nacos Config Alibaba Nacos 学习(三):Spr ...
- Spring Cloud+nacos+Feign,实现注册中心及配置中心
写在前面 注册中心.配置中心的概念就不在这里解释了.发现服务原来一直用的是Eureka,因为这家伙闭源了,不爽.然后就发现了nacos,阿里巴巴的,好东西,一个搞定注册中心和配置中心.官网:https ...
- Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)
Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...
- Spring Cloud Consul Config 知识点
Spring Cloud Consul Config 是 Config Server 和 Client的替代方案. 搭建一个配置中心,可以选择的方案: Spring Cloud Config 或者 S ...
- Spring Cloud - Nacos注册中心入门单机模式及集群模式
近几年微服务很火,Spring Cloud提供了为服务领域的一整套解决方案.其中Spring Cloud Alibaba是我们SpringCloud的一个子项目,是提供微服务开发的一站式解决方案. 包 ...
- Spring Cloud Nacos实现动态配置加载的源码分析
理解了上述Environment的基本原理后,如何从远程服务器上加载配置到Spring的Environment中. NacosPropertySourceLocator 顺着前面的分析思路,我们很自然 ...
- spring cloud学习(七)Spring Cloud Config(续)
Spring Cloud Config(续) 个人参考项目 个人博客 : https://zggdczfr.cn/ 个人参考项目 : (整合到上一个案例中)https://github.com/Fun ...
- spring cloud学习(六)Spring Cloud Config
Spring Cloud Config 参考个人项目 参考个人项目 : (希望大家能给个star~) https://github.com/FunriLy/springcloud-study/tree ...
- Spring Cloud 学习 (六) Spring Cloud Config
在实际开发过程中,每个服务都有大量的配置文件,例如数据库的配置.日志输出级别的配置等,而往往这些配置在不同的环境中也是不一样的.随着服务数量的增加,配置文件的管理也是一件非常复杂的事 在微服务架构中, ...
随机推荐
- 魏永明: MiniGUI的涅槃重生之路
本文系转载,著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者: 魏永明 来源: 微信公众号linux阅码场(id: linuxdev) 本文背景 MiniGUI是最负盛名的 ...
- mysql字段按逗号,拆分并按条件查找
mysql字段按逗号,拆分并按条件查找 mysql 表结构 CREATE TABLE `subid` ( `id1` varchar(255) DEFAULT NULL, `id2` varchar( ...
- LeetCode 1: single-number
Given an array of integers, every element appears twice except for one. Find that single one. soluti ...
- cxf 调用 .net webservice
1. 问题背景 现在我们两套语言并行,其中必然会涉及到不同系统的相互访问. .net 的会员信息是用 webservice 提供服务的.那如何对现有 .net webservi ...
- 增强for循环遍历HashSet
package cn.bdqn.chatpterone.keben; import java.util.*; public class TestHanshSet { public static voi ...
- rabbitmq学习-如何安装rabbitmq
学习当然还是需要看官网地址的哈 官网地址 你可能会说老铁,看不懂英文咋办?我只能说各大翻译软件以及广大网友总有一款是你喜欢的 广大网友翻译的 中文文档 什么是rabbitmq? rabbitmq (R ...
- Golang 实现华为云 DMS 签名
构造请求 首先构造请求,也就是要对哪个具体接口进行访问,需要提供什么必要的参数.在构造请求(点击查看中可以看到,对 DMS 服务来说必要的请求构成包括以下部分 请求URI,例如 https://dms ...
- 用node实现发送邮箱验证码
首先,你需要注册一个支持发送的邮箱,我注册是网易邮箱,然后配置smtp. 然后,创建一个node项目,输入npm install nodemailer --save安装邮件依赖. 接着创建一个文件(s ...
- springboot(3)——配置文件和自动配置原理详细讲解
原文地址 目录 概述 1. 配置文件作用 2.配置文件位置 3.配置文件的定义 3.1如果是定义普通变量(数字 字符串 布尔) 3.2如果是定义对象.Map 3.3如果是定义数组 4.配置文件的使用 ...
- [考试反思]1006csp-s模拟测试62:隔断
本来说好的好一场烂一场. 那样的日子结束了,连着烂了两场...幸亏T3傻逼了救我一命不算太惨... T1树上的特殊性质会做但是没有继续想下去就死在错贪心上了还没有过那个点... T2迭代至稳定被我错误 ...