个人博客纯净版:https://www.fangzhipeng.com/springcloud/2017/06/07/sc07-config.html
上一篇文章讲述了一个服务如何从配置中心读取文件,配置中心如何从远程git读取配置文件,当服务实例很多时,都从配置中心读取文件,这时可以考虑将配置中心做成一个微服务,将其集群化,从而达到高可用,架构图如下:

一、准备工作
继续使用上一篇文章的工程,创建一个eureka-server工程,用作服务注册中心。
在其pom.xml文件引入Eureka的起步依赖spring-cloud-starter-eureka-server,代码如下:
4.0.0
com.forezp
eureka-server
0.0.1-SNAPSHOT
jar
eureka-server
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.2.RELEASE
UTF-8
UTF-8
1.8
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
Dalston.RC1
pom
import
org.springframework.boot
spring-boot-maven-plugin
spring-milestones
Spring Milestones
https://repo.spring.io/milestone
false
在配置文件application.yml上,指定服务端口为8889,加上作为服务注册中心的基本配置,代码如下:
server: port: 8889 eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
入口类:
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
二、改造config-server
在其pom.xml文件加上EurekaClient的起步依赖spring-cloud-starter-eureka,代码如下:
org.springframework.cloud
spring-cloud-config-server
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-eureka
配置文件application.yml,指定服务注册地址为http://localhost:8889/eureka/,其他配置同上一篇文章,完整的配置如下:
spring.application.name=config-server server.port=8888 spring.cloud.config.server.git.uri=https://github.com/forezp/SpringcloudConfig/ spring.cloud.config.server.git.searchPaths=respo spring.cloud.config.label=master spring.cloud.config.server.git.username= your username spring.cloud.config.server.git.password= your password eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/
最后需要在程序的启动类Application加上@EnableEureka的注解。
三、改造config-client
将其注册微到服务注册中心,作为Eureka客户端,需要pom文件加上起步依赖spring-cloud-starter-eureka,代码如下:
org.springframework.cloud
spring-cloud-starter-config
org.springframework.boot
spring-boot-starter-web
org.springframework.cloud
spring-cloud-starter-eureka
org.springframework.boot
spring-boot-starter-test
test
配置文件bootstrap.properties,注意是bootstrap。加上服务注册地址为http://localhost:8889/eureka/
spring.application.name=config-client spring.cloud.config.label=master spring.cloud.config.profile=dev #spring.cloud.config.uri= http://localhost:8888/ eureka.client.serviceUrl.defaultZone=http://localhost:8889/eureka/ spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=config-server server.port=8881
- spring.cloud.config.discovery.enabled 是从配置中心读取文件。
- spring.cloud.config.discovery.serviceId 配置中心的servieId,即服务名。
这时发现,在读取配置文件不再写ip地址,而是服务名,这时如果配置服务部署多份,通过负载均衡,从而高可用。

访问http://localhost:8881/hi,浏览器显示:
foo version 3
四、参考资料
spring_cloud_config
更多阅读
史上最简单的 SpringCloud 教程汇总
SpringBoot教程汇总
Java面试题系列汇总
扫码关注公众号有惊喜
(转载本站文章请注明作者和出处 方志朋的博客)
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/220657.html原文链接:https://javaforall.net
