当前位置: 首页 > news >正文

[0629].第29节:配置中心业务规则与动态刷新

我的后端学习大纲

SpringCloud学习大纲


1、编码实现3377服务:

1.1.建module:

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


1.2.改pom:

在这里插入图片描述

1.3.写YML:

1.Nacos同Consul一样,在项目初始化时,要保证先从配置中心进行配置拉取,拉取配置之后,才能保证项目的正常启动为了满足动态刷新和全局广播通知,springboot中配置文件的加载是存在优先级顺序的,bootstrap优先级高于application

a.bootstrap.properties:

  • 1.必须使用bootstrap.properties配置文件来配置Nacos Server
# nacos配置
spring:application:name: nacos-config-clientcloud:nacos:discovery:server-addr: localhost:8848 #Nacos服务注册中心地址config:server-addr: localhost:8848 #Nacos作为配置中心地址file-extension: yaml #指定yaml格式的配置# nacos端配置文件DataId的命名规则是:
# ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension}
# 本案例的DataID是:nacos-config-client-dev.yaml

b.application:

  • 1.与Nacos无关的配置写在Application中:
server:port: 3377spring:profiles:active: dev # 表示开发环境#active: prod # 表示生产环境#active: test # 表示测试环境

1.4.主启动:

package com.atguigu.cloudalibaba;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;@EnableDiscoveryClient
@SpringBootApplication
public class NacosConfigClient3377{public static void main(String[] args){SpringApplication.run(NacosConfigClient3377.class,args);}
}

1.5.业务类:

package com.atguigu.cloud.controller;import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
@RefreshScope //在控制器类加入@RefreshScope注解使当前类下的配置支持Nacos的动态刷新功能。
public class NacosConfigClientController
{@Value("${config.info}")private String configInfo;@GetMapping("/config/info")public String getConfigInfo() {return configInfo;}
}

@RefreshScope 注解来实现配置的自动更新


2、在Nacos中的匹配规则:

2.1.设置DataId理论:

a.官网介绍:

  • 1.Nacos中的DataId的组成格式及与SpringBoot配置文件匹配的规则:

最后公式:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

b.配置DataId实操:

  • 公式:${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
  • prefix默认值:spring.application.name的值
  • spring.profiles.active即为当前环境对应的profile,可以通过配置项:spring.profiles.active来配置
  • file-extension为配置文件的数据格式
    在这里插入图片描述

2.2.创建配置:

在这里插入图片描述

2.3.配置DataId和对应文件:

  • Data ID:nacos-config-client-dev.yaml
    在这里插入图片描述

说明:

  • 配置更改后可以自动刷新
  • Nacos会记录配置文件的历史版本,默认保留30天,此外还有一键回滚的功能,回滚也会触发配置更新
http://www.lryc.cn/news/500017.html

相关文章:

  • mac: docker : Command not found解决
  • Django drf基于APIView 快速使用
  • 【MarsCode】每日一题数组 之 数字分组求偶数和
  • 解决:error: subprocess-exited-with-error 的问题
  • 使用腾讯混元(HunYuanVideo)视频模型FP8量化版本来生成绅士动画,模型体积30G,8G甜品卡可玩,2秒视频需要15分钟
  • 使用Ancona安装node,安装vue
  • 如何“安装Android SDK“?
  • 天童教育:提升孩子的语言表达能力
  • Node.js中JWT的token完整生命周期管理:从生成到销毁
  • Kotlin报错:lateinit property xxx has not been initialized
  • debian编译失败
  • flink-connector-mysql-cdc:03 mysql-cdc常见问题汇总
  • JSP技术发展现状
  • 浏览器同源策略、跨域、跨域请求,服务器处理没、跨域解决方案
  • flink-connector-mysql-cdc:02 mysql-cdc高级扩展
  • Couchbase 简介
  • 我们来学mysql -- 事务并发之幻读(原理篇)
  • Ubuntu Linux 图形界面工具管理磁盘分区和文件系统(八)
  • Eclipse IDE 各个版本的用途和区别
  • 国产GPU中,VLLM0.5.0发布Qwen2.5-14B-Instruct-GPTQ-Int8模型,请求返回结果乱码
  • 在 Vue 3 中实现点击按钮后禁止浏览器前进或后退
  • Linux:软硬链接
  • Delphi XE 安卓Web开发 错误:net::ERR_CLEARTEXT_NOT_PERMITTED
  • 深入理解malloc与vector:内存管理的对比
  • 多个输入框联合搜索
  • 笔记03----NeurIPS2024 涨点!SSA:用于语义分割的语义和空间自适应像素级分类器(即插即用)
  • 自定义比较函数 down 作为 sort 函数的参数实现数组元素从大到小排序
  • 在 Spring Boot 中使用 JPA(Java Persistence API)进行数据库操作
  • 简单聊聊PLT和GOT
  • FaRM译文