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
- server:
- port: 8888
- spring:
- profiles:
- active: @profiles.active@
- application:
- name: @artifactId@
- cloud:
- nacos:
- #nacos配置中心(将应用服务注册到nacos中)
- discovery:
- server-addr: localhost:8848
- # 分组
- group: TEST_DEMO
- # 命名空间
- namespace: b3d6b8c4-e85c-4935-9b05-d1b512891a1b
- #nacos注册中心(从nacos中拉取配置文件)
- config:
- server-addr: localhost:8848
- # 指定配置文件后缀名
- file-extension: yml
- group: TEST_DEMO
- namespace: b3d6b8c4-e85c-4935-9b05-d1b512891a1b
- # 指定读取共享配置文件名称,多个使用逗号隔开
- shared-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
- # 指定哪些共享配置文件支持动态刷新,多个使用逗号隔开
- refreshable-dataids: application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-resources-plugin</artifactId>
- <configuration>
- <delimiters>@</delimiters>
- <useDefaultDelimiters>false</useDefaultDelimiters>
- </configuration>
- </plugin>
- </plugins>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- </resource>
- </resources>
- </build>
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <groupId>com.kafka.test</groupId>
- <artifactId>kafka-test</artifactId>
- <version>1.0-SNAPSHOT</version>
-
- <properties>
- <maven.compiler.source>8</maven.compiler.source>
- <maven.compiler.target>8</maven.compiler.target>
- <spring-boot.version>2.4.2</spring-boot.version>
- <spring-cloud.version>2020.0.1</spring-cloud.version>
- <spring-cloud-alibaba.version>2021.1</spring-cloud-alibaba.version>
- </properties>
-
- <dependencies>
- <!--注册中心客户端-->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
- </dependency>
-
- <!--配置中心客户端-->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
- </dependency>
-
- <!--bootstrap 启动器(读取配置nacos文件的必须依赖)-->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-bootstrap</artifactId>
- </dependency>
-
- <!-- spring-boot 启动器 -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <!--kafka 依赖-->
- <dependency>
- <groupId>org.springframework.kafka</groupId>
- <artifactId>spring-kafka</artifactId>
- </dependency>
-
- </dependencies>
-
- <dependencyManagement>
- <dependencies>
- <!-- spring boot 依赖(有此依赖不需要在parent节点配置spring boot依赖信息) -->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-dependencies</artifactId>
- <version>${spring-boot.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <!-- spring cloud 依赖 -->
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-dependencies</artifactId>
- <version>${spring-cloud.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- <!-- spring cloud alibaba 依赖 -->
- <dependency>
- <groupId>com.alibaba.cloud</groupId>
- <artifactId>spring-cloud-alibaba-dependencies</artifactId>
- <version>${spring-cloud-alibaba.version}</version>
- <type>pom</type>
- <scope>import</scope>
- </dependency>
- </dependencies>
- </dependencyManagement>
-
- <profiles>
- <profile>
- <id>dev</id>
- <properties>
- <profiles.active>dev</profiles.active>
- </properties>
- <activation>
- <activeByDefault>true</activeByDefault>
- </activation>
- </profile>
- </profiles>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- </plugin>
- <plugin>
- <groupId>org.apache.maven.plugins</groupId>
- <artifactId>maven-resources-plugin</artifactId>
- <configuration>
- <delimiters>@</delimiters>
- <useDefaultDelimiters>false</useDefaultDelimiters>
- </configuration>
- </plugin>
- </plugins>
- <resources>
- <resource>
- <directory>src/main/resources</directory>
- <filtering>true</filtering>
- </resource>
- </resources>
- </build>
- </project>
- spring:
- #kafka
- kafka:
- bootstrap-servers: localhost:9092
- producer:
- retries: 0
- batch-size: 16384
- buffer-memory: 33554432
- acks: 1
- consumer:
- group-id: TestGroup
- auto-offset-reset: earliest
- enable-auto-commit: false
- auto-commit-interval: 5000
- key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
- value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
- listener:
- concurrency: 3
- ack-mode:manual_immediate
- user:
- name: zhangsan
- email: 1100000@qq.com