史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下:
一、改造config-server
在现有项目基础上,改造。
在其pom.xml文件加上Eureka的起步依赖spring-cloud-starter-eureka,代码如下:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-config-server</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
最后需要在启动类Application加上@EnableConfigServer,@EnableEurekaClient的注解。
- @SpringBootApplication
- @EnableConfigServer
- @EnableEurekaClient
- public class ConfigServerApplication {
- public static void main(String[] args) {
- SpringApplication.run(ConfigServerApplication.class, args);
- }
- }
二、改造config-client
将其注册微到服务注册中心,作为Eureka客户端,需要pom文件加上起步依赖spring-cloud-starter-netflix-eureka-server,代码如下:
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-config</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.cloud</groupId>
- <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
- </dependency>
- </dependencies>
配置文件bootstrap.properties,注意是bootstrap。注解一行,不使用ip注册到服务中,使用服务名"config-server"。
- server.port=8881
- spring.application.name=config-client
- #开启配置服务发现,从配置中心读取文件
- spring.cloud.config.discovery.enabled=true
- #配置服务实例名称
- spring.cloud.config.discovery.serviceId=config-server
- spring.cloud.config.label=master
- #目前可选值有[dev,prod]
- spring.cloud.config.profile=dev
- #注释掉下面这行,使用"config-server"服务名来注册
- #spring.cloud.config.uri= http://localhost:8888/
- spring.cloud.config.discovery.enabled 是从配置中心读取文件。
- spring.cloud.config.discovery.serviceId 配置中心的servieId,即服务名。
这时发现,在读取配置文件不再写ip地址,而是服务名,这时如果配置服务部署多份,通过负载均衡,从而高可用。
依次启动eureka-servr,config-server,config-client
访问网址:http://localhost:8888
访问http://localhost:8881/test,浏览器显示:
我的名字是:devEnv,年龄是11
本文源码下载:
https://github.com/forezp/SpringCloudLearning/tree/master/chapter7
史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)的更多相关文章
- SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如 ...
- SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 ,博主地址:http://blog.csdn.net/forezp. http://blog.csdn.net/forezp/art ...
- 【SpringCloud】第七篇: 高可用的分布式配置中心(Spring Cloud Config)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- SpringCloud学习(七)高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用 准备工作 ...
- 史上最简单的SpringCloud教程 | 第十篇: 高可用的服务注册中心(Finchley版本)
转载请标明出处: 原文首发于 https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f10-eureka/ 本文出自方志朋的博客 文章 史上最简单 ...
- springCloud学习-高可用的分布式配置中心(Spring Cloud Config)
1.简介 高可用的分布式配置中心,即将配置中心做成一个微服务,将其集群化,从而达到高可用.config-server和config-client向eureka-server注册,且将config-se ...
- 史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)
转载请标明出处: https://www.fangzhipeng.com/springcloud/2017/07/12/sc03-feign/ 本文出自方志朋的博客 最新Finchley版本请访问: ...
- 史上最简单的 SpringCloud 教程
史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现(Eureka)史上最简单的SpringCloud教程 | 第二篇: 服务消费者(rest+ribbon)史上最简单的Spri ...
- 史上最简单的 SpringCloud 教程 | 终章
https://blog.csdn.net/forezp/article/details/70148833转载请标明出处:http://blog.csdn.net/forezp/article/det ...
随机推荐
- 获取异步API数据
异步操作应该是以前学习 ajax 时才被明确提及,就目前的理解,同步就是同一时间只能做一件事,如果使用 ajax同步模式,则代码会卡在 xhr.send() 这里,只有请求响应的过程全部完成了才会执行 ...
- 尚硅谷Oracle教程-学习笔记2
控制用户权限 1.创建用户 1) SQL> create user atguigu01 2 identified by atguigu01; User created 2) SQL> gr ...
- ARX工程必须使用release模式编译
在添加如下代码保证debug版本的arx文件也是使用MFC的release库 // 'DEBUG workaround' below prevents the MFC or ATL #includ ...
- VC里判断系统是不是64bit
不过,理论上来说,也可以用一个int的大小作为参考,判断是32位还是64位.sizeof(int) == 4 //32位系统.sizeof(int) == 8 //64位系统. 也可以使用函数如下: ...
- 在 VS 2013/2015 中禁用 nuget 包的源代码管理
对于加入源代码管理如TFS的解决方案,当使用nuget获取包时,下载的包并没有自动从源代码管理中排除,导致包(packages文件夹)会一同上传到服务器. 若要排除nuget包的源代码管理,须在 解决 ...
- (常用)configparser,hashlib,hamc模块
configparser模块 #专门解析my.ini这种形式的文件(cnf) import configparser config=configparser.ConfigParser() conf ...
- [WebRTC/JsSIP] AUDIO RTP REPORTS ERROR: [Remote Address Error!]
问题描述: 在使用FreeSWITCH WebRTC 测试时,FS回复 502 Bad Gateway.查看FS日志 [DEBUG] switch_core_media.c:5147 AUDIO R ...
- 使用Filezilla搭建FTP服务器
1.FTP over TLS is not enabled, users cannot securely http://blog.sina.com.cn/s/blog_4cd978f90102vtwl ...
- String类型作为方法的形参
代码: public class TestString { String str = new String("good"); char [] ch = {'a','b','c'}; ...
- xampp 搭建好本地服务器以后手机无法访问
转载地址:https://blog.csdn.net/weixin_35773751/article/details/80076492 解决办法: 1.将网络连接修改为工作网络,然后关闭工作局域网的防 ...