【SpringCloud应用框架】Nacos服务配置中心

第四章 Spring Cloud Alibaba Nacos之服务配置中心



一、基础配置

Nacos不仅仅可以作为注册中心来使用,同时它支持作为配置中心。
在这里插入图片描述

二、新建子项目

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

1.pom文件

这里我们主要要引入的是此依赖。

<dependency> 
    <groupId> com.alibaba.cloud </groupId> 
    <artifactId> spring-cloud-starter-alibaba-nacos-config </artifactId> 
</dependency>

这个依赖依据在官网上可以找到:官网依据

2.YML配置

要注意,这里要两个配置文件,因为Nacos同SpringCloud-config一样,在项目初始化时,要保证先从配置中心进行配置拉取,拉取配置之后,才能保证项目的正常启动。
springboot中配置文件的加载是存在优先级顺序的,bootstrap优先级高于application。
分别要配置的是,这里bootstrap.yml配置好了以后,作用是两个,第一个让8899这个服务注册到Nacos中,第二个作用就是去Nacos中去读取指定后缀为yaml的配置文件:

bootstrap.yml

# nacos配置
server:
  port: 8899

spring:
  application:
    name: nacos-config-client
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848 #Nacos服务注册中心地址
      config:
        server-addr: localhost:8848 #Nacos作为配置中心地址
        file-extension: yaml #指定yaml格式的配置

application.yml

spring:
  profiles:
    active: dev # 表示开发环境

3.启动类

package com.cy.cloudalibabaconfig8899;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class CloudalibabaConfig8899Application {

    public static void main(String[] args) {
        SpringApplication.run(CloudalibabaConfig8899Application .class, args);
    }

}

4.业务类

package com.cy.cloudalibabaconfig8899.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope //支持Nacos的动态刷新功能
public class ConfigClientController {

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/config/info")
    public String getConfigInfo(){
        return configInfo;
    }

}

这里的@RefreshScope实现配置自动更新,意思为如果想要使配置文件中的配置修改后不用重启项目即生效,可以使用@RefreshScope配置来实现。

5.Nacos配置规则

在 Nacos Spring Cloud 中,dataId 的完整格式如下(详情可以参考官网):

${prefix}-${spring.profiles.active}.${file-extension}
  1. prefix 默认为 spring.application.name 的值,也可以通过配置项 spring.cloud.nacos.config.prefix来配置。
  2. spring.profiles.active 即为当前环境对应的 profile,注意:spring.profiles.active 为空时,对应的连接符 - 也将不存在,dataId 的拼接格式变成 ${prefix}.${file-extension}(不能删除)
  3. file-exetension 为配置内容的数据格式,可以通过配置项 spring.cloud.nacos.config.file-extension 来配置。目前只支持 propertiesyaml 类型。
  4. 通过 Spring Cloud 原生注解 @RefreshScope 实现配置自动更新:
  5. 所以根据官方给出的规则我们最终需要在Nacos配置中心添加的配置文件的名字规则和名字为:
# ${spring.application.name}-${spring.profiles.active}.${file-extension}
# nacos-config-client-dev.yaml
# 微服务名称-当前环境-文件格式

在这里插入图片描述

三、Nacos平台创建配置操作

增加配置:
在这里插入图片描述
在这里插入图片描述

config: 
    info: nacos config center,version = 1

然后在配置中心就会看到刚刚发布的配置:
在这里插入图片描述

四、自动配置更新

修改Nacos配置,不需要重启项目即可自动刷新:
在这里插入图片描述
修改版本号为2,点击发布:
在这里插入图片描述

五、测试

启动服务访问服务来测试(没有修改之前是1,修改之后不需要重启项目既可以直接获取最新配置):http://localhost:8899/config/info
在这里插入图片描述

在本地项目中,如果你需要从Nacos中读取YAML格式的配置,你需要先确保你的项目集成有Nacos客户端库,并设置好相关的依赖。以下是基本步骤: 1. **添加依赖**: 在Maven或Gradle项目中添加Nacos SDK的依赖。例如,如果你使用的是Spring Cloud Alibaba,可以在`pom.xml`或`build.gradle`中加入以下内容(Maven示例): ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-nacos-discovery</artifactId> </dependency> ``` 2. **配置Nacos连接信息**: 配置Nacos的地址、端口、命名空间和应用名称等基本信息。通常在Spring Boot的`application.yml`或`application.properties`文件中添加Nacos配置: ```yaml spring: application: name: your-app-name cloud: nacos: discovery: server-addr: localhost:8848 namespace: default config: center: enabled: true refresh: true ``` 或者在properties文件中: ```properties spring.application.name=your-app-name spring.cloud.nacos.discovery.server-addr=localhost:8848 spring.cloud.nacos.config.center.enabled=true spring.cloud.nacos.config.center.refresh=true ``` 3. **获取配置数据**: 使用Spring Cloud Alibaba提供的`@ConfigurationProperties`注解或者`NacosPropertySourceLocator`来动态加载Nacos中的YAML配置。例如,在一个配置类中: ```java @ConfigurationProperties("your-config-key") public class AppConfig { private String property1; private int property2; // getters and setters } ``` 然后在启动类或其他地方注入这个配置类并访问属性。 4. **读取配置时**: 在代码运行时,Spring会自动从Nacos加载配置数据到对应的配置类实例中。通过调用`AppConfig`类的属性,即可获取到从Nacos读取的YAML配置值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值