首页 > 其他分享 >Gateway整合Sentinel

Gateway整合Sentinel

时间:2022-09-01 20:26:48浏览次数:48  
标签:exchange spring nacos alibaba sentinel 整合 Sentinel Gateway cloud

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

相关文章

  • Springboot整合Sentinel实现流控
    在Springboot项目中整合Sentinel实现流控,Gateway整合Sentinel见Gateway整合Sentinel,Sentinel-daahboard的改造见Sentinel-dashboard改造(普通流控和网关流控规则持久化到Nac......
  • 在项目中整合Sentienl(整体梳理)
    本文主要是对Sentinel整个集成过程的回顾整理,描述Sentinel在系统中的位置以及Sentinel-dashboard和Sentinel-client的数据流转和交互逻辑,便于后面文章的理解和在项目中实际......
  • springboot整合redis,设置缓存过期时间
    注:redis服务器要先开启!或者连接远程服务器上的Redis,但是依然要开启服务,不然会一直TimeOut!Pom文件添加依赖<dependency><groupId>org.springframework.boot</......
  • 微服务网关Gateway实践总结
    有多少请求,被网关截胡;一、Gateway简介微服务架构中,网关服务通常提供动态路由,以及流量控制与请求识别等核心能力,在之前的篇幅中有说过Zuul组件的使用流程,但是当下Gatewa......
  • Swoole实战之手撸HttpServer框架 19 ORM整合(1)初步封装Laravel ORM库、基本查询、使用
    视频地址https://www.bilibili.com/video/BV14E411t7T4?p=28&spm_id_from=pageDriver&vd_source=4a69745b599dffec877b0fcfe130b0921封装composerrequireilluminate......
  • SpringBoot整合Shiro
    11、SpringBoot整合Shiro11.1、什么是ShiroApacheShiro是一个Java的安全(权限)框架。Shiro可以非常容易的开发出足够好的应用,其不仅可以用在JavaSE环境,也可以用在Jav......
  • Sentinel控制台1.8.3修改源码,修改配置后推送到Nacos
    目录1.接着上一篇2.思路3.下载Sentinel源码4.看Gateway里面读取的配置信息5.修改Sentinel控制台源码6.熔断规则测试7.限流规则测试8.打包使用1.接着上一篇简单......
  • SpringBoot整合EMQ
    1.引入依赖<dependency><groupId>org.eclipse.paho</groupId><artifactId>org.eclipse.paho.client.mqttv3</artifactId><version>1.2.5</version></depe......
  • jenkins整合docker及harbor的使用
    修改镜镜仓库地址:  systemctlrestartdocker  repo是harbor仓库的仓库名称          修改为3.0并提交         ......
  • springcloud 中gateway的搭建
    创建maven工程添加pom依赖<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.7.RE......