在前面两篇Spring Cloud Config配置中心的博客中都是需要指定配置服务的地址url:spring.cloud.config.uri,客户端都是直接调用配置中心的server端来获取配置文件信息。如果server端要做集群,客户端只能通过原始的方式来路由,server端改变IP地址的时候,客户端也需要修改配置,不符合springcloud服务治理的理念。springcloud提供了这样的解决方案,我们只需要将server端当做一个服务注册到eureka中,client端去eureka中去获取配置中心server端的服务既可。 一、Server端改造 1.添加依赖 在SpringCloudConfigServer项目中,添加spring-cloud-starter-netflix-eureka-client引用。 View Code View Code 2.增加了eureka注册中心的配置 在application.properties中增加eureka注册中心的配置,http://localhost:8088是后面启动的eureka server的地址 eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/ 3.增加注解 在main方法中增加@EnableDiscoveryClient注解 二、客户端改造 1、添加依赖 也是引入spring-cloud-starter-netflix-eureka-client。 复制代码 org.springframework.cloud spring-cloud-starter-netflix-eureka-client 复制代码 View Code 2.配置文件 在bootstrap.properties中去掉指定配置服务的地址url,增加了最后的三个配置: 复制代码 spring.cloud.config.name=neo-config spring.cloud.config.profile=dev spring.cloud.config.label= spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=spring-cloud-config-server eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/ 复制代码 spring.cloud.config.discovery.enabled :开启Config服务发现支持 spring.cloud.config.discovery.serviceId :指定server端的name,也就是server端spring.application.name的值 eureka.client.serviceUrl.defaultZone :指向注册中心的地址 3.启动类增加注解 在main方法中增加注解@EnableDiscoveryClient。 三、测试 1.分别启动eureka server、SpringCloudConfigServer、SpringCloudConfigClient。 2.浏览器输入http://localhost:8088/,可以看到注入到eureka server中的服务。 3.输入http://localhost:8001/neo-config/dev,可以看到配置文件的配置信息。 4.输入http://localhost:8002/hello,可以读取到配置文件的属性信息 参考:http://www.ityouknow.com/springcloud/2017/05/25/springcloud-config-eureka.html ---------------我是有底线的-------------------- 作者:社会主义接班人 出处:http://www.cnblogs.com/5ishare/ 本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 如果文中有什么错误,欢迎指出。以免更多的人被误导。 分类: Java, JavaWeb, Webhttps://www.cnblogs.com/5ishare/p/11409371.html