hystrix和sentinel简笔

2024-01-18 21:32
文章标签 sentinel hystrix 简笔

本文主要是介绍hystrix和sentinel简笔,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

hystrix和sentinel简笔

  • 简介
  • 技术栈
  • 熔断
    • 导入maven
    • yml配置
    • 配置启动相关数据
    • 代码演示
    • 验证正确性
  • 限流
    • 下载监控
    • 添加配置
    • 代码演示
    • 验证代码
  • 源码
  • 参考文献

简介

在分布式系统开发时,会面临一序列问题,比如雪崩效应。特别是高并发没法处理请求或者接口异常情况下,我们需要降级请求,防止出现雪崩效应。此外,在一些并发系统的情况下,个别接口需要进行限流,防止大量并发占用别的接口服务资源,基于此个人便捷记录熔断和限流笔记!

技术栈

  1. hystrix
  2. java
  3. hystrix-dashboard
  4. maven
  5. idea pc
  6. sentinel

熔断

目前居于原有项目的user-center模块开发,分支为feat/hystrix

导入maven

目前使用hystrix和dashboard,因此此时需要导入actuator

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId></dependency>

yml配置

需要配置暴露地址和hystrix熔断时间以及代理允许监控的上报服务器ip

hystrix:dashboard:proxy-stream-allow-list: localhostmetrics:enabled: truecommand:default:execution:isolation:thread:timeoutInMilliseconds: 5000
management:endpoints:web:base-path: /actuatorexposure:include: '*'

配置启动相关数据

需要@EnableHystrixDashboard启动仪表,@EnableCircuitBreaker启动熔断,此外,由于仪表需要页面,目前新版本没有启动。

@SpringBootApplication
@MapperScan("com.lgh.mapper")
@EnableSwagger2
@EnableFeignClients
@EnableEurekaClient
@EnableHystrixDashboard
@EnableCircuitBreaker
public class RunApplication {public static void main(String[] args) {SpringApplication.run(RunApplication.class, args);}
}

新版本@EnableHystrixDashboard在启动类配置外,还需要配置web页面

 @Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");registry.addResourceHandler("/hystrix/**").addResourceLocations("classpath:/static/hystrix/");}

代码演示

目前注解@HystrixCommand启动熔断,使用fallbackMethod友好降级。

@Service
public class HystrixDemoImpl implements IHystrixDemo {@HystrixCommand(fallbackMethod = "fallbackMethod")@Overridepublic String test(String inputStr) {try {System.out.println("test Hystrix");Thread.sleep(4000);} catch (InterruptedException e) {e.printStackTrace();}return "SUCCESS";}public String fallbackMethod(String inputStr) {System.out.println("fallbackMethod");return "Time out" + inputStr;}
}@Api(tags = "熔断限流测试")
@RestController
@RequestMapping("/hystrixtest")
public class HystrixController {@Autowiredprivate IHystrixDemo iHystrixDemo;@GetMapping("/test")public String test(String inputStr) {return iHystrixDemo.test(inputStr);}
}

验证正确性

查看hystrix-dashboard是否生效http://localhost:8088/hystrix,接着输入监控地址:http://localhost:8088/actuator/hystrix.stream,点击Monitor Stream即可,若没数据,则需要请求才有数据,如下图
在这里插入图片描述在这里插入图片描述
接下来使用jmeter并发请求,具体下载地址:jmeter配置完直接执行
在这里插入图片描述
查看控制台日志或者打断点看熔断情况:
在这里插入图片描述

限流

限流比较简单使用,具体可以参考alibaba官网。sentinel

下载监控

直接官网下载sentinel-dashboard,具体操作官网有详解。打包完jar包后,启动命令:

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

浏览监控http://localhost:8080/
在这里插入图片描述

添加配置

主要pom导入和限流aop配置,具体如下(更多版本参考https://mvnrepository.com/)
pom:

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

SentinelResourceAspect配置

import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SentinelConfig {@Beanpublic SentinelResourceAspect sentinelResourceAspect() {return new SentinelResourceAspect();}
}

上报sentinel-dashboard的配置,sentinel-dashboard上可动态配置规则

spring:cloud:sentinel:transport:dashboard: 127.0.0.1:8080enabled: true

代码演示

这里主要处理限流异常处理类和实现注解@SentinelResource
阻塞异常处理:

package com.lgh.exception;import com.alibaba.csp.sentinel.slots.block.BlockException;public class ExceptionUtil {public static String handleException(String inputStr, BlockException ex) {System.out.println("Oops: " + ex.getClass().getCanonicalName());return "Fail";}
}

异常实现类HystrixDemoImpl

package com.lgh.service.impl;import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.lgh.exception.ExceptionUtil;
import com.lgh.service.IHystrixDemo;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;@Service
public class HystrixDemoImpl implements IHystrixDemo {@SentinelResource(value = "testSentinel",blockHandler = "handleException", blockHandlerClass = ExceptionUtil.class)@Overridepublic String testSentinel(String inputStr) {try {Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}return "SUCCESS";}
}

controller请求处理HystrixController

package com.lgh.controller;import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.lgh.service.IHystrixDemo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@Api(tags = "熔断限流测试")
@RestController
@RequestMapping("/hystrixtest")
public class HystrixController {@Autowiredprivate IHystrixDemo iHystrixDemo;@GetMapping("/testSentinel")public String testSentinel(String inputStr) {return iHystrixDemo.testSentinel(inputStr);}
}

验证代码

启动user-center工程,去sentinel dashboard查看是否已经上报监控,具有user-center说明已经上报,启动工程前需要启动注册中心哦eureka-server
在这里插入图片描述
配置降级规则(没有降级规则,是不生效的哦)
在这里插入图片描述

启动jemter请求验证
在这里插入图片描述

源码

https://github.com/soft1302/project-study/tree/feat/hystrix

参考文献

【1】sentinel
【2】howtodoinjava
【3】简述

这篇关于hystrix和sentinel简笔的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/620320

相关文章

Sentinel 高可用流量管理框架

Sentinel 是面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性。 Sentinel 具有以下特性: 丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应

Redis Sentinel 深度解析:构建高可用性 Redis 集群

Redis Sentinel 深度解析:构建高可用性 Redis 集群 一 . 基本概念1.1 相关名词解释1.2 如何人工恢复主节点故障 ?1.3 哨兵自动恢复主节点故障 二 . 哨兵的安装部署2.1 安装 docker 和 docker-compose2.2 搭建 Redis 的哨兵环境2.2.1 编排 Redis 的主从节点2.2.2 编排 redis-sentinel 节点2.2.3

【Redis】Redis Sentinel(哨兵)系统:自动故障恢复与高可用性配置全解

目录 哨兵 (Sentinel)基本概念主从复制的问题⼈⼯恢复主节点故障哨兵⾃动恢复主节点故障 安装部署 (基于 docker)准备⼯作 以下部分是独立于这一章节的Docker安装Server版本安装CentOS安装实战经验 GUI版本安装(以windows 11为例)安装docker 以上部分是独立于这一章节的重新选举redis-master 宕机之后redis-master 重启之

springboot项目引入Sentinel熔断

本文是springboot项目+nacos进行引入,sentiel需自行按照部署 1.springboot包要是2.2.5或以上 <dubbo.version>2.7.7</dubbo.version><spring-boot.version>2.2.5.RELEASE</spring-boot.version><chainwork-boot.version>1.0.5-SNAPSHOT<

Spring Cloud的Ribbon-Hystrix-Feign

Ribbon 作为负载均衡,在客户端实现,服务段可以启动两个端口不同但servername一样的服务 Hystrix作为熔断流量控制,在客户端实现,在方法上注解,当请求出错时可以调用注解中的方法返回 Feign 可以定义请求到其他服务的接口,用于微服务间的调用,不用自己再写http请求,在客户端实现,调用此接口就像远程调用其他服务一样,当请求出错时可以调用接口的实现类来返回 一、客户端负载均

Google Earth Engine——导入无云 Sentinel-2 图像和NDVI计算

目录 搜索和导入无云 Sentinel-2 图像 Sentinel-2 的背景 打开 GEE 界面 定义您感兴趣的领域 查询 Sentinel-2 图像的存档 过滤图像集合 将图像添加到地图视图 定义真彩色可视化参数 探索影像 定义假色可视化参数 从波段组合中导出指数 NDVI 锻炼 本实验的目的是介绍 Google Earth Engine 处理环境。在本练习

黑马-Cloud21版-面试篇13:Sentinel源码分析

Sentinel源码分析 1.Sentinel的基本概念 Sentinel实现限流、隔离、降级、熔断等功能,本质要做的就是两件事情: 统计数据:统计某个资源的访问数据(QPS、RT等信息)规则判断:判断限流规则、隔离规则、降级规则、熔断规则是否满足 这里的资源就是希望被Sentinel保护的业务,例如项目中定义的controller方法就是默认被Sentinel保护的资源。 1.1.Pr

阿里巴巴宣布 Sentinel 开源,进一步完善 Dubbo 生态(附PPT)

Aliware Open Source•深圳站现场   1、当服务量大到一定程度,流量扛不住的时候,该如何处理? 2、应用之间相互依赖,当应用A出现响应时间过长,影响到应用B的响应,进而产生连锁反应影响整个依赖链上的所有应用,该如何处理?   随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服

Spring Boot 整合 Sentinel 实现流量控制

在微服务架构中,流量控制是保障系统稳定性和高可用性的关键技术之一。阿里巴巴开源的 Sentinel 是一款面向分布式系统的流量防护组件,旨在从流量控制、熔断降级、系统负载保护等多个维度保障服务的稳定性。本文将详细介绍如何在 Spring Boot 项目中整合 Sentinel 实现流量控制。 1. Sentinel 简介 Sentinel 是阿里巴巴开源的一个轻量级流量控制框架,主要用于

Spring Cloud Alibaba教程:Sentinel的使用

什么是Sentinel Sentinel,中文翻译为哨兵,是为微服务提供流量控制、熔断降级的功能,它和Hystrix提供的功能一样,可以有效的解决微服务调用产生的“雪崩”效应,为微服务系统提供了稳定性的解决方案。随着Hytrxi进入了维护期,不再提供新功能,Sentinel是一个不错的替代方案。通常情况,Hystrix采用线程池对服务的调用进行隔离,Sentinel才用了用户线程对接口进行隔离,二