zoukankan      html  css  js  c++  java
  • SpringCloudConfig相关配置简介、使用、整合Eureka

    目的:

      1、SpringCloud Config简介

      2、Config Server基本使用

      3、Config Client基本使用

      4、Config整合Eureka

      5、Config配置搜索路径


    SpringCloud Config简介

     SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。

     SpringCloud Config分为服务端和客户端两部分。
     服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
     客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
    用途:
     集中管理配置文件。
     不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release。
     运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心。
     统一拉取配置自己的信息。
     当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置。
     将配置信息以REST就接口的形式暴露。

    Config Server端主要和Git/SVN服务器

    通俗点,就是统一管理配置,包括方便切换环境配置,以及修改配置无需动代码,省心省力;

    如果用上SpringCloud Bus,能实现无需重启,自动感知配置变化以及应用新配置;


    Config Server基本使用

    首先第一步,要搞个 configServer来联通远程GIT仓库,来读取远程配置;

    这里GIT仓库,我们一般选用GitHub https://github.com/,或者码云  https://gitee.com/  

    我们这里GitHub演示github官网:https://github.com/

    关于github在window系统中使用可以查看博客https://www.cnblogs.com/huangting/p/11684508.html

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

    把仓库克隆到本地仓库以后把项目中的application.yml文件复制进去在push到github仓库中,测试yml文件可否上传到github

    其中出现的window转义问题解决可看我上篇博客:https://www.cnblogs.com/huangting/p/11945774.html

    成功。那么我们来玩一下Springcloudserver和github仓库的配置文件交互

    首先在ideaul中创建一个Springboot工程,microservice-config-server-4001

    然后在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.ht</groupId>
            <artifactId>htSpringCloud</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中添加注释@EnableConfigServer

    package com.ht.microserviceconfigserver4001;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    @EnableConfigServer
    @SpringBootApplication
    public class MicroserviceConfigServer4001Application {
    
        public static void main(String[] args) {
            SpringApplication.run(MicroserviceConfigServer4001Application.class, args);
        }
    
    }

    然后配置yml文件,给它添加一段集合git的url (记住访问git的http地址而不是ssh地址)

    server:
      port: 4001
    
    spring:
      application:
        name:  microservice-config
      cloud:
        config:
          server:
            git:
              uri: https://github.com/haungting/htmicroservice-config.git 
    //从git你的仓库里面复制下来的http地址,url变化因人而异,这样git才可以请求

    单单是这样还不行,我们还要配置本地映射:

    找到本机C:WindowsSystem32driversetc 地址下的host文件

    加以下配置:

    127.0.0.1  configserver.ht.com

     

    然后我们请求:http://configserver.ht.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基本使用

      这里我们需要建立Client端去调用server端,然后实现client端获取远程git配置信息

    因为测试需要所以我们提交三个配置文件到远程git库

    application.yml:

     

      active: dev 这里设置获取的文件名

     

     

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

    crm-dev.yml:

    port:
      777

    crm-test.yml:

    port:
      888

    然后我们新建一个springboot工程  microservice-config-client-5001

     给项目pom文件中添加依赖并且修改父id:

    <parent>
            <groupId>com.ht</groupId>
            <artifactId>htSpringCloud</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
    
    
    <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>

    因为git在读取配置文件的之后最先读取bootstrap.yml,并且bootstrap.yml中存放的都是项目中通用不改变的配置,

    我们项目启动的时候,要调用server config端,获取配置信息

    所以这里要bootstrap.yml配置文件

    spring:
      application:
        name: application-dev
      cloud:
        config:
          name: crm  //做拼接的,所以必须要和git上面的文件开头名一样
          uri: http://configserver.ht.com:4001
          profile: test //这里去设置获取的端口文件名,根据上面的文件这里默认获取test那么端口就是888
          label: master
          fail-fast: true

     这里给git配置了请求那么也需要配置本地映射否则无法访问:

    找到本机C:WindowsSystem32driversetc 地址下的host文件添加配置:

    127.0.0.1  client-config.ht.com

    我们再来添加一个动态获取git仓库里面存放端口的文件类

    ConfigClientController

    package com.ht.microserviceconfigclient5001.controller;
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * 这个类可以根据git中yml文件配置更换去动态获取端口
     */
    @RestController
    public class ConfigClientController {
     
        @Value("${port}")
        private String port;
     
        @GetMapping("/getPort")
        public String getPort() {
            return "测试你访问的yml文件的端口是:【"+port+"】";
        }
    
    }

    启动类不需要添加注释这里就不贴代码了

    先启动4001,在启动5001

    然后页面访问:

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

    因为项目中bootstrap.yml文件中获取test文件中的端口所以获取的是888

    根据上面push到git仓库的文件进行访问测试调用端口

    http://configserver.ht.com:4001/application-dev.yml

     通过git仓库中application.yml文件去调用dev文件的端口

    http://configserver.ht.com:4001/application-test.yml

     通过git仓库中application.yml文件去调用dev文件的端口

     

    我们进去git仓库中去修改active获取的文件为test

     

    然后在去访问

    很显然active环境变成test


    Config整合Eureka

       eureka整合config以及服务器提供者整合config

      首先是eureka整合config

      我们先搞个配置文件到git

    spring:
      profiles:
        active:
        - dev
    ---
    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/

     然后在push到仓库中去

    新建Springboot工程:microservice-eureka-server-config-2004

    添加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.ht.com:4001  # 配置configserver地址
          profile: dev  # 级别
          label: master  # 分支 git中 默认master

    application.yml

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

    启动类:

    package com.ht.microserviceeurekaserverconfig2004;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
    
    @SpringBootApplication
    @EnableEurekaServer
    public class MicroserviceEurekaServerConfig2004Application {
    
        public static void main(String[] args) {
            SpringApplication.run(MicroserviceEurekaServerConfig2004Application.class, args);
        }
    
    }

    先启动 microservice-config-server-4001

    再启动 microservice-eureka-server-config-2004

    测试连接

    http://localhost:2004/

    http://eureka2001.ht.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/test?useUnicode=true&characterEncoding=utf8
        username: root
        password: root
      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.ht.htSpringCloud
      artifactId: microservice-student-provider-config-1007
      version: 1.0-SNAPSHOT
      userName: http://ht.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/test?useUnicode=true&characterEncoding=utf8
        username: root
        password: root
      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.ht.htSpringCloud
      artifactId: microservice-student-provider-config-1008
      version: 1.0-SNAPSHOT
      userName: http://ht.com
      phone: 123456

    新建module:microservice-student-provider-config-1004

    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.ht</groupId>
            <artifactId>htSpringCloud</artifactId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <artifactId>microservice-student-provider-config</artifactId>
    
        <properties>
            <java.version>1.8</java.version>
        </properties>
    
        <dependencies>
            <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.ht</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.ht.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/haungting/htmicroservice-config.git
              search-paths: aaa,bbb

    搞2个目录aaa,bbb 里面分别放3个配置文件 nns1.yml,nns2.yml

    根据github的application.yml文件去写就行啦,差不多就这样

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

    然后push到远程仓库git

    启动:microservice-config-server-4001

    在浏览器中输入:http://configserver.ht.com:4001/nns2-test.yml或者http://localhost:4001/nns-test.yml

    谢谢观看!!

  • 相关阅读:
    eclipse折叠快捷键
    ubuntu下忘记用户名和密码的解决办法
    ubuntu屏幕分辨率问题
    CentOS 5.X安装LAMP最高版本环境
    CentOS 6.X安装LAMP最高版本环境
    Shell脚本升级CentOS php版本v
    运行yum报错Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again
    批处理测试局域网网络连通性ping1-255
    批处理cmd背景颜色
    CentOS6.4安装LAMP环境
  • 原文地址:https://www.cnblogs.com/huangting/p/11946401.html
Copyright © 2011-2022 走看看