微服务sentinel解析部署使用全流程

sentinel源码地址: 介绍 · alibaba/Sentinel Wiki · GitHub

sentinel官方文档: https://sentinelguard.io/zh-cn/docs/introduction.html

Sprong Cloud alibaba Sentinel文档【小例子】 : Sentinel · alibaba/spring-cloud-alibaba Wiki · GitHub

目录

1、sentinel概念

2、入门例子

0、启动nacos和redis

1、使用openfeign项目

2、引入依赖

3、下载sentinel服务

4、启动服务

5、登录sentinel

6、添加sentinel配置信息

7、启动并访问两项目 查看结果

​编辑

【概念了解】

3、自定义流控响应

1、添加过滤器SentinelFilterConfig

2、添加配置类SentinelConfig

3、运行结果

4、熔断feign

1、添加配置

2、修改feign接口注解

3、添加feign接口实现类

4、查看运行结果

5、熔断资源

1、try方式

2、注解方式

6、其他说明


1、sentinel概念

随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。

流量路由,流量控制,流量整形:流量从另外一个角度,也叫请求。

2、入门例子

0、启动nacos和redis

1、使用openfeign项目

详细可见微服务 OpenFeign 解析部署使用全流程-CSDN博客

2、引入依赖

给两个项目都添加如下依赖:

<dependency>
  <groupId>com.alibaba.cloud</groupId>
  <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

<dependency>
  <groupId>com.alibaba.csp</groupId>
  <artifactId>sentinel-web-servlet</artifactId>
</dependency>

3、下载sentinel服务

下载sentinel服务路径: https://github.com/alibaba/Sentinel/releases

注意版本号对应

我们用的是1.8.0版本,下载服务端时下载对应的版本。

4、启动服务

找到当前jar包位置输入cmd

执行

java -jar sentinel-dashboard.jar

5、登录sentinel

http://localhost:8080/#/login

用户名和密码都是sentinel。

6、添加sentinel配置信息

给两个项目的application.yml配置文件里添加如下配置:

spring:
 cloud:
  sentinel:
   transport:
    dashboard: localhost:8080

填入后

修改后

7、启动并访问两项目 查看结果

【访问sentinel网站,设置限流】

http://localhost:8080/#/dashboard/identity/openfeignDemo1


Sentinel Dashboard

注意:设置的单机阈(yu四声)(一秒钟访问的数量)值改为2,这样可以尽快看见限流。

【限流的效果】

一下一下点击访问的时候,还可以看见正常响应:

但是快速点击的时候,就显示被限流了:

【概念了解】

【1、什么是QPS】

每秒请求的数量,要求发送请求并得到响应的整体时间。

【2、什么是RT】

响应时间。

【3、慢调用】

响应时间大于一定值。

【4、慢调用比例】

慢调用 / 总调用 比例值。

【5、比例阈值】

输入0到1之间的浮点型,代表百分比,0是0%,1是100%。

3、自定义流控响应

当被限流的时候,浏览器中展示的效果如下,用户体验度不好,可以进行以下操作。

1、添加过滤器SentinelFilterConfig

在user工程里,添加此过滤器。

package com.jr.config;

import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

@Configuration
public class SentinelFilterConfig {

    @Bean
    public FilterRegistrationBean<Filter> filterFilterRegistrationBean(){
        FilterRegistrationBean<Filter> result = new FilterRegistrationBean<>(new CommonFilter());
        result.addUrlPatterns("/*");
        return result;
    }
}

2、添加配置类SentinelConfig

在user工程里,添加此过配置类。

package com.jr.config;

import com.alibaba.csp.sentinel.adapter.servlet.callback.WebCallbackManager;
import com.alibaba.fastjson.JSON;
import com.jr.util.Result;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SentinelConfig {

    public SentinelConfig() {
        WebCallbackManager.setUrlBlockHandler((request, response, e) -> {
            Result error = Result.error();
            error.setMessage("被限流了!");
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(JSON.toJSONString(error));
        });
    }
}

3、运行结果

重启user项目,重新添加流量阈值,查看运行结果。

4、熔断feign

官方地址: https://github.com/alibaba/Sentinel/wiki/%E7%86%94%E6%96%AD%E9%99%8D%E7%BA%A7

1、添加配置

在user工程里 开启feign的sentinel,写properties文件中可以,写在yaml文件也可以

feign.sentinel.enabled=true

2、修改feign接口注解

/**
 * openfeignDemo2是springsession-1在nacos注册的项目名()
 * ScoreFeignImpl.classs是实现类的名字
 * //fallback 一旦出现熔断,要走哪个类。
 */
@FeignClient(value = "openfeignDemo2", fallback = ScoreFeignImpl.class) 

3、添加feign接口实现类

package com.jr.feign.impl;

import com.jr.entry.Score;
import com.jr.entry.UserDto;
import com.jr.feign.ScoreFeign;
import com.jr.util.Result;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

@Service
public class ScoreFeignImpl implements ScoreFeign {
    @Override
    public Result info() {
        List<Score> list = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            Score score = new Score();
            score.setName("name" + i);
            score.setScore(99.99);
            list.add(score);
        }
        return  Result.ok().put("list",list);
    }

    @Override
    public Result id(String id) {
        return null;
    }

    @Override
    public Result add(UserDto user) {
        return null;
    }
}

FeignClient注解中的fallback属性配置了如果熔断,应该访问的Feign接口实现类。当发生熔断时,会访问ScoreFeignImpl中对接口的实现方法。

4、查看运行结果

关掉Score工程,模拟宕机效果。在使用user工程去访问Score工程,就可以看见熔断处理了。

当遇到宕机的时候,就访问了自己工程里的feign实现类方法。

5、熔断资源

熔断资源官网地址: https://github.com/alibaba/Sentinel/wiki/%E7%86%94%E6%96%AD%E9%99%8D%E7%BA%A7

资源(是自己写的处理器方法,要对自己写的方法进行限流)是自定义的一个名称,这个名称会在Sentinel中显示,就可以对其进行熔断、降级等管理。

实现对资源管理的常用方式有两种,分别是try和注解。

1、try方式

在user工程的UserController里,添加如下方法:

 @GetMapping("/try")
    public Result trySources(){
        String sourcesName = "testTry";
        try(Entry entry = SphU.entry(sourcesName)) { //SphU.entry方法通过传入资源名称和其他参数来获取访问令牌。如果获取到令牌,则可以访问目标资源;如果没有获取到令牌,则无法访问对应资源。
            return Result.ok();
        } catch (BlockException e) {
            return Result.error().setMessage("被限流了!");
        }
    }

注意Entry引包

import com.alibaba.csp.sentinel.Entry;

在user工程的Result里,添加如下方法:

    public Result setMessage(String message) {
        this.message = message;
        return this;
    }

重启两个项目,运行一下看效果,就是自己写的方法,也被限流了。

2、注解方式

在user工程的UserController里,添加如下方法:

 @GetMapping("/annotation")
    @SentinelResource(value = "testAnnotation", blockHandler = "annotationSourcesError")
    public Result annotationSources() {
        return Result.ok();
    }

    public Result annotationSourcesError(BlockException e) {
        return Result.error().setMessage("被限流了!");
    }

重启两个项目,运行一下看效果,就是自己写的方法,也被限流了。

6、其他说明

【1、异常比例】

是发生异常数 / 总请求数。

【2、异常数 】

发生异常次数。

【3、快速失败】

当QPS超过阈值是,直接限流,抛出异常。是默认值,可以用于线程数的限流。

【4、排队等待】

每秒可以处理10个请求,当超出这个值就会等待,等待10000毫秒后,如果还没有被处理会限流。只能用于QPS的限流。

【5、Warm Up】

可以让服务器的QPS“慢慢地”达到阈值,在10秒中之内让QPS到底10。只能用于QPS的限流。

【6、直接拒绝】

只对单一资源,当触发阈值时直接拒绝请求。

【7、关联】

针对两个资源有关联时,当前当前资源会为关联资源让步,保证关联资源有更大的阈值。

【8、链路】

​    从资源入口开始,整个两路的阈值。

【9、热点限流】

当系统中的某些数据被经常引用,可以对这些数据进行限流,减少服务器压力。

【10、系统规则】

针对硬件层面设置的规则,比如限制CPU的使用率

【11、授权规则】

 根据请求的来源设置限流,其实就是黑白名单功能

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值