SpringCloudConfig
- configClient
- configServer
- 远程服务器git
概述:
- 分布式配置问题,将单体服务拆分成多个微服务,系统产生大量微服务,每个服务都需要配置,SpringCloud Config对微服务配置进行统一管理,三种角色,configClient对应每个微服务,连接configServer,统一从server连接到远端云进行统一管理
- 配置中心可以连接本地,也可以连接远程,客户端对应着一个一个的微服务
- 可以设置多个环境来动态化配置更新,/dev环境/test环境,多种环境的切换
- 运行期间,能调整配置,服务不需要重启,就可以应用新的配置,需要加入热部署插件
- 有各种网站能进行存储配置
- 码云,git,coding。。。
创建仓库
- 开源许可证,一般选择GPL,许可证种类可以查看官方文档,有些许可证是需要授权的
mac安装git
-
brew install git
-
出问题收藏夹找方法
git常用命令
-
haoyun@HAOYUN git % git clone https://gitee.com/haoyunlwh/springcloud-config.git 克隆仓库 git config --list 查看配置 https://gitee.com/help/articles/4181#article-header0 添加ssh公钥官方文档 git add . 存放在缓冲区 git status 查看当前状态 git commit -m "first commit" 提交,first commit提交时设置的概要的名字,可以随意设置 git push origin master 推送目标到origin master
clone远程仓库
方法1
- 选择好文件位置进行克隆
- 粘贴clone链接
方法2
- github Desktop软件
- sign in登陆
- 粘贴链接
- 什么用户更新,进行了什么更新了,什么用户对这个仓库进行了提交,都看得到
SpringCloud-config-server读取配置
主要流程:
-
依赖文本
-
web
-
config-server
-
eureka
-
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
-
-
配置application,连接gitee仓库、Eureka
-
server.port=3344 spring.application.name=spring-cloud-config spring.cloud.config.server.git.uri=https://gitee.com/haoyunlwh/springcloud-config.git eureka.client.service-url.defaultZone:http://localhost:7001/eureka/
-
-
启动类添加configserver支持
-
@SpringBootApplication @EnableConfigServer public class ConfigServer3344Application { public static void main(String[] args) { SpringApplication.run(ConfigServer3344Application.class, args); } }
-
-
测试
- 之前上传的配置,测试通过configserver读取配置
- 几种读取的方式
Config-Client读取configServer配置
使用两个角色就能实现一个Demo
config-server和config-client
config-server通过url方式可以访问远程配置文件
格式就是
如:localhost:3344/config-client-dev.yml
uri/name/profile.fileSuffix
config-client通过访问config-server来获取配置,还是通过config-server来连接远程仓库
Demo
-
给要进行远程配置的服务添加依赖文本,这个可以是任何服务
-
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-client</artifactId> </dependency>
-
配置配置文件
-
bootstrap的优先级会比application高
-
spring: cloud: config: name: config-eureka label: master uri: http://localhost:3344 profile: dev
-
先启动config-server,这个服务才能读取到配置文件
-
前提是要有这配置文件上传到本地仓库
-
spring: profiles: active: dev --- server: port: 7001 spring: profiles: dev application: name: springcloud-config-eureka eureka: instance: hostname: eureka7001dev.com --- server: port: 7001 spring: profiles: test application: name: springcloud-config-eureka eureka: instance: hostname: eureka7001test.com
-
后续加上热部署功能,远程更改配置就能实时更新配置