iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >SpringCloud hystrix断路器与局部降级问题怎么解决
  • 565
分享到

SpringCloud hystrix断路器与局部降级问题怎么解决

2023-07-04 10:07:15 565人浏览 八月长安
摘要

这篇文章主要介绍“SpringCloud hystrix断路器与局部降级问题怎么解决”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“springCloud hystrix断路器与局

这篇文章主要介绍“SpringCloud hystrix断路器与局部降级问题怎么解决”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“springCloud hystrix断路器与局部降级问题怎么解决”文章能帮助大家解决问题。

服务降级

服务压力剧增的时候,根据当前的业务情况及流量对一些服务和页面有策略的降级,以此缓解服务器的压力,以保证核心任务的进行。同时保证部分甚至大分客户能得到正确的响应。也就是当前的请求处理不了或者出错了,给一个默认的返回。例如:双11降级产品评价等非核心功能,保证支持和订单的核心任务进行。

服务熔断

就是防止服务雪崩现象出现,是服务降级的一种特殊情况。

一、Hystrix的服务使用前的问题

1、ProductController 中方法异常和超时

在商品服务 ProductController 中的方法中增加异常和超时,ProductController 中方法修改如下:

package com.hwadee.sprinGCloud.controller;import com.hwadee.springcloud.entity.Product;import org.springframework.beans.factory.annotation.Value;import org.springframework.WEB.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.math.BigDecimal;@RestController@RequestMapping("/product")public class ProductController {    //方便后面讲负载均衡,查看ip,此处获取配置中的端口号和ip    @Value("${server.port}")    private String port;    @Value("${spring.cloud.client.ip-address}")    private String ip;    @RequestMapping("/buy/{id}")    public Product findById(@PathVariable Long id) {        Product product = new Product();        product.setId(id);        // 后面需要测试负载均衡,所以返回 ip 地址及端口号        product.setName("当前访问商品服务地址:" + ip + ":" + port+"  "+"查询商品订单,订单号:"+id);        product.setPrice(new BigDecimal(10000.0));        System.out.println(product);        //测试超时熔断        try {            Thread.sleep(5000);            //测试并发熔断            //Thread.sleep(500);        } catch (InterruptedException e) {            e.printStackTrace();        }        return product;    }    @RequestMapping(value = "/delete/{id}")    public Product deleteOrderById(@PathVariable Long id){        Product product = new Product();        product.setId(id);        // 后面需要测试负载均衡,所以返回 ip 地址及端口号        product.setName("当前访问商品服务地址:" + ip + ":" + port+"  "+"从购物车删除订单,订单号:"+id);        product.setPrice(new BigDecimal(10000.0));        System.out.println(product);        //测试异常熔断        System.out.println(10/0);        return product;    }}

2、访问查看效果

订单服务中OrderController 中 调用商品服务 ProductController 中方法时,ProductController 中方法出现异常和超时,访问浏览器的异常结果和订单服务中的内部异常如下:

SpringCloud hystrix断路器与局部降级问题怎么解决

SpringCloud hystrix断路器与局部降级问题怎么解决

SpringCloud hystrix断路器与局部降级问题怎么解决

SpringCloud hystrix断路器与局部降级问题怎么解决

3、问题分析

微服务架构中,我们将系统拆分成了一个个的服务单元,各单元应用间通过服务注册与订阅的方式互相依赖。由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服务自身问题出现调用故障或延迟,而这些问题会直接导致调用方的对外服务也出现延迟,若此时调用方的请求不断增加,最后就会出现因等待出现故障的依赖方响应而形成任务积压,线程资源无法释放,最终导致自身服务的瘫痪,进一步甚至出现故障的蔓延最终导致整个系统的瘫痪。如果这样的架构存在如此严重的隐患,那么相较传统架构就更加的不稳定。

二、 商品服务 Hystrix的 局部降级

1、降级配置

注解 @HistrixCommand

在商品服务 ProductController 中的 findById( )和deleteOrderById( ) 方法上增加降级配置的注解 @HistrixCommand

—旦调用服务方法失败并抛出了超时或错误信息后,会自动调用@HystrixCommand标注好的fallbackMethod调用类中的指定方法。

在@HistrixCommand 指定超时或异常要回调的方法,同时可以指定配置参数,例如使用@HistrixCommand 中的属性commandProperties 指定默认超时时间,如:

fallbackMethod:指定回调方法是findByIdTimeout
commandProperties: 指定@HystrixProperty 的默认值是默认超时时间是3s

@HystrixCommand(fallbackMethod = "findByIdTimeout", commandProperties = {
        @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")
})

2、回调(兜底降级)方法

在商品服务ProductController 中增加回调方法findByIdTimeout( ) 和deleteOrderByIdException( ),当访问ProductController 中的 findById( )和deleteOrderById( ) 发生超时 或 异常时,会调用回调方法。

3、具体代码

降级配置 和回调(兜底降级)方法的具体代码如下:

import com.hwadee.springcloud.entity.Product;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.math.BigDecimal;@RestController@RequestMapping("/product")public class ProductController {    //方便后面讲负载均衡,查看ip,此处获取配置中的端口号和ip    @Value("${server.port}")    private String port;    @Value("${spring.cloud.client.ip-address}")    private String ip;    @RequestMapping("/buy/{id}")    @HystrixCommand(fallbackMethod = "findByIdTimeout", commandProperties = {            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")    })    public Product findById(@PathVariable Long id) {        Product product = new Product();        product.setId(id);        // 后面需要测试负载均衡,所以返回 ip 地址及端口号        product.setName("当前访问商品服务地址:" + ip + ":" + port + "  " + "查询商品订单,订单号:" + id);        product.setPrice(new BigDecimal(50000.0));        System.out.println(product);        //测试超时熔断        try {            Thread.sleep(5000);            //测试并发熔断            //Thread.sleep(500);        } catch (InterruptedException e) {            e.printStackTrace();        }        return product;    }    @RequestMapping(value = "/delete/{id}")    @HystrixCommand(fallbackMethod = "deleteOrderByIdException")    public Product deleteOrderById(@PathVariable Long id) {        Product product = new Product();        product.setId(id);        // 后面需要测试负载均衡,所以返回 ip 地址及端口号        product.setName("当前访问商品服务地址:" + ip + ":" + port + "  " + "从购物车删除订单,订单号:" + id);        product.setPrice(new BigDecimal(10000.0));        System.out.println(product);        //测试异常熔断        System.out.println(10 / 0);        return product;    }    public Product findByIdTimeout(Long id) {        Product product = new Product();        product.setId(id);        product.setName("当前访问商品服务地址:" + ip + ":" + port + "  " + "访问 超时 进行降级服务");        product.setPrice(new BigDecimal(10000.0));        System.out.println(product);        return product;    }    public Product deleteOrderByIdException(Long id) {        Product product = new Product();        product.setId(id);        product.setName("当前访问商品服务地址:" + ip + ":" + port + "  " + "访问 异常 进行降级服务");        product.setPrice(new BigDecimal(10000.0));        System.out.println(product);        return product;    }}

4、主启动类激活Hstrix

在商品服务的主启动类 ProductServerApplication 中使用注解@EnableCircuitBreaker 进行激活Hystrix,代码如下

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClient// 启动 eureka 客户端@EnableCircuitBreaker // 主启动类激活 Hystrixpublic class ProductServerApplication {    public static void main(String[] args) {        SpringApplication.run(ProductServerApplication.class, args);    }}

5、进行测试

将商品服务中ProductController 和 ProductServerApplication复制到 其他所有 商品服务中,进行更新代码。

分别访问:Http://localhost:9001/product/buy/1 和 http://localhost:9001/product/delete/1 查看超时和异常服务降级。看浏览器和内部结果。

SpringCloud hystrix断路器与局部降级问题怎么解决

SpringCloud hystrix断路器与局部降级问题怎么解决

三、 订单服务 Hystrix的 局部降级

1、降级配置

注解 @HistrixCommand

在订单服务 OrderController 中的方法上增加降级配置的注解 @HistrixCommand

2、回调(兜底降级)方法

在订单服务 OrderController 定义 回调方法 buyTimeout( Long id) 、deleteOrderException( Long id),并设置超时服务,超时服务要比将商品服务ProductController 中的 findById( ) 超时时间短。例如:ProductController 中的 findById( ) 超时时间是 3s ,则订单服务 OrderController 定义 回调方法 buyTimeout( ) 设定的超时时间应是 <3s 。

3、具体代码

降级配置 和回调(兜底降级)方法的具体代码如下:

import com.hwadee.springcloud.entity.Product;import com.hwadee.springcloud.service.IOrderFeignService;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping("/order")public class OrderController {    @Autowired    IOrderFeignService orderFeignService;    @RequestMapping("/buy/{id}")    @HystrixCommand(fallbackMethod = "buyTimeout", commandProperties = {            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")    })    public Product buy(@PathVariable Long id) {        System.out.println("进入OrderController的buy方法, orderFeignService 准备调用远端接口 findById");        Product product = orderFeignService.findOrderById(id);        return product;    }    @RequestMapping(value = "/delete/{id}")    @HystrixCommand(fallbackMethod = "deleteOrderException")    public Product deleteOrderById(@PathVariable Long id) {        System.out.println("进入OrderController的deleteOrderById方法, orderFeignService 准备调用远端接口deleteOrderById");        Product product = orderFeignService.deleteOrderById(id);        int i =10/0;        return product;    }    public Product buyTimeout( Long id) {        Product product = new Product();        product.setId(id);        product.setName("当前订单服务访问/order/buy/1 超时:"+id);        return product;    }    public Product deleteOrderException( Long id) {        Product product = orderFeignService.deleteOrderById(id);        product.setName("当前订单服务访问/order/delete/1 10/0异常:"+id);        return product;    }}

4、将商品服务中的超时时间为正常

将商品服务ProductController 中的 findById( ) 超时时间为正常,即程序超时时间是2s,熔断等待时间是3s,并且要比 订单服务 OrderController 定义 回调方法 buyTimeout( ) 中的熔断时间大,代码如下:

@HystrixCommand(fallbackMethod = "findByIdTimeout", commandProperties = {

// 正常超时时间是 3s 比OrderController 中定义的超时时间长。

@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000") }) public Product findById(@PathVariable Long id) { 。。。。。

// 超时时间 2s

Thread.sleep(2000); 。。。。。

return product;

}

完整代码如下:

import com.hwadee.springcloud.entity.Product;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import java.math.BigDecimal;@RestController@RequestMapping("/product")public class ProductController {    //方便后面讲负载均衡,查看ip,此处获取配置中的端口号和ip    @Value("${server.port}")    private String port;    @Value("${spring.cloud.client.ip-address}")    private String ip;    @RequestMapping("/buy/{id}")    @HystrixCommand(fallbackMethod = "findByIdTimeout", commandProperties = {            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000")    })    public Product findById(@PathVariable Long id) {        Product product = new Product();        product.setId(id);        // 后面需要测试负载均衡,所以返回 ip 地址及端口号        product.setName("当前访问商品服务地址:" + ip + ":" + port + "  " + "查询商品订单,订单号:" + id);        product.setPrice(new BigDecimal(10000.0));        System.out.println(product);        //测试超时熔断        try {            Thread.sleep(5000);            //测试并发熔断            //Thread.sleep(500);        } catch (InterruptedException e) {            e.printStackTrace();        }        return product;    }    @RequestMapping(value = "/delete/{id}")    @HystrixCommand(fallbackMethod = "deleteOrderByIdException")    public Product deleteOrderById(@PathVariable Long id) {        Product product = new Product();        product.setId(id);        // 后面需要测试负载均衡,所以返回 ip 地址及端口号        product.setName("当前访问商品服务地址:" + ip + ":" + port + "  " + "从购物车删除订单,订单号:" + id);        product.setPrice(new BigDecimal(10000.0));        System.out.println(product);        //测试异常熔断        System.out.println(10 / 0);        return product;    }    public Product findByIdTimeout(Long id) {        Product product = new Product();        product.setId(id);        product.setName("当前访问商品服务地址:" + ip + ":" + port + "  " + "访问 超时 进行降级服务");        product.setPrice(new BigDecimal(10000.0));        System.out.println(product);        return product;    }    public Product deleteOrderByIdException(Long id) {        Product product = new Product();        product.setId(id);        product.setName("当前访问商品服务地址:" + ip + ":" + port + "  " + "访问 异常 进行降级服务");        product.setPrice(new BigDecimal(10000.0));        System.out.println(product);        return product;    }}

5、主启动类激活Hstrix

在订单服务的主启动类 OrderServerApplication 中使用注解@EnableCircuitBreaker 或 注解@EnableHystrix进行激活Hystrix,注意:@EnableHystrix包括了注解@EnableCircuitBreaker ,代码如下

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication@EnableEurekaClient// 启动 eureka 客户端@EnableFeignClients  // 启动 feign@EnableCircuitBreaker // 或 @EnableHystrix  启动 Hystrixpublic class OrderServerApplication {    public static void main(String[] args) {        SpringApplication.run(OrderServerApplication.class, args);    }}

6、进行测试

分别访问:http://localhost:9000/order/buy/1 和 http://localhost:9000/order/delete/1 查看超时和异常服务降级。

SpringCloud hystrix断路器与局部降级问题怎么解决

SpringCloud hystrix断路器与局部降级问题怎么解决

关于“SpringCloud hystrix断路器与局部降级问题怎么解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网精选频道,小编每天都会为大家更新不同的知识点。

--结束END--

本文标题: SpringCloud hystrix断路器与局部降级问题怎么解决

本文链接: https://www.lsjlt.com/news/344539.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • SpringCloud hystrix断路器与局部降级问题怎么解决
    这篇文章主要介绍“SpringCloud hystrix断路器与局部降级问题怎么解决”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“SpringCloud hystrix断路器与局...
    99+
    2023-07-04
  • SpringCloud hystrix断路器与局部降级全面介绍
    目录服务降级一、Hystrix的服务使用前的问题1、ProductController 中方法异常和超时2、访问查看效果3、问题分析二、 商品服务 Hystrix的 局部降级1、降级...
    99+
    2022-11-13
    SpringCloud hystrix断路器 SpringCloud hystrix服务降级
  • SpringCloud hystrix断路器与全局解耦怎么实现
    这篇文章主要介绍“SpringCloud hystrix断路器与全局解耦怎么实现”,在日常操作中,相信很多人在SpringCloud hystrix断路器与全局解耦怎么实现问题上存在疑惑,小编查阅了各式资料,整理出简单好...
    99+
    2023-07-04
  • SpringCloud hystrix断路器与全局解耦全面介绍
    第七章中在ProductController 和OrderController 中都使用了局部服务降级,但同时也导致两个问题, 通过观察两个局部降级的案例,可以发现: 每个业务方法都...
    99+
    2022-11-13
    SpringCloud hystrix断路器 SpringCloud hystrix服务降级
  • c# chart缩放和局部放大问题怎么解决
    今天小编给大家分享一下c# chart缩放和局部放大问题怎么解决的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。c#...
    99+
    2023-07-05
  • vue keep-alive多层级路由支持问题怎么解决
    这篇文章主要介绍了vue keep-alive多层级路由支持问题怎么解决的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇vue keep-alive多层级路由支持问题怎么解决文章都会有所收获,...
    99+
    2023-07-05
  • Vue3页面局部刷新组件的刷新问题怎么解决
    步入正题,解决今天的问题代码首先我们要对app.vue进行修改代码:<template> <div id="app"> <nav-View v-show="login&...
    99+
    2023-05-17
    Vue3
  • 腾讯云服务器断网怎么解决问题
    1. 检查网络连接 首先,当腾讯云服务器出现断网问题时,我们需要检查网络连接是否正常。可以通过 ping 命令来测试服务器是否能够连接到外网。如果 ping 不通,那么可能是服务器的网络配置出现了问题,需要检查网络配置是否正确。 2. 检...
    99+
    2023-10-27
    腾讯 解决问题 断网
  • 云服务器断开连接怎么解决问题
    如果您的云服务器断开连接并且您无法重新加载其配置文件,那么可能需要检查以下几个方面: 云服务器的网络连接设置。确保云服务器的网络连接设置与您使用的网络连接设置相同或匹配。 重新启动云服务器。尝试重新启动云服务器以解决连接问题。如果您使用...
    99+
    2023-10-26
    解决问题 服务器
  • zabbix代理服务器部署与zabbix-snmp监控问题怎么解决
    今天小编给大家分享一下zabbix代理服务器部署与zabbix-snmp监控问题怎么解决的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了...
    99+
    2023-07-02
  • 云服务器断开连接怎么解决的问题
    如果您的云服务器断开连接需要重新连接,可以按照以下步骤进行解决: 使用远程管理工具:可以使用虚拟专用网络或云存储平台管理员远程管理客户端软件(如 AWS、Google Cloud 等)来连接云服务器并管理其状态。您需要先安装这些工具,以...
    99+
    2023-10-27
    服务器
  • 云服务器断开连接怎么解决问题呢
    如果您的云服务器断开连接并且您无法重新加载其配置文件,那么可能需要检查以下几个方面: 服务是否已停止您是否尝试使用自动更新或其他管理工具确保您的服务一直在运行。如果您无法重新加载配置文件,可能需要手动恢复您的服务。 是否收到来自云服务提...
    99+
    2023-10-27
    解决问题 服务器
  • 云服务器断开连接怎么解决的问题呢
    检查网络连接 首先,确保您的云服务器与互联网连接稳定。可以通过检查网络连接来排除网络故障。检查网络连接是否正常,可以使用ping命令测试。如果ping 服务器时出现问题,可以尝试重新连接。 检查硬件配置 如果云服务器硬件配置不佳,...
    99+
    2023-10-28
    服务器
  • 腾讯云服务器经常断连怎么解决问题呢
    一、服务器硬件故障 服务器硬件故障是导致腾讯云服务器经常断连的主要原因之一。服务器的硬件故障包括但不限于:服务器电源故障、内存故障、硬盘故障等。一旦服务器硬件出现故障,服务器的性能将会受到极大的影响,这将会导致数据的丢失和业务的中断。 解...
    99+
    2023-10-27
    腾讯 解决问题 服务器
  • elementui动态级联选择器回显问题怎么解决
    这篇文章主要讲解了“elementui动态级联选择器回显问题怎么解决”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“elementui动态级联选择器回显问题怎么解决”吧!场景描述后台返回类目数...
    99+
    2023-07-05
  • python指定路径斜杠与反斜杠遇到的问题怎么解决
    这篇“python指定路径斜杠与反斜杠遇到的问题怎么解决”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“python指定路径斜...
    99+
    2023-07-05
  • 云服务器断开连接怎么解决的问题有哪些
    检查网络连接 如果云服务器无法连接,首先需要检查网络连接。确保服务器与互联网之间的网络连接是正确的,并且网络连接没有出现异常或故障。如果网络连接正常,可以尝试使用其他设备连接到服务器,例如无线网络或有线网络。 检查防火墙设置 如果...
    99+
    2023-10-27
    服务器 有哪些
  • MySQL中怎么结合循环与子查询解决高级业务问题
    在MySQL中,可以结合循环与子查询来解决一些高级业务问题。以下是一个示例,展示如何使用循环和子查询来解决一个类似于累加的问题: 假...
    99+
    2024-04-29
    MySQL
  • 云服务器部署node项目失败怎么解决问题
    如果您的云服务器部署失败,请尝试以下解决方案: 查看错误消息:首先,您需要检查您的错误消息,以确定问题发生在哪里。这些错误消息可能包含有关服务器硬件或软件的细节,例如CPU、内存、磁盘空间等等。 重新启动云服务器:如果您的数据仍然存在,...
    99+
    2023-10-27
    解决问题 服务器 项目
  • laravel内部服务器间接口通信问题怎么解决
    本篇内容介绍了“laravel内部服务器间接口通信问题怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!在实际业务中,常有内部服务器间接...
    99+
    2023-07-04
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作