springboot整合nacos配置中心

2021-12-06 14:12:11 浏览数 (1)

1.nacos添加配置

2.项目引入依赖

代码语言:javascript复制
    <dependency>
      <groupId>com.alibaba.boot</groupId>
      <artifactId>nacos-config-spring-boot-starter</artifactId>
      <version>0.2.1</version>
    </dependency>

3.配置

代码语言:javascript复制
spring:
  application:
    name: nacos_test
nacos:
  config:
    namespace: 4f04a2dd-3c9c-49bf-b4d3-3f65f8c9762c
    server-addr: 192.168.173.39:8848
    file-extension: yaml
    enabled: true

4.获取

代码语言:javascript复制
package com.java4all.nacosdemo;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@NacosPropertySource(dataId = "nacos_test.yaml",groupId = "DEFAULT_GROUP",autoRefreshed = true)
public class NacosdemoApplication {

    @NacosValue(value = "${approval.app.appId:1762222222}",autoRefreshed = true)
    private String appId;

    @NacosValue(value = "${name:IT云清}",autoRefreshed = true)
    private String name;

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

    @GetMapping("test1")
    public String test1(String age){
        System.out.println(age "  :   " appId "  :   " name);
        return age "  :   " appId "  :   " name;
    }
}

0 人点赞