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>

新增控制器

应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。这里我们使用 @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的更多相关文章

  1. Alibaba Nacos 学习(三):Spring Cloud Nacos Discovery - FeignClient,Nacos 服务注册与发现

    Alibaba Nacos 学习(一):Nacos介绍与安装 Alibaba Nacos 学习(二):Spring Cloud Nacos Config Alibaba Nacos 学习(三):Spr ...

  2. Spring Cloud+nacos+Feign,实现注册中心及配置中心

    写在前面 注册中心.配置中心的概念就不在这里解释了.发现服务原来一直用的是Eureka,因为这家伙闭源了,不爽.然后就发现了nacos,阿里巴巴的,好东西,一个搞定注册中心和配置中心.官网:https ...

  3. Spring Cloud 学习 之 Spring Cloud Eureka(源码分析)

    Spring Cloud 学习 之 Spring Cloud Eureka(源码分析) Spring Boot版本:2.1.4.RELEASE Spring Cloud版本:Greenwich.SR1 ...

  4. Spring Cloud Consul Config 知识点

    Spring Cloud Consul Config 是 Config Server 和 Client的替代方案. 搭建一个配置中心,可以选择的方案: Spring Cloud Config 或者 S ...

  5. Spring Cloud - Nacos注册中心入门单机模式及集群模式

    近几年微服务很火,Spring Cloud提供了为服务领域的一整套解决方案.其中Spring Cloud Alibaba是我们SpringCloud的一个子项目,是提供微服务开发的一站式解决方案. 包 ...

  6. Spring Cloud Nacos实现动态配置加载的源码分析

    理解了上述Environment的基本原理后,如何从远程服务器上加载配置到Spring的Environment中. NacosPropertySourceLocator 顺着前面的分析思路,我们很自然 ...

  7. spring cloud学习(七)Spring Cloud Config(续)

    Spring Cloud Config(续) 个人参考项目 个人博客 : https://zggdczfr.cn/ 个人参考项目 : (整合到上一个案例中)https://github.com/Fun ...

  8. spring cloud学习(六)Spring Cloud Config

    Spring Cloud Config 参考个人项目 参考个人项目 : (希望大家能给个star~) https://github.com/FunriLy/springcloud-study/tree ...

  9. Spring Cloud 学习 (六) Spring Cloud Config

    在实际开发过程中,每个服务都有大量的配置文件,例如数据库的配置.日志输出级别的配置等,而往往这些配置在不同的环境中也是不一样的.随着服务数量的增加,配置文件的管理也是一件非常复杂的事 在微服务架构中, ...

随机推荐

  1. Arduino学习笔记① 初识Arduino

    1.前言     近段时间,博主陆续更新了ESP8266学习笔记,主要开发平台是Arduino.但是,对于很多无基础的初学者来说,甚至不了解Arduino是什么.因此,博主决定加入一个Arduino学 ...

  2. 继承+派生+分类+钻石继承(day20)

    目录 昨日内容 面对对象编程 类 定义类时发生的事情 调用类时发生的事情 init 对象 对象查找类的属性的顺序 对象的绑定方法 python中万物皆对象 今日内容 继承 什么是继承 为什么要继承 如 ...

  3. Kafka集群的安装和部署

    一.Kafka的下载与解压 http://kafka.apache.org/downloads.html下载kafka_2.11-1.1.1.tgz.gz并解压到/home/jun下 [jun@mas ...

  4. (七)javac编译

    文章目录 1.基本格式 2.目标路径 2.1 缺省项 2.2 指定路径 2.2.1 全路径 2.2.2 相对路径 3.源文件 3.1 无第三方库 3.1.1 基本方法 3.1.2 添加目录 3.1.3 ...

  5. FastReport快速实现条形码,二维码面单打印

    一.什么是FastReport? FastReport是功能齐全的报表控件,使开发者可以快速并高效地为·NET/VCL/COM/ActiveX应用程序添加报表支持. FastReport有很多产品,如 ...

  6. django-URL认识(一)

    URL由三部分组成:资源类型.存放资源的主机域名.资源文件名.也可认为由4部分组成:协议.主机.端口.路径 URL的一般语法格式为:(带方括号[]的为可选项): protocol :// hostna ...

  7. fenby C语言 P16

    while先判断,不符合,不执行 dowhile后判断,不符合,执行一次 #include <stdio.h> int main(){ int i=1,sum=0; do{ sum=sum ...

  8. C语言I—2019秋作业01

    1您对软件工程专业或计算机科学与技术专业了解是什么? 工程专业将成为一个新的热门专业.软件工程专业以计算机科学与技术学科为基础,突出软件开发的工程性,使学生在掌握计算机科学与技术方面知识和技能的基础上 ...

  9. C函数库ctype.h概况

    1 字符测试函数 1> 函数原型均为int isxxxx(int) 2> 参数为int, 任何实参均被提升成整型 3> 只能正确处理处于[0, 127]之间的值 2 字符映射函数 1 ...

  10. 一个九位数-python

    有一个9位数由1~9的9个数字组成, 每个数字只能出现一次:其第一位能被1整除, 前两位能被2整除, 前三位能被3整除...依次类推,前9位能被9整除.所有的9位数中,只有一个数字满足这些条件,请你输 ...