资讯专栏INFORMATION COLUMN

SpringCloud(第 029 篇)配置客户端 ConfigClient 接入配置服务端

YFan / 3740人阅读

SpringCloud(第 029 篇)配置客户端 ConfigClient 接入配置服务端

-

一、大致介绍

</>复制代码

  1. 1、有配置服务端,那么势必就会有与之对应的客户端,SpringCloud 文档中集成也非常简单;
  2. 2、但是这里有点需要注意,就是 bootstrap 配置文件,官方建议我们在bootstrap中放置不更改的属性,我们同样也需要在这里做一些简单不易于改变的配置;
  3. 3、这里还顺便列举下配置路径的规则:
  4. /****************************************************************************************
  5. * 配置服务的路劲规则:
  6. *
  7. * /{application}/{profile}[/{label}]
  8. * /{application}-{profile}.yml
  9. * /{label}/{application}-{profile}.yml
  10. * /{application}-{profile}.properties
  11. * /{label}/{application}-{profile}.properties
  12. ****************************************************************************************/
二、实现步骤 2.1 添加 maven 引用包

</>复制代码

  1. 4.0.0
  2. springms-config-client
  3. 1.0-SNAPSHOT
  4. jar
  5. com.springms.cloud
  6. springms-spring-cloud
  7. 1.0-SNAPSHOT
  8. org.springframework.cloud
  9. spring-cloud-starter-config
  10. org.springframework.boot
  11. spring-boot-starter-web
2.2 添加应用配置文件(springms-config-clientsrcmainresourcesapplication.yml)

</>复制代码

  1. server:
  2. port: 8225
  3. #####################################################################################################
  4. # 测试一:配置服务客户端Client应用入口(正常测试 ConfigClient )
  5. profile: profile-dev(local)
  6. #####################################################################################################
  7. #####################################################################################################
  8. # 测试二:配置服务客户端Client应用入口(链接 ClientServer 测试)
  9. #spring:
  10. # cloud:
  11. # config:
  12. # uri: http://localhost:8220
  13. # profile: dev
  14. # label: master #当 ConfigServer 的后端存储的是 Git 的时候,默认就是 master
  15. #
  16. # application:
  17. # name: foobar #取 foobar-dev.yml 这个文件的 application 名字,即为 foobar 名称
  18. #####################################################################################################
  19. #####################################################################################################
  20. # 测试四:配置服务客户端Client应用入口(链接 ClientServer 测试,同时本地也有一份配置文件,那么该如何抉择呢?)
  21. #profile: profile-local-dev
  22. #####################################################################################################
2.3 添加 bootstrap.yml 应用配置文件(springms-config-clientsrcmainresourcesbootstrap.yml)

</>复制代码

  1. #####################################################################################################
  2. # 测试三:配置服务客户端Client应用入口(链接 ClientServer 测试)
  3. #spring:
  4. # cloud:
  5. # config:
  6. # uri: http://localhost:8220
  7. # profile: dev
  8. # label: master #当 ConfigServer 的后端存储的是 Git 的时候,默认就是 master
  9. #
  10. # application:
  11. # name: foobar #取 foobar-dev.yml 这个文件的 application 名字,即为 foobar 名称
  12. #####################################################################################################
2.4 添加Web控制层类(springms-config-clientsrcmainjavacomspringmscloudcontrollerConfigClientController.java)

</>复制代码

  1. package com.springms.cloud.controller;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import org.springframework.web.bind.annotation.RestController;
  5. /**
  6. * 配置客户端Controller。
  7. *
  8. * @author hmilyylimh
  9. *
  10. * @version 0.0.1
  11. *
  12. * @date 2017/10/15
  13. *
  14. */
  15. @RestController
  16. public class ConfigClientController {
  17. @Value("${profile}")
  18. private String profile;
  19. @GetMapping("/profile")
  20. public String getProfile(){
  21. return this.profile;
  22. }
  23. }
2.4 添加应用启动类(springms-config-clientsrcmainjavacomspringmscloudMsConfigClientApplication.java)

</>复制代码

  1. package com.springms.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. /**
  5. * 配置客户端ConfigClient接入配置服务端。
  6. *
  7. * @author hmilyylimh
  8. *
  9. * @version 0.0.1
  10. *
  11. * @date 2017/10/15
  12. *
  13. */
  14. @SpringBootApplication
  15. public class MsConfigClientApplication {
  16. public static void main(String[] args) {
  17. SpringApplication.run(MsConfigClientApplication.class, args);
  18. System.out.println("【【【【【【 ConfigClient微服务 】】】】】】已启动.");
  19. }
  20. }
三、测试

</>复制代码

  1. /****************************************************************************************
  2. 一、配置客户端ConfigClient接入配置服务端(正常测试 ConfigClient ):
  3. 1、注解:pom.xml 先删除 configclient 的引用模块,以便测试正常情况 ConfigClientController 接口是否畅通;
  4. 2、编辑 application.yml 文件,注意添加 profile: profile-dev(local) 属性;
  5. 3、启动 springms-config-client 模块服务,启动1个端口;
  6. 4、在浏览器输入地址 http://localhost:8225/profile 正常情况下会输出配置文件的内容(内容为:profile-dev(local));
  7. 注意:这里还暂时不需要 bootstrap.yml 配置文件,所以测试一是不需要添加 bootstrap.yml 文件的;
  8. ****************************************************************************************/
  9. /****************************************************************************************
  10. 二、配置客户端ConfigClient接入配置服务端(链接 ClientServer 测试遇到挫折):
  11. 1、注解:pom.xml 先添加 configclient 的引用模;
  12. 2、编辑 application.yml 文件,注意注释 profile 属性,然后添加相关客户端配置;
  13. spring:
  14. cloud:
  15. config:
  16. uri: http://localhost:8220
  17. profile: dev
  18. label: master #当 ConfigServer 的后端存储的是 Git 的时候,默认就是 master
  19. application:
  20. name: foobar #取 foobar-dev.yml 这个文件的 application 名字,即为 foobar 名称
  21. 3、启动 springms-config-server 模块服务,启动1个端口;
  22. 4、启动 springms-config-client 模块服务,启动1个端口;
  23. 5、然后发现启动 springms-config-client 模块出现错误,报错信息为:Fetching config from server at: http://localhost:8888, Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/foobar/dev/master": Connection refused;
  24. 6、发现错误信息中,为什么链接的是远端的 8888 端口呢?百思不得其解,难道是默认加载的配置 8888 端口???
  25. 7、SpringCloud里面有个“启动上下文”,主要是用于加载远端的配置,也就是加载ConfigServer里面的配置,默认加载顺序为:加载bootstrap.*里面的配置 --> 链接configserver,加载远程配置 --> 加载application.*里面的配置;
  26. 总结:这里需要借助于“启动上下文”来处理加载远程配置,请看下面环节测试三。
  27. ****************************************************************************************/
  28. /****************************************************************************************
  29. 三、配置客户端ConfigClient接入配置服务端(链接 ClientServer 测试遇到挫折):
  30. 1、注解:pom.xml 先添加 configclient 的引用模;
  31. 2、编辑 application.yml 文件,注释"测试二"的属性配置;
  32. 3、新建一个 bootstrap.yml 文件,将相关客户端配置挪到 bootstrap.yml 文件即可;
  33. 4、启动 springms-config-server 模块服务,启动1个端口;
  34. 5、启动 springms-config-client 模块服务,启动1个端口;
  35. 6、在浏览器输入地址 http://localhost:8225/profile 正常情况下会输出配置文件的内容(内容为:profile-dev);
  36. 总结:这里成功获取了远端配置,并成功打印了属性值出来,说明添加 bootstrap.yml 配置文件对我们项目的顺利进行起到了有效的作用。
  37. ****************************************************************************************/
  38. /****************************************************************************************
  39. 四、配置客户端ConfigClient接入配置服务端(链接 ClientServer 测试,同时本地也有一份配置文件,那么该如何抉择呢?):
  40. 1、在测试三的基础上,咱们再做点其它配置测试;
  41. 2、在 application.yml 文件,再次添加 profile 属性,看看加载的是本地配置还是远端配置?
  42. 3、停止并重新启动 springms-config-client 模块服务,启动1个端口;
  43. 4、在浏览器输入地址 http://localhost:8225/profile 正常情况下会输出远端服务的配置内容;
  44. 总结:在ConfigServer服务启动的时候,bootstrap 拿到远端配置注入到 profile 的属性中的话,那么就不会再次覆盖这个属性了,所以只会选择远端配置的内容。
  45. 那是不是会有人认为把ConfigServer再重启一下就行了呢?答案是不行的,因为首选的是远端配置内容;
  46. ****************************************************************************************/
四、下载地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接

欢迎关注,您的肯定是对我最大的支持!!!

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/67769.html

相关文章

  • SpringCloud 037 )通过bus/refresh半自动刷新ConfigClient

    摘要:添加应用启动类通过半自动刷新配置。配置客户端服务想要实现自动刷新配置的话,一端是不要做任何处理,只需要在一端处理即可。 SpringCloud(第 037 篇)通过bus/refresh半自动刷新ConfigClient配置 - 一、大致介绍 1、上章节我们讲到了手动刷新配置,但是我们假设如果微服务一多的话,那么我们是不是需要对每台服务进行手动刷新呢? 2、答案肯定是不需要的,我们也可...

    CloudwiseAPM 评论0 收藏0
  • SpringCloud 036 )单点手动动态刷新ConfigClient配置

    摘要:添加应用启动类单点手动动态刷新配置。配置客户端服务想要实现自动刷新配置的话,一端是不要做任何处理,只需要在一端处理即可。 SpringCloud(第 036 篇)单点手动动态刷新ConfigClient配置 - 一、大致介绍 1、当ConfigServer启动后,假如我们新增配置内容的话,是不是要重新启动一下ConfigServer呢? 2、答案肯定是不需要重新启动的,因为 Sprin...

    wanglu1209 评论0 收藏0
  • SpringCloud 035 配置服务客户ConfigClient链接经过认证的配置服务

    SpringCloud(第 035 篇)配置服务客户端ConfigClient链接经过认证的配置服务端 - 一、大致介绍 1、前面一章节讲解了服务端配置安全认证,那么本章节就讲解如何链接上服务端的认证; 2、这里还顺便列举下配置路径的规则: /*****************************************************************************...

    klivitamJ 评论0 收藏0
  • SpringCloud 031 配置客户ConfigClient链接经过对称加解密的配置

    摘要:添加应用启动类配置客户端链接经过对称加解密的配置微服务专门为测试经过对称加解密的配置微服务微服务模块。 SpringCloud(第 031 篇)配置客户端ConfigClient链接经过对称加解密的配置微服务 - 一、大致介绍 1、Git服务端的文件内容进行了加密处理,那么是不是配置客户端拿到内容之后需要解密呢? 2、答案显然不是的,因为这样解密的话,先不说实现起来的难易程度,单从表面...

    DDreach 评论0 收藏0
  • SpringCloud 033 配置客户ConfigClient链接经过对称加解密的配置

    SpringCloud(第 033 篇)配置客户端ConfigClient链接经过对称加解密的配置微服务 - 一、大致介绍 1、在(第 031 篇)讲解了如何链接对称加密的配置服务端,而链接对称非对称加密的配置微服务也是同样的; 2、配置客户端不需要做什么加解密的配置,加解密的配置在服务端做就好了; 3、这里还顺便列举下配置路径的规则: /****************************...

    ARGUS 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<