美文网首页
springcloud-config

springcloud-config

作者: jianshuqiang | 来源:发表于2019-07-11 08:36 被阅读0次

    一、config-server

    1、配置文件:application.properties

    server.port=8888
    spring.cloud.config.server.git.uri=https://github.com/zzzqqq123/springcloudtest.git
    spring.cloud.config.server.git.username=zhangq@webyun.cn
    spring.cloud.config.server.git.password=zhangqiang@123
    

    2、在启动类中添加注解

    @EnableConfigServer
    

    二、config-client

    1、application.properties

    spring.application.name=company-info-config-client
    server.port=8889
    

    2、bootstrap.properties

    spring.cloud.config.uri=http://localhost:8888/
    #git中的文件名称为application-dev.properties
    spring.cloud.config.name=application
    spring.cloud.config.profile=dev
    spring.cloud.config.label=master
    

    注意git仓库中的文件名称为:application-dev.properties

    三、刷新

    刷新的是客户端:http://localhost:8889/actuator/refresh(post请求)
    前提是需要开启刷新端点

    management.endpoints.web.exposure.include=*
    #  health,info,env
    management.endpoint.health.show-details=always
    

    4、实现自动刷新的功能[简单实现]

    @SpringBootApplication
    @EnableScheduling
    public class CompanyInfoConfigClientApplication {
    
        //定时更新数据
        private final ContextRefresher contextRefresher;
        CompanyInfoConfigClientApplication(ContextRefresher contextRefresher){
            this.contextRefresher=contextRefresher;
        }
        @Scheduled(fixedRate = 10*1000,initialDelay = 10*1000)
        public void refreshAuto(){
            Set<String> refresh = contextRefresher.refresh();
            Iterator<String> iterableSet=refresh.iterator();
            System.out.println("------------------》");
            while(iterableSet.hasNext()){
                System.out.println(iterableSet.next());
            }
    
        }
    
        public static void main(String[] args) {
            SpringApplication.run(CompanyInfoConfigClientApplication.class, args);
        }
    
    }
    

    5获取配置文件中的数据

    package com.company.info.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.cloud.context.config.annotation.RefreshScope;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    @RefreshScope//刷新
    @RequestMapping("user")
    public class UserController {
        @Value("${user.name}")
        private String name;
        @GetMapping("name")
        public String testValue(){
            return name;
        }
    }
    
    

    6注意:

    由于config一般采用集群配置,所以我们可以采用,单个依次重启服务的方式,实现配置文件生效

    相关文章

      网友评论

          本文标题:springcloud-config

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