Spring Cloud(八):分布式配置中心服务化和高可用
在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息。这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。springcloud提供了这样的解决方案,我们只需要将server端当做一个服务注册到eureka中,client端去eureka中去获取配置中心server端的服务既可。
这篇文章我们基于配置中心git版本的内容来改造
server端改造
1、添加依赖
- <dependencies>
- <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>
需要多引入spring-cloud-starter-eureka
包,来添加对eureka的支持。
2、配置文件
- server:
- server:
- port:
- spring:
- application:
- name: spring-cloud-config-server
- cloud:
- config:
- server:
- git:
- uri: https://github.com/ityouknow/spring-cloud-starter/ # 配置git仓库的地址
- search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。
- username: username # git仓库的账号
- password: password # git仓库的密码
- eureka:
- client:
- serviceUrl:
- defaultZone: http://localhost:8000/eureka/ ## 注册中心eurka地址
增加了eureka注册中心的配置
3、启动类
启动类添加@EnableDiscoveryClient
激活对配置中心的支持
- @EnableDiscoveryClient
- @EnableConfigServer
- @SpringBootApplication
- public class ConfigServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConfigServerApplication.class, args);
- }
- }
这样server端的改造就完成了。先启动eureka注册中心,在启动server端,在浏览器中访问:http://localhost:8000/
就会看到server端已经注册了到注册中心了。
按照上篇的测试步骤对server端进行测试服务正常。
客户端改造
1、添加依赖
- <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-eureka</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- </dependencies>
需要多引入spring-cloud-starter-eureka
包,来添加对eureka的支持。
2、配置文件
- spring.application.name=spring-cloud-config-client
- server.port=
- spring.cloud.config.name=neo-config
- spring.cloud.config.profile=dev
- spring.cloud.config.label=master
- spring.cloud.config.discovery.enabled=true
- spring.cloud.config.discovery.serviceId=spring-cloud-config-server
- 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
激活对配置中心的支持
- @EnableDiscoveryClient
- @SpringBootApplication
- public class ConfigClientApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConfigClientApplication.class, args);
- }
- }
启动client端,在浏览器中访问:http://localhost:8000/
就会看到server端和client端都已经注册了到注册中心了。
高可用
为了模拟生产集群环境,我们改动server端的端口为8003,再启动一个server端来做服务的负载,提供高可用的server端支持。
如上图就可发现会有两个server端同时提供配置中心的服务,防止某一台down掉之后影响整个系统的使用。
我们先单独测试服务端,分别访问:http://localhost:8001/neo-config/dev
、http://localhost:8003/neo-config/dev
返回信息:
- {
- "name": "neo-config",
- "profiles": [
- "dev"
- ],
- "label": null,
- "version": null,
- "state": null,
- "propertySources": [
- {
- "name": "https://github.com/ityouknow/spring-cloud-starter/config-repo/neo-config-dev.properties",
- "source": {
- "neo.hello": "hello im dev"
- }
- }
- ]
- }
说明两个server端都正常读取到了配置信息。
再次访问:http://localhost:8002/hello
,返回:hello im dev update
。说明客户端已经读取到了server端的内容,我们随机停掉一台server端的服务,再次访问http://localhost:8002/hello
,返回:hello im dev update
,说明达到了高可用的目的。
Spring Cloud(八):分布式配置中心服务化和高可用的更多相关文章
- springcloud(八):配置中心服务化和高可用
在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...
- Spring Cloud Config 分布式配置中心使用教程
一.简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,有分布式配置中心组件spring cloud config ...
- Spring Cloud 2-Config 分布式配置中心(七)
Spring Cloud Config 1.github配置 2.服务端配置 pom.xml application.xml Application.java 3.配置和命名 1. 配置加载顺序 ...
- Spring Cloud Config 分布式配置中心【Finchley 版】
一. 介绍 1,为什么需要配置中心? 当服务部署的越来越多,规模越来越大,对应的机器数量也越来越庞大,靠人工来管理和维护服务的配置信息,变得困难,容易出错. 因此,需要一个能够动态注册和获取服务信息的 ...
- Spring Cloud (5) 分布式配置中心
Spring Cloud Config 在分布式系统中,由于服务数量很多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,使用Spring Cloud ...
- spring cloud 集成分布式配置中心 apollo(单机部署apollo)
一.什么是apollo? Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用 ...
- spring cloud深入学习(九)-----配置中心服务化和高可用
在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...
- Spring Cloud (6) 分布式配置中心-高可用
高可用 现在已经可以从配置中心读取配置文件了,当微服务很多时都从配置中心读取配置文件,这时可以将配置中心做成一个微服务,将其集群化,从而达到高可用. 改造config-server 加入eureka ...
- Spring Cloud之分布式配置中心
用服务的方式来实现 ConfigAppApplication.java package com.packtpub.ConfigApp; import org.springframework.boot. ...
随机推荐
- 【java】java工具类StringUtils,org.apache.commons.lang3.StringUtils
使用过程中,发现StringUtils工具类功能非常的多. 例如,判断元素是否为数字: StringUtils.isNumeric(string)
- Object 转换为 BigDecimal
项目中遇到读取Excel文件里面的数据转为金额的情况,为了程序更加的健壮,进行处理如下: import java.math.BigDecimal; import java.math.BigIntege ...
- Computer Generated Angular Fisheye Projections [转]
Computer GeneratedAngular Fisheye Projections Written by Paul Bourke May 2001 There are two main ide ...
- Mapper 与 Reducer 解析
1 . 旧版 API 的 Mapper/Reducer 解析 Mapper/Reducer 中封装了应用程序的数据处理逻辑.为了简化接口,MapReduce 要求所有存储在底层分布式文件系统上的数据均 ...
- java中构建同时兼容linux和windows程序时遇到的文件路径分割符问题解决方案
最近在做一个自动上传文件的客户端,因为 file.getAbsolutePath() 在Mac和linux下的分割符是“/”,而在windows操作系统下的分割符则是“\”,我们程序中固然可以通过调 ...
- Download Visual Studio
Welcome to a new way to install Visual Studio! In our newest version, we've made it easier for you t ...
- nGrinder工具进行接口性能测试
1.背景 之前在这篇文章中性能测试初探—接口性能测试介绍过nGrinder,本文将介绍在nGrinder脚本中使用资源文件中数据作为接口参数和解析生成的CSV结果,生成TPS标准差,TPS波动率,最小 ...
- iOS XMPPFramework 环境配置
iOS XMPPFramework 环境配置 1:下载 iOS XMPPFramework https://github.com/robbiehanson/XMPPFramework 2:下载解压zi ...
- 算法笔记_134:字符串编辑距离(Java)
目录 1 问题描述 2 解决方案 1 问题描述 给定一个源串和目标串,能够进行如下操作: 在任意位置上插入一个字符: 替换掉任意字符: 删除任意字符. 写一个程序,实现返回最小操作次数,使得对源串 ...
- QtGui.QPen
The QtGui.QPen is an elementary graphics object. It is used to draw lines, curves and outlines of re ...