springboot 接入 nacos

1.依赖

注意springcloud的版本回和springboot版本有兼容性问题,详情

spring-cloud-context 用于支持动态刷新bean,@RefreshScope

                 <nacos.version>2.0.4.RELEASE</nacos.version>
                 <spring.cloud-version>Hoxton.SR1</spring.cloud-version>
                 
          <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud-version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
         <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
                <version>${nacos.version}</version>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
                <version>${nacos.version}</version>
            </dependency>
            
            <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-context</artifactId>
        </dependency>

2.配置

添加 boostrap.yml 其中 profiles 是一个自定义的 env 配置 profiles=local

spring:
  profiles:
    active: ${profiles}
  application:
    name: appName

#local本地环境
---
spring:
  profiles: local
  cloud:
    nacos:
      discovery:
        server-addr: 10.1.1.1:8848
        enabled: false
      config:
        server-addr: 10.1.1.1:8848
        file-extension: yml
        group: DEFAULT_GROUP
        enabled: false

#开发环境
---
spring:
  profiles: dev
  cloud:
    nacos:
      discovery:
        server-addr: 10.1.1.1:8848
        namespace: 9e42be07-7a89-48bc-87d1-2fd9e5109ca8
      config:
        server-addr: 10.1.1.1:8848
        file-extension: yml
        group: DEFAULT_GROUP
        namespace: 9e42be07-7a89-48bc-87d1-2fd9e5109ca8
上次更新时间: 2024/5/7 05:59:02