23

SpringCloud Nacos 配置中心

 5 years ago
source link: http://www.eknown.cn/index.php/springcloud/config-nacos.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

上一篇介绍了 Spring Cloud 中配置中心的使用,地址: https://mp.weixin.qq.com/s/QcIaGAYUvPBIqJM8oMbVvQ

这一节介绍 Nacos 作为配置中心使用,不需要 Github

这里首先需要注意,Nacos 在Spring、Spring Boot、Spring Cloud 这三种环境下的依赖、配置和使用方法都是不同的。这里仅以 Spring Cloud + Nacos 为例,阐述 Spring Cloud 中使用 Nacos 作为服务注册中心和配置中心。更具体的资料可以参考官方文档: https://nacos.io/zh-cn/docs/quick-start.html

一、Nacos Server 创建配置

启动 Nacos Server 端,浏览器访问。

在 Nacos 中创建三个配置文件: consumer-demo.yamlconsumer-demo-dev.yamlconsumer-demo-prod.yaml ,内容略有区别。

i2EfAbz.png!web

consumer-demo.yml

JbmyUna.png!web

二、Nacos-Consumer

创建一个 consumer-demo 项目,依赖如下:

<properties>
    <java.version>1.8</java.version>
    <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
    <nacos.version>0.9.0.RELEASE</nacos.version>
</properties>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

在启动类加上 @EnableDiscoveryClient 注解(这表示是启用服务注册,也可以不启用而仅从 Nacos 读取配置信息)。

删除 application.yml

配置信息应写在 bootstrap.propertiesbootstrap.yml 文件中:

spring:
  application:
    name: consumer-demo
  cloud:
    nacos:
      config:
        file-extension: yaml
        server-addr: 127.0.0.1:8848
        prefix: ${spring.application.name} # 默认值就是 spring.application.name
      discovery:
        server-addr: 127.0.0.1:8848
  profiles:
    active: dev

查找的配置文件,即 nacos 中的 dataId,应该是: ${prefix}-${spring.profile.active}.${file-extension}

如果 ${active} 不存在,那么会查找: ${prefix}.${file-extension}

启动项目,控制台出现类似如下信息:

2020-04-15 17:00:17.946  INFO 16392 --- [           main] o.s.c.a.n.c.NacosPropertySourceBuilder   : Loading nacos data, dataId: 'consumer-demo.yaml', group: 'DEFAULT_GROUP'
2020-04-15 17:00:17.953  INFO 16392 --- [           main] o.s.c.a.n.c.NacosPropertySourceBuilder   : Loading nacos data, dataId: 'consumer-demo-dev.yaml', group: 'DEFAULT_GROUP'
2020-04-15 17:00:17.955  INFO 16392 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: [BootstrapPropertySource {name='bootstrapProperties-consumer-demo-dev.yaml'}, BootstrapPropertySource {name='bootstrapProperties-consumer-demo.yaml'}]
2020-04-15 17:00:17.960  INFO 16392 --- [           main] c.e.c.ConsumerDemoApplication            : The following profiles are active: dev

我们可以看到从 Nacos 加载了 consumer-demo.yaml 和 consumer-demo-dev.yaml

编写一个测试接口:

@RefreshScope // 注意要加上 @RefreshScope 注解,否则即使 RefreshEventListener 检测到配置信息被更新了,下面的 msg 还是会保持之前的值
@RestController
@RequestMapping(value = "test")
public class TestAction {

    @NacosValue("${msg}")
    private String msg; // 用 @NacosValue 获取不到值 (null)

    @Value("${msg}")
    private String vMsg; // 用 @Value 获取到了值

    @GetMapping(value = "")
    public String hello() {
        return msg + ":" + vMsg;
    }
}

打开浏览器,输入 test 接口地址:

faYFnmi.png!web

在 nacos 上修改一下 msg 的值,观察控制台,出现:

2020-04-15 17:11:43.355  INFO 16392 --- [-127.0.0.1_8848] o.s.c.e.event.RefreshEventListener       : Refresh keys changed: [msg]

表明检测到 msg 配置信息改变。刷新 test 接口,发现 msg 的值随之改变了。

三、持久化

关闭 nacos server,重新启动,发现上面创建的配置信息没有丢失,这说明 nacos 把配置信息持久化存储了。

Nacos 默认使用嵌入式数据库,也支持修改数据源为 MySQL

参考:


Recommend

  • 8

    SpringCloud Nacos 本文主要分为SpringCloud Nacos的设计思路 简单分析一下触发刷新事件后发生的过程以及一些踩坑经验 org.springframework.cloud.bootstrap.config.PropertySourceLocator

  • 13
    • tomoya92.github.io 4 years ago
    • Cache

    SpringCloud学习记录 - Nacos

    作者:朋也 日期:2021-02-02 版权声明:自由转载-非商用-非衍生-保持署名( 创意共享3.0许可证 ) Nacos是啥? Nacos是alibaba...

  • 5
    • blog.winterchen.com 3 years ago
    • Cache

    SpringCloud系列教程(二)之Nacos

    阅读提醒: 本文面向的是有一定springboot基础者 本次教程使用的Spring Cloud Hoxton RELEASE版本 什么是注册中心? 什么是配置中心? 如何在springcloud中使用Nacos?

  • 3
    • www.cnblogs.com 3 years ago
    • Cache

    springcloud~nacos中的共享配置文件

    共享配置文件,即多个应用配置中有共同的配置项,如redis,mysql,hystrix等等,这些公共配置,我们可以抽象出来,通过nacos的shared-configs来共享。 共享配置,需要有扩展名,我们以yaml为例,添加一个例子 const.yaml const: ema...

  • 4

    Nacos作为配置中心,必然需要保证服务节点的高可用性,那么Nacos是如何实现集群的呢?下面这个图,表示Nacos集群的部署图。Nacos集群工作原理...

  • 7

    springAlibaba实践(11)----Nacos配置中心配置 Nacos配置中心我们还可以用nacos作为配置中...

  • 8

    前面我们分析了携程的 apollo(见 详解apollo的设计与使用),现在再来看看阿里的 nacos。 和 apollo 一样,nacos 也是一款配置中心,同样可以实现配置的集中管理、...

  • 5
    • blog.51cto.com 2 years ago
    • Cache

    Nacos 配置中心用法详细教程

    一、为什么需要配置中心:在没有配置中心之前,传统应用配置的存在以下痛点:(1)采用本地静态配置,无法保证实时性:修改配置不灵活且需要经过较长的测试发布周期,无法尽快通知到客户端,还有些配置对实时性要求很高,...

  • 7

    微服务 - 配置中心 - Nacos 🏆 一个有梦有戏的人 @怒放吧德德

  • 4

    教你用 Python 驾驭 Nacos 配置中心 作者:星安果 2023-08-03 08:51:07 Nacos 是阿里巴巴开源的项目,用于构建云原生应用的动态服务发现、配置管理和服务管理平台,核心特征包含:服务发现、服务健康监测、动态配...

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK