0

我到目前爲止所實施的是:Spring Cloud Config Server如何將純文本文件壓入配置客戶端應用程序?

  1. 具有「本機」回購的Spring Cloud Config Server。

spring.profiles.active: native

spring.cloud.config.server.native.searchLocations: file:/path/to/config-repo

  • 配置服務器是推通知通過RabbitMQ的配置客戶端應用,如http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_push_notifications_and_spring_cloud_bus

  • 配置客戶端應用具有@在服務bean上註釋的RefreshScope。

  • 所以/配置回購有3個文件 - application.yaml,client.yaml和client.json 所有YAML特性的變化會自動通過配置客戶端應用肯定重載。但是,client.json沒有。

    基於https://github.com/spring-cloud/spring-cloud-config/issues/147,我能賣到通過REST API調用來配置服務器上配置客戶端應用的文件,用/{appname}/{profile}/{label}/client.json

    問題是:

    1)Config服務器監視這個純文本文件是否改變爲「native」?

    2)配置客戶端應用程序在更新後如何自動重新載入此client.json? (我可以有計劃任務調用配置服務器,但這不是理想的。)

    +0

    嗨, 如果你遇到這樣的問題,我想知道:HTTPS ://github.com/spring-cloud/spring-cloud-config/issues/546 –

    +0

    請參閱https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_push_notifications_and_spring_cloud_bus – spencergibb

    +0

    我有遵循這個指示。但它不適用於純文本文件。 –

    回答

    1

    我喜歡這樣sloving(尚未Finnished): 我有spring.cloud.config.server.native.serach-在逗號的形式位置分隔的URI列表

    file:/c:/repo/a,file:/c:/repo/b 
    

    我創建FileMonitorConfiguration豆(但它有一些問題,因爲它被安排2次,豆本身和彈簧enhaced情況下,我不是這個fammiliar)

    並付諸實施(只是草案)NativePropertyPathNotificationExtractor

    @Configuration 
    @EnableAutoConfiguration 
    @EnableConfigServer 
    public class ConfigServerApplication { 
        public static void main(String[] args) { 
         SpringApplication.run(ConfigServerApplication.class, args); 
        } 
    
        @Bean 
        NativePropertyPathNotificationExtractor nativePropertyPathNotificationExtractor(@Autowired(required = false) NativeEnvironmentRepository nativeRepo) { 
         return new NativePropertyPathNotificationExtractor(nativeRepo); 
        } 
    
        @Bean 
        FileMonitorConfiguration fileMonitorConfiguration() { 
         return new FileMonitorConfiguration(); 
        } 
    } 
    
    @Order(Ordered.LOWEST_PRECEDENCE - 500) 
    public class NativePropertyPathNotificationExtractor implements PropertyPathNotificationExtractor { 
        private final Set<Path> searchPaths; 
    
        public NativePropertyPathNotificationExtractor(NativeEnvironmentRepository nativeRepo) { 
         searchPaths = searchLocations(nativeRepo); 
        } 
    
        @Override 
        public PropertyPathNotification extract(MultiValueMap<String, String> headers, Map<String, Object> payload) { 
    
         // FileMonitor with empty headers, so if some there, ignore 
         if (false == headers.isEmpty()) { 
          return null; 
         } 
         if (null == searchPaths) { 
          return null; 
         } 
    
         Path path = pathFromPayload(payload); 
         if (null == path) { 
          return null; 
         } 
    
         for (Path searchPath : searchPaths) { 
          Path relative = searchPath.relativize(path); 
          // just a try ;-) 
          if (true == relative.startsWith("..")) { 
           continue; 
          } 
    
          return new PropertyPathNotification(relative.toString()); 
         } 
    
         return null; 
        } 
    
        private Path pathFromPayload(Map<String, Object> payload) { 
         if (null == payload) { 
          return null; 
         } 
         if (true == payload.isEmpty()) { 
          return null; 
         } 
         if (false == payload.containsKey("path")) { 
          return null; 
         } 
         if (null == payload.get("path")) { 
          return null; 
         } 
         if (true == StringUtils.isEmpty(payload.get("path").toString())) { 
          return null; 
         } 
         return Paths.get(payload.get("path").toString()).normalize().toAbsolutePath(); 
        } 
    
        private Set<Path> searchLocations(NativeEnvironmentRepository nativeRepo) { 
         if (null == nativeRepo) { 
          return null; 
         } 
         if (null == nativeRepo.getSearchLocations()) { 
          return null; 
         } 
    
         final Set<Path> paths = new LinkedHashSet<>(); 
         for (String location : nativeRepo.getSearchLocations()) { 
          try { 
           paths.add(Paths.get(new URI(location)).normalize().toAbsolutePath()); 
          } catch (Exception e) { 
           System.err.println("Nevalidne search location uri: " + location); 
          } 
         } 
         return paths; 
    
        } 
    } 
    
    相關問題