繁体   English   中英

Spring Cloud Config Server如何将纯文本文件推送到Config Client Application?

[英]How Spring Cloud Config Server PUSH plain text files to Config Client Application?

到目前为止,我已经实现了:

  1. 具有“本机”存储库的Spring Cloud Config Server。

spring.profiles.active: native

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

  1. Config Server正在通过RabbitMQ将通知推送到Config Client App,如http://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#_push_notifications_and_spring_cloud_bus

  2. Config Client应用程序的Service Bean上带有@RefreshScope批注。

因此,/ config-repo具有3个文件-application.yaml,client.yaml和client.json,所有yaml属性更改将由Config Client App确定自动重新加载。 但是,client.json没有。

基于https://github.com/spring-cloud/spring-cloud-config/issues/147 ,我可以使用/ {appname} / {profile}通过对Config Server的REST api调用在Config Client App上获取文件。 /{label}/client.json

问题是:

1)Config Server是否通过“本地”监视此纯文本文件的更改?

2)配置客户端应用程序一旦更新,如何自动重新加载此client.json? (我可以安排任务来调用Config服务器,但这并不理想。)

我喜欢这样的方式(尚未完成):我有以逗号分隔的URI列表形式的spring.cloud.config.server.native.serach-locations

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

我创建了FileMonitorConfiguration bean(但是它有一些问题,因为它被调度了2次,一个bean本身和一个spring增强实例,对此我并不陌生)

并实现(只是草稿)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;

    }
}

Config客户端:restTemplate.getForObject(“ http:// localhost:8080 / application / default / master / testing-dev.json ”,String.class);

可以获取.json后缀文件内容,但是我认为它没有获取文件内容,还有其他获取文件内容的方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM