美文网首页
《九》、springcloud微服务——Config分布式配置中

《九》、springcloud微服务——Config分布式配置中

作者: 神奇作手 | 来源:发表于2019-07-28 15:12 被阅读0次

一、概述

1、分布式系统面临的配置问题

  微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务,由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题,我们每一个微服务自己带着一个application.yml,上百个配置文件的管理....

2、是什么?

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

3、怎么玩?

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

  由于SpringCloud Config默认使用Git来存储配置文件(也有其它方式,比如支持SVN和本地文件),但最推荐的还是Git,而且使用的是http/https访问形式

二、SpringCloud Config 服务端配置

(1)、用自己的GitHub账号在GitHub 上新建一个名为 microservicecloud-config 的新Repository


(2)、由上一步获得SSH协议的git地址:git@github.com:ll996960921/microservicecloud-config.git
(3)、本地磁盘目录上新建git仓库clone
    |--- 本地地址:E:\my-project\190728\mySpringCloud
    |--- git 命令:git clone git@github.com:ll996960921/microservicecloud-config.git
(4)、本地 E:\my-project\190728\mySpringCloud 里面新建一个 application.yml
    |--- yml内容如下:
spring:
profiles:
  active:
   - dev
   
---
spring:
 profiles: dev #开发环境
 application:
  name: microservicecloud-config-ll-dev
   
---
spring:
 profiles: test #测试环境
 application:
   name: microservicecloud-config-ll-dev
   
# 请保存为UTF-8格式
     

    |--- 格式必须为UTF-8

(5)、将上一步的yml文件推送到github上
    |--- git add .
    |--- git commit -m "init file"
    |--- git push origin master
    |--- 步骤结果:

      |--- 命令清单
      |---GitHub

(6)、新建Module模块 microservicecloud-config-3344 它即为Cloud的配置中心模块

(7)、pom文件
    |--- 修改内容:

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

    |--- 全部内容:

<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>cn.smilexl.springcloud</groupId>
      <artifactId>microservicecloud</artifactId>
      <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservicecloud-config-3344</artifactId>
  
  <dependencies>
      <!-- springCloud Config -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-config-server</artifactId>
      </dependency>
      <!-- 避免Config的Git插件报错:>org/eclipse/jgit/api/TransportConfigCallback -->
      <dependency>
          <groupId>org.eclipse.jgit</groupId>
          <artifactId>org.eclipse.jgit</artifactId>
          <!-- <version>4.10.0.201712302008-r</version> -->
      </dependency>
      <!-- 图形化监控 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-jetty</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>
      </dependency>
      <!-- 热部署插件 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
      </dependency>
  </dependencies>

</project>


(8)、yml文件

server: 
 port: 3344 
 
spring:
 application:
   name: microservicecloud-config
 cloud:
  config:
    server:
      git:
        uri: https://github.com/ll996960921/microservicecloud-config.git    #GitHub上面的git仓库名字
              

(9)、创建主启动类 Config_3344_StartSpringCloudApp.java 并添加
@EnableConfigServer 注解

package cn.smilexl.springcloud;

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

@EnableConfigServer
@SpringBootApplication
public class Config_3344_StartSpringCloudApp {
  
  public static void main(String[] args) {
      SpringApplication.run(Config_3344_StartSpringCloudApp.class, args);
  }

}

(10)、windows下修改host文件,增加映射 127.0.0.1 config-3344.com

(11)、测试通过 Config 微服务是否可以从 GitHub 上获取配置内容
     |--- 启动微服务3344
     |--- http://config-3344.com:3344/application-dev.yml
     |--- http://config-3344.com:3344/application-testyml
     |--- http://config-3344.com:3344/application-xxx.yml(不存在的配置)

(12)、配置读取规则
     |--- /{application}-{profile}.yml
       |--- http://config-3344.com:3344/application-dev.yml
       |--- http://config-3344.com:3344/application-test.yml
       |--- http://config-3344.com:3344/application-xxx.yml(不存在的配置)
     |--- /{application}/{profile}/[/{label}]
       |--- http://config-3344.com:3344/application/dev/master
       |--- http://config-3344.com:3344/application/test/master
       |--- http://config-3344.com:3344/application/xxx/master
     |--- /{label}/{application}-{profile}.yml
       |--- http://config-3344.com:3344/master/application-dev.yml
       |--- http://config-3344.com:3344/master/application-test.yml

(13)、成功实现了用SpringCloud Config 通过Github 获取配置信息

三、SpringCloud Config 客户端配置与测试

(1)、在本地 E:\my-project\190728\mySpringCloud\microservicecloud-config 路径下新建文件 microservicecloud-config-client.yml

(2)、 microservicecloud-config-client.yml 中添加如下内容

spring:
 profiles:
   active:
   - dev

---
server:
 port: 8201
spring:
 profiles: dev
 application:
   name: microservicecloud-config-client
eureka:
 client:
   service-url:
     defaultZone: http://eureka-dev.com:7001/eureka/   
 
---
server:
 port: 8202
spring:
 profiles: test
 application:
   name: microservicecloud-config-client
eureka:
 client:
   service-url:
     defaultZone: http://eureka-test.com:7001/eureka/   
     

(3)、将上一步的 microservicecloud-config-client.yml 文件提交到GitHub上

(4)、新建 microservicecloud-config-client-3355

(5)、pom文件
    |--- 修改内容:

 <!-- springCloud Config客户端 -->
 <dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-config</artifactId>
 </dependency>

    |--- 全部内容:

<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>cn.smilexl.springcloud</groupId>
      <artifactId>microservicecloud</artifactId>
      <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservicecloud-config-client-3355</artifactId>

  <dependencies>
      <!-- springCloud Config客户端 -->
      <dependency>
          <groupId>org.springframework.cloud</groupId>
          <artifactId>spring-cloud-starter-config</artifactId>
      </dependency>
      <!-- 图形化监控 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-actuator</artifactId>
      </dependency>
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-jetty</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>
      </dependency>
      <!-- 热部署插件 -->
      <dependency>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-devtools</artifactId>
      </dependency>
  </dependencies>

</project>


(6)、新建bootstrap.yml文件
    |--- application.yml 是用户级的资源配置项
    |--- bootstrap.yml 是系统级的,优先级更高
      |--- SpringCloud 会创建一个 `Bootstrap Context`,作为Spring应用的`Application Context`的父上下文。初始化的时候,`Bootsrap Context`负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的`Environment`。`Bootstrap`属性有高优先级,默认情况下,他们不会被本地配置覆盖。`Bootstrap Context`和`Application Context`有着不同的约定,所以新增了一个`Bootstrap.yml`文件,保证`Bootstrap Context`和`Application Context`配置分离。
      |--- 内容如下:

spring:
 cloud:
   config:
     name: microservicecloud-config-client #需要从GitHub上读取的资源名称,注意没有yml后缀名
     profile: dev #本次访问的配置项
     label:  master
     uri: http://config-3344.com:3344 #本微服务启动后先去找3344号服务,通过SpringCloudConfig获取GitHub的服务地址
  

(7)、新建application.yml文件

spring:
 application:
   name: microservicecloud-config-client

(8)、windows 下修改host文件,增加映射 127.0.0.1 client-config.com

(9)、主启动类 Config_3355_StartSpringCloudApp.java

package cn.smilexl.springcloud;

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

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

(10)、新建rest类,验证是否能从GitHub上读取配置
   类 ConfigClientRest.java

package cn.smilexl.springcloud.res;

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

@RestController
public class ConfigClientRest {
  
  @Value("${spring.application.name}")
  private String applicationName;
  
  @Value("${eureka.client.service-url.defaultZone}")
  private String eurekaServers;
  
  @Value("${server.port}")
  private String port;
  
  @GetMapping("/config")
  public String getConfig() {
      String str = "appliactionName: "+applicationName+ "-------eurekaServers: "
              +eurekaServers+"-----------port: "+port;
      System.out.println("*******str: "+str);
      return str;
  }
}

(11)、测试
   |--- 启动Config配置中心3344微服务并自测:http://config-3344.com:3344/application-dev.yml
   |--- 启动3355作为Client准备访问
   |--- bootstrap.yml 里面的 profile 值是什么,决定从GitHub上读取什么
     |--- 假如目前是 profile: dev
       |--- dev默认在GitHub上对应的端口是8201
       |--- http://client-config.com:8201/config
     |--- 假如目前是 profile: tes
       |--- test默认在GitHub上对应的端口是8202
       |--- http://client-config.com:8202/config

(12)、成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息

四、SpringCloud Config 配置实战

1、目前情况

(1)、Config服务端配置,配置OK且测试通过,我们可以和config+GitHub进行配置修改并且获得内容
(2)、此时我们做一个eureka服务+一个Dept访问的微服务,将两个微服务的配置统一由GitHub获得实现统一配置分布式管理,完成多环境的变更。

2、步骤

(1)、Git配置文件本地配置

相关文章

网友评论

      本文标题:《九》、springcloud微服务——Config分布式配置中

      本文链接:https://www.haomeiwen.com/subject/gozurctx.html