springcloud教程——Hystrix组件

目录

概述

线程隔离

服务熔断

案例

1.在消费端引入服务hystrix依赖

2.实现错误信息返回

  2.1在方法上添加注解,实现注解中声明的方法,此方法的返回值的请求参数与该方法一致

  2.2 在类上添加注解,返回类型与返回错误信息类型一致


概述

在微服务架构中,很多时候一个请求需要调用多个微服务实现,其中一个微服务出现阻塞,那么整个请求就会失败,如果不及时处理,其他请求会继续访问这个异常的微服务,系统的线程和资源是有限的,随着请求的不断增加,系统资源被耗尽从而导致其他服务也不可用。为了解决这个问题,springcloud整合了如Hystrix、Resilience4j等熔断器。

熔断器的作用主要为服务降级,降级的方式有两点:线程隔离,服务熔断。

线程隔离

每个微服务分配一个线程池,用户通过线程池中的线程去访问微服务,如果线程池中的线程被用完则进行服务降级,向用户返回一个执行结果,例如“服务器忙!”,不会让请求一直等待,由于请求的线程池线程有限,也不会影响服务器其它服务的正常运行。

服务熔断

close:所有请求正常

open:熔断器打开,根据熔断器配置进行熔断处理

half open:reset timeout部分请求通过

说明:熔断开启的机制就是监测请求是否正常,如果这些请求都是成功的,熔断器关闭,反之开启熔断再次进入休眠,继续监测请求是否正常,全部正常关闭熔断,继续循环。

 

案例

1.在消费端引入服务hystrix依赖

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

2.实现错误信息返回

  2.1在方法上添加注解,实现注解中声明的方法,此方法的返回值的请求参数与该方法一致

package com.example.controller;

        import com.example.pojo.User;
        import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
        import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
        import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.cloud.client.ServiceInstance;
        import org.springframework.cloud.client.discovery.DiscoveryClient;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;
        import org.springframework.web.client.RestTemplate;

        import java.util.List;

/**
 * @program: mylearncode
 * @description:
 * @author: yuan long
 * @create: 2020-12-14 12:00
 */
@RestController
@RequestMapping("/consumer")
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/{id}")
    @HystrixCommand(fallbackMethod = "backMessage")
    public String getUser(@PathVariable Integer id) {

        String url = "http://springcloud-server/testserver/" + id;
        return restTemplate.getForObject(url, String.class);
    }

    public String backMessage(Integer id){
      return "服务器拥挤";
    }

}

  2.2 在类上添加注解,返回类型与返回错误信息类型一致

package com.example.controller;

        import com.example.pojo.User;
        import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
        import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
        import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.cloud.client.ServiceInstance;
        import org.springframework.cloud.client.discovery.DiscoveryClient;
        import org.springframework.web.bind.annotation.GetMapping;
        import org.springframework.web.bind.annotation.PathVariable;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RestController;
        import org.springframework.web.client.RestTemplate;

        import java.util.List;

/**
 * @program: mylearncode
 * @description:
 * @author: yuan long
 * @create: 2020-12-14 12:00
 */
@RestController
@RequestMapping("/consumer")
@DefaultProperties(defaultFallback = "backMessage1")
public class ConsumerController {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    DiscoveryClient discoveryClient;

    @GetMapping("/{id}")
    @HystrixCommand
    public String getUser(@PathVariable Integer id) {
        String url = "http://springcloud-server/testserver/" + id;
        return restTemplate.getForObject(url, String.class);
    }


    public String backMessage1(){
        return "网络拥挤";
    }


}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值