Gateway整合Sentinel比较方便,基本分为一下几步:
1、依赖的引入
2、全局异常处理
3、配置文件修改
1、依赖的引入
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-datasource</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
</dependency>
2、全局异常处理
由于sentinel自带的异常信息不够友好,所以需要自己在代码中进行统一处理,可以根据不同的异常类型,返回不同的提示消息,示例代码如下:
public class SentinelFallbackHandler implements WebExceptionHandler {
private Mono<Void> writeResponse(ServerWebExchange exchange, String msg) {
exchange.getResponse().setStatusCode(HttpStatus.INTERNAL_SERVER_ERROR);
exchange.getResponse().getHeaders().add(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE);
Map<String, Object> map = new HashMap<>(2);
map.put("code", -1);
map.put("msg", msg);
DataBuffer dataBuffer = exchange.getResponse().bufferFactory().wrap(JSON.toJSONString(map).getBytes());
return exchange.getResponse().writeWith(Mono.just(dataBuffer));
}
@Override
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
if (exchange.getResponse().isCommitted()) {
return Mono.error(ex);
}
if (!BlockException.isBlockException(ex)) {
return Mono.error(ex);
}
return GatewayCallbackManager.getBlockHandler().handleRequest(exchange, ex).flatMap(response -> writeResponse(exchange, "请求超过最大数量,请稍后重试"));
}
}
3、配置文件修改
server:
port: 8080
spring:
application:
name: gateway
cloud:
inetutils:
#本地机器存在多个ip时,以指定前缀开头的ip注册到服务
preferred-networks: 192.168.63
#nacos相关配置
nacos:
discovery:
server-addr: 127.0.0.1:8848
#分组名
group: 'DEFAULT_GROUP'
#命名空间的ID
namespace: 'sentinel'
username: nacos
password: nacos
config:
server-addr: 127.0.0.1:8848
#分组名
group: 'DEFAULT_GROUP'
#命名空间的ID
namespace: 'sentinel'
username: nacos
password: nacos
file-extension: yml
#sentinel相关配置
sentinel:
transport:
dashboard: 127.0.0.1:8089
eager: true
datasource:
#================流控===============
gw-flow:
nacos:
server-addr: 127.0.0.1:8848
#与sentinel控制台的namespeace统一
namespace: 6db12247-153b-4d0c-bef6-5c7c5e48faa3
#nacos中存储规则的dataId,对于dataId使用了${spring.application.name}变量,这样可以根据应用名来区分不同的规则配置
data-id: ${spring.application.name}-gateway-flow
#nacos中存储规则的groupId(可以自己定义)
group-id: SENTINEL_GROUP
#具体的类型见,org.springframework.cloud.alibaba.sentinel.datasource.RuleType
rule-type: gw-flow
#nacos的用户名和密码
username: nacos
password: nacos
#================API管理===============
gw-api:
nacos:
server-addr: 127.0.0.1:8848
#与sentinel控制台的namespeace统一
namespace: 6db12247-153b-4d0c-bef6-5c7c5e48faa3
#nacos中存储规则的dataId,对于dataId使用了${spring.application.name}变量,这样可以根据应用名来区分不同的规则配置
data-id: ${spring.application.name}-gateway-api
#nacos中存储规则的groupId(可以自己定义)
group-id: SENTINEL_GROUP
#具体的类型见,org.springframework.cloud.alibaba.sentinel.datasource.RuleType,gw-api-group
rule-type: gw-api-group
#nacos的用户名和密码
username: nacos
password: nacos
#================流控===============
degrade:
nacos:
server-addr: 127.0.0.1:8848
namespace: 6db12247-153b-4d0c-bef6-5c7c5e48faa3
data-id: ${spring.application.name}-degrade-rules
group-id: SENTINEL_GROUP
rule-type: degrade
username: nacos
password: nacos
#================系统规则===============
system:
nacos:
server-addr: 127.0.0.1:8848
namespace: 6db12247-153b-4d0c-bef6-5c7c5e48faa3
data-id: ${spring.application.name}-system-rules
group-id: SENTINEL_GROUP
rule-type: system
username: nacos
password: nacos
gateway:
discovery:
locator:
lower-case-service-id: true
enabled: true
标签:exchange,spring,nacos,alibaba,sentinel,整合,Sentinel,Gateway,cloud
From: https://www.cnblogs.com/zhaodalei/p/16647705.html