nacos配置中心

2022-08-02  本文已影响0人  sunpy

导包


<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>

编写bootstrap.yml文件:

spring:
  application:
    name: teacher-service
  profiles:
    active: TEST
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yml

@RefreshScope动态刷新


spring:
  application:
    name: teacher-service
  profiles:
    active: TEST
  cloud:
    nacos:
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yml

Author:
  name: sunpy
@RefreshScope
@Log
@RequestMapping("/teacher")
@RestController
public class TeacherController {
    private final static List<String> data = new ArrayList<>();

    static {
        data.add("甲");
        data.add("乙");
        data.add("丙");
        data.add("丁");
        data.add("戊");
    }

    @Value("${Author.name}")
    private String authorName;

    @SentinelResource(value="listTeacher", fallback = "fallbackResult")
    @GetMapping("/list")
    public ResultModel<Object> listTeacher() {
        ResultModel<Object> resultModel = new ResultModel<>();
        resultModel.setMsg("作者名称:" + authorName);
        resultModel.setRes(data);
        resultModel.setTime(TimeUtil.getNowTime());
        return resultModel;
    }

    public ResultModel<Object> fallbackResult() {
        ResultModel<Object> resultModel = new ResultModel<>();
        resultModel.setCode(500);
        resultModel.setSuccess(false);
        resultModel.setMsg("当前系统已经达到阙值,请稍后访问!");
        resultModel.setTime(TimeUtil.getNowTime());
        log.info("flow exception:" + "当前系统已经达到阙值,请稍后访问!");
        return resultModel;
    }
}

动态配置:
结果:
上一篇下一篇

猜你喜欢

热点阅读