SpringBoot引入Nacos作为配置中心

SpringBoot引入Nacos作为配置中心

环境配置

  • MacBook Air M1
  • Idea 2022.1
  • Zulu JDK1.8
  • SpringBoot2.7.0
  • Nacos Server 2.0.2

Pom配置

<!-- 配置中心 -->
<dependency>
	<groupId>com.alibaba.boot</groupId>
    <artifactId>nacos-config-spring-boot-starter</artifactId>
	<version>${nacos.version}</version>
</dependency>

<!-- 注册中心 -->
<dependency>
	<groupId>com.alibaba.boot</groupId>
	<artifactId>nacos-discovery-spring-boot-starter</artifactId>
	<version>${nacos.version}</version>
</dependency>

Application配置

nacos:
  config:
    server-addr: host:port
    type: yaml
    data-id: dataId
    bootstrap:
      enable: true
      log-enable: true
  discovery:
    server-addr: host:port

Application Class

package com.cc.nacos;

import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
@NacosPropertySource(dataId = "example", autoRefreshed = true)
public class NacosDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(NacosDemoApplication.class, args);
    }

}
Comment