SpringCloud(第 028 篇)ConfigServer 配置管理微服务
来源:互联网 发布:联合网络图书采访 编辑:程序博客网 时间:2024/06/01 12:00
SpringCloud(第 028 篇)ConfigServer 配置管理微服务
-
一、大致介绍
1、在讲到配置时,不得不说 SpringCloud 提供了一套解决分布式的配置管理方案,它既包含了服务端ConfigServer也包含了客户端ConfigClient;2、SpringCloud 将配置文件当作源代码一样存储到 git 或者 svn 服务器上,虽然说这样没有什么管理界面配置啥的,既然能用 svn 上传上去,那也能做成管理界面,只是花的工作量多少而已了,而既然都说了是配置,那就是只要会稍微学些git或者svn的提交文件方式,基本上任何都极易掌握;3、当我们把配置文件放在 git 上的时候,我们如果要做到更新的话,我们需要借助于 git 网页上的 push 操作来触发更新操作;4、说的有点多了,那么我们今天就来小试牛刀,简单试试 ConfigServer 是如何和 Git 服务器相关联存储配置的,至于更新机制操作的话,请看后序章节讲解;5、这里还顺便列举下配置路径的规则:/**************************************************************************************** * 配置服务的路劲规则: * * /{application}/{profile}[/{label}] * /{application}-{profile}.yml * /{label}/{application}-{profile}.yml * /{application}-{profile}.properties * /{label}/{application}-{profile}.properties ****************************************************************************************/
二、实现步骤
2.1 添加 maven 引用包
<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <artifactId>springms-config-server</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <parent> <groupId>com.springms.cloud</groupId> <artifactId>springms-spring-cloud</artifactId> <version>1.0-SNAPSHOT</version> </parent> <dependencies> <!-- Config 服务端配置模块 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies></project>
2.2 添加应用配置文件(springms-config-server\src\main\resources\application.yml)
server: port: 8220spring: application: name: springms-config-server cloud: config: server: git: uri: https://gitee.com/ylimhhmily/OpenSource_CustomCircleLineProgressBar
2.3 添加sidecar微服务启动类(springms-config-server\src\main\java\com\springms\cloud\MsConfigServerApplication.java)
package com.springms.cloud;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;/** * ConfigServer 配置管理微服务。 * * @author hmilyylimh * * @version 0.0.1 * * @date 2017/9/28 * */@SpringBootApplication@EnableConfigServerpublic class MsConfigServerApplication { public static void main(String[] args) { SpringApplication.run(MsConfigServerApplication.class, args); System.out.println("【【【【【【 ConfigServer微服务 】】】】】】已启动."); }}
三、测试
/**************************************************************************************** 一、ConfigServer 配置管理微服务(正常测试)(添加一个配置文件 application.yml 来做测试): 1、注解:EnableConfigServer 2、编辑 application.yml 文件,注意只填写 cloud.config.server.git.uri 属性; 3、启动 springms-config-server 模块服务,启动1个端口; 4、在浏览器输入地址 http://localhost:8220/abc-default.properties 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 5、在浏览器输入地址 http://localhost:8220/abc-default.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 6、在浏览器输入地址 http://localhost:8220/abc-hehui.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 7、在浏览器输入地址 http://localhost:8220/aaa-bbb.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 8、在浏览器输入地址 http://localhost:8220/aaa-bbb.properties 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 总结一:按照 /{application}-{profile}.yml 和 /{application}-{profile}.properties 这种规则来测试的,当找不到路径的话,会默认找到 application.yml 文件读取字段内容; 9、在浏览器输入地址 http://localhost:8220/master/abc-default.properties 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 10、在浏览器输入地址 http://localhost:8220/master/abc-default.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 11、在浏览器输入地址 http://localhost:8220/master/abc-hehui.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 12、在浏览器输入地址 http://localhost:8220/master/aaa-bbb.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 13、在浏览器输入地址 http://localhost:8220/master/aaa-bbb.properties 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 14、在浏览器输入地址 http://localhost:8220/springms-config-server-dev.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 总结二:按照 /{label}/{application}-{profile}.yml 和 /{label}/{application}-{profile}.properties 这种规则来测试,当找不到路径的话,会默认找到 application.yml 文件读取字段内容; 总结三:所以不管怎么测试路径规则,找不到的话,反正也不会抛什么异常,反正一律会映射到 application.yml 文件上; ****************************************************************************************//**************************************************************************************** 二、ConfigServer 配置管理微服务(再添加一个配置文件 foobar-dev.yml 来做测试): 1、注解:EnableConfigServer 2、编辑 application.yml 文件,注意只填写 cloud.config.server.git.uri 属性; 3、启动 springms-config-server 模块服务,启动1个端口; 4、在浏览器输入地址 http://localhost:8220/foobar-dev.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-dev); 5、在浏览器输入地址 http://localhost:8220/foobar-dev.properties 正常情况下会输出配置文件的内容(内容为:profile: profile-dev); 6、在浏览器输入地址 http://localhost:8220/master/foobar-dev.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-dev); 7、在浏览器输入地址 http://localhost:8220/master/foobar-dev.properties 正常情况下会输出配置文件的内容(内容为:profile: profile-dev); 总结一:按照 /{application}-{profile}.yml 和 /{application}-{profile}.properties /{label}/{application}-{profile}.yml 和 /{label}/{application}-{profile}.properties 这种规则来测试的,会找到 foobar-dev.yml 文件,既然找到了 foobar-dev.yml 文件的话,那自然就没 application.yml 文件什么事情了; 8、在浏览器输入地址 http://localhost:8220/foobar-aaa.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 9、在浏览器输入地址 http://localhost:8220/foobar-aaa.properties 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 10、在浏览器输入地址 http://localhost:8220/master/foobar-aaa.yml 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 11、在浏览器输入地址 http://localhost:8220/master/foobar-aaa.properties 正常情况下会输出配置文件的内容(内容为:profile: profile-default); 总结二:将dev改成aaa的话,按照配置服务的路径规则配置,当路径中的配置文件不再url的目录下的话,那么则会找到默认配置的文件 application.yml 来加载显示。 ****************************************************************************************/
四、下载地址
https://gitee.com/ylimhhmily/SpringCloudTutorial.git
SpringCloudTutorial交流QQ群: 235322432
SpringCloudTutorial交流微信群: 微信沟通群二维码图片链接
欢迎关注,您的肯定是对我最大的支持!!!
阅读全文
0 0
- SpringCloud(第 028 篇)ConfigServer 配置管理微服务
- SpringCloud(第 034 篇)配置服务端ConfigServer设置安全认证
- SpringCloud(第 001 篇)简单用户微服务
- SpringCloud--configServer
- SpringCloud(第 027 篇)集成异构微服务系统到 SpringCloud 生态圈中(比如集成 nodejs 微服务)
- SpringCloud(第 002 篇)简单电影微服务类(消费方,而提供方为用户微服务)
- SpringCloud(第 003 篇)服务发现服务端EurekaServer微服务
- SpringCloud(第 006 篇)电影微服务,使用 Ribbon 在客户端进行负载均衡
- SpringCloud(第 009 篇)简单 Quartz 微服务,不支持分布式
- SpringCloud(第 018 篇)Zuul 服务 API 网关微服务之代理与反向代理
- SpringCloud(第 019 篇)Zuul 网关微服务的一些属性应用测试文章标题
- SpringCloud(第 026 篇)简单异构系统之 nodejs 微服务
- SpringCloud(第 038 篇)idea环境热部署微服务开发
- SpringCloud(第 054 篇)简单 Quartz-Cluster 微服务,采用注解配置 Quartz 分布式集群
- SpringCloud(第 024 篇)简单文件上传微服务,并加入 zuul 微服务后用 zuul 微服务地址采取curl或者页面点击实现文件上传
- SpringCloud(第 004 篇)用户服务类(添加服务注册,将用户微服务注册到 EurekaServer 中)
- SpringCloud(第 011 篇)电影Ribbon微服务,脱离Eureka使用配置listOfServers进行客户端负载均衡调度
- SpringCloud(第 014 篇)电影 Ribbon 微服务集成 Hystrix 断路器实现失败快速响应,达到熔断效果
- Sql 四大排名函数(ROW_NUMBER、RANK、DENSE_RANK、NTILE)简介
- JavaSE集合框架-01-集合框架体系及表数据结构
- effective c++读书笔记(三)
- AUTOCAD学习笔记3:常用电器元件的绘制
- Quora比赛代码学习笔记
- SpringCloud(第 028 篇)ConfigServer 配置管理微服务
- Redis的安装和部署
- Android自定义View的实现方法,带你一步步深入了解View(四)
- Linux中vim常用快捷键
- spring各个jar包的作用及功能总结
- VMware12提示 已将该虚拟机配置为使用 64 位客户机操作系统。但是,无法执行 64 位操作
- Java学习路上的对一些书籍的推荐
- 脑科学对“天才”的科学分析——新书《无为》解读
- 数据结构实验之栈与队列二:一般算术表达式转换成后缀式