Cyan Blog Cyan Blog
首页
  • Java (opens new window)
  • JUC (opens new window)
  • JVM (opens new window)
  • Redis

    • Redis安装 (opens new window)
    • Redis基础 (opens new window)
    • Redis实战 (opens new window)
    • Redis集群安装 (opens new window)
    • Redis分布式缓存 (opens new window)
    • Redis多级缓存 (opens new window)
    • Redis原理 (opens new window)
  • 管理工具

    • Maven (opens new window)
    • Git (opens new window)
  • SSM

    • Spring (opens new window)
    • SpringBoot (opens new window)
    • Mybatis (opens new window)
    • MybatisPlus (opens new window)
  • 微服务

    • Docker (opens new window)
    • RabbitMQ (opens new window)
    • SpringCloud (opens new window)
    • Dubbo (opens new window)
    • MongoDB (opens new window)
    • Zookeeper (opens new window)
  • Java面试题 (opens new window)
  • JUC面试题 (opens new window)
  • JVM面试题 (opens new window)
  • Linux面试题 (opens new window)
  • SQL面试题 (opens new window)
  • Maven面试题 (opens new window)
  • Redis面试题 (opens new window)
  • SSM面试题 (opens new window)
  • SpringCloud面试题 (opens new window)
  • Linux (opens new window)
  • C++ (opens new window)
  • 数据库

    • MySQL (opens new window)
    • NoSQL (opens new window)
  • 软件测试

    • 软件测试 (opens new window)
  • 加密解密 (opens new window)
  • bilibili字幕提取 (opens new window)
  • 道理 (opens new window)
  • 关于博主

    • Github (opens new window)
    • CSDN (opens new window)
  • 关于本站

    • 如何搭建博客网站 (opens new window)
首页
  • Java (opens new window)
  • JUC (opens new window)
  • JVM (opens new window)
  • Redis

    • Redis安装 (opens new window)
    • Redis基础 (opens new window)
    • Redis实战 (opens new window)
    • Redis集群安装 (opens new window)
    • Redis分布式缓存 (opens new window)
    • Redis多级缓存 (opens new window)
    • Redis原理 (opens new window)
  • 管理工具

    • Maven (opens new window)
    • Git (opens new window)
  • SSM

    • Spring (opens new window)
    • SpringBoot (opens new window)
    • Mybatis (opens new window)
    • MybatisPlus (opens new window)
  • 微服务

    • Docker (opens new window)
    • RabbitMQ (opens new window)
    • SpringCloud (opens new window)
    • Dubbo (opens new window)
    • MongoDB (opens new window)
    • Zookeeper (opens new window)
  • Java面试题 (opens new window)
  • JUC面试题 (opens new window)
  • JVM面试题 (opens new window)
  • Linux面试题 (opens new window)
  • SQL面试题 (opens new window)
  • Maven面试题 (opens new window)
  • Redis面试题 (opens new window)
  • SSM面试题 (opens new window)
  • SpringCloud面试题 (opens new window)
  • Linux (opens new window)
  • C++ (opens new window)
  • 数据库

    • MySQL (opens new window)
    • NoSQL (opens new window)
  • 软件测试

    • 软件测试 (opens new window)
  • 加密解密 (opens new window)
  • bilibili字幕提取 (opens new window)
  • 道理 (opens new window)
  • 关于博主

    • Github (opens new window)
    • CSDN (opens new window)
  • 关于本站

    • 如何搭建博客网站 (opens new window)
  • SSM

  • SpringBoot

  • SpringCloud

    • SpringCloud
    • Nacos
      • OpenFeign
      • Sentinel
      • Gateway
      • Seata——分布式事务
    • Docker
    • Dubbo
    • MongoDB
    • Zookeeper
    • Spring生态
    • SpringCloud
    2025-01-17
    0
    0
    目录

    Nacos

    服务注册

    服务发现

    # 3.1基础入门

    Nacos /nɑ:kəus/ 是 Dynamic Naming and Configuration Service的⾸字⺟简称, —个更易于构 建云原生应用的动态服务发现 、配置管理和服务管理平台。

    安装

    ● Docker 安装

     9848:9848docker run -d -p 8848:8848 os/nacos-server:v2.4.3 -p 9848:9848 -e MODE=standalone --name nacos nac
    

    ● 下载软件包:nacos-server-2.4.3.zip (opens new window)

    ● 启动: startup.cmd -m standalone img

    # 依赖引入

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

    在 application.properties img 中配置如下

    spring.cloud.nacos.discovery.server-add r=127.0.0.1:8848
    #暂未⽤到配置中⼼功能,需要关闭配置检查
    #spring.cloud.nacos.config.import-check.enabled=false
    

    开启服务注册/发现功能

    @EnableDiscoveryClient //核⼼注解
    @SpringBootApplication
    public class OrderMainApplication {
        public static void main(String [] args) {
        	SpringApplication.run(OrderMainApplication.class, args);
        }
    }
    

    # 服务注册

    访问:http://localhost:8848/nacos 可以看到服务已经注册上来;

    # 注册更多

    1. 创建 service-product img 服务
    2. 引⼊ nacos依赖 img
    3. 配置 nacos地址信息 img

    a. 注意:每个微服务端⼝不—样

    1. 启动应⽤, 查看是否注册成功

    # 启动集群

    以 service-order img 为例, 启动 movie 的三个服务。

    1. idea 搜索 services img ⾯板, 添加 SpringBoot 项⽬ 。
    2. 复制 OrderMainApplication img 三份, 每个启动命令重新指定端⼝

    # 3.3.2. 动态刷新

    3.3.2.1. @RefreshScope

    配置更改不会自动生效,需要添加注解@RefreshScope才会自动刷新

    导入nacos后如果没有给项目指定与nacos关联的config属性,会报错,需要通过解决

    • 指定nacos为optional可选的spring.config.import=optional:nacos: instead

    • spring.cloud.nacos.config.import-check.enabled=false

    3.3.2.2. ConfigurationProperties⭐

    无需 @RefreshScope,会自动绑定配置,动态更新

    @Component
    @ConfigurationProperties(prefix = "order") //配置批量绑定在nacos下,可以⽆需@R
    efreshScope就能实现⾃动刷新
    @Data
    public class OrderProperties {
     String timeout;
     String autoConfirm;
     String dbUrl;
    }
    

    # 面试题

    Nacos中的数据集和application.properties有相同的配置项,哪个优先级高

    • 通过Nacos控制台或API发布的配置默认会覆盖本地配置,这是Spring Cloud Alibaba Nacos的默认行为,目的是实现配置的动态更新和集中管理。
    • 如果Nacos中未找到对应配置项,才会使用本地的配置。
    • Spring Cloud的PropertySource机制会优先使用后加载(优先使用后加载的配置项)的配置源。Nacos的配置通常在应用启动后通过Bootstrap阶段加载,优先级高于本地文件。
    spring:
    	profile: 
    		active: dev
    	application:
    		name:service-order
        cloud:
        	nacos:
        		server-addr: 127.0.0.1:8848
        		config:
                    import-check:
                    	enabled: false
                    namespace: ${spring.profiles.active:public}
    ---
    spring:
        config:
            import:
                nacos: common.properties?group=order
                nacos: database.properties?group=order
        activate:
        	on-profile: dev
    ---
    spring:
        config:
        	import:
                nacos: common.properties?group=order
                nacos: database.properties?group=order
                nacos: haha.properties?group=order
        activate:
        	on-profile: prod
    

    :public表示默认值为 public

    上次更新: 2025/6/10 17:21:17
    SpringCloud
    OpenFeign

    ← SpringCloud OpenFeign→

    最近更新
    01
    md
    06-10
    02
    Redis
    06-10
    03
    HBase
    06-10
    更多文章>
    Theme by Vdoing | Copyright © 2025-2025 Cyan Blog
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式