SpringCloud戦闘(F) - 高可用性分散構成センター(スプリングクラウド構成)

この記事では、SpringCloud戦闘(F)である - 可用性の高い分散構成・センター(春クラウドコンフィグ)、最初の記事に集中する、ポータルをクリックしてください。

SpringCloud戦闘(5) - ルーティングゲートウェイ(zuul)

我々は以前Zuulルーティングゲートウェイを導入しました。事業の増加の仕事では、ますます複雑化の需要、我々はマイクロビジネスサービスモジュール部門の多くを分割する必要があり、この時間は、各モジュールは、万のモジュールがある場合は、我々は1を持っているこの時点では、独自の設定ファイルを持っています1万設定ファイルは、我々は統一され、設定ファイル管理のためのセンターを設定する必要がありますので、SpringCloudは、ここで私は、春の雲コンフィグの単一ノードを開くために書くのが面倒ナンセンスない、私たちのためのconfigureファイル管理に春クラウドコンフィグを提供(単一ノード基本的には何もない)の記事、直接書き込み春クラウドコンフィグクラスタ構造は、以下の組織図:

アズール(3).PNG

A、春の雲Configを紹介

春クラウドコンフィグを外部分散システムとして構成されたサーバーとクライアントのサポートを提供します。使用コンフィグServerは、あなたはすべての環境でのアプリケーションのための外部プロパティを管理することができます。それは、春のアプリケーションに理想的です、アプリケーションはまた、他の言語で使用することができます。テストと運用展開への開発からプロセスを介してアプリケーションを使用すると、これらの環境間の構成を管理することができますし、アプリケーションを決定するためには、移行を実行するために必要なすべてを持っています。それは構成環境のサポートラベルのバージョンに簡単で、コンテンツを管理するためのさまざまなツールにアクセスできるように、デフォルトのストレージバックエンドサーバの実装は、gitのを使用します。

第二に、仕事の準備

我々はリアルタイム更新クライアント側の設定ファイルを達成したいので、我々は、メッセージングミドルウェアをインストールする必要がありますので、上記をプロジェクトに設定することに基づいて、まず、ポートは8759,8760,8761あり、ユーレカクラスターを開始し、ここで私はRabbitMQのを選択してください。

RabbitMQのダウンロード

RabbitMQのインストールのチュートリアル

RabbitMQの設定リモートアクセス

接続するためのリモートアクセスポートを設定するには、RabbitMQのニーズの後に(私のリモートアクセスポートの設定が5672である)プログラムインチ

第三には、設定 - サーバープロジェクトを作成します

設定 - サーバー:という名前の新しい春ブートプロジェクトを作成します。

3.1 Mavenの依存性

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.oal.microservice</groupId>
        <artifactId>openAiLab</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.oal.microservice</groupId>
    <artifactId>config-server</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-server</name>
    <description>config</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

私たちは、春・クラウド・コンフィグ・サーバーを導入する必要があり、私たちはバスメッセージ・バスを導入し、スプリング・クラウド・スターター・バス・AMQP、目的は、更新クライアントを達成するように、MQは、設定ファイルを引っ張るためにクライアントに通知することができることです構造は下に示されている場合には、オブジェクトコンフィギュレーションファイル、。

3.2設定ファイル(application.yml)

eureka:
  client:
    serviceUrl:
      defaultZone: http://eureka-serve-01:8761/eureka/

server:
  port: 8888

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/AndyWebJava/SpringcloudConfig
          searchPaths: respo
          label: master
          username:
          password:
    bus:
      enabled: true
      trace:
        enabled: true
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

私たちはバスリフレッシュインタフェースバスを公開する必要があることに注意してください、私たちはバスで構成ファイルを更新する必要があります。

spring.cloud.config.server.git.uriはgitのリポジトリのアドレスであります

spring.cloud.config.server.git.searchPathsパスの設定ファイルであります

spring.cloud.config.server.git.label設定ファイルにある支店

spring.cloud.config.server.git.usernameは、それが総倉庫でspring.cloud.config.server.git.password場合は、空欄に記入することができます。

management.endpoints.web.exposure.include =バスリフレッシュは、私たちがポストマンによってクライアント構成ファイルを更新するために行くことができますので、バスリフレッシュリフレッシュポートのバスコンフィグサーバの設定ファイルを公開することであり、私はここにいることに注意してくださいリフレッシュする作業を行うための設定サーバの設定ファイルをしましょう。

3.3起動クラス(ConfigServerApplication)

@SpringBootApplication
@EnableDiscoveryClient
@EnableConfigServer
public class ConfigServerApplication {

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

開始CONFIG-サーバ、ポート8888、及び我々が完了するサーバを設定、次に示すように、ユーレカクラスタを登録するために、高可用性設定サーバインスタンスの複数の(異なるポート)を活性化します。

第四に、設定 - クライアントプロジェクトを作成します

設定 - クライアント:という名前の新しい春ブートプロジェクトを作成します。

4.1 Mavenの依存性

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.oal.microservice</groupId>
        <artifactId>openAiLab</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <groupId>com.oal.microservice</groupId>
    <artifactId>config-client</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-client</name>
    <description>config</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

4.2コンフィギュレーションファイル(bootstrap.yml)

spring:
  profiles:
    active: ${CONFIG_ACTIVE:dev}

---
spring:
  profiles: dev
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      discovery:
        enabled: true
        service-id: config-server
    bus:
      enabled: true
      trace:
        enabled: true
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest

server:
  port: 8881

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

注意这里配置文件必须是以bootstrap.*开头,不然无法从服务端获取到配置信息。

spirng.config.config.label 是资源文件所在分支,它默认会去git上找文件名为config-client-dev.*的文件进行读取,这里我们可以通过判断不同的运行环境来读取到不同环境的配置文件。

spring.cloud.config.profile 是运行环境,用来区分开发、测试、生产环境。

spring.cloud.config.discovery.service-id 是配置中心服务端实例名。

spring.profiles.active= ${CONFIG_ACTIVE:dev} ,这种写法会从环境变量中读取key为CONFIG_ACTIVE的值,如果环境变量中找不到就直接去默认值,这里默认值是dev。

4.3 启动类(ConfigClientApplication)

@SpringBootApplication
@EnableDiscoveryClient
@RefreshScope
@RestController
public class ConfigClientApplication {

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

    @Value("${foo}")
    String foo;

    @RequestMapping(value = "/hi")
    public String hi() {
        return foo;
    }
}

我们需要在客户端的启动类上添加@RefreshScope注解,表示当配置改变的时候进行刷新。

启动config-client工程,访问 http://localhost:8881/hi,浏览器返回结果:

    foo version 1

然后我们改变git仓库中config-client-dev.properties文件中的foo值为50,访问 http://localhost:8888/actuator查看config-server端暴露出来的对外端口,如图所示:

我们可以看到全局配置文件的刷新地址是

    http://localhost:8888/actuator/bus-refresh

我们可以通过指定{destination}来进行局部配置文件刷新,例如 http://localhost:8888/actuator/bus-refresh/config-client*

    http://localhost:8888/actuator/bus-refresh/{destination}

その後、我々の方法により、オープンアクセスの郵便配達郵便HTTP:// localhostを:8888 /アクチュエータ/バスリフレッシュ/ configにクライアント*は、図示のように:

ウェブブラウザの成功の後。  HTTP:// localhostを:8881 / HI、リターン結果

    foo version 50

それは成功したクライアント側の設定ファイルが壊れていたと説明しました。

高可用性分散構成・センター(春クラウドConfigが)構造を完了しました。

公開された352元の記事 ウォンの賞賛390 ビュー370 000 +

おすすめ

転載: blog.csdn.net/qq_19734597/article/details/90041582