springcloud(八):配置中心服务化和高可用
在前两篇的介绍中,客户端都是直接调用配置中心的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: 8001
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=8002
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,说明达到了高可用的目的。
作者:纯洁的微笑
出处:http://www.ityouknow.com/
版权归作者所有,转载请注明出处
springcloud(八):配置中心服务化和高可用的更多相关文章
- 二十、springcloud(六)配置中心服务化和高可用
1.问题描述 前一篇,spring-cloud-houge-provider(称之为客户端)直接从spring-cloud-houge-config(称之为服务端)读取配置,客户端和服务端的耦合性太高 ...
- Spring Cloud(八):分布式配置中心服务化和高可用
在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...
- spring cloud深入学习(九)-----配置中心服务化和高可用
在前两篇的介绍中,客户端都是直接调用配置中心的server端来获取配置文件信息.这样就存在了一个问题,客户端和服务端的耦合性太高,如果server端要做集群,客户端只能通过原始的方式来路由,serve ...
- Spring Cloud(八):配置中心(服务化与高可用)【Finchley 版】
Spring Cloud(八):配置中心(服务化与高可用)[Finchley 版] 发表于 2018-04-19 | 更新于 2018-04-26 | 本文接之前的<Spring Clou ...
- 七、springcloud之配置中心Config(二)之高可用集群
方案一:传统作法(不推荐) 服务端负载均衡 将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护,而客户端在指定Config Server位置时, ...
- keepalived安装配置实战心得(实现高可用保证网络服务不间断)
keepalived安装配置实战心得(实现高可用保证网络服务不间断) 一.准备2台虚拟机 安装的系统是:centos-release-7-1.1503.el7.centos.2.8.x86_6 ...
- Redis安装、主从配置及两种高可用集群搭建
Redis安装.主从配置及两种高可用集群搭建 一. 准备 Kali Linux虚拟机 三台:192.168.154.129.192.168.154.130.192.168.154 ...
- RabbitMQ高级指南:从配置、使用到高可用集群搭建
本文大纲: 1. RabbitMQ简介 2. RabbitMQ安装与配置 3. C# 如何使用RabbitMQ 4. 几种Exchange模式 5. RPC 远程过程调用 6. RabbitMQ高可用 ...
- Spring Cloud实战之初级入门(五)— 配置中心服务化与配置实时刷新
目录 1.环境介绍 2.配置中心服务化 2.1 改造mirco-service-spring-config 2.2 改造mirco-service-provider.mirco-service-con ...
随机推荐
- c#遍历文件夹获得所有文件
在c#中,想要获得一个文件夹下的所有子目录以及文件十分简单. 首先,获取目录的情况下,DirectoryInfo.GetDirectories():获取目录(不包含子目录)的子目录,返回类型为Dire ...
- Vxlan与网卡offload性能
背景 由于数据链路层MTU的限制,发送端TCP/UDP数据在交付到IP层时需要与MTU相匹配,TCP数据不能超过mss,较长的UDP需要分片(Fragmentation)以满足MTU要求:接收端协议栈 ...
- javaScript 基础学习笔记
边看视频和书记得有点杂. 1.插入JS标签 一种是在文档中插入<script></script>标签.另一种是把javaScript代码放在.js文件中.放在head中如. & ...
- ios 视频拼接/合成
上面的图说明的是这个混合的过程,下面放代码: - (void)mergeAndExportVideos:(NSArray*)videosPathArray withOutPath:(NSString* ...
- TCP/IP笔记(六)TCP与UDP
终于来到了传输层,这个面试问的最多了,内容比较多,要分两篇来总结,这是第一篇
- 【Windows 10 应用开发】输入模拟
---恢复内容开始--- Input Injection 直译为:输入注入.通俗的译法为:模拟输入.此注入行为可以模拟以下几种输入行为: 1.键盘按键. 2.鼠标. 3.触控. 4.书写笔输入. 5. ...
- 闭包(匿名函数) php
php中的闭包,之前不理解.以前项目中虽然有用到,也是别人怎么用,自己也跟着怎么用,也没具体去看一下,时间长了就忘了,也不知道闭包是怎么回事.今天网上搜集了关于php闭包相关的文章,看了7,8篇,干货 ...
- 为大数据软件准备JAVA、Python环境
环境:SUSE 11 64位 安装JAVA JDK 1.确定版本.一般都是安装最新的JDK(Java SE Development Kit).个别软件和系统需要特定版本的JDK,根据实际需要下载. 2 ...
- 配置RMAN备份环境
关于配置RMAN备份环境你可以给每个目标数据库设置一些固定的配置,这些配置控制着RMAN多个方面的行为.例如,你可配置备份的保存策略.默认的备份目录.默认的备份设备类型等.你可以用show命令来查看配 ...
- JDK安装、java环境配置
JDK是Java语言的软件开发工具包,主要用于移动设备.嵌入式设备上的java应用程序.JDK是整个java开发的核心,它包含了JAVA的运行环境,JAVA工具和JAVA基础的类库. JRE(Java ...