Java springcloud 用nacos做为外部配置

1、版本

nacos:2.2.2

2、pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.nosya</groupId>
    <artifactId>abc</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>abc</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>8</java.version>
        <spring.cloud.alibaba.version>2.2.2.RELEASE</spring.cloud.alibaba.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <optional>true</optional>
        </dependency>

        <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>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>${spring.cloud.alibaba.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>${spring.cloud.alibaba.version}</version>
            <exclusions>
                <exclusion>
                    <groupId>com.alibaba.nacos</groupId>
                    <artifactId>nacos-client</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>2.2.2</version>
        </dependency>        

    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!--解决找不到@profile.name@-->
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <!-- dev 开发环境 -->
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
        </profile>
        <!-- prod 线上环境 -->
        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
        </profile>
        <!-- test 测试环境 -->
        <profile>
            <id>test</id>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

</project>

3、配置文件

bootstrap.yml
spring:
  profiles:
    active: @spring.profiles.active@
  application:
    name: test
bootstrap-dev.yml
spring:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        group: DEFAULT_GROUP
        username: nacos
        password: nacos
      config:
        server-addr: 127.0.0.1:8848
        group: DEFAULT_GROUP
        username: nacos
        password: nacos
        file-extension: yaml # properties yaml
  main:
    allow-bean-definition-overriding: true

4、示例代码

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/config")
@RefreshScope //通过 Spring Cloud 原生注解 @RefreshScope 实现配置自动更新
public class ConfigController {

    @Value("${id:0}")
    private int id;

    @Value("${spring.application.name:null}")
    private String name;

    @Value("${a.b.c:null}")
    private String abc;

    @RequestMapping("/get")
    public int get() {
        return id;
    }

    @RequestMapping("/name")
    public String name() {
        return name;
    }

    @RequestMapping("/abc")
    public String abc() {
        return abc;
    }
}

5、一些注意事项

a、springboot的版本与springcloud要匹配。
b、nacos-client的版本要单独处理(2.2.2用户认证)
c、nacos的配置写在bootstrap.properties或者bootstrap.yml文件中,不能写在application.properties和application.yml。

 

posted @ 2023-04-27 15:53  都是城市惹的祸  阅读(62)  评论(0)    收藏  举报