クライアントプロジェクトを作成し、サービスセンター(領事+春の雲コンフィグ)の設定を読んで

クライアントプロジェクトを作成し、サービスセンターの構成を読んで

流通センターは、サービスセンター(領事)に登録されています

POMファイルには、依存関係を追加します。

                <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-consul-discovery</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>

設定ファイルの内容を追加します。

spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#注册到consul的服务名称
spring.cloud.consul.discovery.serviceName=spring-cloud-config

領事サービスディスカバリを開始します。

@SpringBootApplication
//启动配置中心
@EnableConfigServer
//启动服务发现
@EnableDiscoveryClient
public class SpringCloudConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigServerApplication.class, args);
    }

}

ブート設定のセンターサーバとビュー領事背景:
altテキスト

2つの物流センターを展開サイン

2つのセンターを起動して、可用性の高いサービス名の形成に登録するように構成されたコマンドと設定ファイルのパラメータを使用した後、春・クラウド-config設定 - サーバープロジェクトのルートディレクトリで実行:

mvn install

設定を変更し、その後、application.properties /春-クラウド-config設定 - サーバー/ターゲット/重複ディレクトリを投影し、そしてapplication-1.propertiesとapplication-2.propertiesと改名し、スプリング・クラウド-config設定 - サーバー文書をコピーします。ファイル内のアプリケーション名とポート番号:


server.port=9004
spring.application.name=spring-cloud-config-server-01
server.port=9005
spring.application.name=spring-cloud-config-server-02

2つの物流センターは/ターゲット/ディレクトリで次のコマンドを実行して起動します。

java -jar spring-cloud-config-server-0.0.1-SNAPSHOT.jar --spring.config.location=application-1.properties
java -jar spring-cloud-config-server-0.0.1-SNAPSHOT.jar --spring.config.location=application-2.properties

ブート設定のセンターサーバとビュー領事背景:
altテキスト

クライアントを作成すると、コンフィギュレーション・センター・プロジェクトを読み込みます

altテキスト
altテキスト
altテキスト
altテキスト

コンフィギュレーション・ファイルapplication.propertiesを変更します。

spring.application.name=spring-cloud-config-client
server.port=9006
spring.cloud.consul.host=127.0.0.1
spring.cloud.consul.port=8500
#设置不需要注册到 consul 中
spring.cloud.consul.discovery.register=false

新規プロファイルのbootstrap.properties:

#配置文件名称中定义的配置项目名称
spring.cloud.config.name=ConfigDepot
#配置文件名称中定义的环境名称
spring.cloud.config.profile=Test
spring.cloud.config.label=master
#开启配置中心的服务发现
spring.cloud.config.discovery.enabled=true
#配置中心注册在服务中心的名字
spring.cloud.config.discovery.serviceId=spring-cloud-config

コンテンツapplication.propertiesにロードされる前に流通センターの要件なので、bootstrap.propertiesのコンフィギュレーションセンターの構成情報bootstrap.properties構成ファイルがapplication.properties前にロードされますので、設定ファイルを記述します。

読むためのインターフェイスクラスを設定追加:

@RestController
public class ConfigTestController {

    //配置信息通过@Value注解读取,配置项用${配置项}读取
    @Value("${bluersw.config}")
    private String configBluersw;

    @RequestMapping("/ConfigTest")
    public String ConfigTest(){
        return this.configBluersw;
    }
}

クライアントのテストを開始

アクセス127.0.0.1:9006/ConfigTest、コンフィギュレーション・テスト-3(私は何回か変更中央)の情報を取得します:
altテキスト

この時点で、内容のGitリポジトリの設定を変更し、ページのページの内容は変更されません更新し、プログラムの起動時にコンテンツの設定がロードされているので、設定の変更は、自動的にクライアントプログラムに反映されていないことが中心であるが、あなたは、クライアントを呼び出すことによって、コンフィギュレーション・インターフェースの更新内容をリロードすることができ、このセクションでは、我々はバスを入れて何を示しています。

ソース

Githubのリポジトリます。https://github.com/sunweisheng/spring-cloud-example

おすすめ

転載: www.cnblogs.com/bluersw/p/11610719.html