diff --git a/labx-08/labx-08-sc-gateway-demo07-sentinel/src/main/java/cn/iocoder/springcloud/labx08/gatewaydemo/GatewayApplication.java b/labx-08/labx-08-sc-gateway-demo07-sentinel/src/main/java/cn/iocoder/springcloud/labx08/gatewaydemo/GatewayApplication.java index 1ba74349..1799fc89 100644 --- a/labx-08/labx-08-sc-gateway-demo07-sentinel/src/main/java/cn/iocoder/springcloud/labx08/gatewaydemo/GatewayApplication.java +++ b/labx-08/labx-08-sc-gateway-demo07-sentinel/src/main/java/cn/iocoder/springcloud/labx08/gatewaydemo/GatewayApplication.java @@ -1,5 +1,7 @@ package cn.iocoder.springcloud.labx08.gatewaydemo; +import com.alibaba.cloud.sentinel.gateway.ConfigConstants; +import com.alibaba.csp.sentinel.config.SentinelConfig; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -7,8 +9,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; public class GatewayApplication { public static void main(String[] args) { -// System.setProperty(SentinelConfig.APP_TYPE, ConfigConstants.APP_TYPE_SCG_GATEWAY); -// System.setProperty(SentinelConfig.APP_TYPE, "1"); + System.setProperty(SentinelConfig.APP_TYPE, ConfigConstants.APP_TYPE_SCG_GATEWAY); // 【重点】设置应用类型为 Spring Cloud Gateway SpringApplication.run(GatewayApplication.class, args); } diff --git a/labx-21/labx-21-sc-zuul-demo07-hystrix/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/fallback/ApiFallbackProvider.java b/labx-21/labx-21-sc-zuul-demo07-hystrix/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/fallback/ApiFallbackProvider.java index b877c026..9da8b550 100644 --- a/labx-21/labx-21-sc-zuul-demo07-hystrix/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/fallback/ApiFallbackProvider.java +++ b/labx-21/labx-21-sc-zuul-demo07-hystrix/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/fallback/ApiFallbackProvider.java @@ -8,7 +8,6 @@ import org.springframework.http.client.ClientHttpResponse; import org.springframework.stereotype.Component; import java.io.ByteArrayInputStream; -import java.io.IOException; import java.io.InputStream; @Component @@ -21,26 +20,26 @@ public class ApiFallbackProvider implements FallbackProvider { public ClientHttpResponse fallbackResponse(String route, final Throwable cause) { return new ClientHttpResponse() { - public HttpStatus getStatusCode() throws IOException { + public HttpStatus getStatusCode() { return HttpStatus.OK; } - public int getRawStatusCode() throws IOException { + public int getRawStatusCode() { return HttpStatus.OK.value(); } - public String getStatusText() throws IOException { - return "OK"; + public String getStatusText() { + return HttpStatus.OK.getReasonPhrase(); } public void close() {} - public InputStream getBody() throws IOException { + public InputStream getBody() { // 响应内容 String bodyText = String.format("{\"code\": 500,\"message\": \"Service unavailable:%s\"}", cause.getMessage()); return new ByteArrayInputStream(bodyText.getBytes()); } - public HttpHeaders getHeaders() { + public HttpHeaders getHeaders() { // 响应头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); // json 返回 return headers; diff --git a/labx-21/labx-21-sc-zuul-demo07-sentinel/pom.xml b/labx-21/labx-21-sc-zuul-demo07-sentinel/pom.xml new file mode 100644 index 00000000..10261cd3 --- /dev/null +++ b/labx-21/labx-21-sc-zuul-demo07-sentinel/pom.xml @@ -0,0 +1,74 @@ + + + + labx-21 + cn.iocoder.springboot.labs + 1.0-SNAPSHOT + + 4.0.0 + + labx-21-sc-zuul-demo07-sentinel + + + 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-netflix-zuul + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + com.alibaba.cloud + spring-cloud-starter-alibaba-sentinel + + + com.alibaba.cloud + spring-cloud-alibaba-sentinel-gateway + + + + diff --git a/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/CustomBlockFallbackProvider.java b/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/CustomBlockFallbackProvider.java new file mode 100644 index 00000000..c10c6974 --- /dev/null +++ b/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/CustomBlockFallbackProvider.java @@ -0,0 +1,31 @@ +package cn.iocoder.springcloud.labx21.zuuldemo; + +import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.BlockResponse; +import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackManager; +import com.alibaba.csp.sentinel.adapter.gateway.zuul.fallback.ZuulBlockFallbackProvider; +import com.alibaba.csp.sentinel.slots.block.BlockException; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; + +@Component +public class CustomBlockFallbackProvider implements ZuulBlockFallbackProvider { + + @PostConstruct + public void init() { + ZuulBlockFallbackManager.registerProvider(this); + } + + public String getRoute() { + return "*"; + } + + public BlockResponse fallbackResponse(String route, Throwable cause) { + if (cause instanceof BlockException) { + return new BlockResponse(429, "你被 Block 啦!", route); + } else { + return new BlockResponse(500, "系统异常", route); + } + } + +} diff --git a/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/ZuulApplication.java b/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/ZuulApplication.java new file mode 100644 index 00000000..3cbb34fc --- /dev/null +++ b/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/java/cn/iocoder/springcloud/labx21/zuuldemo/ZuulApplication.java @@ -0,0 +1,18 @@ +package cn.iocoder.springcloud.labx21.zuuldemo; + +import com.alibaba.cloud.sentinel.gateway.ConfigConstants; +import com.alibaba.csp.sentinel.config.SentinelConfig; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; + +@SpringBootApplication +@EnableZuulProxy // 开启 Zuul 网关 +public class ZuulApplication { + + public static void main(String[] args) { + System.setProperty(SentinelConfig.APP_TYPE, ConfigConstants.APP_TYPE_ZUUL_GATEWAY); // 【重点】设置应用类型为 Spring Cloud Gateway + SpringApplication.run(ZuulApplication.class, args); + } + +} diff --git a/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/resources/application.yaml b/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/resources/application.yaml new file mode 100644 index 00000000..c9d01992 --- /dev/null +++ b/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/resources/application.yaml @@ -0,0 +1,42 @@ +server: + port: 8888 + +spring: + application: + name: zuul-application + + cloud: + nacos: + # Nacos 作为注册中心的配置项 + discovery: + server-addr: 127.0.0.1:8848 # Nacos 服务器地址 + + sentinel: + eager: true # 是否饥饿加载。默认为 false 关闭 + transport: + dashboard: localhost:7070 # 是否饥饿加载。默认为 false 关闭 + # # 数据源的配置项 + # datasource: + # ds1.file: + # file: "classpath: sentinel-gw-flow.json" + # ruleType: gw-flow + # ds2.file: + # file: "classpath: sentinel-gw-api-group.json" + # ruleType: gw-api-group + # Sentinel 对 Zuul 的专属配置项,对应 SentinelZuulProperties 类 + zuul: + order: + pre: 10000 # 前置过滤器 SentinelZuulPreFilter 的顺序 + post: 1000 # 后置过滤器 SentinelZuulPostFilter 的顺序 + error: -1 # 错误过滤器 SentinelZuulErrorFilter 的顺序 + +# Zuul 配置项,对应 ZuulProperties 配置类 +zuul: + servlet-path: / # ZuulServlet 匹配的路径,默认为 /zuul + # 路由配置项,对应 ZuulRoute Map + routes: + route_yudaoyuanma: + path: /** + url: http://www.iocoder.cn + + diff --git a/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/resources/sentinel-gw-api-group.json b/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/resources/sentinel-gw-api-group.json new file mode 100644 index 00000000..0e47591b --- /dev/null +++ b/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/resources/sentinel-gw-api-group.json @@ -0,0 +1,23 @@ +[ + { + "apiName": "yudaoyuanma_customized_api", + "predicateItems": [ + { + "pattern": "/categories/**", + "matchStrategy": 1 + }, + { + "items": [ + { + "pattern": "/Dubbo/good-collection/", + "matchStrategy": 0 + }, + { + "pattern": "/SkyWalking/**", + "matchStrategy": 1 + } + ] + } + ] + } +] diff --git a/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/resources/sentinel-gw-flow.json b/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/resources/sentinel-gw-flow.json new file mode 100644 index 00000000..8a53df34 --- /dev/null +++ b/labx-21/labx-21-sc-zuul-demo07-sentinel/src/main/resources/sentinel-gw-flow.json @@ -0,0 +1,10 @@ +[ + { + "resource": "yudaoyuanma", + "count": 3 + }, + { + "resource": "yudaoyuanma_customized_api", + "count": 1 + } +] diff --git a/labx-21/pom.xml b/labx-21/pom.xml index 04af21e8..5f47baa9 100644 --- a/labx-21/pom.xml +++ b/labx-21/pom.xml @@ -18,6 +18,7 @@ labx-21-sc-zuul-demo03-config-nacos labx-21-sc-zuul-demo05-custom-zuul-filter labx-21-sc-zuul-demo07-hystrix + labx-21-sc-zuul-demo07-sentinel labx-21-sc-user-service