十:Spring Cloud 之配置中心HA版-config

版权声明:本文为博主原创文章,觉得稍有帮助,可点赞、转载注明出处。 https://blog.csdn.net/chenghuaying/article/details/82722036

1. 简介

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments.

  • Spring Cloud Config 支持服务端与客户端
  • 是一个分布式配置中心
  • 提供配置信息多环境切换
  • 配置信息更新,实时同步
  • 借助eureka实现高可用

2. 代码实现

2.1 涉及的模块及整体步骤

2.1.1 涉及的模块

  • eureka-server-singleton:eureka服务发布注册中心
  • config-server-ha:配置中心服务端,通过指定不同端口启动两个实例模拟服务端集群
  • config-client-use-ha:新建的通过HA版服务配置中心访问远程配置信息模块,也可以使用原有模块
  • config-repository:放置于GitHub的配置,config-client-use-ha-test.properties是对应的本次测试的保存配置信息的文件名称

2.1.2 整体步骤

  1. GitHub创建存放配置信息的config-repository目录与配置信息config-client-use-ha-test.properties,可通过demo中的config-repository模块关联GitHub上的配置。
  2. 实现eureka-server-singleton:eureka服务发布注册中心,与前面没有任何区别
  3. 实现config-server-ha:关键是启动Spring Cloud Config Server功能,指定配置仓库的配置信息
  4. 实现config-client-use-ha:从配置仓库读取配置信息
  5. config-repository中添加config-client-use-ha-test.properties:放置4步骤的配置信息

2.2 源代码

2.2.1 Github地址

https://github.com/andyChenHuaYing/spring-cloud-demo

2.2.2 配置信息地址

配置信息在dev-20180827、与master分支都有,但是代码中使用的是dev-20180827分支的代码,如果想修改值验证,记得确认客户端bootstrap.properties中配置信息与想要访问的仓库地址一致。
https://github.com/andyChenHuaYing/spring-cloud-demo/tree/dev-20180827/config-repository

2.3 eureka-server-singleton

Spring Cloud 之服务发现与调用-Ribbon#2.3 eureka-server-singleton 没有任何区别

2.4 config-server-ha

2.4.1 整体实现

  1. pom.xml引入spring-cloud-config-server
  2. application.yml指定配置仓库连接信息
  3. 启动类使用注解启动类使用注解@EnableConfigServer开启配置服务功能

2.4.2 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>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>

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

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

2.4.3 application-{profile}.yml

两个配置文件除端口之外,其他配置项完一致,配置文件:application-8772.ymlapplication-8773.yml

spring:
  application:
    name: config-server-ha
  cloud:
    config:
      server:
        git:
          uri: https://github.com/andyChenHuaYing/spring-cloud-demo
          searchPaths: config-repository
          username:
          password:
          forcePull: true
      label: dev-20180827

server:
  port: 8772

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

2.4.4 ConfigServerHaApplication

package org.oscar.scd.config.server.ha;

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

@EnableConfigServer
@SpringBootApplication
public class ConfigServerHaApplication {

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

2.5 config-client-use-ha

2.5.1 整体实现

  1. pom.xml文件中引入依赖spring-cloud-starter-configspring-cloud-starter-netflix-eureka-client
  2. bootstrap.yml中指定Config Server连接信息以及需要访问配置中心的具体配置信息
  3. ConfigClientUseHaApplication常规Spring Boot启动类

2.5.2 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>spring-cloud-finchley-demo</artifactId>
        <groupId>org.oscar.scd</groupId>
        <version>1.0.0</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client-use-ha</artifactId>

    <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.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>
</project>

2.5.3 bootstrap.yml

下方信息与配置中心的配置文件的对应关系见后续验证部分
spring:
  application:
    name: config-client-use-ha
  cloud:
    config:
      label: dev-20180827
      profile: test
      discovery:
        enabled: true
        serviceId: config-server-ha

server:
  port: 8774
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

2.6 config-repository

保存配置文件

2.6.1 config-client-use-ha配置文件

config-client-use-ha-test.properties

foo=config-client-use-ha foo value.

3. 验证

3.1 创建SpringBoot启动类

简单创建Spring Boot启动类即可

3.1.1 EurekaServerSingletonApplication

最简单的方式添加一个SpringBoot启动类型的启动类就行。

这里写图片描述

3.1.2 ConfigServerHaApplication-8772

在这里插入图片描述

3.1.3 ConfigServerHaApplication-8773

参考上一节,修改Active profiles:8773

3.1.4 ConfigClientUseHaApplication

最简单的方式添加一个SpringBoot启动类型的启动类就行。

3.2 启动

  1. EurekaServerSingletonApplication
  2. ConfigServerHaApplication-8772
  3. ConfigServerHaApplication-8773
  4. ConfigClientUseHaApplication

3.3查看eureka服务信息界面

在这里插入图片描述

3.3 读取远程配置信息

3.3.1 ConfigClientUseHaApplication

  • 启动之后可以根据指定规则访问远程配置完整信息,/{name}/{profile}/{label}
    • name:spring.application.name
    • profile:spring boot 的profile,即 application-{profile}.yml中的profile值,如application-dev.yml文件的profile是dev。
    • label:application.yml配置文件中spring.cloud.config.label的值,这里是dev-20180827
  • 查看config-client-use-ha的bootstrap.yml信息可知,其对应的远程配置文件为https://github.com/andyChenHuaYing/spring-cloud-demo/config-repository/config-client-use-ha-test.properties.properties

访问地址:http://localhost:8773/config-client-use-ha/test/dev-20180827 可查看config-client-use-ha完整配置信息
在这里插入图片描述

3.3.2 ConfigClientApplication

3.3.3 ConfigAnotherClientApplication

###3.3.4 Config Server HA验证

  1. 重复启动ConfigClientUseHaApplication:观察启动日志中读取远程配置信息的server地址在http://localhost:8772/http://localhost:8773/之间切换
  2. 停止ConfigServerHaApplication-8772,再重复启动ConfigClientUseHaApplication:观察启动日志中读取远程配置信息的server地址只会是http://localhost:8772/
  3. 停止ConfigServerHaApplication-8773,再重复启动ConfigClientUseHaApplication:启动失败

4. 思考

  • HA也涉及到负载均衡,有哪些负载均衡算法可用,如何指定
  • Spring Cloud Config有哪些常用的配置项,适用场景

5. 补充

5.1 资料

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_client.html

http://cloud.spring.io/spring-cloud-static/Finchley.SR1/multi/multi__spring_cloud_config_server.html

猜你喜欢

转载自blog.csdn.net/chenghuaying/article/details/82722036