在學習或使用Zuul的時候我們可以進行服務的熔斷處理,比如通過zuul去訪問呢A服務,這是A服務並沒有開啓服務或者因其他的異常導致不發提供服務時,這是我們調用肯定會有異常,這時就必須進行服務的熔斷處理!如果還不太瞭解Gateway的一些常用配置的可以看上一篇張SpringCloudGateway配置介紹
一.創建項目
文末有源代碼地址哦
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
二.配置文件
server:
port: 7000
spring:
application:
name: javayh-gateway
main:
allow-bean-definition-overriding: true #當遇到同樣名字的時候,是否允許覆蓋註冊
profiles:
active: gateway
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
cluster-name: javayh-nacos
config:
group: javayh
server-addr: 127.0.0.1:8848
prefix: javayh-gateway
file-extension: yml
gateway:
discovery:
locator:
enabled: true
# 服務名小寫
lower-case-service-id: true
routes:
- id: api-javayh-demo
# lb代表從註冊中心獲取服務,且已負載均衡方式轉發
uri: lb://javayh-demo
predicates:
- Path=/demo/**
# 加上StripPrefix=1,否則轉發到後端服務時會帶上demo前綴
filters:
- StripPrefix=1
# 服務降級 觸發調用 HystrixFallbackHandler GatewayFallbackConfig
- name: Hystrix
args:
name: default
fallbackUri: 'forward:/defaultfallback'
三.定義 HystrixFallback
@Configuration
public class GatewayFallbackConfig {
@Autowired
private HystrixFallbackHandler hystrixFallbackHandler;
@Bean
public RouterFunction routerFunction() {
return RouterFunctions.route(
RequestPredicates.GET("/defaultfallback")
.and(RequestPredicates.accept(MediaType.TEXT_PLAIN)), hystrixFallbackHandler);
}
}
@Slf4j
@Component
public class HystrixFallbackHandler implements HandlerFunction<ServerResponse> {
@Override
public Mono<ServerResponse> handle(ServerRequest serverRequest) {
serverRequest.attribute(ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR)
.ifPresent(originalUrls -> log.error("網關執行請求:{}失敗,hystrix服務降級處理", originalUrls));
return ServerResponse
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.contentType(MediaType.APPLICATION_JSON_UTF8)
.body(BodyInserters.fromObject(ResultData.fail("HystrixFallbackHandler")));
}
}
四.驗證
此時啓動我的註冊中心(本文使用的時nacos),啓動gateway服務,訪問A服務的請求路徑 如: http://localhost:7000/demo/javayh/demo/gateway
這是沒有A服務我們即可驗證配置是否生效
根據訪問的效果,服務熔斷已經實現!msg內是我們的提示信息!
項目源代碼,已上傳到Github上 ,歡迎star; https://github.com/Dylan-haiji/javayh-platform/tree/master/javayh-route/javayh-api-gateway
本文的分享暫時就到這裏,希望對您有所幫助
關注 Java有貨領取更多資料
聯繫小編。微信:372787553,帶您進羣互相學習
左側小編微信,右側獲取免費資料
- SpringCloud 自定義封裝架構https://github.com/Dylan-haiji/javayh-platform
- Java 設計模式學習代碼 https://github.com/Dylan-haiji/design-pattern
- SpringCloud學習代碼: https://github.com/Dylan-haiji/javayh-cloud
- AlibabaCloud學習代碼:https://github.com/Dylan-haiji/javayh-cloud-nacos
- SpringBoot+Mybatis 多數據源切換:https://github.com/Dylan-haiji/javayh-boot-data-soure
- Redis、Mongo、Rabbitmq、Kafka學習代碼: https://github.com/Dylan-haiji/javayh-middleware
- SpringBoot+SpringSecurity實現自定義登錄學習代碼:https://github.com/Dylan-haiji/javayh-distribution