Nacos注册中心配置文件读取及热部署读取文件

pom文件

        <!--    nacos 服务发现    -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
        <!--   nacos 配置中心     -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>

配置文件
–优先读取
bootstrap.yml
读取什么结尾的文件
在这里插入图片描述
唯一id
在这里插入图片描述

server:
  port: 端口号

spring:
  profiles:
    active: 后缀(例:test)
  application:
    name: 服务名字

启动类上需要注解
@EnableDiscoveryClient

注意调整对应版本

版本不对应会出现冲突注册不上的情况–可以参照官网推荐匹配版本

文件热部署

配置文件
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
bootstrap.yml

spring:
  cloud:
    nacos:
      server-addr: #nacos地址端口号
      config:
        file-extension: yml
        namespace: #配置管理分组uuid
        #添加配置时 Group 的值一定要和 spring.cloud.nacos.config.group 的配置值一致。我这里没有添加group分组
        ## 可以配置多个 Data Id 同时配置时,他的优先级关系是 [n]其中 n 的值越大,优先级越高
      extension-configs[0]:
          data-id: config-student.yaml
          refresh: true
        extension-configs[1]:
          data-id: config-teacher.yaml
          refresh: true
      discovery:
        namespace: 配置管理分组uuid

@RefreshScope 热部署关键注解
实体类Student

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Data
//@Configuration或者@Component
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "student")
public class Student {
    private String name;
    private Integer age;
}

实体类Teacher

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

@Data
//@Configuration或者@Component
@Component
@RefreshScope
@ConfigurationProperties(prefix = "teacher")
public class Teacher {
    private String name;
    private Integer age;
}

实体类Teacher 第二种写法

import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

@Data
//@Configuration或者@Component
@Component
@RefreshScope
//@ConfigurationProperties(prefix = "teacher")
public class Teacher {
    @Value("${teacher.name}")
    private String name;
    @Value("${teacher.age}")
    private Integer age;
}

测试

package com.iot;

import com.iot.common.api.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestDemo {
    @Autowired
    private Student students;
    @Autowired
    private Teacher teachers;
    @GetMapping("/test/Teacher")
    public ApiResult Teacher() {

        Teacher teacher = new Teacher();
        teacher.setAge(teachers.getAge());
        teacher.setName(teachers.getName());
        return ApiResult.ok(teacher);
    }
    @GetMapping("/test/Student")
    public ApiResult Student() {

        Student student = new Student();
        student.setAge(students.getAge());
        student.setName(students.getName());
        return ApiResult.ok(student);
    }
}

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

读取文件不指定类型

    @SneakyThrows
    @Test
    public void test6() {
        String serverAddr="nacos地址";
        String dataId="test-demo";
        String group="DEFAULT_GROUP";
        String namespace="配置管理分组uuid/为分组可不写";
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.SERVER_ADDR,serverAddr);
        properties.put(PropertyKeyConst.NAMESPACE, namespace);
        ConfigService configService = NacosFactory.createConfigService(properties);
        String config = configService.getConfig(dataId, group, 5000);
        System.out.println("config = " + config);
        System.out.println("end>>>>>>>>>");
    }

在这里插入图片描述
结果:

在这里插入图片描述

注:获取文件的项目要注册到nacos注册中心上不然会出现获取null值得情况

也可以直接复制图片位置中得代码加以修改
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值