SpringCloudサービス設定センター

SpringCloud Configを紹介

春クラウドConfigが二つの部分に、サーバーとクライアントに分かれて集中マイクロ外部のコンフィギュレーションのサポートを提供する分散システムインフラストラクチャとアプリケーションサービスのために作成された新規プロジェクト春のクラウドチームです。サービスセンター側は、分散構成と呼ばれており、それは独立したマイクロサービスアプリケーション、倉庫および構成情報、暗号化/復号化情報アクセスインタフェース等へのアクセスを提供するためにクライアントを接続するように構成され、そしてクライアントは、マイクロサービスアーキテクチャであります個々のマイクロサービスアプリケーションやインフラストラクチャが、彼らは、ブート時にコンフィギュレーション・センターからアプリケーションのリソースと設定指定された設定の中心を通るビジネス関連コンテンツ、および取得とロードの設定情報を管理します。外部のSpringアプリケーションを構築するだけでなく、適用されますので、春の雲コンフィグは、サーバとクライアントの環境変数と構成プロパティの抽象マッピングを達成するため、また他の言語で実行する任意のアプリケーションで使用することができます。春クラウドコンフィグストアの構成情報にGitリポジトリを使用して、コンフィギュレーション・センターのデフォルトを達成するので、春クラウドコンフィグはマイクロサービスアプリケーションの構成情報のためのサポートバージョン管理に自然に構築構成サーバーを使用して、管理のGitのクライアントツールで簡単にできます設定およびアクセス内容。GITリポジトリ、SVNリポジトリ、ローカライゼーション・ファイル・システム:もちろん、それはまたのような他の保存方法、サポートを提供します。

Configをサーバー側のプライマリとのGit / SVNサーバ

人気のポイントは、ハンドオーバーの環境設定が容易含め、管理を統一し、コード、心配と労力を移動せずに設定を変更しています。

あなたがSpringCloudバスを過ごす場合は、再起動せずに達成し、自動検出の設定変更や新しい構成を適用することができます。

リモート設定を読み取るために遠隔GITリポジトリをリンクするCONFIGSERVERを提唱する前SpringCloudチャートによれば、第一工程と、

ここGIT倉庫、我々は一般的にGitHubの使用  https://github.com/を、コードまたはクラウド   https://gitee.com/  

私たちは、GitHubのとここに示されました

建个仓库 microservice-config  然后 Git下载本地;

上传一个配置文件上到git仓库,application.yml 记住要utf-8编码,否则乱码,解析各种问题;

文件内容

profile:hello

 

随便搞,目前只要能读取到配置即可;

新建module:microservice-config-server-4001

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
</dependency>

完整pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.liuwenwu</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>microservice-config-server-4001</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

    </dependencies>

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

</project>

启动类ConfigServerApplication_4001:

package com.liuwenwu.microserviceconfigserver4001;

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

@SpringBootApplication
@EnableConfigServer
public class MicroserviceConfigServer4001Application {

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

}

这里要加下注解:@EnableConfigServer

这里我们搞下仓库的Http地址:

然后项目的application.yml配置下:

server:
  port: 4001

spring:
  application:
    name:  microservice-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/liuwenwu1/lww-microservice-config.git
        

主要是要配置一个git请求地址:

本地Hosts加个本地域名映射:

127.0.0.1  configserver.liuwenwu.com

然后我们请求:http://configserver.liuwenwu.com:4001/application-xxx.yml 

返回结果了正确的文本结果;

至于请求路径,有匹配规则:

The HTTP service has resources in the form:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

参考:

https://www.cnblogs.com/hellxz/p/9306507.html

Config Client基本使用

根据前面的config原理图,我们需要建立Client端调用server端,最终实现client端获取远程git配置信息;

为了后面演示方便,我们提交三个配置文件到远程git库;

application.yml:

---
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
port: 111
---
spring:
  profiles: test
port: 222

crm-dev.yml

port:
  777

crm-test.yml

port:
  888

然后我们新建一个module  microservice-config-client-5001

加下依赖:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

我们项目启动的时候,就要调用server config端,获取配置信息,所以这里要bootstrap.yml配置文件,优先级最高:

spring:
  application:
    name: application-dev
  cloud:
    config:
      name: crm
      uri: http://configserver.liuwenwu.com:4001
      profile: test
      label: master
      fail-fast: true

application.yml:

server:
  port: 5001
  context-path: /

再搞一个 ConfigClientController 类 测试显示端口:

package com.liuwenwu.microserviceconfigclient5001.controller;

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

@RestController
public class ConfigClientController {
 
    @Value("${port}")
    private String port;
 
    @GetMapping("/getPort")
    public String getPort() {
        return "测试你访问的yml文件的端口是:【"+port+"";
    }

}

启动类:ConfigClientApplication_5001

package com.liuwenwu.microserviceconfigclient5001;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MicroserviceConfigClient5001Application {

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

}

最后 本地hosts我们加给配置:

127.0.0.1  client-config.liuwenwu.com

我们启动项目:然后页面访问:

http://client-config.liuwenwu.com:5001/getPort

即可获取远程端口配置信息;

 

 

 

 

 

Config整合Eureka

我们现在搞个实例来演示下,eureka整合config以及服务器提供者整合config,这样大伙可以举一反一,方便理解;

首先是eureka整合config

我们先搞个配置文件到git;

eureka_config.yml

spring:
  profiles:
    active:
     - test
---
server:
  port: 2004
  context-path: /
spring:
  profiles: dev
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
---
server:
  port: 2005
  context-path: /
spring:
  profiles: test
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

新建module microservice-eureka-server-config

pom.xml:

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

bootstrap.yml

spring:
  application:
    name: microservice-eureka-server-config
  cloud:
    config:
      name: eureka_config
      uri: http://configserver.liuwenwu.com:4001  # 配置configserver地址
      profile: dev  # 级别
      label: master  # 分支 git中 默认master

application.yml

spring:
  application:
    name: microservice-eureka-server-config

启动类:

package com.liuwenwu.microserviceeurekaserverconfig;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class MicroserviceEurekaServerConfigApplication {

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

}

我们启动 microservice-config-server-4001

再启动 microservice-eureka-server-config

测试连接

http://localhost:2004/   或

http://eureka2001.liuwenwu.com:2004/

说明成功读取远程Git配置,然后eureka启动OK;

然后我们就是把服务提供者和config整合,把服务提供者注册到eureka;

我们搞个配置provider_config.yml,push到远程GIT;

spring:
  profiles:
    active: dev
---
server:
  port: 1007
  context-path: /

# 数据源配置
spring:
  profiles: dev
  application:
    name: microservice-student
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/t224?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1007
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:2004/eureka

info:
  groupId: com.liuwenwu.springcloud
  artifactId: microservice-student-provider-config-1007
  version: 1.0-SNAPSHOT
  userName: http://liuwenwu.com
  phone: 123456
---
server:
  port: 1008
  context-path: /

# 数据源配置
spring:
  profiles: test
  application:
    name: microservice-student
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/t224?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1008
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:2004/eureka

info:
  groupId: com.liuwenwu.springcloud
  artifactId: microservice-student-provider-config-1008
  version: 1.0-SNAPSHOT
  userName: http://liuwenwu.com
  phone: 123456

新建module:microservice-student-provider-config

pom.xml:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.liuwenwu</groupId>
        <artifactId>springcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>microservice-student-provider-config</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.liuwenwu</groupId>
            <artifactId>microservice-common</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>com.liuwenwu</groupId>
            <artifactId>microservice-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <!--添加注册中心Eureka相关配置-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <!-- actuator监控引入 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>


    </dependencies>

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

</project>

bootstrap.yml:

spring:
  application:
    name: microservice-student-provider-config
  cloud:
    config:
      name: provider_config
      uri: http://configserver.liuwenwu.com:4001  # 配置configserver地址
      profile: dev  # 级别
      label: master  # 分支 git中 默认master

application.yml

spring:
  application:
    name: microservice-student-provider-config

其他类文件从 原先的服务提供者里直接复制一份即可,这里不贴了;

启动下这个项目;

说明成功注册到服务注册中心了;

 

Config配置搜索路径

前面我们所有的GIT远程端配置文件都是跟目录的,所有请求默认都是根目录,但是有时候,项目很多,配置文件需要根据子目录来划分,这时候,就需要来配置搜索路径了;比如aaa项目的配置文件放aaa目录下,bbb项目的配置文件放bbb目录下,不配置的话 是找不到的那些配置文件的,我们需要配置search-paths属性实现;

 

microservice-config-server-4001 configserver端 加个配置 

server:
  port: 4001

spring:
  application:
    name:  microservice-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/liuwenwu1/lww-microservice-config.git
          search-paths: aaa,bbb

 

 

分别搞3个目录aaa,bbb,ccc 里面分别放3个配置文件 nns.yml,nns2.yml,nn3.yml;

配置内容大体差不多,随便写;

spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
name: aaadev
---
spring:
  profiles: test
name: aaatest

然后传到远端git;

我们启动:microservice-config-server-4001

浏览器:http://configserver.liuwenwu.com:4001/nns-test.yml

 

 

 

 

 

因为没配置 ccc这个搜索路径 所有 nn3里的找不到;

 

おすすめ

転載: www.cnblogs.com/liuwenwu9527/p/11944990.html
おすすめ