Spring Cloud实战系列(六) - 分布式配置中心Spring Cloud Config

相关

  1. Spring Cloud实战系列(一) - 服务注册与发现Eureka php

  2. Spring Cloud实战系列(二) - 客户端调用Rest + Ribbon java

  3. Spring Cloud实战系列(三) - 声明式客户端Feign git

  4. Spring Cloud实战系列(四) - 熔断器Hystrix github

  5. Spring Cloud实战系列(五) - 服务网关Zuul web

  6. Spring Cloud实战系列(六) - 分布式配置中心Spring Cloud Configspring

  7. Spring Cloud实战系列(七) - 服务链路追踪Spring Cloud Sleuthapache

  8. Spring Cloud实战系列(八) - 微服务监控Spring Boot Admin编程

  9. Spring Cloud实战系列(九) - 服务认证受权Spring Cloud OAuth 2.0 bootstrap

  10. Spring Cloud实战系列(十) - 单点登陆JWT与Spring Security OAuth 后端

前言

分布式系统 中,因为服务数量巨多,为了方便 服务配置文件统一管理实时更新,因此须要 分布式配置中心 组件。 Spring Cloud 提供的 分布式配置中心 组件是 Spring Cloud Config,它支持将 配置服务 放在配置服务的 内存 中(即 本地),也支持放在 远程 Git 仓库中。Spring Cloud Config 提供了两个角色,一个是 Config Server,二个是 Config Client

正文

1. Config Server从本地读取配置文件

建立一个新的 Spring Boot 项目模块,取名为 config-server,它的 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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.github.ostenant.springcloud</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-server</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
    </properties>

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

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
复制代码

在应用启动类上使用 注解 @EnableConfigServer 开启 配置服务器

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}
复制代码

须要在程序的 配置文件 application.properties 里面进行以下配置。经过 spring.profile.active=native 来配置 Config Server本地读取配置,读取的路径为 Classpath 下的 shared 目录。

server:
 port: 8769
spring:
 application:
 name: config-server
 profiles:
 active: native
 cloud:
 config:
 server:
 native:
 search-locations: classpath:/shared
复制代码

src/main/resources 目录下新建 shared 文件夹,在 shared 文件夹下新建一个 config-client-dev.yml 文件。

server:
 port: 8762
foo: foo version 1
复制代码

运行 ConfigServerApplicationmain() 方法,在 8769 端口启动 Config Server 应用程序。

2. 构建Config Client

建立一个 Spring Boot 项目模块,取名为 config-client,它的 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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.3.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>io.github.ostenant.springcloud</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Dalston.RELEASE</spring-cloud.version>
    </properties>

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

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
复制代码

resources 目录下新建 bootstrap.yml 文件,由于 bootstrap 相对于 application 具备 优先的执行顺序

变量 {spring.application.name}{spring.profiles.active},二者使用 “-” 相连,做为 Config ClientConfig Server 读取的 配置文件名

bootstrap.yml

spring:
 application:
 name: config-client
 cloud:
 config:
 uri: http://localhost:8769
 fail-fast: true # 读取没有成功,执行快速失败
 profiles:
 active: dev
复制代码

配置一个接口,用于测试 读取配置文件foo 变量,并经过 API 接口返回客户端。

@RestController
@SpringBootApplication
public class ConfigClientApplication {

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

    @Value("${foo}")
    private String foo;

    @RequestMapping(value = "/foo")
    public String foo(){
        return foo;
    }
}
复制代码

启动 config-client 应用程序,访问 http://localhost:8762/foo,服务端的响应数据以下:

foo version 1

可见 config-client 成功地从 config-server 项目的 shared 本地文件目录 读取到 配置文件 config-client-dev.yml 中的 foo 变量。

3. Config Server从远程Git仓库读取配置文件

修改 config-server 的配置文件 application.yml,代码以下.

server:
 port: 8769
spring:
 application:
 name: config-server
 cloud:
 config:
 server:
 git:
 uri: https://coding.net/ostenant/config-repo
 search-paths: test
 username: ostenant@163.com
 password: xxxx
 label: master
复制代码

若是 Git 仓库为 公开仓库,能够不填写 用户名密码;若是是 私有仓库 则须要填写,本例子配置了一个 私有仓库

配置 解释
spring.cloud.config.server.git.uri 配置git仓库地址
spring.cloud.config.server.git.searchPaths 配置仓库路径
spring.cloud.config.label 配置仓库的分支
spring.cloud.config.server.git.username 访问git仓库的用户名
spring.cloud.config.server.git.password 访问git仓库的用户密码

远程仓库 https://coding.net/ostenant/config-repo 中有个 名为 config-client-dev.properties配置文件,里面配置有一个属性:

foo = foo version 2
复制代码

从新启动应用程序 config-serverconfig-client,再次访问 http://localhost:8762/foo,服务端的响应数据以下:

foo version 2

可见,config-server 从远程 Git 仓库读取了 配置文件,进一步 config-clientconfig-server 读取了相关的 配置属性

4. 构建高可用的Config Server

配置中心 config-server 作成一个 微服务,而且将其 集群化,从而实现 高可用

4.1. 改造Config Server

Config Server 中引入 Eureka 客户端的 起步依赖 spring-cloud-starter-eureka

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
复制代码

在应用的 启动类 上加上 注解 @EnableEurekaClient,将 Config Server 注册到 Eureka Server 上面。

在配置文件 application.yml 加入 服务注册地址

eureka:
 client:
 service-url:
 defaultZone: http://locahost:8761/eureka/
复制代码

4.2. 改造Config Client

一样的在 Config Client 中引入 Eureka 客户端的 起步依赖 spring-cloud-starter-eureka

<dependency>
     <groupId>org.springframework.cloud</groupId>
     <artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
复制代码

在应用的 启动类 上加上 注解 @EnableEurekaClient,将 Config Client 注册到 Eureka Server 上面。

在配置文件 application.yml 加入 服务注册地址

eureka:
 client:
 service-url:
 defaultZone: http://locahost:8761/eureka/
复制代码

在配置文件 application.yml 加入相关配置,从 service-idconfig-server配置服务 读取相关 配置文件

spring:
 application:
 name: config-client
 cloud:
 config:
 fail-fast: true
 discovery:
 enabled: true
 service-id: config-server
 profiles:
 active: dev
server:
 port: 8762
eureka:
 client:
 service-url:
 defaultZone: http://localhost:8761/eureka/
复制代码

从新启动应用程序 config-serverconfig-client,再次访问 http://localhost:8762/foo,服务端的响应数据以下:

foo version 2

只须要启动多个 config-server 实例,便可搭建一个 高可用config-server 集群。

5. 使用Spring Cloud Bus刷新配置

Spring Cloud Bus分布式节点 经过轻量级的 消息代理 链接起来。它能够用于 广播配置文件 的更改或者 服务之间 的通信,也能够用于 监控。在 分布式配置文件 被更改后,能够经过 Spring Cloud Bus 通知各个微服务中的即时刷新 本地配置

5.1. 改造config-client

config-clientpom.xml 里面加入 起步依赖 spring-cloud-starter-bus-amqp

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
复制代码

在项目的配置文件 application.yml 文件中添加 RabbitMQ 的相关配置,包括 RabbitMQ地址端口用户名密码。为了方便验证,将 management.security.enabled 改成 false

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
management.security.enabled=false
复制代码

最后,在须要更新属性的 配置类 上加 @RefreshScope 注解。

@RefreshScope
@RestController
@SpringBootApplication
public class ConfigClientApplication {

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

    @Value("${foo}")
    private String foo;

    @RequestMapping(value = "/foo")
    public String foo(){
        return foo;
    }
}
复制代码

依次启动应用,启动两个 config-client 实例,端口 分别为 87628763。启动完成后,访问 http://localhost:8762/foo 或者 http://localhost:8763/foo,服务端响应数据以下:

foo version 2

更改远程Git 仓库 配置文件config-client-dev.properties,将 foo 的值改成 “foo version 3”

访问 http://localhost:8762/bus/refresh 请求 刷新配置,设置 “destination” 参数为 待刷新 属性的 服务名称。例如 “http://localhost:8762/bus/refresh?destination=config-client:**”,即 刷新服务名config-client 的全部 服务实例

再次访问 http://localhost:8762/foohttp://localhost:8763/foo,服务端响应数据以下:

foo version 3

测试结果代表,/bus/refresh 通知服务名为 config-client 的全部实例 刷新 了本地的 foo 属性配置。

参考

  • 方志朋《深刻理解Spring Cloud与微服务构建》

欢迎关注技术公众号: 零壹技术栈

零壹技术栈

本账号将持续分享后端技术干货,包括虚拟机基础,多线程编程,高性能框架,异步、缓存和消息中间件,分布式和微服务,架构学习和进阶等学习资料和文章。

相关文章
相关标签/搜索