Spring启动配置服务器

pee*_*kay 8 spring spring-boot spring-cloud

我一直试图抓住位于以下位置的spring boot配置服务器:https://github.com/spring-cloud/spring-cloud-config并且在更详尽地阅读文档后我能够完成大部分工作我的问题 但是我必须为基于文件的PropertySourceLocator编写一个额外的类

/*
 * Copyright 2013-2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");


* you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.config.client;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.env.PropertiesPropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.util.StringUtils;

/**
 * @author Al Dispennette
 *
 */
@ConfigurationProperties("spring.cloud.config")
public class ConfigServiceFilePropertySourceLocator implements PropertySourceLocator {
    private Logger logger = LoggerFactory.getLogger(ConfigServiceFilePropertySourceLocator.class);

    private String env = "default";

    @Value("${spring.application.name:'application'}")
    private String name;

    private String label = name;

    private String basedir = System.getProperty("user.home");

    @Override
    public PropertySource<?> locate() {
        try {
            return getPropertySource();
        } catch (IOException e) {
            logger.error("An error ocurred while loading the properties.",e);
        }

        return null;
    }

    /**
     * @throws IOException
     */
    private PropertySource getPropertySource() throws IOException {
        Properties source = new Properties();
        Path path = Paths.get(getUri());
        if(Files.isDirectory(path)){
            Iterator<Path> itr = Files.newDirectoryStream(path).iterator();
            String fileName = null!=label||StringUtils.hasText(label)?label:name+".properties";
            logger.info("Searching for {}",fileName);
            while(itr.hasNext()){
                Path tmpPath = itr.next();
                if(tmpPath.getFileName().getName(0).toString().equals(fileName)){
                    logger.info("Found file: {}",fileName);
                    source.load(Files.newInputStream(tmpPath));
                }
            }
        }
        return new PropertiesPropertySource("configService",source);
    }

    public String getUri() {
        StringBuilder bldr = new StringBuilder(basedir)
        .append(File.separator)
        .append(env)
        .append(File.separator)
        .append(name);

        logger.info("loading properties directory: {}",bldr.toString());
        return bldr.toString();
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEnv() {
        return env;
    }

    public void setEnv(String env) {
        this.env = env;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public String getBasedir() {
        return basedir;
    }

    public void setBasedir(String basedir) {
        this.basedir = basedir;
    }

}
Run Code Online (Sandbox Code Playgroud)

然后我将其添加到ConfigServiceBootstrapConfiguration.java中

@Bean
public PropertySourceLocator configServiceFilePropertySource(
        ConfigurableEnvironment environment) {
    ConfigServiceFilePropertySourceLocator locator = new ConfigServiceFilePropertySourceLocator();
    String[] profiles = environment.getActiveProfiles();
    if (profiles.length==0) {
        profiles = environment.getDefaultProfiles();
    }
    locator.setEnv(StringUtils.arrayToCommaDelimitedString(profiles));
    return locator;
}
Run Code Online (Sandbox Code Playgroud)

最后这完成了我想要的.现在我很想知道这是不是我应该做的,或者我是否仍然遗漏了一些东西,这已经处理过了,我错过了它.

*****编辑Dave******要求的信息

如果我取出文件属性源加载器并使用更新bootstrap.yml

uri: file://${user.home}/resources
Run Code Online (Sandbox Code Playgroud)

示例应用程序在启动时抛出以下错误:

ConfigServiceBootstrapConfiguration : Could not locate PropertySource: Object of class [sun.net.www.protocol.file.FileURLConnection] must be an instance of class java.net.HttpURLConnection
Run Code Online (Sandbox Code Playgroud)

这就是我认为需要额外课程的原因.就测试用例而言,我相信你在谈论SpringApplicationEnvironmentRepositoryTests.java并且我同意创建环境是有效的,但是当uri协议是'file'时,整个应用程序似乎没有按预期运行.

******补充编辑*******

这就是我理解这是如何工作的:示例项目依赖于spring-cloud-config-client工件,因此对spring-cloud-config-server工件具有传递依赖性.客户端工件中的ConfigServiceBootstrapConfiguration.java创建一个ConfigServicePropertySourceLocator类型的属性源定位器bean.配置客户端工件中的ConfigServicePropertySourceLocator.java具有注释@ConfigurationProperties("spring.cloud.config")并且属性uri存在于所述类中,因此在bootstrap.yml文件中设置了spring.cloud.config.uri.

我相信这是由quickstart.adoc中的以下语句重新强化的:

当它运行时,它将从端口8888上的默认本地配置服务器获取外部配置(如果它正在运行).要修改启动行为,您可以使用bootstrap.properties(例如application.properties,对于应用程序上下文的引导阶段)更改配置服务器的位置,例如

---- spring.cloud.config.uri: http ://myconfigserver.com

此时,一些如何使用JGitEnvironmentRepository bean并寻找与github的连接.我假设因为uri是在ConfigServicePropertySourceLocator中设置的属性,所以任何有效的uri协议都可用于指向某个位置.这就是为什么我使用'file://'协议认为服务器会选择NativeEnvironmentRepository.

所以在这一点上我确定我要么缺少一些步骤,要么需要添加文件系统属性源定位器.

我希望这更清楚一些.

全栈:

java.lang.IllegalArgumentException: Object of class [sun.net.www.protocol.file.FileURLConnection] must be an instance of class java.net.HttpURLConnection
    at org.springframework.util.Assert.isInstanceOf(Assert.java:339)
    at org.springframework.util.Assert.isInstanceOf(Assert.java:319)
    at org.springframework.http.client.SimpleClientHttpRequestFactory.openConnection(SimpleClientHttpRequestFactory.java:182)
    at org.springframework.http.client.SimpleClientHttpRequestFactory.createRequest(SimpleClientHttpRequestFactory.java:140)
    at org.springframework.http.client.support.HttpAccessor.createRequest(HttpAccessor.java:76)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:541)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:506)
    at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:448)
    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:68)
    at org.springframework.cloud.bootstrap.config.ConfigServiceBootstrapConfiguration.initialize(ConfigServiceBootstrapConfiguration.java:70)
    at org.springframework.boot.SpringApplication.applyInitializers(SpringApplication.java:572)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
    at sample.Application.main(Application.java:20)
Run Code Online (Sandbox Code Playgroud)

Ton*_*phy 5

我昨天读了这篇帖子,它遗漏了一条至关重要的信息

如果您不想将git用作存储库,那么您需要将spring cloud服务器配置为具有spring.profiles.active = native

检查spring-config-server代码以了解它org.springframework.cloud.config.server.NativeEnvironmentRepository

 spring:
   application:
    name: configserver
  jmx:
    default_domain: cloud.config.server
  profiles:
    active: native
  cloud:
    config:
      server:
        file :
          url : <path to config files>  
Run Code Online (Sandbox Code Playgroud)


pee*_*kay 2

我想我根据您最后的评论有了最终的解决方案在我添加的 configserver.yml 中

spring.profiles.active: file
spring.cloud.config.server.uri: file://${user.home}/resources
Run Code Online (Sandbox Code Playgroud)

在 ConfigServerConfiguration.java 中我添加了

@Configuration
@Profile("file")
protected static class SpringApplicationConfiguration {
@Value("${spring.cloud.config.server.uri}")
String locations;

@Bean
public SpringApplicationEnvironmentRepository repository() {
SpringApplicationEnvironmentRepository repo = new SpringApplicationEnvironmentRepository();
repo.setSearchLocations(locations);
return repo;
}
}
Run Code Online (Sandbox Code Playgroud)

我可以通过以下方式查看属性:

curl localhost:8888/bar/default
curl localhost:8888/foo/development
Run Code Online (Sandbox Code Playgroud)