Spring Cloud gateway 五 Sentinel整合

微服务当前这么火爆的程度,若是不能学会一种微服务框架技术。怎么能升职加薪,增长简历的筹码?spring cloud 和 Dubbo 须要单独学习。说没有时间?没有精力?要学俩个框架?而Spring Cloud alibaba只须要你学会一个就会拥有俩种微服务治理框架技术。何乐而不为呢?加油吧!骚猿年

Sentinel 熔断限流

以前咱们zuul 网关服务使用的接入方式是按照 Sentinel 方式接入方式。其实在Spring Cloud alibaba 体系里面 有这个很是好用的Sentinel starter 依赖。只须要依赖一个jar 包。而后配置好Sentinel 服务器地址。java

Sentinel 的服务搭建和启动

Sentinel的快速搭建

以前的篇章有讲过怎么搭建。此次在贴一次。直接下载官网已经打好的jar包git

release地址 https://github.com/alibaba/Sentinel/releasesgithub

源码编译
git clone https://github.com/alibaba/Sentinel.gitweb

而后进入目录执行 mvn clean packagespring

命令启动docker

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard.jar

若是须要docker 的话 可编写 docker Dockerfilebootstrap

# 基于哪一个镜像
FROM java:8
# 拷贝文件到容器,也能够直接写成ADD microservice-discovery-eureka-0.0.1-SNAPSHOT.jar /app.jar
ADD ./*.jar app.jar
RUN mkdir -p /var/logs/Sentinel
RUN mkdir -p /var/logs/jvm
RUN mkdir -p /var/logs/dump
RUN bash -c 'touch /app.jar'
# 开放8080端口
EXPOSE 8080
# 配置容器启动后执行的命令
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-Dsentinel.dashboard.auth.username=sentinel","-Dsentinel.dashboard.auth.password=123456","-Dserver.servlet.session.timeout=7200","-XX:-PrintGCDetails","-XX:-PrintGCTimeStamps","-XX:-HeapDumpOnOutOfMemoryError","-XX:HeapDumpPath=/var/logs/dump/oom_dump.dump","-Xloggc:/var/logs/jvm/app.log","-Dfile.encoding=UTF8","-Duser.timezone=GMT+08","-XX:CMSInitiatingOccupancyFraction=90","-XX:MaxGCPauseMillis=200","-XX:StringTableSize=20000","-XX:+UseG1GC","-Xss256k","-Xmx1024m","-Xms512m","-jar","/app.jar"]

执行 docker 镜像制做api

docker  build --tag sentinel:1.0 .

--tag projectname:version 注意写法bash

file

file
而后docker run 启动镜像。这里做者使用的docker镜像方式启动
docker run -d -p8890:8080 -p8891:8080 304342c105e9服务器

而后控制台输入 http://localhost:9088/ 用户名密码 sentinel/123456

登陆参数配置:

从 Sentinel 1.6.0 起,Sentinel 控制台引入基本的登陆功能,默认用户名和密码都是 sentinel。能够参考 鉴权模块文档 配置用户名和密码。

  • Dsentinel.dashboard.auth.username=sentinel 用于指定控制台的登陆用户名为 sentinel;
  • Dsentinel.dashboard.auth.password=123456 用于指定控制台的登陆密码为 123456;若是省略这两个参数,默认用户和密码均为 sentinel;
  • Dserver.servlet.session.timeout=7200 用于指定 Spring Boot 服务端 session 的过时时间,如 7200 表示 7200 秒;60m 表示 60 分钟,默认为 30 分钟;

输入密码登陆

登陆完成之后,咱们开始整合 gateway的整合操做。

在咱们gateway 服务中 引入pom

<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>

建立RulesController 暴露接口

package com.xian.cloud.controller;

import com.alibaba.csp.sentinel.adapter.gateway.common.api.ApiDefinition;
import com.alibaba.csp.sentinel.adapter.gateway.common.api.GatewayApiDefinitionManager;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
 */
@RestController
public class RulesController {

    @GetMapping("/api")
    @SentinelResource("api")
    public Set<ApiDefinition> apiRules() {
        return GatewayApiDefinitionManager.getApiDefinitions();
    }

    @GetMapping("/gateway")
    @SentinelResource("gateway")
    public Set<GatewayFlowRule> apiGateway() {
        return GatewayRuleManager.getRules();
    }

    @GetMapping("/flow")
    @SentinelResource("flow")
    public List<FlowRule> apiFlow() {
        return FlowRuleManager.getRules();
    }
}

bootstrap.yml 文件添加制定sentinel服务地址

spring:
    cloud:
    sentinel:
      transport:
        dashboard: localhost:8890
        port: 8890
      # 服务启动直接创建心跳链接
      eager: true

启动服务

屡次请求 curl http://localhost:9000/api

实时监控数据

file

簇点链路
file

在右侧能够设置流控、降级、热点、受权操做

流控设置
file

对应的参数属性

  • resource:资源名,即限流规则的做用对象
  • count: 限流阈值
  • grade: 限流阈值类型(QPS 或并发线程数)
  • limitApp: 流控针对的调用来源,若为 default 则不区分调用来源
  • strategy: 调用关系限流策略
  • controlBehavior: 流量控制效果(直接拒绝、Warm Up、匀速排队)

总结

以上就是对Spring Cloud gateway 与 Sentinel 的整合方案。心细的同窗可能会想到,咱们设置的限流规则若是重启服务都将不复存在,这样确定是咱们不能接受的。下一篇将如何将Sentinel 设置进行存储。

往期资料、参考资料

Sentinel 官方文档地址

摘自参考 spring cloud 官方文档

Spring Cloud alibaba 官网地址

示例代码地址

服务器nacos 地址 http://47.99.209.72:8848/nacos

往期地址 spring cloud alibaba 地址

spring cloud alibaba 简介

Spring Cloud Alibaba (nacos 注册中心搭建)

Spring Cloud Alibaba 使用nacos 注册中心

Spring Cloud Alibaba nacos 配置中心使用

spring cloud 网关服务

Spring Cloud zuul网关服务 一

Spring Cloud 网关服务 zuul 二

Spring Cloud 网关服务 zuul 三 动态路由

Spring Cloud alibaba网关 sentinel zuul 四 限流熔断

Spring Cloud gateway 网关服务 一

Spring Cloud gateway 网关服务二 断言、过滤器

Spring Cloud gateway 三 自定义过滤器GatewayFilter

Spring Cloud gateway 网关四 动态路由

如何喜欢能够关注分享本公众号。
file