分布式配置中心搭建(SVN版本管理)

背景

现如今,应用开发首要考虑的都是要可伸缩、扩展性,微服务逐渐风靡,。当我们的后台服务一点点增多,各个服务的配置也越来越多,随之而来的就是一个配置管理问题,各自管各自的开发时没什么问题,到了线上之后管理就会很头疼,到了要大规模更新就更烦了。我们的后台服务就是如此,各种语言开发的都有,为了方便服务配置文件统一管理,国内外知名公司,会有一些分布式配置管理项目,例:国内有 携程 Apollo 百度 Disconf 阿里 Nacos 国外有Spring Cloud Config、Archaius、Apache Zookeeper等 ,Spring Cloud Config分布式配置中心是一个比较好的解决方案。

特性

  • 提供服务端和客户端支持
  • 集中管理各环境的配置文件
  • 可以进行版本管理(默认实现基于 git 仓库 可替换自定义实现。 例:svn)
  • 支持数据结构丰富,yml、json、 properties 等
  • 配置文件修改之后,可以快速的生效(配合 SpringCloud Bus 可实现配置推送更新)
  • 基于 Spring 环境,无缝 与 Spring 应用集成
  • 配合 eureke 可实现服务发现
  • 支持大的并发查询
  • 支持各种语言
  • 简单可靠,有丰富的配套方案

示例介绍

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了\color{red}{客户端}\color{red}{服务端}两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git,我们先以svn为例做一套示例。

环境

  • JDK 1.8
  • SpringCloud 版本 Edgware.SR5
  • SpringBoot 版本 1.5.20.RELEASE
  • SVN版本 1.9.2 Windows版
  • RabbitMQ 1.5.6

构建服务端

  1. 利用Spring Initializr 快速创建工程
    image.png
  2. pom配置
<?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.20.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springCloudConfigServiceDemo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR5</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.tmatesoft.svnkit</groupId>
            <artifactId>svnkit</artifactId>
            <version>1.9.2</version>
        </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>

  1. application.properties
#服务端口
server.port=8888
#服务名称
spring.application.name=config-server
#指定配置服务中心使用版本管理工具(svn、git)
spring.profiles.active=subversion
#版本管理工具lable
spring.cloud.config.server.default-label=trunk
#版本管理工具地址
spring.cloud.config.server.svn.uri=https://xxx.xxx.x.xxx/svn/chitutest/config
#版本管理工具账号
spring.cloud.config.server.svn.username=username
#版本管理工具密码
spring.cloud.config.server.svn.password=password
#关闭权限
management.security.enabled=false

  1. 启动类添加@EnableConfigServer
@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigServiceDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigServiceDemoApplication.class, args);
    }

}
  1. svn中创建应用配置文件
    在svn库chitutest下创建config文件夹,在config下创建trunk文件夹,在trunk中创建如下三个文件:
  • spring-cloud-dev.properties 开发环境配置
  • spring-cloud-test.properties 测试环境配置
  • spring-cloud-pro.properties 生产环境配置

spring-cloud-dev.properties配置如下:

test:kaifa

spring-cloud-test.properties配置如下:

test:ceshi

spring-cloud-pro.properties配置如下:

test:shengchan
  1. 启动测试
    http请求地址和资源文件映射如下:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

分别输入下列地址看看测试结果

http://localhost:8888/spring-cloud/dev
http://localhost:8888/spring-cloud-dev.properties
http://localhost:8888/trunk/spring-cloud-dev.properties

若能正确的返回配置相关信息,说明配置中心已经正常启动

构建客户端

  1. 利用Spring Initializr 快速创建工程
    11.png
  2. pom配置
<?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.20.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springCloudConfigClientDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springCloudConfigClientDemo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Edgware.SR5</spring-cloud.version>
    </properties>

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

  1. bootstrap.properties
#服务端口
server.port=8080
#服务名称
spring.application.name=spring-cloud
#配置中心服务地址
spring.cloud.config.uri=http://localhost:8888/
#配置中心库配置文件件服务名称(默认服务名称)
spring.cloud.config.name=spring-cloud
#配置中心库配置文件lable
spring.cloud.config.label=trunk
  1. 测试类(在启动类基础上修改)
@SpringBootApplication
@RestController
public class SpringCloudConfigClientDemoApplication {
    @Value("${test}")
    private String name;
    @RequestMapping("/hello")
    public String home(){
        return "Hello "+name;
    }
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigClientDemoApplication.class, args);
    }
}
  1. idea添加--spring.profiles.active=dev

    222.png

  2. 测试结果

浏览器输入地址 http://localhost:8080/hello
正确结果:Hello kaifa
修改idea启动参数为 --spring.profiles.active=test 重启服务,查看结果试试看.

至此我们的配置中心,\color{red}{客户端}\color{red}{服务端}已经搭建完毕。

\color{red}{注意事项:先启动服务端,再启动客户端}

配置推送更新

细心的朋友可能已经发现了,前面搭建的配置中心存在一个致命的问题,当我们在svn修改对应配置文件的时候,\color{red}{客户端}配置不能更新!那么这个问题如何解决呢?我们如何才能实现,svn库中配置修改,相关客户端会自动刷新配置呢?SpringCloud Bus可以帮助我们实现,接下来我们就看看SpringCloud Bus是如何帮助我们实现的吧。

  1. SpringCloud Bus工作原理

    配置服务器结合Spring Cloud Bus总线自动刷新原理.png

  2. 服务端pom文件添加

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
  1. 服务端application.properties文件添加
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

4.客户端pom文件添加

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
  1. 客户端bootstrap.properties文件添加
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
  1. 客户端启动类添加@RefreshScope
@SpringBootApplication
@RestController
@RefreshScope
public class SpringCloudConfigClientDemoApplication {
    @Value("${test}")
    private String name;
    @RequestMapping("/hello")
    public String home(){
        return "Hello "+name;
    }
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigClientDemoApplication.class, args);
    }

}
  1. 测试结果

启动服务端、启动客户端
浏览器输入地址 http://localhost:8080/hello
正确结果:Hello kaifa
修改spring-cloud-dev.properties配置如下:

test:kaifa111111

浏览器输入地址 http://localhost:8080/hello
正确结果:Hello kaifa
使用postman 发送post请求 地址为 http://localhost:8888/bus/refresh
浏览器输入地址 http://localhost:8080/hello
正确结果:Hello kaifa111111

  1. svn钩子hooks

在上面的示例中我们,通过手动形式刷新了配置,那么结合svn我们如何能做到自动刷新呢?
其实呢,在svn中的hooks钩子函数能帮我们解决这个问题. svn储藏库repositories中hooks下
新建post-commit.bat文件,文件内容为:

E:\curl-7.64.1-win64-mingw\bin\curl.exe -X POST http://192.168.1.13:8888/bus/refresh

\color{red}{注意事项1:} E:\curl-7.64.1-win64-mingw\bin\curl.exe \color{red}{为curl的命令路径}
\color{red}{注意事项2:} http://192.168.1.13:8888\color{red}{为配置服务器地址}

修改spring-cloud-dev.properties配置如下:

test:kaifa22222

svn commit
浏览器输入地址 http://localhost:8080/hello
正确结果:Hello kaifa22222

源码地址
https://github.com/LiXiangGitHub/springCloudConfigClientDemo.git
https://github.com/LiXiangGitHub/springCloudConfigServiceDemo.git

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容