「 从0到1学习微服务SpringCloud 」06 统一配置中心Spring Cloud Config
系列文章(更新ing):
「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现
「 从0到1学习微服务SpringCloud 」03 Eureka的自我保护机制
「 从0到1学习微服务SpringCloud 」04服务消费者Ribbon+RestTemplate
「 从0到1学习微服务SpringCloud 」05消费者Fegin
是什么?
如名字一样,就是用来统一管理配置文件的组件
为什么需要它?
1.不方便维护
多人同时对配置文件进行修改,冲突不断,很难维护
2.配置内容安全和权限
主要是针对线上的配置来说,一般不对开发公开,只有运维有权限所以需要将配置文件隔离,不放到项目代码里
3.更新配置项目需要重启
每次更新配置文件都需要重启项目,很耗时。使用了配置中心后,即可实现配置实时更新
原理
1.配置文件存储在远端git(比如github,gitee等仓库),config-server从远端git拉取配置文件,并保存到本地git。
2.本地git和config-server的交互是双向的,因为当远端git无法访问时,会从本地git获取配置文件
3.config-client(即各个微服务),从config-server拉取配置文件
实战
RabbitMQ
项目需要配合使用RabbitMQ实现,需要先安装RabbitMQ。
公众号提供64位win安装包(公众号后台回复【rbmq】获取)
安装成功后,可打开RabbitMQ管理界面http://localhost:15672
默认账号密码:guest/guest
git仓库创建配置文件
1.master分支新建client.yml
server:
port: 8861
spring:
application:
name: client
label: master
2.master分支新建client-dev.yml
server:
port: 8861
spring:
application:
name: client
profile: dev
config-server
1.新建一个配置中心服务端项目,同时勾选Config Server和Eureka Discovery,作为配置中心服务端和Eureka客户端
2.启动类加注解@EnableEurekaClient和@EnableConfigServer
@SpringBootApplication
@EnableEurekaClient
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
3.编写configServer配置
server:
port: 8961
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: config
cloud:
config:
server:
git:
uri: 配置中心git地址
username: 账号
password: 密码
#本地git保存地址
basedir: D:\Work\HqProject\config\basedir
#向外暴露接口
#主要是为了暴露/actuator/bus-refresh,刷新配置信息
management:
endpoints:
web:
exposure:
include: "*"
4.添加Spring cloud bus的maven(Spring cloud bus作为消息总线,mq作为消息中间件,传递信息)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
5.启动Config 项目,注册到Eureka,启动成功后会自动添加RabbitMQ队列
6.访问地址http://localhost:8961/client-dev.yml可以访问到配置文件
config-client
继续使用上节的项目eureka-client
1.添加Config-client和Spring cloud bus的maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2.修改配置文件名字,改为bootstrap.yml,这个配置文件默认优先级最高,如果不这样做,项目会默认Config-Server的端口号为localhost:8888(bootstrap意为:引导启动 只是刚好和前端框架bootstrap名字相同,它们没有关系)
3.修改配置
server:
port: 8861
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
spring:
application:
name: client
cloud:
#Config相关配置
config:
discovery:
enabled: true
#Config-server的服务名
service-id: CONFIG
#环境
profile: dev
#分支
label: master
这里的配置关乎拉取的规则,如下
1.{name}.yml
2.{name}-{profile}.yml
3.{label}/{name}-{profile}.yml
name:服务名
profile:环境
label:分支
1.若没有声明profile(环境),采用第1种规则,默认为master分支
2.若没有声明label(分支)时,采用第2种规则,默认为master分支
ps:当声明了profile(环境)时,会同时获取{name}.yml和{name}-{profile}.yml文件,然后合并。所以在{name}.yml文件中可写一些通用的配置
4.新建两个controller,测试从获取到的配置
@RestController
//这个注解为 刷新区域
//有这个注解,配置才能刷新
@RefreshScope
public class EnvController {
@Value("${env}")
private String env;
@GetMapping("env")
public String getEnv(){
return env;
}
}
@RestController
@RefreshScope
public class FenzhiController {
@Value("${fenzhi}")
private String fenzhi;
@GetMapping("fenzhi")
public String getFenzhi(){
return fenzhi;
}
}
5.启动项目,注册到Eureka,启动成功后,控制台会显示成功拉取配置,并自动添加RabbitMQ队列
6.分别访问http://localhost:8861/env和http://localhost:8861/fenzhi可获取到相应配置
下面的效果就表名,声明了profile(环境)时,会同时获取{name}.yml和{name}-{profile}.yml文件
手动刷新配置
1.修改git仓库中client-dev.yml的env改为test
2.使用Postman发送Post请求http://localhost:8961/actuator/bus-refresh(ConfigServer的服务),告诉ConfigServer,配置更新了,拉取最新配置到ConfigServer,并通过bus发送给各个ConfigClient,让它们拉取最新配置
3.访问http://localhost:8861/env,env已修改为了test,表明已获取最新配置
自动刷新配置
1.安装内网穿透软件(使外网能访问本机),我这里使用的ngrok(官网:https://ngrok.com) 官方了提供免费的,可用于测试
注册登录后,会有操作说明,如下(我想,你应该能看懂)
2.我这直接跳到第4步,开启ngrok,映射到本地8961端口(ConfigServer的端口)
到ngrok所在目录,打开console控制台,输入ngrok http 8961
3.访问ngrok所给的域名,已成功映射(有时可能会有网络问题,多刷新几次)
4.ConfigServer应用添加maven依赖,重启应用
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
5.配置 WebHooks
5.1 Gitlab WebHooks 配置方法:
5.2 Github WebHooks 配置方法:
5.3 Gitee(码云) WebHooks 配置方法:
可点击测试按钮,测试一下钩子是否能访问。(如果失败,可更换内网穿透软件,比如natapp等)
配置成功后,会在每次 push 代码后,都会给远程 HTTP URL 发送一个 POST 请求。
上面的URL就需要使用到内网穿透获取到的域名了,url为内网穿透域名/monitor
6.修改git仓库上client-dev.yml的env改为test123,通过ConfigClient获取env已更新为test123,已实现自动刷新
好叻,搞定,下节继续!
如果觉得不错,分享给你的朋友!
THANDKS
- End -
一个立志成大腿而每天努力奋斗的年轻人
伴学习伴成长,成长之路你并不孤单!
「 从0到1学习微服务SpringCloud 」06 统一配置中心Spring Cloud Config的更多相关文章
- 「 从0到1学习微服务SpringCloud 」10 服务网关Zuul
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」06 统一配置中心Spring Cloud Config 「 从0到1学习微服务SpringCloud 」07 RabbitM ...
- 「 从0到1学习微服务SpringCloud 」09 补充篇-maven父子模块项目
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」06 统一配置中心Spring Cloud Config 「 从0到1学习微服务SpringCloud 」07 RabbitM ...
- 「 从0到1学习微服务SpringCloud 」08 构建消息驱动微服务的框架 Spring Cloud Stream
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...
- 「 从0到1学习微服务SpringCloud 」07 RabbitMq的基本使用
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...
- 「 从0到1学习微服务SpringCloud 」05服务消费者Fegin
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...
- 「 从0到1学习微服务SpringCloud 」04服务消费者Ribbon+RestTemplate
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 「 从0到1学习微服务S ...
- 「 从0到1学习微服务SpringCloud 」03 Eureka的自我保护机制
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现 Eureka的高可用需要 ...
- 「 从0到1学习微服务SpringCloud 」02 Eureka服务注册与发现
系列文章(更新ing): 「 从0到1学习微服务SpringCloud 」01 一起来学呀! Spring Cloud Eureka 基于Netflix Eureka做了二次封装(Spring Clo ...
- 「 从0到1学习微服务SpringCloud 」01 一起来学呀!
有想学微服务的小伙伴没?一起来从0开始学习微服务SpringCloud,我会把学习成果总结下来,供大家参考学习,有兴趣可以一起来学!如有错误,望指正! Spring .SpringBoot.Sprin ...
随机推荐
- H3C配置历史命令缓冲大小--接口视图(console为准)
[wang]user-interface console 0 [wang-ui-console0]history-command max-size 30 //配置缓冲区大小 [wang-ui-c ...
- FreeNOS学习3——了解目录结构和Main函数
下载源码的文件夹,看到里面有以下内容: 解释一下里面都有啥 lib -> 共享代码 1.libfs 文件系统(磁盘管理) 2.liballoc 内存分配和虚拟内存映射(内存管理) 3.libex ...
- LeetCode20_Valid Parentheses有效的括号(栈相关问题)
题目: 给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合.左括号必须以正确的顺序闭合.注意空字符串可 ...
- VRChat模型制作及上传总篇(201912)
1.视频资源及软件插件模型资源: Kodoku-shi :https://www.bilibili.com/video/av20097333(自己目前找到讲的最详细的教程,但是blender减免部分貌 ...
- lambda应用
def test(a, b, func): result = func(a, b) print(result) test(10, 15, lambda x, y: x + y) #coding=utf ...
- TCP/IP||ICMP
1.概述 ICMP为IP组成部分之一,传递差错报文并返回用户进程,在IP数据报内部被传输 类型字段可以有15个不同的值,以描述特定类型的ICMP报文,检验和字段覆盖整个ICMP报文. 2.报文类型 在 ...
- MyISAM与InnoDB的索引实现区别
一 MyISAM索引实现 1. 主键索引 MyISAM引擎使用B+树作为索引结果,叶节点的data域存放的是数据记录的地址.下图为MyISAM表的主索引,Col1为主键. 2. 辅助索引 在MyISA ...
- Tangram: Optimized Coarse-Grained Dataflow for Scalable NN Accelerators 阅读笔记
目录 Tangram: Optimized Coarse-Grained Dataflow for Scalable NN Accelerators 1.Abstract 2.Introduction ...
- 从头学pytorch(十二):模型保存和加载
模型读取和存储 总结下来,就是几个函数 torch.load()/torch.save() 通过python的pickle完成序列化与反序列化.完成内存<-->磁盘转换. Module.s ...
- 【题解/模板】P1248 加工生产调度(贪心)
[题解/模板]P1248 加工生产调度(贪心) 分析: \(A\)流水线的时间是确定的,所以现在就是要让\(b\)的时间尽量短 \(tB > tA\),除非所有东西都不需要\(b\).(t指结束 ...