1.maven依赖

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.3.3</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

其中第二个依赖是为了解决一个问题(一个http相关的bean找不到)
2.配置文件
需要配置文件bootstrap.yml,bootstrap-dev.yml,bootstrap-test.yml
bootstrap.yml中写一些基础信息,比如

server:
  port: 8080

spring:
  application:
    name: 123
  profiles:
    active: @profiles.active@

bootstrap-dev.yml,bootstrap-test.yml中写不同环境的配置,比如

spring:
  cloud:
    nacos:
      discovery:
        server-addr: ip:port
        namespace: 
      config:
        server-addr: ip:port
        namespace: 
        file-extension: yaml
  datasource:
    username: 
    password: 
    url: jdbc:mysql://localhost:3306/xxx?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
    driver-class-name: com.mysql.cj.jdbc.Driver
  redis:
    host: 
    port: 
    password: 
    database: 

nacos配置中心的配置,命名规范为:服务名-环境.yaml,服务名和环境在配置中已经配好
3.pom中添加多环境

    <profiles>
        <profile>
            <id>local</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
            <properties>
                <profiles.active>dev</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>uat</id>
            <properties>
                <profiles.active>uat</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>public</id>
            <properties>
                <profiles.active>public</profiles.active>
            </properties>
        </profile>
    </profiles>
    <build>
        <finalName>xxx</finalName>
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <excludes>
                    <exclude>bootstrap-*.yml</exclude>
                </excludes>
            </resource>
            <resource>
                <filtering>true</filtering>
                <directory>src/main/resources</directory>
                <includes>
                    <include>bootstrap-${profiles.active}.yml</include>
                </includes>
            </resource>
        </resources>
        <!-- 插件 -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.7</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml
                    </configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
            </plugin>
        </plugins>
    </build>

其中

<filtering>true</filtering>

是为了在bootstrap中使用@@引入配置
多环境配好以后,在IDEA的maven中就可以选择一个环境进行启动,获取对应环境的配置文件