内容概述
Config 分布式配置中心
Bus 消息总线
Stream 消息驱动
Sleuth+Zipkin 链路追踪
一、config-概述
疑问:什么是springcloud的config,有什么用?
分析:
springcloud用来管理分布式微服务的配置文件。
• Spring Cloud Config 解决了在分布式场景下多环境配置文件的管理和维护。
• 好处:
• 集中管理配置文件
• 不同环境不同配置,动态化的配置更新
• 配置信息改变时,不需要重启即可更新配置信息到服务
二、config-快速入门-gitee搭建远程仓库
为了让微服务的各个应用都能获取到配置,我们将配置放在云端,创建基于码云gitee,搭建的远程git代码仓库。
该仓库就是为了放置配置文件,和我们之前搭建的git代码仓库无不同。
config有两部分组成:
1. config server :通过远程的git代码仓库,管理配置文件。
2. config client : 就是之前开发的springcloud的各种需要进行配置的应用,连接config server获取配置文件。
操作:
-
登录码云
https://gitee.com/
- 创建远程仓库
- 使用客户端连接远程仓库
-
上传配置文件到仓库
配置文件的名称要求: 带有-,前面是配置名称,后面是profile的名称。后面配置需要使用。
SpringCloud(三、Config、Bus、Stream 、Sleuth+Zipkin)
三、config-快速入门-config server搭建
搭建config server应用。
操作步骤:
- 创建spring cloud模块
- 添加依赖
<dependencies> <!-- config-server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> </dependencies>
- 编写启动类,添加启用config server注解
@SpringBootApplication @EnableConfigServer // 启用config server功能 public class ConfigServerApp { public static void main(String[] args) { SpringApplication.run(ConfigServerApp.class,args); } }
- 配置文件
server: port: 9527 spring: application: name: config-server # spring cloud config cloud: config: server: # git 的 远程仓库地址 git: uri: https://gitee.com/itheima_cch/itheima-configs.git label: master # 分支配置
-
访问config server
http://localhost:9527/master/config-dev.yml
说明: master是分支名。
config-dev.yml是仓库中的文件名
四、config-快速入门-config client搭建
搭建config client应用。需要配置文件的应用都是客户端。这里有config-provider,config-consumer。
案例中以config-provider应用为例。
- 在config-provider应用中,添加依赖
<!--config client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
-
在应用中新建配置文件:bootstrap.yml
bootstrap.yml也是spring boot的默认配置文件,并且优先级要比application.yml高。
- 配置bootstrap.yml
# 配置config-server地址 # 配置获得配置文件的名称等信息 spring: cloud: config: # 配置config-server地址 uri: http://localhost:9527 # 配置获得配置文件的名称等信息 name: config # 文件名 profile: dev # profile指定, config-dev.yml label: master # 分支
- 编写controller,获取配置文件中的数据
- 启动eureka-server-config和config-provider进行测试。
SpringCloud(三、Config、Bus、Stream 、Sleuth+Zipkin)
五、config-快速入门-config client刷新
spring cloud config可以在git仓库中配置文件内容发生变化的时候,不需要重启就能获取新的配置文件。
服务端config server: 无需做任何设置,当git中的配置文件修改后,自动获取最新配置文件。
客户端config client: config-provider默认不会获取最新配置文件
config client刷新操作步骤:
- 在 config 客户端引入 actuator 依赖【案例已经添加】
- 获取配置信息类上,添加 @RefreshScope 注解
@RestController @RequestMapping("/goods") @RefreshScope // 开启刷新功能 public class GoodsController {
-
添加配置 bootstrap.yml
management.endpoints.web.exposure.include: refresh
management:
endpoints:
web:
exposure:
include: '*' # 暴漏的endpoint,*表示所有
-
使用curl工具发送post请求
curl -X POST http://localhost:8001/actuator/refresh
六、config-快速入门-集成Eureka
疑问: 客户端配置config server的uri是固定写死的地址,我们需要从注册中心中获取。
将config server注册到Eureka中,config client从注册中心通过application name获取config server的地址。
-
config server服务端配置
添加eureka客户端坐标
配置文件(已经添加)<!-- eureka-client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
spring: application: name: config-server
-
config client客户端配置,config-provider
bootstrap.yml
spring: cloud: config: # 配置config-server地址 #uri: http://localhost:9527 # 配置获得配置文件的名称等信息 name: config # 文件名 profile: dev # profile指定, config-dev.yml label: master # 分支 discovery: enabled: true service-id: config-server
七、Bus 消息总线-概述
疑问:
通过config server我们远程统一管理配置文件,但是当配置发生变化的时候,我们需要依次的执行:
http://localhost:8001/actuator/refresh 进行应用配置的刷新,不太方便
什么是bug总线?
• Spring Cloud Bus 是用轻量的消息中间件将分布式的节点连接起来,可以用于广播配置文件的更改或者服务的监控管理。关键的思想就是,消息总线可以为微服务做监控,也可以实现应用程序之间相通信。
• Spring Cloud Bus 可选的消息中间件包括 RabbitMQ 和 Kafka 。
为什么要用bus?
当config server中的配置文件发生变化,通过bus通知所有的客户端应用,让他们自己去刷新,达到只需要发生一个命令,所有连接在config server中的客户端都可以刷新配置的作用。
八、Bus 消息总线-快速入门
config-server通过bus发送消息
config-client通过bus接收消息
准备工作:
因为bus总线可以让多个客户端同时更新,所有我们将config-consumer也作为一个config-client。
config-consumer模块。
-
<!--config client --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>
-
进行配置
直接复制config-provider\src\main\resources\bootstrap.yml文件,无需修改。
- 在controller上添加刷新注解,并使用配置信息
@RestController @RequestMapping("/order") @RefreshScope public class OrderController { @Value("${itheima}") private String itheima; @Autowired private GoodsFeignClient goodsFeignClient; @GetMapping("/goods/{id}") public Goods findGoodsById(@PathVariable("id") int id){ Goods goods = goodsFeignClient.findGoodsById(id); goods.setTitle(goods.getTitle()+"--"+itheima); return goods; } }
- 访问测试
访问1会返回降级的信息,就不会出现两次配置信息。
使用BUS操作步骤:
- 分别在 config-server 和 config-client中引入 bus依赖:bus-amqp
<!-- bus --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency>
-
分别在 config-server 和 config-client中配置 RabbitMQ(3个模块都配置)
bootstrap.yml和config-server的application.yml
注意位置:是spring节点的属性#配置rabbitmq信息 rabbitmq: host: 192.168.200.129 port: 5672 username: guest password: guest virtual-host: /
-
在config-server中设置暴露监控断点:bus-refresh
是通过actuator来实现,添加对actuator的依赖
暴漏endpoint的配置<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
# 暴露bus的刷新端点 management: endpoints: web: exposure: include: 'bus-refresh'
-
启动测试
访问的是config-server的刷新节点:
curl -X POST http://localhost:9527/actuator/bus-refresh
九、Stream消息驱动-概述
疑问:什么是Stream消息驱动?有什么用?
• Spring Cloud Stream 是一个构建消息驱动微服务应用的框架。
• Stream 解决了开发人员无感知的使用消息中间件的问题,因为Stream对消息中间件的进一步封装,可以做到代码层面对中间件的无感知,甚至于动态的切换中间件,使得微服务开发的高度解耦,服务可以关注更多自己的业务流程。
• Spring Cloud Stream目前支持两种消息中间件RabbitMQ和Kafka
Stream消息驱动有什么用?
和具体的消息中间件解耦
十、Stream消息驱动-组件
• Spring Cloud Stream 构建的应用程序与消息中间件之间是通过绑定器 Binder相关联的。绑定器对于应用程序而言起到了隔离作用, 它使得不同消息中间件的实现细节对应用程序来说是透明的。
组件:
绑定器 Binder
动作:
• binding 是我们通过配置把应用和spring cloud stream 的 binder 绑定在一起
• output:发送消息 Channel,内置 Source接口
• input:接收消息 Channel,内置 Sink接口
十一、Stream消息驱动-消息生产者
疑问: 如何发送消息?
- 创建消息生产者模块,引入依赖 starter-stream-rabbit
<dependencies> <!--spring boot web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- stream --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency> </dependencies>
- 编写启动类。
@SpringBootApplication public class ProducerApp { public static void main(String[] args) { SpringApplication.run(ProducerApp.class,args); } }
- 编写配置,定义 binder,和 bingings
server: port: 8000 spring: cloud: stream: # 定义绑定器,绑定到哪个消息中间件上 binders: itheima_binder: # 自定义的绑定器名称 type: rabbit # 绑定器类型 environment: # 指定mq的环境 spring: rabbitmq: host: localhost port: 5672 username: guest password: guest virtual-host: / bindings: output: # channel名称 binder: itheima_binder #指定使用哪一个binder destination: itheima_exchange # 消息目的地,交换机的名称
配置两个方面:
binders:主要就是操作的MQ的消息队列信息(必配)
bindings: 生产者就是发送消息的信息(生产者配置output)
- 定义消息发送业务类。添加 @EnableBinding(Source.class),注入MessageChannel output ,完成消息发送
import org.springframework.cloud.stream.messaging.Source;
@Component
@EnableBinding(Source.class)
public class MessageProducer {
@Autowired
private MessageChannel output;
public void send(){
String msessage = "hello stream~~~";
//发送消息
output.send(MessageBuilder.withPayload(msessage).build());
System.out.println("消息发送成功~~~");
}
}
通过controller发送消息
@RestController
public class ProducerController {
@Autowired
private MessageProducer producer;
@RequestMapping("/send")
public String sendMsg(){
producer.send();
return "success";
}
}
-
测试
http://localhost:8000/send
十二、Stream消息驱动-消息消费者
疑问: 如何接收消息?
- 创建消息消费者模块,引入依赖 starter-stream-rabbit
<dependencies> <!--spring boot web--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- stream --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-rabbit</artifactId> </dependency> </dependencies>
-
@SpringBootApplication public class ConsumerApp { public static void main(String[] args) { SpringApplication.run(ConsumerApp.class,args); } }
-
server: port: 9000 spring: cloud: stream: # 定义绑定器,绑定到哪个消息中间件上 binders: itheima_binder: # 自定义的绑定器名称 type: rabbit # 绑定器类型 environment: # 指定mq的环境 spring: rabbitmq: host: localhost port: 5672 username: guest password: guest virtual-host: / bindings: input: # channel名称 binder: itheima_binder #指定使用哪一个binder destination: itheima_exchange # 消息目的地
-
定义消息接收业务类。添加 @EnableBinding(Sink.class),使用
@StreamListener(Sink.INPUT),完成消息接收。
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;
/**
* 消息接收类
*/
@EnableBinding({Sink.class})
@Component
public class MessageListener {
@StreamListener(Sink.INPUT)
public void receive(Message message){
System.out.println(message);
System.out.println(message.getPayload());
}
}
十三、Sleuth+Zipkin 链路追踪-概述
疑问:微服务调用复杂,出现问题如何发现?
链路追踪是什么?干吗用?
• Spring Cloud Sleuth 其实是一个工具,它在整个分布式系统中能跟踪一个用户请求的过程,捕获这些跟踪数据,就能构建微服务的整个调用链的视图,这是调试和监控微服务的关键工具。
• 耗时分析
• 可视化错误
• 链路优化
• Zipkin 是 Twitter 的一个开源项目,它致力于收集服务的定时数据,以解决微服务架构中的延迟问题,包括数据的收集、存储、查找和展现。
总结:
Sleuth 收集spring cloud调用中的数据
Zipkin 图形化的展示收集的数据
十四、Sleuth+Zipkin 链路追踪-快速入门
分析:搭建Sleuth+Zipkin 链路追踪。Sleuth是个依赖,直接添加。Zipkin 是开源项目,需要启动应用。
- 安装启动zipkin。 java –jar zipkin.jar
SpringCloud(三、Config、Bus、Stream 、Sleuth+Zipkin) - 访问zipkin web界面。 http://localhost:9411/
SpringCloud(三、Config、Bus、Stream 、Sleuth+Zipkin) - 在服务提供方和消费方分别引入 sleuth 和 zipkin 依赖
zipkin依赖了sleuth,添加了zipkin,两个就都有了。<!-- sleuth-zipkin --> <!--<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zipkin</artifactId> </dependency>
-
分别配置服务提供方和消费方。
sleuth-provider
sleuth-consumerspring: application: name: sleuth-provider zipkin: base-url: http://localhost:9411/ # 设置zipkin的服务端路径 sleuth: sampler: probability: 1 # 采集率 默认 0.1 百分之十。
spring: application: name: sleuth-consumer # 设置当前应用的名称。将来会在eureka中Application显示。将来需要使用该名称来获取路径 zipkin: base-url: http://localhost:9411/ # 设置zipkin的服务端路径 sleuth: sampler: probability: 1 # 采集率 默认 0.1 百分之十。 logging: level: com.itheima: debug
-
启动,测试
http://localhost:9411/
调用花费时间:
调用的请求: