博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
史上最简单的SpringCloud教程 | 第七篇: 高可用的分布式配置中心(Spring Cloud Config)(Finchley版本)...
阅读量:4503 次
发布时间:2019-06-08

本文共 3826 字,大约阅读时间需要 12 分钟。

转载请标明出处:

原文首发于:
本文出自

上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下:

Azure (3).png

一、准备工作

继续使用上一篇文章的工程,创建一个eureka-server工程,用作服务注册中心。

在其pom.xml文件引入Eureka的起步依赖spring-cloud-starter-netflix- eureka-server,代码如下:

4.0.0
com.forezp
config-server
0.0.1-SNAPSHOT
jar
config-server
Demo project for Spring Boot
com.forezp
sc-f-chapter7
0.0.1-SNAPSHOT
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-maven-plugin

在配置文件application.yml上,指定服务端口为8889,加上作为服务注册中心的基本配置,代码如下:

server:  port: 8889eureka:  instance:    hostname: localhost  client:    registerWithEureka: false    fetchRegistry: false    serviceUrl:      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

入口类:

@EnableEurekaServer@SpringBootApplicationpublic class EurekaServerApplication {	public static void main(String[] args) {		SpringApplication.run(EurekaServerApplication.class, args);	}}

二、改造config-server

在其pom.xml文件加上EurekaClient的起步依赖spring-cloud-starter-netflix-eureka-client,代码如下:

org.springframework.cloud
spring-cloud-starter-netflix-eureka-client
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-config-server

配置文件application.yml,指定服务注册地址为http://localhost:8889/eureka/,其他配置同上一篇文章,完整的配置如下:

spring.application.name=config-serverserver.port=8888spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/spring.cloud.config.server.git.searchPaths=respospring.cloud.config.label=masterspring.cloud.config.server.git.username= your usernamespring.cloud.config.server.git.password= your passwordeureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/

最后需要在程序的启动类Application加上@EnableEureka的注解。

三、改造config-client

将其注册微到服务注册中心,作为Eureka客户端,需要pom文件加上起步依赖spring-cloud-starter-netflix-eureka-client,代码如下:

org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-netflix-eureka-client

配置文件bootstrap.properties,注意是bootstrap。加上服务注册地址为http://localhost:8889/eureka/

spring.application.name=config-clientspring.cloud.config.label=masterspring.cloud.config.profile=dev#spring.cloud.config.uri= http://localhost:8888/eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/spring.cloud.config.discovery.enabled=truespring.cloud.config.discovery.serviceId=config-serverserver.port=8881
  • spring.cloud.config.discovery.enabled 是从配置中心读取文件。
  • spring.cloud.config.discovery.serviceId 配置中心的servieId,即服务名。

这时发现,在读取配置文件不再写ip地址,而是服务名,这时如果配置服务部署多份,通过负载均衡,从而高可用。

依次启动eureka-servr,config-server,config-client

访问网址:

Paste_Image.png

访问http://localhost:8881/hi,浏览器显示:

foo version 3

本文源码下载:

四、参考资料

优秀文章推荐:

SouthEast

扫码关注有惊喜

(转载本站文章请注明作者和出处 )

转载于:https://www.cnblogs.com/forezp/p/9852081.html

你可能感兴趣的文章
MySQL巡检
查看>>
学习笔记之传说中的圣杯布局
查看>>
oh-my-zsh的使用
查看>>
共享内存的设计
查看>>
deque容器
查看>>
2017-2018-1 20155203 20155204 实验二 固件程序设计
查看>>
三方贸易-drop ship
查看>>
Android RenderScript 使用 Struct 及其下标的赋值
查看>>
【题解】BZOJ P1801 dp
查看>>
杂项-软件生命周期:软件生命周期
查看>>
小程序:小程序能力
查看>>
P1578 奶牛浴场 有障碍点的最大子矩形
查看>>
OpenCV学习:体验ImageWatch
查看>>
socket_循环接收消息
查看>>
I/O基础之概念
查看>>
各种算法的优缺点:
查看>>
poj 2513 Colored Sticks 字典树
查看>>
BZOJ 1266: [AHOI2006]上学路线route Floyd_最小割
查看>>
Softmax函数
查看>>
.NET 向SQL里写入非Text类型
查看>>