复习

  • 导入依赖
  • 在子模块里面创建 bootstrap.yaml 文件
  • bootstrap.yaml 文件 添加需要的远程配置信息
  • 开始拆分以前的配置信息

未拆分前的yaml文件

在这里插入图片描述

拆分配置文件

在这里插入图片描述

dataSource.yaml

存放数据源等信息
在这里插入图片描述

mybatis.yaml

在这里插入图片描述

other.yaml

剩余其他配置填到这里
在这里插入图片描述

微服务同时加载多个远程配置文件

修改配置文件

bootstrap.yaml

spring:
  application:
    # 应用程序的地址
    name: gulimall-coupon
  cloud:
    nacos:
      config:
        # nacos config的地址
        server-addr: 127.0.0.1:8848
        # 指定命名空间
        namespace: efac0368-c0e4-4e46-aa50-f86cf767afbd
        ext-config[0]:
          data-id: dataSource.yaml
          group: dev
          refresh: true
        ext-config[1]:
          # 指定data id
          data-id: mybatis.yaml
          # 指定分组
          group: dev
          # 当前配置文件更改实现动态刷新
          refresh: true
        ext-config[2]:
          data-id: other.yaml
          group: dev
          refresh: true
        # 读取指定yaml格式的配置
        file-extension: yaml
        # 指定分组
        group: dev

再次启动子模块,控制台会提示出 加载了哪些配置文件

2023-07-14 18:15:32.538  INFO 15120 --- [  restartedMain] b.c.PropertySourceBootstrapConfiguration :
 Located property source: CompositePropertySource 
 {name='NACOS',
  propertySources=
  	[NacosPropertySource {name='gulimall-coupon.yaml'}, 
  	NacosPropertySource {name='other.yaml'}, 
  	NacosPropertySource {name='mybatis.yaml'}, 
  	NacosPropertySource {name='dataSource.yaml'}]
  }

测试调用接口,确保调取成功:
在这里插入图片描述

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐