mirror of
https://gitee.com/yudaocode/SpringBoot-Labs.git
synced 2026-09-03 05:53:54 +08:00
增加 Zuul 示例
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
74
labx-21/labx-21-sc-zuul-demo07-sentinel/pom.xml
Normal file
74
labx-21/labx-21-sc-zuul-demo07-sentinel/pom.xml
Normal file
@@ -0,0 +1,74 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>labx-21</artifactId>
|
||||
<groupId>cn.iocoder.springboot.labs</groupId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>labx-21-sc-zuul-demo07-sentinel</artifactId>
|
||||
|
||||
<properties>
|
||||
<spring.boot.version>2.2.4.RELEASE</spring.boot.version>
|
||||
<spring.cloud.version>Hoxton.SR1</spring.cloud.version>
|
||||
<spring.cloud.alibaba.version>2.2.0.RELEASE</spring.cloud.alibaba.version>
|
||||
</properties>
|
||||
|
||||
<!--
|
||||
引入 Spring Boot、Spring Cloud、Spring Cloud Alibaba 三者 BOM 文件,进行依赖版本的管理,防止不兼容。
|
||||
在 https://dwz.cn/mcLIfNKt 文章中,Spring Cloud Alibaba 开发团队推荐了三者的依赖关系
|
||||
-->
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring.cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
|
||||
<version>${spring.cloud.alibaba.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
<dependencies>
|
||||
<!-- 引入 Spring Cloud Zuul 相关依赖,使用它作为网关,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Nacos Discovery 相关依赖,将 Nacos 作为注册中心,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- 引入 Spring Cloud Alibaba Sentinel 相关依赖,使用 Sentinel 提供服务保障,并实现对其的自动配置 -->
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
[
|
||||
{
|
||||
"apiName": "yudaoyuanma_customized_api",
|
||||
"predicateItems": [
|
||||
{
|
||||
"pattern": "/categories/**",
|
||||
"matchStrategy": 1
|
||||
},
|
||||
{
|
||||
"items": [
|
||||
{
|
||||
"pattern": "/Dubbo/good-collection/",
|
||||
"matchStrategy": 0
|
||||
},
|
||||
{
|
||||
"pattern": "/SkyWalking/**",
|
||||
"matchStrategy": 1
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,10 @@
|
||||
[
|
||||
{
|
||||
"resource": "yudaoyuanma",
|
||||
"count": 3
|
||||
},
|
||||
{
|
||||
"resource": "yudaoyuanma_customized_api",
|
||||
"count": 1
|
||||
}
|
||||
]
|
||||
@@ -18,6 +18,7 @@
|
||||
<module>labx-21-sc-zuul-demo03-config-nacos</module>
|
||||
<module>labx-21-sc-zuul-demo05-custom-zuul-filter</module>
|
||||
<module>labx-21-sc-zuul-demo07-hystrix</module>
|
||||
<module>labx-21-sc-zuul-demo07-sentinel</module>
|
||||
|
||||
<module>labx-21-sc-user-service</module>
|
||||
</modules>
|
||||
|
||||
Reference in New Issue
Block a user