diff --git a/labx-08/labx-08-sc-gateway-demo07-hystrix/pom.xml b/labx-08/labx-08-sc-gateway-demo07-hystrix/pom.xml new file mode 100644 index 00000000..2d6ee395 --- /dev/null +++ b/labx-08/labx-08-sc-gateway-demo07-hystrix/pom.xml @@ -0,0 +1,64 @@ + + + + labx-08 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + labx-08-sc-gateway-demo07-hystrix + + + 2.2.4.RELEASE + Hoxton.SR1 + 2.2.0.RELEASE + + + + + + + org.springframework.boot + spring-boot-starter-parent + ${spring.boot.version} + pom + import + + + org.springframework.cloud + spring-cloud-dependencies + ${spring.cloud.version} + pom + import + + + com.alibaba.cloud + spring-cloud-alibaba-dependencies + ${spring.cloud.alibaba.version} + pom + import + + + + + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + + + org.springframework.cloud + spring-cloud-starter-netflix-hystrix + + + + diff --git a/labx-08/labx-08-sc-gateway-demo07-hystrix/src/main/java/cn/iocoder/springcloud/labx08/gatewaydemo/GatewayApplication.java b/labx-08/labx-08-sc-gateway-demo07-hystrix/src/main/java/cn/iocoder/springcloud/labx08/gatewaydemo/GatewayApplication.java new file mode 100644 index 00000000..f9575ef3 --- /dev/null +++ b/labx-08/labx-08-sc-gateway-demo07-hystrix/src/main/java/cn/iocoder/springcloud/labx08/gatewaydemo/GatewayApplication.java @@ -0,0 +1,13 @@ +package cn.iocoder.springcloud.labx08.gatewaydemo; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class GatewayApplication { + + public static void main(String[] args) { + SpringApplication.run(GatewayApplication.class, args); + } + +} diff --git a/labx-08/labx-08-sc-gateway-demo07-hystrix/src/main/java/cn/iocoder/springcloud/labx08/gatewaydemo/controller/FallbackController.java b/labx-08/labx-08-sc-gateway-demo07-hystrix/src/main/java/cn/iocoder/springcloud/labx08/gatewaydemo/controller/FallbackController.java new file mode 100644 index 00000000..ad97a80c --- /dev/null +++ b/labx-08/labx-08-sc-gateway-demo07-hystrix/src/main/java/cn/iocoder/springcloud/labx08/gatewaydemo/controller/FallbackController.java @@ -0,0 +1,24 @@ +package cn.iocoder.springcloud.labx08.gatewaydemo.controller; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.server.ServerWebExchange; + +@RestController +public class FallbackController { + + private Logger logger = LoggerFactory.getLogger(FallbackController.class); + + @GetMapping("/fallback") + public String fallback(ServerWebExchange exchange) { +// URI requestUrl = exchange.getAttribute(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR); + Throwable executionException = exchange.getAttribute(ServerWebExchangeUtils.HYSTRIX_EXECUTION_EXCEPTION_ATTR); + logger.error("[fallback][发生异常]", executionException); + + return "服务降级..." + executionException.getMessage(); + } + +} diff --git a/labx-08/labx-08-sc-gateway-demo07-hystrix/src/main/resources/application.yaml b/labx-08/labx-08-sc-gateway-demo07-hystrix/src/main/resources/application.yaml new file mode 100644 index 00000000..cbe1dba3 --- /dev/null +++ b/labx-08/labx-08-sc-gateway-demo07-hystrix/src/main/resources/application.yaml @@ -0,0 +1,21 @@ +server: + port: 8888 + +spring: + application: + name: gateway-application + + cloud: + # Spring Cloud Gateway 配置项,对应 GatewayProperties 类 + gateway: + # 路由配置项,对应 RouteDefinition 数组 + routes: + - id: hystrix_test + uri: http://127.0.0.1:18181 + predicates: + - Path=/** + filters: + - name: Hystrix + args: + name: fallbackcmd # 对应的 Hystrix Command 名字 + fallbackUri: forward:/fallback # 处理 Hystrix fallback 的情况,重定向到指定地址 diff --git a/labx-08/pom.xml b/labx-08/pom.xml index c3d84b13..fe37468f 100644 --- a/labx-08/pom.xml +++ b/labx-08/pom.xml @@ -24,6 +24,8 @@ labx-08-sc-gateway-demo06-rate-limiter + labx-08-sc-gateway-demo07-hystrix + labx-08-sc-user-service