技术方案Spring Cloud dibo

使用Sentinel实现接口限流(十一)

2019-07-15  本文已影响359人  匆匆岁月

上一篇文章我们已经对 Sentinel 有个简单的了解,接下来我们将讲解 Sentinel的具体使用。

使用 Sentinel 实现接口限流

Sentinel的使用分为两部分:

下面我们就分两部分来看,如果使用Sentinel来实现接口限流。

部署Sentinel Dashboard

命令行启动

java -jar sentinel-dashboard-1.6.2.jar

sentinel-dashboard 不像Nacos的服务端那样还提供了外置配置文件,比较容易修改参数。不过没关系,由于 sentinel-dashboard是一个标准的SpringBoot应用,所以如果需要自定义端口号等配置的话,可以通过在启动参数中增加参数来调整,比如:-Dserver.port=8888

默认情况下,sentinel-dashboard以8080端口启动,所以可以通过访问: http://localhost:8080 来验证是否启动成功,如果一切顺利,可以看到如下界面:

注意: 只要 1.6.0 及以上的版本,才有这个简单的登录页面。默认用户名密码都是: sentinel。对于用户登录相关的配置可在启动命令中添加下面参数来修改默认配置:

输入用户名密码登录后,会看下如下页面:


整合Sentinel

第一步: 创建 alibaba-sentinel-rate-limiting web应用,并在的pom.xml 中引入 Spring Cloud Alibaba的Sentinel模块依赖:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Finchley.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.2.2.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.2</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

第二步: 在Spring Cloud 应用中 application.properties 配置 sentinel dashboard 的访问地址:

注意:不要写在 bootstrap.properties ,不然无效

spring.application.name=alibaba-sentinel-rate-limiting
server.port=9005

# sentinel dashboard
spring.cloud.sentinel.transport.dashboard=localhost:8080

第三步: 创建应用主类和rest接口:

@SpringBootApplication
public class SentinelRateLimitApplication {

    public static void main(String[] args) {
        SpringApplication.run(SentinelRateLimitApplication.class, args);
    }
}
@Slf4j
@RestController
public class TestController {
    
    @GetMapping("/hello")
    public String hello() {
        return "hello sentinel dashboard";
    }
}

第四步: 启动应用,然后通过 postman 访问:http://localhost:9005/hello 接口。

此时,在Sentinel Dashboard界面中就可以看到我们启动的这个服务以及接口调用的实时监控了。如下图所示:


配置限流规则

在完成了上面配置以后,我们在 Sentinel 控制台的 alibaba-sentinel-rate-limiting服务下,单击 ** 簇点链路** 菜单,可以看到如下页面:

其中 ** /hello** 接口,就是我们实现并调用的测试接口。通过单击 流控 按钮,来为这个接口设置限流规则,比如:

这里做简单的配置:

单击 新增 按钮后,可以看到下图:

其实就是左侧菜单 ** 流控规则** 的界面,这里可看到当前设置的所有限流策略。

验证限流规则

在完成上面所有配置之后,我们可以尝试下快速的调用这个接口,看看是否会触发限流控制。
这里我们就不用什么压力测试工具,就简单的通过 curl快速手动访问。可以看到调用 两次 /hello接口后,第三次调用被限流(flow limiting)了。

$ curl localhost:8001/hello
didispace.com
$ curl localhost:8001/hello
didispace.com
$ curl localhost:8001/hello
Blocked by Sentinel (flow limiting)
上一篇 下一篇

猜你喜欢

热点阅读