在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息。这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。springcloud提供了这样的解决方案,我们只需要将server端当做一个服务注册到eureka中,client端去eureka中去获取配置中心server端的服务既可。

这篇文章我们基于配置中心git版本的内容来改造

server端改造

1、添加依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-config-server</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.cloud</groupId>
  8. <artifactId>spring-cloud-starter-eureka</artifactId>
  9. </dependency>
  10. </dependencies>

需要多引入spring-cloud-starter-eureka包,来添加对eureka的支持。

2、配置文件

  1. server:
  2. server:
  3. port:
  4. spring:
  5. application:
  6. name: spring-cloud-config-server
  7. cloud:
  8. config:
  9. server:
  10. git:
  11. uri: https://github.com/ityouknow/spring-cloud-starter/ # 配置git仓库的地址
  12. search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。
  13. username: username # git仓库的账号
  14. password: password # git仓库的密码
  15. eureka:
  16. client:
  17. serviceUrl:
  18. defaultZone: http://localhost:8000/eureka/ ## 注册中心eurka地址

增加了eureka注册中心的配置

3、启动类

启动类添加@EnableDiscoveryClient激活对配置中心的支持

  1. @EnableDiscoveryClient
  2. @EnableConfigServer
  3. @SpringBootApplication
  4. public class ConfigServerApplication {
  5.  
  6. public static void main(String[] args) {
  7. SpringApplication.run(ConfigServerApplication.class, args);
  8. }
  9. }

这样server端的改造就完成了。先启动eureka注册中心,在启动server端,在浏览器中访问:http://localhost:8000/ 就会看到server端已经注册了到注册中心了。

按照上篇的测试步骤对server端进行测试服务正常。

客户端改造

1、添加依赖

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.cloud</groupId>
  4. <artifactId>spring-cloud-starter-config</artifactId>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-web</artifactId>
  9. </dependency>
  10. <dependency>
  11. <groupId>org.springframework.cloud</groupId>
  12. <artifactId>spring-cloud-starter-eureka</artifactId>
  13. </dependency>
  14. <dependency>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-test</artifactId>
  17. <scope>test</scope>
  18. </dependency>
  19. </dependencies>

需要多引入spring-cloud-starter-eureka包,来添加对eureka的支持。

2、配置文件

  1. spring.application.name=spring-cloud-config-client
  2. server.port=
  3.  
  4. spring.cloud.config.name=neo-config
  5. spring.cloud.config.profile=dev
  6. spring.cloud.config.label=master
  7. spring.cloud.config.discovery.enabled=true
  8. spring.cloud.config.discovery.serviceId=spring-cloud-config-server
  9.  
  10. eureka.client.serviceUrl.defaultZone=http://localhost:8000/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的配置中

3、启动类

启动类添加@EnableDiscoveryClient激活对配置中心的支持

  1. @EnableDiscoveryClient
  2. @SpringBootApplication
  3. public class ConfigClientApplication {
  4.  
  5. public static void main(String[] args) {
  6. SpringApplication.run(ConfigClientApplication.class, args);
  7. }
  8. }

启动client端,在浏览器中访问:http://localhost:8000/ 就会看到server端和client端都已经注册了到注册中心了。

高可用

为了模拟生产集群环境,我们改动server端的端口为8003,再启动一个server端来做服务的负载,提供高可用的server端支持。

如上图就可发现会有两个server端同时提供配置中心的服务,防止某一台down掉之后影响整个系统的使用。

我们先单独测试服务端,分别访问:http://localhost:8001/neo-config/devhttp://localhost:8003/neo-config/dev返回信息:

  1. {
  2. "name": "neo-config",
  3. "profiles": [
  4. "dev"
  5. ],
  6. "label": null,
  7. "version": null,
  8. "state": null,
  9. "propertySources": [
  10. {
  11. "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties",
  12. "source": {
  13. "neo.hello": "hello im dev"
  14. }
  15. }
  16. ]
  17. }

说明两个server端都正常读取到了配置信息。

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

Spring Cloud(八):分布式配置中心服务化和高可用的更多相关文章

  1. springcloud(八):配置中心服务化和高可用

    在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...

  2. Spring Cloud Config 分布式配置中心使用教程

    一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...

  3. Spring Cloud 2-Config 分布式配置中心(七)

    Spring Cloud  Config  1.github配置 2.服务端配置 pom.xml application.xml Application.java 3.配置和命名 1. 配置加载顺序 ...

  4. Spring Cloud Config 分布式配置中心【Finchley 版】

    一. 介绍 1,为什么需要配置中心? 当服务部署的越来越多,规模越来越大,对应的机器数量也越来越庞大,靠人工来管理和维护服务的配置信息,变得困难,容易出错. 因此,需要一个能够动态注册和获取服务信息的 ...

  5. Spring Cloud (5) 分布式配置中心

    Spring Cloud Config 在分布式系统中,由于服务数量很多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,使用Spring Cloud ...

  6. spring cloud 集成分布式配置中心 apollo(单机部署apollo)

    一.什么是apollo? Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用 ...

  7. spring cloud深入学习(九)-----配置中心服务化和高可用

    在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...

  8. Spring Cloud (6) 分布式配置中心-高可用

    高可用 现在已经可以从配置中心读取配置文件了,当微服务很多时都从配置中心读取配置文件,这时可以将配置中心做成一个微服务,将其集群化,从而达到高可用. 改造config-server 加入eureka ...

  9. Spring Cloud之分布式配置中心

    用服务的方式来实现 ConfigAppApplication.java package com.packtpub.ConfigApp; import org.springframework.boot. ...

随机推荐

  1. 【java】java工具类StringUtils,org.apache.commons.lang3.StringUtils

    使用过程中,发现StringUtils工具类功能非常的多. 例如,判断元素是否为数字: StringUtils.isNumeric(string)

  2. Object 转换为 BigDecimal

    项目中遇到读取Excel文件里面的数据转为金额的情况,为了程序更加的健壮,进行处理如下: import java.math.BigDecimal; import java.math.BigIntege ...

  3. Computer Generated Angular Fisheye Projections [转]

    Computer GeneratedAngular Fisheye Projections Written by Paul Bourke May 2001 There are two main ide ...

  4. Mapper 与 Reducer 解析

    1 . 旧版 API 的 Mapper/Reducer 解析 Mapper/Reducer 中封装了应用程序的数据处理逻辑.为了简化接口,MapReduce 要求所有存储在底层分布式文件系统上的数据均 ...

  5. java中构建同时兼容linux和windows程序时遇到的文件路径分割符问题解决方案

    最近在做一个自动上传文件的客户端,因为 file.getAbsolutePath()  在Mac和linux下的分割符是“/”,而在windows操作系统下的分割符则是“\”,我们程序中固然可以通过调 ...

  6. Download Visual Studio

    Welcome to a new way to install Visual Studio! In our newest version, we've made it easier for you t ...

  7. nGrinder工具进行接口性能测试

    1.背景 之前在这篇文章中性能测试初探—接口性能测试介绍过nGrinder,本文将介绍在nGrinder脚本中使用资源文件中数据作为接口参数和解析生成的CSV结果,生成TPS标准差,TPS波动率,最小 ...

  8. iOS XMPPFramework 环境配置

    iOS XMPPFramework 环境配置 1:下载 iOS XMPPFramework https://github.com/robbiehanson/XMPPFramework 2:下载解压zi ...

  9. 算法笔记_134:字符串编辑距离(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 给定一个源串和目标串,能够进行如下操作: 在任意位置上插入一个字符: 替换掉任意字符: 删除任意字符. 写一个程序,实现返回最小操作次数,使得对源串 ...

  10. QtGui.QPen

    The QtGui.QPen is an elementary graphics object. It is used to draw lines, curves and outlines of re ...