在前面两篇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引用。

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.6.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>SpringCloudConfigServer</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <packaging>war</packaging>
  15. <name>SpringCloudConfigServer</name>
  16. <description>Demo project for Spring Boot</description>
  17.  
  18. <properties>
  19. <java.version>1.8</java.version>
  20. <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
  21. </properties>
  22.  
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-config-server</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-tomcat</artifactId>
  39. <scope>provided</scope>
  40. </dependency>
  41. <dependency>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-starter-test</artifactId>
  44. <scope>test</scope>
  45. </dependency>
  46. </dependencies>
  47.  
  48. <dependencyManagement>
  49. <dependencies>
  50. <dependency>
  51. <groupId>org.springframework.cloud</groupId>
  52. <artifactId>spring-cloud-dependencies</artifactId>
  53. <version>${spring-cloud.version}</version>
  54. <type>pom</type>
  55. <scope>import</scope>
  56. </dependency>
  57. </dependencies>
  58. </dependencyManagement>
  59.  
  60. <build>
  61. <plugins>
  62. <plugin>
  63. <groupId>org.springframework.boot</groupId>
  64. <artifactId>spring-boot-maven-plugin</artifactId>
  65. </plugin>
  66. </plugins>
  67. </build>
  68.  
  69. </project>

2.增加了eureka注册中心的配置

在application.properties中增加eureka注册中心的配置,http://localhost:8088是后面启动的eureka server的地址

  1. eureka.client.serviceUrl.defaultZone=http://localhost:8088/eureka/

3.增加注解

在main方法中增加@EnableDiscoveryClient注解

二、客户端改造

1、添加依赖

也是引入spring-cloud-starter-netflix-eureka-client。

  1. <dependency>
  2. <groupId>org.springframework.cloud</groupId>
  3. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  4. </dependency>
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.6.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>SpringCloudConfigClient</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <packaging>war</packaging>
  15. <name>SpringCloudConfigClient</name>
  16. <description>Demo project for Spring Boot</description>
  17.  
  18. <properties>
  19. <java.version>1.8</java.version>
  20. <spring-cloud.version>Greenwich.SR2</spring-cloud.version>
  21. </properties>
  22.  
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-config</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-actuator</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-tomcat</artifactId>
  43. <scope>provided</scope>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-test</artifactId>
  48. <scope>test</scope>
  49. </dependency>
  50. </dependencies>
  51.  
  52. <dependencyManagement>
  53. <dependencies>
  54. <dependency>
  55. <groupId>org.springframework.cloud</groupId>
  56. <artifactId>spring-cloud-dependencies</artifactId>
  57. <version>${spring-cloud.version}</version>
  58. <type>pom</type>
  59. <scope>import</scope>
  60. </dependency>
  61. </dependencies>
  62. </dependencyManagement>
  63.  
  64. <build>
  65. <plugins>
  66. <plugin>
  67. <groupId>org.springframework.boot</groupId>
  68. <artifactId>spring-boot-maven-plugin</artifactId>
  69. </plugin>
  70. </plugins>
  71. </build>
  72.  
  73. </project>

2.配置文件

在bootstrap.properties中去掉指定配置服务的地址url,增加了最后的三个配置:

  1. spring.cloud.config.name=neo-config
  2. spring.cloud.config.profile=dev
  3. spring.cloud.config.label=
  4. spring.cloud.config.discovery.enabled=true
  5. spring.cloud.config.discovery.serviceId=spring-cloud-config-server
  6. 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

微服务SpringCloud之Spring Cloud Config配置中心服务化的更多相关文章

  1. 微服务SpringCloud之Spring Cloud Config配置中心Git

    微服务以单个接口为颗粒度,一个接口可能就是一个项目,如果每个项目都包含一个配置文件,一个系统可能有几十或上百个小项目组成,那配置文件也会有好多,对后续修改维护也是比较麻烦,就和前面的服务注册一样,服务 ...

  2. 微服务SpringCloud之Spring Cloud Config配置中心SVN

    在回来的路上看到一个个的都抱着花,吃了一路的狗粮,原本想着去旁边的工业园里跑跑步呢,想想还是算了,人家过七夕,俺们过巴西.上一博客学习了Spring Cloud Config使用git作为配置中心,本 ...

  3. 跟我学SpringCloud | 第七篇:Spring Cloud Config 配置中心高可用和refresh

    SpringCloud系列教程 | 第七篇:Spring Cloud Config 配置中心高可用和refresh Springboot: 2.1.6.RELEASE SpringCloud: Gre ...

  4. spring cloud --- config 配置中心 [本地、git获取配置文件]

    spring boot      1.5.9.RELEASE spring cloud    Dalston.SR1 1.前言 spring cloud config 配置中心是什么? 为了统一管理配 ...

  5. SpringCloud学习笔记(7):使用Spring Cloud Config配置中心

    简介 Spring Cloud Config为分布式系统中的外部化配置提供了服务器端和客户端支持,服务器端统一管理所有配置文件,客户端在启动时从服务端获取配置信息.服务器端有多种配置方式,如将配置文件 ...

  6. Spring Cloud Config 配置中心实践过程中,你需要了解这些细节!

    本文导读: Spring Cloud Config 基本概念 Spring Cloud Config 客户端加载流程 Spring Cloud Config 基于消息总线配置 Spring Cloud ...

  7. Spring Cloud Config 配置中心高可用

    详细参见 <Spring Cloud 与 Docker微服务架构实战> p163-9.10 Spring Cloud Config 与 Eureka 配合使用 p163-9.12 Conf ...

  8. Spring Cloud Config 配置中心 自动加解密功能 jasypt方式

    使用此种方式会存在一种问题:如果我配置了自动配置刷新,则刷新过后,加密过后的密文无法被解密.具体原因分析,看 SpringCloud 详解配置刷新的原理 使用  jasypt-spring-boot- ...

  9. Spring Cloud Config 配置中心

    请将远程配置文件的格式写对: 比如使用 *.yml 或者 *.properties yml: testconfig: testvalue properties: testconfig=testvalu ...

随机推荐

  1. UVA-10608 Friends 【并查集】

    There is a town with N citizens. It is known that some pairs of people are friends. According to the ...

  2. 个人永久性免费-Excel催化剂功能第85波-灵活便捷的批量发送短信功能(使用腾讯云接口)

    微信时代的今天,短信一样不可缺席,大系统都有集成短信接口.若只是临时用一下,若能够直接在Excel上加工好内容就可以直接发送,这些假设在此篇批量群发短信功能中都为大家带来完美答案. 业务场景 不多说, ...

  3. 2017day2

    系统模块: # Author: sonny# -*- coding:utf-8 -*-import sys; #print(sys.path);print(sys.argv);print(sys.ar ...

  4. Linnux命令大全(vim)

    vim复制和粘贴的基本命令(注:需先退出编辑模式)    yy复制游标所在行整行.或大写一个Y. (常用)    2yy或y2y复制两行. (常用)    y^复制至行首,或y0.不含游标所在处字元. ...

  5. EF Core懒人小技巧之拒绝DbSet

    前言 最近在项目中使用EF Core的频率越来越高,当项目比较大的时候,疯狂往DbContext中加各种DbSet,你会不会特难受?如果你是一键生成的大佬,那么请忽略本文.本文旨在不写 DbSet,那 ...

  6. js - 使用jquery发送前台请求给服务器,并显示数据

    1.使用jquery发送前台请求给服务器,并显示数据 <%@ page contentType="text/html;charset=UTF-8" language=&quo ...

  7. 从flink-example分析flink组件(3)WordCount 流式实战及源码分析

    前面介绍了批量处理的WorkCount是如何执行的 <从flink-example分析flink组件(1)WordCount batch实战及源码分析> <从flink-exampl ...

  8. ios下,微信小程序scrollview组件中的fixed元素抖得和帕金森病人一样

    问题现象 这个问题是最近在优化小程序代码时发现的. 在ios环境下,微信小程序的scrollview组件包裹着一个position:fixed的view. 当在scrollview组件上滑动时,这个v ...

  9. 【SVN】SVN Working copy is too old

    前天在使用 SVN 客户端 CornerStone 的时候遇到了这个问题,代码不能提交了…… 遇到这个问题的时候怎么办? 解决办法: 找到报错对应的文件夹,里面有个 .svn 的文件夹,去掉再 com ...

  10. 设置Myeclipse的jvm内存参数

    Myeclipse经常会遇到内存溢出和Gc开销过大的情况,这时候就需要修改Myeclipse的Jvm内存参数 修改如下:(使用Extjs做公司大项目时候,不要让项目Builders的Javascrip ...