SpringCloud 基礎 Hystrix、Zuul、Config
springcloud 基礎項目 github
https://github.com/ybsdegit/SpringCloud
服務熔斷 Hystrix (斷路器)服務端
-
服務雪崩
對於高流量的應用,單一的後端依賴坑你導致所有服務在幾秒內飽和
我們需要 棄車保帥 -
Hystrix
避免級聯故障,提高分佈式系統的彈性
備選響應 -
服務熔斷
熔斷機制是應對雪崩效應的一種微服務鏈路保護機制當鏈路某個微服務不可用獲取響應時間太長時,會進行服務的降級,進而熔斷該節點微服務的調用,快速返回錯誤的響應信息
服務降級 Hystrix (客戶端)
- 服務熔斷
- 服務端
- 某個服務超時或者異常,引起熔斷
- 服務降級
- 客戶端
- 從整體網站請求負載考慮 當某個服務熔斷或者關閉之後,服務將不被調用
- 此時客戶端可以準備一個 FallbackFactory, 返回一個默認的值(缺省值)
- 整體服服務水平下降了。好歹能用,比直接掛掉強
@SpringBootApplication
@EnableEurekaClient // 在服務啓動後自動註冊到eureka中
@EnableDiscoveryClient // 服務發現
@EnableCircuitBreaker // 添加對熔斷的支持 斷路器
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
Zuul 網關
- 實現外部訪問統一入口
- 過濾
- Zuul 和 Eureka 整合
zuul 配置
server:
port: 9527
spring:
application:
name: springcloud-zuul
# Eureka 配置,服務註冊地址
eureka:
client:
service-url:
defaultZone: http://localhost:7001/eureka/
instance:
instance-id: springcloud-zuul-9527 # 修改Eureka上的默認,描述信息
prefer-ip-address: true # true 可以顯示ip地址
info:
app.name: paulson-springcloud
company.name: ybs
zuul:
routes:
mydept.serviceId: springcloud-provider-dept
mydept.path: /mydept/**
ignored-services: springcloud-provider-dept # 不能再使用這個路徑訪問了
prefix: /ybs # 公共的訪問前綴
# ignored-services: * # 隱藏全部
Spring Cloud Config
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
server:
port: 3344
spring:
application:
name: springcloud-config-server
cloud:
config:
server:
git:
uri: https://gitee.com/paulsonwier/springcloud-config.git
# 通過config-server可以連接到git訪問其中的資源和配置