第八篇:SpringCloud之高可用的分布式配置中心(Spring Cloud Config)

前一篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息。这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合Spring Cloud服务治理的理念。Spring Cloud提供了这样的解决方案,我们可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下:
在这里插入图片描述

准备工作

继续使用上一篇文章的工程,创建一个eureka-server工程,用作服务注册中心。在其pom.xml文件引入Eureka的起步依赖spring-cloud-starter-eureka-server

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

在配置文件application.yml上,指定服务端口为8889,加上作为服务注册中心的基本配置,代码如下:

server:
  port: 8088

eureka:
  instance:
    hostname: localhost
  client:
    #表明自己是一个eureka server
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

入口类:

@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaserverApplication.class, args);
    }
}

改造Config-Server

在其pom.xml文件加上EurekaClient的起步依赖spring-cloud-starter-eureka

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

配置文件application.yml,指定服务注册地址为http://localhost:8088/eureka/,其他配置同上一篇文章,完整的配置如下:

server:
  port: 8090

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

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/chenjary/SpringCloud/
          searchPaths: config-repo
          username:
          password:
      label: master

最后需要在程序的启动类Application加上@EnableEurekaClient的注解。

改造Config-Client

将其注册微到服务注册中心,作为Eureka客户端,需要pom文件加上起步依赖spring-cloud-starter-eureka,代码如下:

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

配置文件

spring.cloud.config.name=config
spring.cloud.config.profile=test
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.label=master

spring.application.name=config-client
server.port=8089

eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/

主要是去掉了spring.cloud.config.uri直接指向server端地址的配置,增加了最后的三个配置:

  • spring.cloud.config.discovery.enabled :开启Config服务发现支持
  • spring.cloud.config.discovery.serviceId :指定server端的name,也就是server端spring.application.name的值
  • eureka.client.serviceUrl.defaultZone :指向配置中心的地址

这三个配置文件都需要放到bootstrap.properties的配置中

最后在启动类添加@EnableEurekaClient激活对配置中心的支持

依次启动sso-servr,config-server,config-client,在浏览器中访问:http://localhost:8088/ 就会看到server端和client端都已经注册了到注册中心了。
在这里插入图片描述
访问http://localhost:8089/hello,浏览器显示:
在这里插入图片描述

高可用

为了模拟生产集群环境,我们改动server端的端口为8091,再启动一个server端来做服务的负载,提供高可用的server端支持。
在这里插入图片描述
如上图就可发现会有两个server端同时提供配置中心的服务,防止某一台down掉之后影响整个系统的使用。
我们先单独测试服务端,分别访问:http://localhost:8090/config/tset、http://localhost:8091/neo-config/test返回信息:
在这里插入图片描述
在这里插入图片描述
说明两个server端都正常读取到了配置信息。

再次访问:http://localhost:8089/hello,返回:hello im test。说明客户端已经读取到了server端的内容,我们随机停掉一台server端的服务,再次访问http://localhost:8089/hello,返回:hello im test,说明达到了高可用的目的。

源码下载:https://github.com/chenjary/SpringCloud/tree/master/springcloud-config-service

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值