java框架之SpringCloud(7)-Config分布式配置中心

前言

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

微服务意味着要将单体应用中的业务拆分红一个个子服务,每一个服务的粒度相对较小,所以系统中标会出现大量的服务。因为每一个服务都须要必要的配置信息才能运行,因此一套集中式的、动态的配置管理设施是必不可少的。咱们每个微服务本身有一个 application.yml 文件,若是有上百个这样的文件维护起来确定容易让人崩溃,因此 SpringCloud 提供了 ConfigServer 来解决这个问题。git

SpringCloud Config是什么

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

SpringCloud Config 分为服务端和客户端两部分:web

  • 服务端也成为分布式配置中心,它是一个独立的微服务应用,用来链接配置服务器并为客户端提供获取配置信息。
  • 客户端则是经过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用 git 来存储配置信息,这样有助于对环境配置进行版本管理,而且能够经过 git 客户端工具来方便的管理和访问配置内容。

SpringCloud Config的做用

  • 集中管理配置文件。
  • 不一样环境不一样配置,动态化的配置更新,分环境部署好比 dev/test/prod/beta/release。
  • 运行期间动态调整配置,再也不须要在每一个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置本身的信息。
  • 当配置发生变更时,服务不须要重启便可感应到配置的变化并应用新的配置。
  • 将配置信息以 REST 接口的形式暴露。

使用

Config服务端

一、在 GitHub 新建一个名为 "microservicecloud-config" 的 Repository,提交以下编码为 UTF-8 的文件:spring

spring:
  profiles:
    active:
      - dev
---
spring:
  profiles: dev
  application:
    name: microservicecloud-config-dev
---
spring:
  profiles: test
  application:
    name: microservicecloud-config-test
application.yml

二、新建名为 "microservicecloud-config-3344" 的子工程做为配置中心服务,依赖以下:apache

<?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">
    <parent>
        <artifactId>microservicecloud</artifactId>
        <groupId>zze.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservicecloud-config-3344</artifactId>

    <dependencies>
        <!-- SpringCloud Config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- 图形化监控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 熔断 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>
pom.xml

三、配置仓库地址:bootstrap

server:
  port: 3344

spring:
  application:
    name: microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/zze326/microservicecloud-config.git # 对应配置文件的 git 仓库连接
application.yml

四、编写主启动类,使用注解启用配置中心功能:服务器

package zze.springcloud;

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

@SpringBootApplication
@EnableConfigServer // 标识当前服务为配置中心
public class Application_3344 {

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

五、测试:架构

启动项目,访问 http://localhost:3344/application-dev.yml,能够看到,返回内容为 gibhub 中存放的配置:

test
HTTP服务具备如下格式的资源:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其中“应用程序”做为 SpringApplication 中的 spring.config.name 注入(即常规的 Spring Boot 应用程序中一般是“应用程序”),“配置文件”是活动配置文件(或逗号分隔列表的属性),“label”是可选的 git 标签(默认为“master”)。

Config客户端

一、新建以下配置文件,提交到 GitHub 的配置仓库中:app

spring:
  profiles:
    active:
      - dev

---
server:
  port: 8201
spring:
  profiles: dev
  application:
    name: microservicecloud-config-client

eureka:
  client:
    service-url:
      defaultZone: http://www.eurekaserver1.com:7001/eureka

---
server:
  port: 8202
spring:
  profiles: test
  application:
    name: microservicecloud-config-client

eureka:
  client:
    service-url:
      defaultZone: http://www.eurekaserver1.com:7001/eureka
microservicecloud-config-client.yml

二、新建名为 "microservicecloud-config-client-3355" 的子工程做为使用配置中心的客户端,依赖以下: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">
    <parent>
        <artifactId>microservicecloud</artifactId>
        <groupId>zze.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <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.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</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</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>
pom.xml

三、新建系统级配置文件,指定配置中心地址和要读取环境的配置:

spring:
  cloud:
    config:
      name: microservicecloud-config-client # 须要从 github 上读取的资源名称
      profile: dev # 环境,决定读取哪一个环境的配置内容
      label: master
      uri: http://localhost:3344 # SpringCloud Config Server 地址
bootstrap.yml

四、新建主启动类:

package zze.springcloud;

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

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

五、新建获取配置信息的 Controller:

package zze.springcloud.controller;

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

/**
 * 经过 config 客户端从 config server 获取配置信息
 */
@RestController
public class ClientConfigController {

    @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="applicationName = %s <br> eurekaServer = %s <br> port = %s";
        return String.format(str, applicationName, eurekaServers, port);
    }
}
zze.springcloud.controller.ClientConfigController

六、测试:

一、启动 7001 Eureka Server
二、启动 3344 配置中心服务
三、启动 "microservicecloud-config-client-3355" 服务,能够看到占用端口为 github 配置文件中 dev 环境配置下的 8201,访问 localhost:8201/config:

能够看到成功获取并应用了 github 中的配置项
test

“application.yml”是用户级的资源配置项,而“bootstrap.yml”是系统级的,优先级更高。

SpringCloud 会建立一个“Bootstrap Context”,做为 Spring 应用的“Application Context”的父级上下文。初始化时,“Bootstrap Context”负责从外部源加载配置属性并解析配置。这两个上下文共享一个外部获取的“Environment”。

“Bootstrap”属性有高优先级,默认状况下,它们不会被本地配置覆盖。“Bootstrap Context”和“Application Context”有着不一样的约定,因此新增了一个“bootstrap.yml”文件,保证“Bootstrap Context”和“Application Context”配置的分离。

完整示例下载 | 提取码:3lb2

相关文章
相关标签/搜索