springboot整合nacos配置中心

1.nacos添加配置

在这里插入图片描述
在这里插入图片描述

2.项目引入依赖

    <dependency>
      <groupId>com.alibaba.boot</groupId>
      <artifactId>nacos-config-spring-boot-starter</artifactId>
      <version>0.2.1</version>
    </dependency>

3.配置

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.获取

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

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 鲸 设计师:meimeiellie 返回首页