內容概述
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的名稱。後面配置需要使用。
三、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進行測試。
五、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
#配置rabbitmq信息 rabbitmq: host: 192.168.200.129 port: 5672 username: guest password: guest virtual-host: /
注意位置:是spring節點的屬性
-
在config-server中設置暴露監控斷點:bus-refresh
是通過actuator來實現,添加對actuator的依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
暴漏endpoint的配置
# 暴露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); } }
-
編寫配置,定義 binder,和 bingings
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
-
訪問zipkin web界面。 http://localhost:9411/
-
在服務提供方和消費方分別引入 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>
zipkin依賴了sleuth,添加了zipkin,兩個就都有了。
-
分別配置服務提供方和消費方。
sleuth-provider
spring: application: name: sleuth-provider zipkin: base-url: http://localhost:9411/ # 設置zipkin的服務端路徑 sleuth: sampler: probability: 1 # 採集率 默認 0.1 百分之十。
sleuth-consumer
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/
調用花費時間:
調用的請求:
請求的細節信息:
服務之間依賴的關係