Spring Cloud Alibaba(五) Nacos Config 分布式配置中心

为什么Nacos Config?

在分布式系统中,由于服务特别的多,那是不是我的配置文件也特别的多,我总不可能要改配置就一个个改把,打个比方我需要修改数据库的地址账号密码等,我一个个提供方去改,这未免效率也太低了把(〃>皿<)

所以这个时候我们就需要Nacos Config

Nacos Config 介绍

Nacos 提供用于存储配置和其他元数据的 key/value 存储,为分布式系统中的外部化配置提供服务器端和客户端支持。使用 Spring Cloud Alibaba Nacos Config,您可以在 Nacos Server 集中管理你 Spring Cloud 应用的外部属性配置。

如果有学习过Eureka 的Config应该就知道了.

就是Spring Cloud Config Server 和 Client 的代替方案,也就是Nacos 的 Config …

Nacos Config 的缺点: 没有提供配置的管理,还是我们需要自己去管理.

Nacos Config 的优点:
方便呀! 直接给你集成进了Nacos还不爽?嘻嘻. 其实在 Config Server的优点都有.

使用过程

POM

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>${latest.version}</version>
</dependency>

Nacos注册中心

进入Nacos中心 新建配置

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

创建名为 bootstrap.yml 的配置文件并删除之前创建的 application.yml 配置文件

知道config server的都知道,不删除还是会走自己这里的application.yml

注意:** Spring Boot 配置文件的加载顺序,依次为 bootstrap.properties -> bootstrap.yml -> application.properties -> application.yml ,其中 bootstrap.properties 配置为最高优先级

# 注意了 :  配置顺序不要放反了,不然会出问题

spring:
  application:
    #nacos配置文件的名字
    name: service-consumer-config

  cloud:
    nacos:
      config:
        #nacos的端口
        server-addr: localhost:8848
        #指明是什么格式,不加默认是propertes
        file-extension: yaml
        

搞定收工

在这里插入图片描述

附:扩展阅读

Endpoint 信息查看

Spring Boot 应用支持通过 Endpoint 来暴露相关信息,Nacos Config Starter 也支持这一点。在使用之前需要在 maven 中添加 spring-boot-starter-actuator 依赖,并在配置中允许 Endpoints 的访问。

{
    "subscribe": [
        {
            "jsonFromServer": "",
            "name": "service-consumer",
            "groupName": "DEFAULT_GROUP",
            "clusters": null,
            "cacheMillis": 1000,
            "hosts": [],
            "lastRefTime": 0,
            "checksum": "",
            "allIPs": false,
            "key": "service-consumer",
            "valid": true,
            "keyEncoded": "service-consumer"
        },
        {
            "jsonFromServer": "",
            "name": "service-provider",
            "groupName": "DEFAULT_GROUP",
            "clusters": null,
            "cacheMillis": 1000,
            "hosts": [],
            "lastRefTime": 0,
            "checksum": "",
            "allIPs": false,
            "key": "service-provider",
            "valid": true,
            "keyEncoded": "service-provider"
        }
    ],
    "NacosDiscoveryProperties": {
        "serverAddr": "192.168.141.132:8848",
        "endpoint": "",
        "namespace": "",
        "watchDelay": 30000,
        "logName": "",
        "service": "service-consumer",
        "weight": 1,
        "clusterName": "DEFAULT",
        "namingLoadCacheAtStart": "false",
        "metadata": {
            "preserved.register.source": "SPRING_CLOUD"
        },
        "registerEnabled": true,
        "ip": "192.168.141.1",
        "networkInterface": "",
        "port": 8080,
        "secure": false,
        "accessKey": "",
        "secretKey": ""
    }
}
{
    "NacosConfigProperties": {
        "serverAddr": "192.168.141.132:8848",
        "encode": null,
        "group": "DEFAULT_GROUP",
        "prefix": null,
        "fileExtension": "yaml",
        "timeout": 3000,
        "endpoint": null,
        "namespace": null,
        "accessKey": null,
        "secretKey": null,
        "contextPath": null,
        "clusterName": null,
        "name": null,
        "sharedDataids": null,
        "refreshableDataids": null,
        "extConfig": null
    },
    "RefreshHistory": [
        {
            "timestamp": "2019-06-30 18:30:45",
            "dataId": "service-consumer-config.yaml",
            "md5": "690fd78b5ae9fac5b545c1a8d7ec4e2b"
        },
        {
            "timestamp": "2019-06-30 18:22:08",
            "dataId": "service-consumer-config.yaml",
            "md5": "40b12ffc9d305ec1cad65d606e8f4708"
        }
    ],
    "Sources": [
        {
            "lastSynced": "2019-06-30 18:22:05",
            "dataId": "service-consumer-config.yaml"
        }
    ]
}

注意: Sources 表示此客户端从哪些 Nacos Config 配置项中获取了信息,RefreshHistory 表示动态刷新的历史记录,最多保存20条,NacosConfigProperties 则为 Nacos Config Starter 本身的配置

更多配置

配置项 key 默认值 说明
服务端地址 spring.cloud.nacos.config.server-addr
DataId前缀 spring.cloud.nacos.config.prefix spring.application.name
Group spring.cloud.nacos.config.group DEFAULT_GROUP
dataID后缀及内容文件格式 spring.cloud.nacos.config.file-extension properties dataId的后缀,同时也是配置内容的文件格式,目前只支持 properties
配置内容的编码方式 spring.cloud.nacos.config.encode UTF-8 配置的编码
获取配置的超时时间 spring.cloud.nacos.config.timeout 3000 单位为 ms
配置的命名空间 spring.cloud.nacos.config.namespace 常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源隔离等。
AccessKey spring.cloud.nacos.config.access-key
SecretKey spring.cloud.nacos.config.secret-key
相对路径 spring.cloud.nacos.config.context-path 服务端 API 的相对路径
接入点 spring.cloud.nacos.config.endpoint UTF-8 地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址
是否开启监听和自动刷新 spring.cloud.nacos.config.refresh.enabled true

更多介绍

Nacos为用户提供包括动态服务发现,配置管理,服务管理等服务基础设施,帮助用户更灵活,更轻松地构建,交付和管理他们的微服务平台,基于 Nacos, 用户可以更快速的构建以“服务”为中心的现代云原生应用。Nacos 可以和 Spring CloudKubernetes/CNCFDubbo 等微服务生态无缝融合,为用户提供更卓越的体验

配置文件动态刷新

例如:

这里我们使用 @Value 注解来将对应的配置注入到 某个类某个变量字段,并添加 @RefreshScope 打开动态刷新功能

发布了56 篇原创文章 · 获赞 3 · 访问量 4417

猜你喜欢

转载自blog.csdn.net/Tang_5253/article/details/102751654