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. ...
随机推荐
- animate()--jq动画
2参数,样式,时间 $(this).animate({top:"100px","opacity": "0"},1000) 3参数,样式,时间 ...
- pytest文档8-html报告报错截图+失败重跑
前言 做web自动化的小伙伴应该都希望在html报告中展示失败后的截图,提升报告的档次,pytest-html也可以生成带截图的报告. conftest.py 1.失败截图可以写到conftest.p ...
- CentOS6.5配置网络
[ CleverCode发表在csdn博客中的原创作品,请勿转载.原创地址:http://blog.csdn.net/clevercode/article/details/46376985] 1 网卡 ...
- Servlet实现的三种方法
(1)方法一: //这是第一个实现servlet的方法.使用时限servlet接口的方法来实现,使用的时候须要引用servlet-api.jar package com.lc; import java ...
- mac下的一些命令
1.显示隐藏文件 defaults write com.apple.finder AppleShowAllFiles -bool true 2.隐藏隐藏文件 defaults write com.ap ...
- go语言基础之流程控制 if语句
Go语言支持最基本的三种程序运行结构:顺序结构.选择结构.循环结构. 顺序结构:程序按顺序执行,不发生跳转. 选择结构:依据是否满足条件,有选择的执行相应功能. 循环结构:依据条件是否满足,循环多次执 ...
- django from表单基础知识点
今日概要: - 内容拾遗 - 分页 - Form验证 1.内容拾遗 - 新URL -提交时,保留之前的内容? - 对话框 - var data = $('#fmForm表单的ID').serializ ...
- mongodb聚合管道用法
基本用法 db.collection.aggregate( [ { <stage> }, ... ] ) stage如下 名称 描述 $addFields 将新的字段添加到文档中,输出的文 ...
- qt study 泛型和容器
所谓泛型(generic) 能够像操作基本类型一样轻松操作对象的类和函数. qt容器类就是泛型类,基于模板的泛型类. 重载运算符overloaed operator, 托管容器 managed con ...
- [Angular-Scaled web] 5. ui-router $stateParams for sharing information
When using ui-route, we want to pass the information with the url. Example: angular.module('categori ...