• Spring Cloud 拉取 Nacos 中配置文件


    一.读取配置文件流程

    1.程序启动首先读取应用中bootstarp.yml配置文件
    2.再到 nacos 读取 DEFAULT_GROUP 默认分组下的
      application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} 配置文件。
      例如:application-dev.yml
    3.最后到 nacos 读取 ${spring.cloud.nacos.config.group} 配置分组下的
      ${spring.application.name}.${spring.cloud.nacos.config.file-extension} 配置文件。
      例如:kafka-test-dev.yml

    二.配置bootstrap.yml文件

    1. server:
    2. port: 8888
    3. spring:
    4. profiles:
    5. active: @profiles.active@
    6. application:
    7. name: @artifactId@
    8. cloud:
    9. nacos:
    10. #nacos配置中心(将应用服务注册到nacos中)
    11. discovery:
    12. server-addr: localhost:8848
    13. # 分组
    14. group: TEST_DEMO
    15. # 命名空间
    16. namespace: b3d6b8c4-e85c-4935-9b05-d1b512891a1b
    17. #nacos注册中心(从nacos中拉取配置文件)
    18. config:
    19. server-addr: localhost:8848
    20. # 指定配置文件后缀名
    21. file-extension: yml
    22. group: TEST_DEMO
    23. namespace: b3d6b8c4-e85c-4935-9b05-d1b512891a1b
    24. # 指定读取共享配置文件名称,多个使用逗号隔开
    25. shared-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
    26. # 指定哪些共享配置文件支持动态刷新,多个使用逗号隔开
    27. refreshable-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

    ps: 如果读取不到@@值,在maven的pom文件配置以下插件即可

    1. <build>
    2. <plugins>
    3. <plugin>
    4. <groupId>org.apache.maven.plugins</groupId>
    5. <artifactId>maven-resources-plugin</artifactId>
    6. <configuration>
    7. <delimiters>@</delimiters>
    8. <useDefaultDelimiters>false</useDefaultDelimiters>
    9. </configuration>
    10. </plugin>
    11. </plugins>
    12. <resources>
    13. <resource>
    14. <directory>src/main/resources</directory>
    15. <filtering>true</filtering>
    16. </resource>
    17. </resources>
    18. </build>

    三.配置pom.xml文件

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <project xmlns="http://maven.apache.org/POM/4.0.0"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    5. <modelVersion>4.0.0</modelVersion>
    6. <groupId>com.kafka.test</groupId>
    7. <artifactId>kafka-test</artifactId>
    8. <version>1.0-SNAPSHOT</version>
    9. <properties>
    10. <maven.compiler.source>8</maven.compiler.source>
    11. <maven.compiler.target>8</maven.compiler.target>
    12. <spring-boot.version>2.4.2</spring-boot.version>
    13. <spring-cloud.version>2020.0.1</spring-cloud.version>
    14. <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
    15. </properties>
    16. <dependencies>
    17. <!--注册中心客户端-->
    18. <dependency>
    19. <groupId>com.alibaba.cloud</groupId>
    20. <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    21. </dependency>
    22. <!--配置中心客户端-->
    23. <dependency>
    24. <groupId>com.alibaba.cloud</groupId>
    25. <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    26. </dependency>
    27. <!--bootstrap 启动器(读取配置nacos文件的必须依赖)-->
    28. <dependency>
    29. <groupId>org.springframework.cloud</groupId>
    30. <artifactId>spring-cloud-starter-bootstrap</artifactId>
    31. </dependency>
    32. <!-- spring-boot 启动器 -->
    33. <dependency>
    34. <groupId>org.springframework.boot</groupId>
    35. <artifactId>spring-boot-starter-web</artifactId>
    36. </dependency>
    37. <!--kafka 依赖-->
    38. <dependency>
    39. <groupId>org.springframework.kafka</groupId>
    40. <artifactId>spring-kafka</artifactId>
    41. </dependency>
    42. </dependencies>
    43. <dependencyManagement>
    44. <dependencies>
    45. <!-- spring boot 依赖(有此依赖不需要在parent节点配置spring boot依赖信息) -->
    46. <dependency>
    47. <groupId>org.springframework.boot</groupId>
    48. <artifactId>spring-boot-dependencies</artifactId>
    49. <version>${spring-boot.version}</version>
    50. <type>pom</type>
    51. <scope>import</scope>
    52. </dependency>
    53. <!-- spring cloud 依赖 -->
    54. <dependency>
    55. <groupId>org.springframework.cloud</groupId>
    56. <artifactId>spring-cloud-dependencies</artifactId>
    57. <version>${spring-cloud.version}</version>
    58. <type>pom</type>
    59. <scope>import</scope>
    60. </dependency>
    61. <!-- spring cloud alibaba 依赖 -->
    62. <dependency>
    63. <groupId>com.alibaba.cloud</groupId>
    64. <artifactId>spring-cloud-alibaba-dependencies</artifactId>
    65. <version>${spring-cloud-alibaba.version}</version>
    66. <type>pom</type>
    67. <scope>import</scope>
    68. </dependency>
    69. </dependencies>
    70. </dependencyManagement>
    71. <profiles>
    72. <profile>
    73. <id>dev</id>
    74. <properties>
    75. <profiles.active>dev</profiles.active>
    76. </properties>
    77. <activation>
    78. <activeByDefault>true</activeByDefault>
    79. </activation>
    80. </profile>
    81. </profiles>
    82. <build>
    83. <plugins>
    84. <plugin>
    85. <groupId>org.springframework.boot</groupId>
    86. <artifactId>spring-boot-maven-plugin</artifactId>
    87. </plugin>
    88. <plugin>
    89. <groupId>org.apache.maven.plugins</groupId>
    90. <artifactId>maven-resources-plugin</artifactId>
    91. <configuration>
    92. <delimiters>@</delimiters>
    93. <useDefaultDelimiters>false</useDefaultDelimiters>
    94. </configuration>
    95. </plugin>
    96. </plugins>
    97. <resources>
    98. <resource>
    99. <directory>src/main/resources</directory>
    100. <filtering>true</filtering>
    101. </resource>
    102. </resources>
    103. </build>
    104. </project>

    四.在nacos中配置application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}文件

    例如:application-dev.yml

    1. spring:
    2. #kafka
    3. kafka:
    4. bootstrap-servers: localhost:9092
    5. producer:
    6. retries: 0
    7. batch-size: 16384
    8. buffer-memory: 33554432
    9. acks: 1
    10. consumer:
    11. group-id: TestGroup
    12. auto-offset-reset: earliest
    13. enable-auto-commit: false
    14. auto-commit-interval: 5000
    15. key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
    16. value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
    17. listener:
    18. concurrency: 3
    19. ack-mode:manual_immediate

    六.配置${spring.application.name}-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension} 文件

    例如:kafka-test-dev.yml

    1. user:
    2. name: zhangsan
    3. email: 1100000@qq.com

  • 相关阅读:
    深圳市第十二届职工技术创新运动会暨2022年深圳技能大赛—集成电路应用开发职业技能竞赛
    在线问诊 Python、FastAPI、Neo4j — 创建 检查节点
    【Java初阶】面向对象三大特性之多态
    最新泛微java面试题及答案
    LM1875L-TB5-T 音频功率放大器 PDF中文资料_参数_引脚图
    Linux安装omnet++
    Python爬取数据分析
    基于小程序车辆违章停放执法移动APP-计算机毕业设计源码+LW文档
    VBA_MF系列技术资料1-202
    Java接口统一格式模板以及获取前一天时间固定时间方法(add和set区别)
  • 原文地址:https://blog.csdn.net/qq_37778018/article/details/126779477