春の雲の2.xバージョンコンフィグ設定のチュートリアルセンター

序文

本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3

ベースこのユーレカ・サーバーの以前の記事を実装します。
参照

アウトライン

一元管理を容易にするために、マルチサービス、サービスプロファイルの膨大な数に起因する分散システムでは、あなたは、分散中心的なコンポーネントを設定する必要があります。春クラウドコンフィグは、分散システムを外部化するように構成されたサーバーとクライアントのサポートを提供します。

BenpianはコンフィグServerスタンドアロン・モードのためのプロジェクト構造に関与し、サーバーを介してディスプレイプロファイルデータへのリンクのGitリポジトリ、Configをクライアントに適用されます。コンフィギュレーション・ファイルを更新するには、両方のアナログバスを使用してください。

設定 - サーバープロジェクトを作成します。

コンフィグ・サーバ機能

  • HTTP外部設定、リソースベースのAPIについて
  • 暗号化と復号化のプロパティ
  • アプリケーションを使用すると、スプリングブーツに埋め込むことができます

1.1コンフィギュレーション・センター・サーバを作成します。config-サーバーを

依存の1.2のpom.xmlの設定 - サーバーを追加

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

アドオンに適用するための1.3の追加設定 - サーバー構成情報

#加载本地文件配置
spring:
  application:
    name: config-server
  profiles:
    active: native #加载本地配置
  cloud:
    config:
      server:
        native: #加载本地目录文件
          search-locations: /Users/xxxx/config-server

server:
  port: 13081

eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

ConfigServerApplicationは1.4スタートアップクラスのノートを増加しました

package spring.cloud.demo.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

}

このように、単一のローカル設定 - サーバーは、完全な構築します

設定 - クライアント:Configuration Centerのクライアントを作成する1.5

設定 - クライアントの1.6の追加ポンポン関連の依存性

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

bootstrap.yml 1.7コンフィギュレーションの設定 - クライアントを追加します(これはapplication.ymlではないことに注意してください)

bootstrap.yml

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      fail-fast: true
      uri: http://localhost:13081 #通过域名访问配置中心服务端
      discovery:
        enabled: true
eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

サーバーの構成ファイルの設定 - クライアントdev.ymlから1.8新しい要求

クライアントの取得パスは、サーバからのリソースの割り当てを支配します:

  • / {アプリケーション} / {プロファイル} / {ラベル}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

本論文では、二番目のルール。

設定内容:

spring:
  application:
    name: config-client

server:
  port: 52601

eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

info: local-config-client-dev

/ユーザ/ XXXX / configのサーバディレクトリ設定 - サーバ構成に新しい設定 - クライアントdev.ymlに、それが設定ディレクトリ・サーバ設定されていない場合は、リソースを使用してデフォルトのディレクトリ。

ノートの1.9 ConfigClientApplication増加

package spring.cloud.demo.configclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {

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

}

2.0テストクラスの作成

コンフィグ・クライアントでテストコントローラーを作成します。ConfigClientControllerを

package spring.cloud.demo.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 测试是否能获取到配置信息的controller
 * @auther: maomao
 * @DateT: 2019-09-17
 */
@RestController
public class ConfigClientController {

    @Value("${info}")
    private String info;

    @RequestMapping("/config/info")
    public String info() {
        return info;
    }

}

2.1を起動し、関連サービス

それぞれユーレカ・サーバー、設定サーバ、設定・クライアントを起動します。ログインします。http:// localhost:以下に示す52601 /設定/情報、

証明ローカル設定ファイルは力に既にあります。

リモートのgitから2.2ロードリソース構成ファイル

application.yml設定の修正・サーバーの構成ファイル:

##加载本地文件配置
#spring:
#  application:
#    name: config-server
#  profiles:
#    active: native #加载本地配置
#  cloud:
#    config:
#      server:
#        native: #加载本地目录文件
#          search-locations: /Users/fengfujie/config-server

#加载远程git仓库资源文件
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          # 配置git仓库的地址
          uri: https://github.com/fengfujie25/sping-cloud-config
          # git仓库的账号
          username: xxxxxx
          # git仓库的密码
          password: xxxxxx

server:
  port: 13081

eureka:
  instance:
    hostname: eureka1.client.com
    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

再起動して設定 - サーバーサービス。そして、リフレッシュは、http:// localhostを:52601 /設定/情報アドレスは以下の通り

証明は成功し、リモートのgitリソースの設定情報を取得しています。

コンフィグサーバサービス名にアクセスすることにより2.3

上記の構成本番環境サービスセンターの構成は、すべてのクライアントが唯一のサービス名を介してアクセスすることが、クラスタ構成であるため、システムの高可用性を確保するために、ドメイン名の設定 - サーバーを介してアクセス可能です。

のbootstrap.yml設定 - クライアントを変更します

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      fail-fast: true
      #uri: http://localhost:13081 #通过域名访问配置中心服务端
      discovery:
        enabled: true
        service-id: config-server #通过服务访问配置中心服务端

eureka:
  instance:
    hostname: eureka1.client.com

    lease-renewal-interval-in-seconds: 5
    lease-expiration-duration-in-seconds: 10
  client:
    service-url:
      defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

spring.cloud.config.discovery.service-ID:サービスセンターのサーバー名によるアクセスを設定します

再起動して設定 - クライアントと訪問のhttp :. // localhostを:5301 /設定/情報、同じ結果を示し[2.2]構成情報は、サービス名の設定 - サーバーがアクセスすることを証明し、力に既にある表します。

この時点では、春のクラウド統合config設定が完了します。あなたは、リモートのgitリポジトリリソースの割り当てを変更した場合でも、問題がある、プロジェクトが構成情報は力ではないが、更新されません。

2.4動的な更新設定 - サーバー設定

ダイナミックリフレッシュ配置を設定サーブ

  • ダイナミックリフレッシュのRabbitMQに基づいて
  • ネイティブリフレッシュ(擬似動的リフレッシュ)

本論文では、比較的単純なネイティブリフレッシュモード。

増加した信頼のpom.xmlに関連付け2.4.1
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.4.2増加@RefreshScope注釈ConfigClientController
package spring.cloud.demo.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @auther: maomao
 * @DateT: 2019-09-17
 */
@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${info:error}")
    private String info;

    @RequestMapping("/config/info")
    public String info() {
        return info;
    }

}

また、@value注釈コンテンツ@valueを(「$ {情報:エラー}」)を変更しながら、このように、あなたは、構成情報を更新する必要があるときため、デフォルト値を持っている、それ以外の場合はエラーになります。

2.4.3再起動の設定、クライアント

訪問は、http:// localhostを:5301 /設定/情報、サービスにアクセスできるかどうかを確認します。

その後、設定情報資源gitリポジトリを変更することができます。

  • ポスト訪問することでます。http:// localhost:5301 /アクチュエータ/リフレッシュ、以下に示します。

  • リフレッシュのhttp:// localhostを:5301 /設定/情報、結果は示しました:

力の証明リフレッシュ。

この方法は、あなたがジョブを手動で更新する必要があるたびにあまりにも面倒です。GitHubの手動たびにリフレッシュすることなく、ウェブフック方法を実施することができる提供します。

ペイロードURL:コールバックURLをトリガーした後

コンテンツの種類:データフォーマット、二つの一般的な使用JSON

シークレット:ボディはHMACを使用して、POSTに文字列を暗号化して

イベント:イベントリストトリガー

イベントの種類 説明
ただ、pushイベント 倉庫時にプッシュトリガデフォルトのイベント
私にすべてを送ります 送られた私のすべてのもの
私は、個々のイベントを選択してみましょう 個々のイベントを選択します

这样我们就可以利用Webhook的机制去触发客户端的更新,但是当客户端越来越多的时候,Webhook机制也不够优雅,每次增加客户端都需要改动Webhook也不现实。

其实,Spring cloud给了我们更好的解决方案-spring cloud bus。

spring cloud bus后续更新。

总结

本文简单的实现了config-server和config-client的单机和远程git仓库的配置的调用以及配置信息的简单的动态更新。

代码地址


《Srping Cloud 2.X小白教程》目录

转载请注明出处,

おすすめ

転載: www.cnblogs.com/fengfujie/p/11815840.html