美文网首页
nacos配置中心

nacos配置中心

作者: sunpy | 来源:发表于2022-08-02 12:56 被阅读0次

    导包


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

    动态配置:
    结果:

    相关文章

      网友评论

          本文标题:nacos配置中心

          本文链接:https://www.haomeiwen.com/subject/utxjwrtx.html