nacos配置管理

本文介绍了如何使用Nacos作为配置中心,实现实时配置管理和热更新,包括配置文件结构、如何声明Nacos地址、配置文件优先级以及两种实现热更新的方法:@Value和@ConfigurationProperties。
摘要由CSDN通过智能技术生成

nacos不仅可以作注册中心,还可以做配置管理实现热更新和对多个服务统一进行配置管理(需要更改配置时,就不用一个一个的去服务中改了),就是把需要变动的配置(比如开关配置)写在nacos配置管理中,服务启动时执行的是服务本身的配置加上nacos中的配置。且更改nacos中的配置,服务不需要重启就可以立即更新执行。

新建的nacos配置的date id必填,一般格式为:服务名称- profile(开发环境).后缀名。

 在这其中配置nacos配置,点击右上方的➕ ,添加新配置:

这就是userservice服务public环境下的配置。然后点击发布就行了。 

 

服务运行的过程是先读取nacos中的配置再去读取本地服务的配置(application.yml),然后再去创建spring容器加载bean。

在服务读取nacos中的配置之前,服务肯定要知道nacos的地址,但这个地址肯定不能写在本地配置中(application.yml),因为在访问完nacos配置后才会去访问本地配置。所以我们需要在服务中新建一个bootstrap.yml引导文件,这个引导文件比application.yml的优先级高很多。

spring:
  application:
    name: userservice  # 服务名称
  profiles:
    active: public # 开发环境
  cloud:
    nacos:
      server-addr: localhost:8848 # nacos地址
      config:
        file-extension: yaml # 文件后缀名
#        cluster-name: HZ

 在该文件中声明好nacos的地址、服务名称和环境及对应集群。

nacos配置多环境配置共享。当nacos的多个不同配置环境中有大量配置是重复的时候,我们可以把重复代理统一写入一个总配置中,命名为:服务名称.后缀名。保持服务名称一致,这样不同环境的服务在读取本环境的nacos配置时还会读取该总配置。

 就比如其中的userservice.yml配置  。配置读取优先级:bootstrap.yml>服务本环境nacos配置(userservice- public.yaml)>相同服务名称nacos总配置(userservice.yml)>本地配置(application.yml)

下面是实现nacos配置热更新:就是更改nacos中的配置后,不需要重启该服务就可以立马生效。

让我们先写个请求,好在页面中显示出配置

package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;ser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope
public class UserController {

    @Autowired
    private PatternProperties properties;

    @GetMapping("/prop")
    public PatternProperties properties(){
        return properties;
    }

    @GetMapping("now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));
    }
}

访问http://localhost:8081/user/now  网址就可以显示出现在的时间。

实现热更新方法一:

使用@Value注入和@RefreshScope注解

package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
@RefreshScope
public class UserController {

    @Autowired
    private PatternProperties properties;

    
     @Value("${pattern.dateformat}")
     private String dateformat;
     @GetMapping("/now")
     public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(dateformat));
    }
}

实现热更新方法二:使用@ConfigurationProperties(prefix = "pattern")注解

首先我们需要新建一个config配置类

package cn.itcast.user.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@Data
@Component
@ConfigurationProperties(prefix = "pattern")//前缀名与变量名一致就可以实现变量的自动注入
public class PatternProperties {
    private String dateformat;
}

然后是controller层:

package cn.itcast.user.web;

import cn.itcast.user.config.PatternProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Slf4j
@RestController
@RequestMapping("/user")
//@RefreshScope
public class UserController {

    @Autowired
    private PatternProperties properties;


    @GetMapping("now")
    public String now(){
        return LocalDateTime.now().format(DateTimeFormatter.ofPattern(properties.getDateformat()));
    }
}

上面俩个方法任选其一就行。

我们现在配置的时间格式是:yyyy-MM-dd HH:mm:ss

网页中显示的是:

然后修改nacos中的格式:

 编辑userservice-public.yaml,修改配置内容:

 将yyyy-MM-dd改成了yyyy/MM/dd

然后发布。在回到http://localhost:8081/user/now 网址,刷新该网址就可以了。就发现时间格式已经变动了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值