6--SpringCloud搭建分布式配置中心(续-高可用性)

  本文接之前的《5--SpringCloud搭建分布式配置中心》,继续来说说Spring Cloud Config的使用。

  先来回顾一下,在前文中我们完成了什么:

  • 构建了config-server,连接到Git仓库
  • 在Git上创建了一个5--SpringCloud--Config目录,用来存储配置信息
  • 构建了config-client,来获取Git中的配置信息

  在本文中,我们继续来看看Spring Cloud Config的一些其他能力。

 

高可用问题

  Config Server与服务注册中心一样,我们也需要将其扩展为高可用的集群。

  所以,简单的方法就是把config-server也注册为服务,这样所有客户端就能以服务的方式进行访问。通过这种方法,只需要启动多个指向同一Git仓库位置的config-server就能实现高可用了。

  首先搭建一个服务注册中心:如果不知道怎么搭建服务注册中心请阅读:《1--SpringCloud的服务注册与发现Eureka

config-server配置

  pom.xml的dependencies节点中引入如下依赖,相比之前的config-server就,加入了spring-cloud-starter-eureka,用来注册服务

<dependencies>

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

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

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Brixton.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

 

   

  配置application.properties新增# 配置服务注册中心 eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

spring.application.name=config-server
server.port=7001
# 配置服务注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
# git配置
#所在项目根目录
spring.cloud.config.server.git.uri=https://gitee.com/cengjiang/springcloud_learning/
#所在地址目录
spring.cloud.config.server.git.searchPaths=5--SpringCloud--Config
#如果是公开项目则不用写用户名密码
spring.cloud.config.server.git.username=username
spring.cloud.config.server.git.password=password

 

 

  在应用主类中,新增@EnableDiscoveryClient注解,用来将config-server注册到上面配置的服务注册中心上去。

@EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

 

  

  启动该应用,并访问http://localhost:1111/,可以在Eureka Server的信息面板中看到config-server已经被注册了。

  

 

config-client配置

   同config-server一样,在pom.xml的dependencies节点中新增spring-cloud-starter-eureka依赖,用来注册服务:

 <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-eureka</artifactId>
    </dependency>

 

  

  bootstrap.properties中,按如下配置:

server.port=7002
#对应前配置文件中的{application}部分
spring.application.name=ghghspace
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
#参数设置为true,开启通过服务来访问Config Server的功能
spring.cloud.config.discovery.enabled=true
#对应前配置文件中的{profile}部分
spring.cloud.config.profile=dev
#对应前配置文件的git分支
spring.cloud.config.label=master
#配置中心的访问地址
spring.cloud.config.uri=http://localhost:7001/

 

  通过eureka.client.serviceUrl.defaultZone参数指定服务注册中心,用于服务的注册与发现,再将spring.cloud.config.discovery.enabled参数设置为true,开启通过服务来访问Config Server的功能

 

  在应用主类中,增加@EnableDiscoveryClient注解,用来发现config-server服务,利用其来加载应用配置

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@EnableDiscoveryClient
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        new SpringApplicationBuilder(Application.class).web(true).run(args);
    }

}

 

 

  沿用之前我们创建的Controller来加载Git中的配置信息

@RefreshScope
@RestController
public class TestController {

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

    @RequestMapping("/from")
    public String from() {

        return this.from;
    }

}

 

 

  完成了上述配置之后,我们启动该客户端应用。若启动成功,访问http://localhost:1111/,可以在Eureka Server的信息面板中看到该应用已经被注册成功了。

  

 

   访问客户端应用提供的服务:http://localhost:7002/from 会返回信息

   

配置刷新

   有时候,我们需要对配置内容做一些实时更新的场景,那么Spring Cloud Config是否可以实现呢?答案显然是可以的。下面,我们看看如何进行改造来实现配置内容的实时更新。

   在改造程序之前,我们先将config-server和config-client都启动起来,并访问客户端提供的REST APIhttp://localhost:7002/from来获取配置信息,可以获得返回内容为:git-dev-1.0

   接着,我们可以尝试使用Git工具修改当前配置的内容,比如,将5--SpringCloud--Config/didispace-dev.properties中的from的值从from=git-dev-1.0修改为from=git-dev-2.0,再访问http://localhost:7002/from,可以看到其返回内容还是git-dev-1.0

   下面,我们将在config-client端增加一些内容和操作以实现配置的刷新:

  在config-clinet的pom.xml中新增spring-boot-starter-actuator监控模块,其中包含了/refresh刷新API。

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

 

  重新启动config-clinet,访问一次http://localhost:7002/from,可以看到当前的配置值

  • 修改Git仓库5--SpringCloud--Config/didispace-dev.properties文件中from的值
  • 再次访问一次http://localhost:7002/from,可以看到配置值没有改变
  • 通过POST请求发送到http://localhost:7002/refresh,我们可以看到返回内容如下,代表from参数的配置内容被更新了(请求需要为POST请求,get请求为405)
[
  "from"
]

 

  再次访问一次http://localhost:7002/from,可以看到配置值已经是更新后的值了通过上面的介绍,大家不难想到,当有Git提交变化时,就给对应的配置主机发送/refresh请求来实现配置信息的实时更新。

  源码下载: 6--SpringCloud搭建分布式配置中心(续-高可用性)

posted @ 2018-11-08 12:59  曾将  阅读(577)  评论(0编辑  收藏  举报
//目录 欢迎评论,一起学习,对大家有用请点个赞。