nacos获取动态配置信息

#目前版本@NacosValue还是不能用,阿里的解释是:

After completing the above two steps, the application will get the externalized configuration from Nacos Server and put it in the Spring Environment's PropertySources.We use the @Value annotation to inject the corresponding configuration into the userName and age fields of the SampleController, and add @RefreshScope to turn on dynamic refresh .

用@Value获取动态解释和@Value为什么正常情况不能注入,在我的第一篇nacos学习里

#1 yml

server:
  port: 56010 #启动端口 命令行注入
  address: 0.0.0.0  # 允许所有网络接口访问

spring:
  application:
    name: test
  cloud:
    nacos:
      config:
      #同时挂起多个服务
        server-addr: 127.0.0.1:8848,127.0.0.1:8850,127.0.0.1:8852
        file-extension: properties
        namespace: db84d9d4-4f09-4fac-846f-38064f7ef28a
        group: DEFAULT_GROUP

        #config external configuration
        #1.Data Id在默认的组DEFAULT_GROUP,不支持配置的动态刷新<DEFAULT_GROUP不用加group>
        ext-config[0]:
          data-id: ext-config-common01.properties

        #2.Data Id不在默认的组DEFAULT_GROUP
        ext-config[1]:
          data-id: ext-config-common02.properties
          group: GLOBALE_GROUP

        #3..Data Id既不在默认组,也支持动态刷新
        ext-config[2]:
          data-id: ext-config-common03.properties
          group: REFRESH_GROUP
          refresh: true

#        shared-dataids: ext-config-common01.properties,ext-config-common02.properties,ext-config-common03.properties
#        refreshable-dataids: ext-config-common01.properties


#2 xml

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.newnacoslearning</groupId>
  <artifactId>NacosNewLearningCode</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>NacosNewLearningCode</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <nacos.version>2.1.1</nacos.version>
    <springboot.version>2.1.3.RELEASE</springboot.version>
    <slf4j.version>1.7.36</slf4j.version> <!-- 添加SLF4J版本 -->
    <java.version>11</java.version>
  </properties>

  <dependencies>
    <!--nacos总依赖-->
    <dependency>
      <groupId>com.alibaba.nacos</groupId>
      <artifactId>nacos-client</artifactId>
      <version>${nacos.version}</version>
    </dependency>

    <dependency>
      <groupId>com.alibaba.nacos</groupId>
      <artifactId>nacos-spring-context</artifactId>
      <version>${nacos.version}</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
      <version>2.1.1.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>${springboot.version}</version>
    </dependency>


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

  </dependencies>
</project>

#3 Controller

package com.newnacoslearning.nacosconfiguration.controller;

import com.alibaba.nacos.api.config.annotation.NacosValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class clusterCntroller {

    @Value("${spring.datasource.username}")  // 添加默认值防止启动失败
    private String configs1;
    @Autowired
    ConfigurableApplicationContext applicationContext;
    @GetMapping("/configs")
    public String getConfigs() {
        //读取配置文件
//        return "label:"+configs1;
        return applicationContext.getEnvironment().getProperty("spring.datasource.username");
    }
    @GetMapping("/config2")
    public String getConfigs2(){
        String name=applicationContext.getEnvironment().getProperty("common.name");
        String age=applicationContext.getEnvironment().getProperty("common.age");
        String address=applicationContext.getEnvironment().getProperty("common.address");
        String birthday=applicationContext.getEnvironment().getProperty("common.birthday");
        String fullname=applicationContext.getEnvironment().getProperty("common.fullname");
        return name + "+" + age + "+" + address + "+" + birthday + "+" + fullname;
    }

}

#4 App

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

@SpringBootApplication
@NacosPropertySource(dataId = "test.properties",autoRefreshed = true)
public class clusterApplicationTest {
    public static void main(String[] args) {
        SpringApplication.run(clusterApplicationTest.class, args);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值