10

ここにあるスプリングブート構成サーバーを把握しようとしてきました: 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;
    }

}

次に、これを 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;
}

結局、これは私が望んでいたことをしました。これが私がすべきことだったのか、それともまだ何かが欠けていて、これはすでに処理されていて、見逃しただけなのか知りたいです。

*****デイブが求めた情報を編集*****

ファイル プロパティのソース ローダーを取り出して、bootstrap.yml を次のように更新すると、

uri: file://${user.home}/resources

サンプル アプリケーションは、起動時に次のエラーをスローします。

ConfigServiceBootstrapConfiguration : Could not locate PropertySource: Object of class [sun.net.www.protocol.file.FileURLConnection] must be an instance of class java.net.HttpURLConnection

これが、追加のクラスが必要になると思った理由です。テストケースに関する限り、SpringApplicationEnvironmentRepositoryTests.javaについて話していると思います。環境の作成に同意しますが、全体として、uriプロトコルが「ファイル」の場合、アプリケーションは期待どおりに動作していないようです。

******追加編集*******

これが私がこれが機能していることを理解する方法です: サンプル プロジェクトは 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 プロトコルが場所を指すために機能すると想定しました。そのため、サーバーが NativeEnvironmentRepository を取得すると考えて、「file://」プロトコルを使用しました。

したがって、この時点で、いくつかの手順が抜けているか、ファイル システム プロパティのソース ロケータを追加する必要があると確信しています。

それが少し明確になることを願っています。

フルスタック:

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)
4

3 に答える 3

6

昨日このスレッドを読みましたが、重要な情報が欠けていました

git をリポジトリとして使用したくない場合は、spring クラウド サーバーを設定して 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>  
于 2015-05-06T08:22:00.913 に答える