13_springcloudHystrix订单服务调用支付服务出现卡顿——服务降级

Hystrix订单服务调用支付服务出现卡顿

支付服务JMete高并发压测后卡顿:

在这里用JMete工具模拟了一下生产者调用的高并发压力测试,如下图
在这里插入图片描述
对上面情况的说明:
如上图可以看出,只是对localhost:8001/payment/hystrix/timeout/31这个接口进行了测试,
得出的结果是这种高并发的情况下影响了,localhost:8001/payment/hystrix/ok/31这个接口访问的性能
上面还是服务提供者8001自己测试,假如此时外部消费者80也来访问,那消费者只能干等,最终导致消费端80不满意,服务端8001直接拖死。
必须要有hystrix容错限流框架,对我们系统进行保护。

hystrix对系统进行保护方式:

场景:
消费者80订单服务访问支付服务

1.新建module cloud-consumer-feign-hystrix-order80

2.pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>cloud2021</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-consumer-feign-hystrix-order80</artifactId>


    <dependencies>
        <!--hystrix-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--eureka client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--引入自定义的api通用包,可用使用Payment支付Entity-->
        <dependency>
            <groupId>com.atguigu.springcloud</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>${project.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

3.application.yml

server:
  port: 80
  ​
eureka:
  client:
     register-with-eureka: false
     service-url:
       defaultZone: http://eureka7001.com:7001/eureka/

4.主启动类

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@SpringBootApplication
@EnableFeignClients
public class OrderFeignMail80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMail80.class,args);
    }
}

5.业务类
service层接口:

package com.atguigu.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Component
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT")
public interface PaymentHystrixService {

    @GetMapping(value = "/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id);

    @GetMapping(value = "/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}

controller层:

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class OrderHystrixController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_OK(id);
        return result;
    }

    @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){

        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        return result;
    }
}

测试访问路径

得出的结论:

8001同一层次的其他接口服务困死,因为tomcat线程池里面的工作限流已经被挤占完毕。
80此时调用8001,客户端访问响应缓慢,转圈圈。

解决上述的问题:

思路:

  • 超时导致服务器变慢——超时不再等待 也就是当一直转圈圈甚至给客户呈现ERROE page的时候,不给呈现,给客户显示服务器异常
  • 出错(宕机或者程序运行出错)——出错要兜底

具体到模拟项目中的解决思路

  • 对方服务(8001)超时了,调用者(80) 不能一直卡死等待,必须有服务降级
  • 对方服务(8001)宕机了,调用者(80)不能一直卡死等待,必须有服务降级
  • 对方服务(8001)ok了,调用者(80)自己出故障或有自我要求(自己等待的时间小于服务提供者)

降级容错解决的维度:

先解决服务8001——代码更改

package com.atguigu.springcloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class PaymentService {
    /*
    * 这个模拟的是一条链路是正常的情况
    * */
    public String paymentInfo_OK(Integer id){
        return "线程池:"+Thread.currentThread().getName()+"   paymentInfo_OK,id:"+id+"\t"+"O(∩_∩)O哈哈~";
    }
    /*
    * 模拟超时3秒钟  这里不会报错只是会超时
    * */
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMillisecond",value = "3000")})
    public String paymentInfo_Timeout(Integer id){
        int timeNumber = 5;
        try{
            TimeUnit.SECONDS.sleep(timeNumber);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        return "线程池:"+Thread.currentThread().getName()+"   paymentInfo_Timeout,id:"+id+"\t"+"O(∩_∩)O哈哈~"+"   耗时(秒):"+timeNumber;
    }
    public String paymentInfo_TimeoutHandler(Integer id){
        return "线程池:"+Thread.currentThread().getName()+"系统繁忙或者运行报错,请稍后再试"+id+"\t"+"/(ㄒoㄒ)/~~";
    }
}

对上述注解的说明:
当服务出现超时时,不给用户显示一直转圈圈,而是显示超时页面。
也就是哪一个方法上面出现超时转圈圈,就给哪一个方法上面添加@HystrixCommand注解,让其跳转到另外一个页面,paymentInfo_TimeOutHandler这个方法就是需要跳转的页面,可以自己定义。
value = "3000"说明在3秒之后出现错误跳转到出错页面。
timeNumber运行需要5秒,按照这个设置5>3秒,出错跳转到出错页面。

8001服务主启动类添加@EnableCircuitBreaker注解——激活

解决服务80

80订单微服务,也可以更好的保护自己,自己也一样进行客户端降级保护
fallback消费端和生产端都可以使用,一般上也是放在消费端

修改配置文件application.yml开启feign 和 hystrix

server:
  port: 80
  ​
eureka:
  client:
     register-with-eureka: false
     service-url:
       defaultZone: http://eureka7001.com:7001/eureka/

feign:
  hystrix:
    enabled: true #开启feign和hystrix属性

主启动类添加@EnableHystrix注解

package com.atguigu.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableHystrix
public class OrderFeignMail80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderFeignMail80.class,args);
    }
}

80controller层更改:

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class OrderHystrixController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_OK(id);
        return result;
    }

    @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMillisecond",value = "1500")})
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){

        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        return result;
    }
    public String paymentInfo_TimeoutHandler(Integer id){
        return "线程池:"+Thread.currentThread().getName()+"系统繁忙或者运行报错,请稍后再试"+id+"\t"+"/(ㄒoㄒ)/~~";
    }
}

缺点:

这样的情况说明:每个业务方法对应一个兜底的方法,代码膨胀
进行统一和自定义的分开

全局服务降级:

@DefaultProperties(defaultFallback = “”)

  1. 每一个方法配置一个服务降级方法,技术上可以,但是很傻 比
  2. N除了个别重要核心业务有专属,其他普通的可以通过@DefaultProperties(defaultFallback = “”)统一跳转到统一处理结果页面
    通用的和独享的各自分开,避免了代码膨胀,合理减少了代码量

80controller层更改:

package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")
public class OrderHystrixController {

    @Autowired
    private PaymentHystrixService paymentHystrixService;

    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id){
        String result = paymentHystrixService.paymentInfo_OK(id);
        return result;
    }

    @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
    /*@HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
            @HystrixProperty(name="execution.isolation.thread.timeoutInMillisecond",value = "1500")})*/
    @HystrixCommand
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id){

        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        return result;
    }
    public String paymentInfo_TimeoutHandler(Integer id){
        return "线程池:"+Thread.currentThread().getName()+"系统繁忙或者运行报错,请稍后再试"+id+"\t"+"/(ㄒoㄒ)/~~";
    }

    //下面是全局处理fallback的方法
    public String payment_Global_FallbackMethod(){
        return "Global异常处理信息,请稍后重试。/(ㄒoㄒ)/~~";
    }
}

当生产者服务端down了,但是我们做了服务降级处理,让客户端在服务端不可用时也会获得提示信息而不会挂起耗死服务器

根据cloud-consumer-feign-hystrix-order80已经有的PaymentHystrixService接口,重新新建一个类实现该接口,统一为接口里面的方法进行异常处理。

package com.atguigu.springcloud.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Component
@FeignClient(value = "CLOUD-PRIVER-HYSTRIX-PAYMENT",fallback = PaymentHystrixService.class)
public interface PaymentHystrixService {

    @GetMapping(value = "/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id);

    @GetMapping(value = "/payment/hystrix/timeout/{id}")
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id);
}

package com.atguigu.springcloud.service;

import org.springframework.stereotype.Component;

@Component
public class PaymentHystrixServiceImpl implements PaymentHystrixService {
    @Override
    public String paymentInfo_OK(Integer id) {
        return "-----paymentFallBackService fall back-paymentInfo_ok";
    }

    @Override
    public String paymentInfo_TimeOut(Integer id) {
        return "-----paymentFallBackService fall back-paymentInfo_TimeOut";
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值