先写个题外话,今晚是怎么了,就因为我的阿里云快到期了让我续费,把我网站搞成502了。
怎么把我网站搞崩了,不行今晚搞一搞,不能总宕机启动服务。
我的nginx、我的tomcat、我的redis、我的rabbitmq全都得重启。
本章学习目标:本节课是Spring Cloud微服务实战课的最后一讲后续就要讲解容器docker学习。
学习目标:SpringCloudConfig组件使用
SpringCloudBus消息组件使用
2大组件在实际项目中的应用
1集中配置组件SpringCloudConfig
1.1spring Cloud config简介
在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。在spring cloud config 组件中,分两个角色,一是config server,二是configclient。Config Server是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用Git存储配置文件内容,也可以使用SVN存储,或者是本地文件存储。
Config Client是Config Server的客户端,用于操作存储在Config Server中的配置内容。微服务在启动时会请求Config Server获取配置文件的内容,请求到后再启动容器。
1.2配置服务端
1.2.1首先码云上建立shop_config仓库,将shop_product的application.yml 改名为:product-dev.yml,见下图
1.2.2配置shop商城项目配置中心微服务
(1)创建工程模块 配置中心微服务 shop_config ,pom.xml引入依赖
1. <dependencies>
2. <dependency>
3. <groupId>org.springframework.cloud</groupId>
4. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
5. </dependency>
6. <!--微服务配置中心-->
7. <dependency>
8. <groupId>org.springframework.cloud</groupId>
9. <artifactId>spring-cloud-config-server</artifactId>
10. </dependency>
11. </dependencies>
(2)创建启动类ConfigServerApplication
1. package com.shop.config;
2. import org.springframework.boot.SpringApplication;
3. import org.springframework.boot.autoconfigure.SpringBootApplication;
4. import org.springframework.cloud.config.server.EnableConfigServer;
5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
6. @EnableConfigServer //开启配置服务
7. @SpringBootApplication
8. @EnableEurekaClient
9. public class ConfigServerApplication {
10. public static void main(String[] args) {
11. SpringApplication.run(ConfigServerApplication.class, args);
12. }
13.
14. }
(3)编写配置文件application.yml
1. server:
2. port: 1200
3. spring:
4. application:
5. name: shop-config #指定服务名
6. cloud:
7. config:
8. server:
9. git:
10. uri: https://gitee.com/LRyab/shop_config.git
11. eureka:
12. client:
13. service‐url:
14. defaultZone: http://localhost:9898/eureka
15. instance:
16. prefer‐ip‐address: true
(4)浏览器测试:http://localhost:1200/dev/product-dev.yml 可以看到配置内容
1.2.3Shop商城中心配置客户端
(1) 在shop_product工程添加依赖
49. <dependency>
50. <groupId>org.springframework.cloud</groupId>
51. <artifactId>spring-cloud-starter-config</artifactId>
52. </dependency>
(2)添加bootstrap.yml ,删除application.yml
1. spring:
2. cloud:
3. config:
4. name: product
5. profile: dev
6. label: dev
7. uri: http://127.0.0.1:1200
(3)测试: 启动工程shop_eureka shop_config shop_product,看是
否可以正常运行.
看一下是否正常:相当于通过配置中心远程去加载product微服务的yml配置。
127.0.0.1::7002/product
请求数据没问题:
1.2.4spring Cloud作用
将所有微服务的配置文件集中到码云公共仓库上去处理,便于配置文件的集中管理。开发的配置中心和配置客户端实质是客户端通过一定的规则去远程仓库上读取数据信息。
2消息总线组件SpringCloudBus
2.1SpringCloudBus简介
如果我们更新码云中的配置文件,那客户端工程是否可以及时接受新的配置信息呢?我们现在来做有一个测试,修改一下码云中的配置文件中mysql的端口 ,然后测试http://localhost:7002/product 数据依然可以查询出来,证明修改服务器中的配置并没有更新立刻到工程,只有重新启动程序才会读取配置。 那我们如果想在不重启微服务的情况下更新配置如何来实现呢? 我们使用SpringCloudBus来实现配置的自动更新。
2.2 SHOP代码实战
配置客户端
测试
修改码云:数据库为3306还是不行
然后我去postman请求一下
不用改工程:配置文件更新生效了
3个人说明
经验是由一点一点积累的,思维也是由一天一天训练出来的。
谢谢大家的阅读,原创不易,如果你认为文章对你有所帮助,就点个赞感谢大家支持,你的点赞是我持续写作的动力。