栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

搭建config-server实现动态刷新配置文件

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

搭建config-server实现动态刷新配置文件

Config-Server搭建及配置文件的动态刷新
  1. pom.xml

    
    
        
            cloud-iaas
            com.xiaoge
            1.0-SNAPSHOT
        
        4.0.0
    
        config-server
        ego 商城的配置文件中
    
        
            
            org.springframework.cloud
            spring-cloud-starter-netflix-eureka-client
            
            
            
                org.springframework.cloud
                spring-cloud-config-server
            
            
            
                org.springframework.cloud
                spring-cloud-starter-bus-amqp
            
        
    
        
        
            
                
                org.springframework.boot
                spring-boot-maven-plugin
                
            
        
    
    
    
  2. application.yml

    server:
      port: ${APP_PORT:8888}
    spring:
      application:
        name: config-server
      cloud:
        config:
          discovery:
            enabled: true
          server:
            #开启配置文件发现功能
            git:
              #配置远端配置文件地址 用户名和密码以及文件夹名称  gitee地址
              uri: https://gitee.com/xiaogectwbq/ego-shop-config.git
              username: xiaogectwbq
              password: *******
              search-paths: /ego-shop-config
    @RestController
    public class ConfigController {
    
        @Value("${server.port}")
        private String port;
    
        @Autowired
        private RestTemplate restTemplate;
    
        
        @PostMapping("bus-refresh")
        public ResponseEntity refresh() {
    
            //组装 url
            String url = "http://localhost:" + port + "/actuator/bus-refresh";
    
            // 创建请求头
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add("content-type", "application/json;charset=utf-8");
            // 创建request
            HttpEntity httpEntity = new HttpEntity<>(httpHeaders);
    
            // 发送post请求
            ResponseEntity responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
    
            if (responseEntity.getStatusCode().equals(HttpStatus.NO_CONTENT)) {
                return ResponseEntity.ok("刷新配置文件成功!");
            }
    
            return ResponseEntity.badRequest().body("配置文件刷新失败!");
        }
    
    }
    
  3. 启动类

    package com.xiaoge;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.web.client.RestTemplate;
    
    
    @SpringBootApplication
    @EnableEurekaClient // 开启eureka客户端
    @EnableConfigServer //开启配置文件中心
    public class ConfigServerApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ConfigServerApplication.class, args);
        }
    
    
        
        @Bean
        public RestTemplate restTemplate() {
            return new RestTemplate();
        }
    }
    
  4. 在对应的gitee创建对应的仓库

  5. 通过网络接口发起post请求刷新
    对配置中心发生刷新请求http://localhost:8888/actuator/refresh 发起post请求,以刷新微服务的配置信息。发生post请求之后,就会触发bus的广播效应,Bus本质还是利用了消息中间件做发布订阅,从而实现广播效果。

    结果就像下面的图一样(网络图片):

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/921531.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号