侧边栏壁纸
博主头像
张种恩的技术小栈博主等级

行动起来,活在当下

  • 累计撰写 747 篇文章
  • 累计创建 65 个标签
  • 累计收到 39 条评论

目 录CONTENT

文章目录

SpringCloud(13)之Config配置中心

zze
zze
2018-11-05 / 0 评论 / 0 点赞 / 627 阅读 / 11840 字

不定期更新相关视频,抖音点击左上角加号后扫一扫右方侧边栏二维码关注我~正在更新《Shell其实很简单》系列

前言

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

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

SpringCloud Config 是什么

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

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

服务端也成为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息。

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用 git 来存储配置信息,这样有助于对环境配置进行版本管理,并且可以通过 git 客户端工具来方便的管理和访问配置内容。

SpringCloud Config 的作用

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

使用

Config 服务端

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

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

2、新建名为 "microservicecloud-config-3344" 的子工程作为配置中心服务,依赖如下:

<!-- 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 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>

3、配置仓库地址:

# application.yml
server:
  port: 3344

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

4、编写主启动类,使用注解启用配置中心功能:

# zze.springcloud.Application_3344
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);
    }
}

5、测试:

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

image.png

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 客户端

1、新建如下配置文件,提交到 GitHub 的配置仓库中:

# 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://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

2、新建名为 "microservicecloud-config-client-3355" 的子工程作为使用配置中心的客户端,依赖如下:

<!-- 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 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>

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

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

4、新建主启动类:

// zze.springcloud.Application_3355
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);
    }
}

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

// zze.springcloud.controller.ClientConfigController
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);
    }
}

6、测试:

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

image.png

可以看到成功获取并应用了 github 中的配置项

“application.yml”是用户级的资源配置项,而“bootstrap.yml”是系统级的,优先级更高。
SpringCloud 会创建一个“Bootstrap Context”,作为 Spring 应用的“Application Context”的父级上下文。初始化时,“Bootstrap Context”负责从外部源加载配置属性并解析配置。这两个上下文共享一个外部获取的“Environment”。
“Bootstrap”属性有高优先级,默认情况下,它们不会被本地配置覆盖。“Bootstrap Context”和“Application Context”有着不同的约定,所以新增了一个“bootstrap.yml”文件,保证“Bootstrap Context”和“Application Context”配置的分离。
完整示例下载:https://pan.baidu.com/s/1lXzRD0_WhjAglfJAa4DkaA 提取码:3lb2

0

评论区