SpringCloud Gateway动态路由及Sentinel

2020-10-29  本文已影响0人  老童看书

SpringCloud Gateway动态路由及Sentinel限流

说明:本demo旨在把网关的限流降级直接委托到Sentinel控制台中进行处理,并实现动态路由,并没有实现sentinel规则持久化,接下来的demo会实现

Step1:搭建Sentinel

1、下载Sentinel,下载一个Sentinel Dashboard:可以从 release
页面下载最新版本的控制台 jar 包

2、本地启动sentinel
java -jar -Dserver.port=8899 -jar sentinel-dashboard.jar

3、访问sentinel
用户名和密码默认:sentinel/sentinel

Step2:搭建项目

1、加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    <version>0.9.0.RELEASE</version>
</dependency>

<!-- 该依赖提供sentinel对SpringCloud gateway的适配 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-spring-cloud-gateway-adapter</artifactId>
    <version>1.8.0</version>
</dependency>

<!-- 没添加该依赖的话不能从application配置文件配置sentinel -->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    <version>2.2.3.RELEASE</version>
</dependency>

<!-- 连接sentinel控制台 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
    <version>1.8.0</version>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    <version>0.9.0.RELEASE</version>
</dependency>

2、加配置
详情见项目application.yml和bootstrap.yml,为啥拆开?因为有时候不把spring相关配置放bootstrap.yml中启动项目nacos会报个错

spring:
  application:
    name: api-gateway-sentinel-nacos
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        namespace: 3ca423a5-e582-43f5-bd11-06101c939e6c
        ext-config:
          dataId: api-gateway-sentinel-nacos
          group: DEFAULT_GROUP
    sentinel:
      transport:
        dashboard: localhost:8899
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true

3、将Gateway路由配置在Nacos配置列表中添加个dataId为api-gateway-sentinel-nacos的配置,格式选择json,内容如下:

[{
    "id": "api-gate-product1",
    "uri": "lb://product-service",
    "predicates": [{
        "name": "Path",
        "args": {
            "pattern": "/product/api1/**"
        }        
    }],
    "filters": [{
        "name": "StripPrefix",
        "args": {
            "parts": "1"
        }
    }]
},
{
    "id": "api-gate-product2",
    "uri": "lb://product-service",
    "predicates": [{
        "name": "Path",
        "args": {
            "pattern": "/product/api2/**"
        }        
    }],
    "filters": [{
        "name": "StripPrefix",
        "args": {
            "parts": "1"
        }
    }]
}]

4、准备好启动api-gateway-product项目

        @GetMapping("/info")
        public String info() {
            log.info("invoked product/info");
            return "iPhone 12 pro";
        }

        @GetMapping("/api1/test1")
        public String api1() {
            log.info("invoked product/api1/test1111");
            return "iPhone 12 pro";
        }

        @GetMapping("/api1/test2")
        public String api2() {
            log.info("invoked product/api1/test2222");
            return "iPhone 12 pro";
        }

        @GetMapping("/api2/test3")
        public String api3() {
            log.info("invoked product/api2/test3333");
            return "iPhone 12 pro";
        }

        @GetMapping("/api2/test4")
        public String api4() {
            log.info("invoked product/api2/test4444");
            return "iPhone 12 pro";
        }

Step3、测试

1、用浏览器分别访问:

上一篇下一篇

猜你喜欢

热点阅读