SpringCloud微服务架构开发——05服务容错保护措施Hystrix

目录

什么是Hystrix?

为什么用Hystrix?

分布式系统中服务请求一切正常情况

分布式系统中服务有一个系统有延迟请求情况

分布式系统中服务高流量下请求情况

分布式系统中Hystrix解决服务请求堵塞情况

Hystrix快速入门 

创建eureka-server项目作为注册中心

 创建eureka-hystrix-client项目,导入依赖

创建config包,在config包下创建HystrixConfig类 

新建controller包,在controller包下新建LocalItemController类 

 新建service包,在service包下新建LocalItemService类

创建hystrix-provider项目 

新建controller包,在controller包下的创建HystrixController类

 启动测试项目

 结论

在Feign中使用Hystrix熔断 

开启Hystrix熔断 

开启Feign Client功能 

修改项目中接口 

启动测试项目 

 结论

在Feign中使用Hystrix熔断

基本流程:

(1)开启Hystrix熔断

(2)开启Feign Client功能

(3)修改项目中接口

结论


什么是Hystrix?

熔断机制是解决微服务架构中因等待出现故障的依赖方响应而形成任务挤压,最终导致自身服务瘫痪的一种机制,它的功能类似电路的保险丝,其目的是为了阻断故障,从而保护系统稳定性。Hystrix作为Spring Cloud中实现了熔断机制的组件,具有服务容错保护功能。

概述:Hystrix是Netflix开源的一款针对分布式系统延迟和容错的库。

作用:通过添加延迟容忍和容错逻辑,从而控制分布式服务之间的交互。

为什么用Hystrix?

对于一个复杂的分布式系统,包含的应用可能多达数十个,这些应用有许多依赖项目,每个依赖项目在某个时刻不可避免会失败导致故障,如果不对这些故障进行隔离,整个分布式系统都可能会崩溃。

分布式系统中服务请求一切正常情况

分布式系统中服务有一个系统有延迟请求情况

当分布式系统中其中有一个系统有延迟时,它可能阻塞整个用户请求

分布式系统中服务高流量下请求情况

在高流量情况下,一个后端的依赖延迟可能会导致所有服务的资源在数秒内变的饱和,这也就意味着,后续如果再有请求将无法提供服务,应用会出现故障。比故障更糟糕的是,这些应用程序还可能导致服务之间的延迟增加,从而备份队列、线程和其他资源,从而导致整个系统出现更多级联故障.如图:

  

分布式系统中Hystrix解决服务请求堵塞情况

 Hystrix的出现就是为了解决上述问题的,它封装了每个依赖项,每个依赖项彼此隔离,当延迟发生时,它会被限制在资源中,并包含回退逻辑,该逻辑决定在依赖发生任何类型故障时应作出何种响应。  

Hystrix快速入门 

创建eureka-server项目作为注册中心

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}
spring:
  application:
    name: eureka-server
server:
  port: 7000
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
  instance:
    hostname: localhost
  server:
    enable-self-preservation: false

 创建eureka-hystrix-client项目,导入依赖

使用Spring Initializr方式创建一个名称为eureka-hystrix-client的Spring Boot项目,并添加Eureka Client、Web、Feign、Ribbon、Hystrix依赖。     其中Hystrix依赖如下:

<dependency>
     <groupId>org.springframework.cloud</groupId>    
     <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
@EnableEurekaClient
@EnableHystrix   #开启熔断功能
@SpringBootApplication
public class EurekaHystrixClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaHystrixClientApplication.class, args);
    }

}
server:
  port: 8764
spring:
  application:
    name: eureka-hystrix-client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7000/eureka/
    instance:
      hostname: localhost
feign:
  hystrix:
    enabled: true

创建config包,在config包下创建HystrixConfig类 

@Configuration
public class HystrixConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }
}

新建controller包,在controller包下新建LocalItemController类 

@RestController
 public class LocalItemController {
    @Autowired
    LocalItemService localItemService;
    @GetMapping("/hi")
    public String hi(String id) {
          return localItemService.hi(id);
   }
}

 新建service包,在service包下新建LocalItemService类

@Service
public class LocalItemService {
   @Autowired
   private RestTemplate restTemplate;
   @HystrixCommand(fallbackMethod = "hiError")
   public String hi(String id) {
        return restTemplate.getForObject
       ("http://localhost:hystrix-provider/hi?id=" + id, String.class);
    }
    public String hiError(String id) {
        return  String.format("Hi,your message is : %s but 
    request bad.",id);
    }
}

 被@HystrixCommand注解修饰的hi()方法就启动了Hystrix熔断器的功能fallbackMethod属性定义的是处理回退(fallback)逻辑的方法。如果必须在fallback逻辑方法中远程调度其他服务,最好在远程调度其他服务时,也加上熔断器。

创建hystrix-provider项目 

使用Spring Initializr方式创建一个名称为hystrix-provider的Spring Boot项目,并添加Eureka Client、Test、Web依赖。

新建controller包,在controller包下的创建HystrixController类

@RequestMapping("/hi")
public String hi(String id){
      return "Hello World, I'm from hystix!"+id;
  }

 启动测试项目

启动服务并进行测试。依次启动eureka-server,hystrix-provider,eureka-hystrix-client,在浏览器输入http://localhost:8764/hi?id=12,浏览器显示的效果。

关闭服务提供者eureka-provider,制造服务不可用的情形。再次请求 http://localhost:8764/hi?id=12浏览器显示的效果。 

 结论

当服务提供者hystrix-provider不可用的情况下,如果服务消费者eureka-hystrix-client调用服务提供者hystrix-provider的“/hi”方法,就会失败,此时会开启熔断器。熔断器打开后,请求会直接执行fallbackMethod逻辑,通过快速失败,做到及时处理请求,避免线程被阻塞。

在Feign中使用Hystrix熔断 

开启Hystrix熔断 

Feign自带熔断功能,默认情况下,熔断功能是关闭的。如果要开启熔断,只需在配置文件中将hystrix.enabled设置为true即可。

feign:
  hystrix:
    enabled: true

开启Feign Client功能 

在eureka-hystrix-client的启动类中添加@EnableFeignClients开启Feign Client功能。

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

修改项目中接口 

进入项目eureka-hystrix-client,在LocalItemService接口上方的@FeignClient注解中,增加fallback属性配置,指定LocalItemServiceImpl类为失败逻辑处理类

@FeignClient (value = "hystrix-provider",
              configuration =HystrixConfig.class,
              fallback = LocalItemServiceImpl.class)
public interface LocalItemService {
     @RequestMapping(value = "/hi", method = RequestMethod.GET)
     public String hi(@RequestParam(value = "id")String id);
}  

注意的是,使用fallback属性指定的用于处理回退逻辑的类,必须实现@FeignClient注解修饰的接口。 

在service包中新建失败逻辑处理类LocalItemServiceImpl,并实现被@FeignClient修饰的LocalItemService接口。

@Component
public class LocalItemServiceImpl implements LocalItemService
 {
    @Override
    public String hi(String id) {
        return String.format("Hi,your message is : %s but request 
        bad.",id);
    }
}

LocalItemServiceImpl实现了LocalItemService接口,并定义了一个hi()方法用于编写处理熔断的具体逻辑。这里,我们使用@Component注解修饰类LocalItemServiceImpl,其目的是将该类交给Spring容器管理

启动测试项目 

启动服务并进行测试。依次启动服务eureka-server, hystrix-provider, eureka-hystrix-client,访问http://localhost:8764/hi?id=hello,浏览器显示的效果如下所示。

 关闭提供者hystrix-provider并请求http://localhost:8764/hi?id=hello,发现调用了熔断器的失败处理逻辑方法,表明熔断器已经启动。

 结论

当服务提供者hystrix-provider不可用的情况下,如果服务消费者eureka-hystrix-client调用服务提供者hystrix-provider的“/hi”方法,就会失败,此时会开启熔断器。熔断器打开后,请求会直接执行fallback属性指定的用于处理回退逻辑的类,通过快速失败,做到及时处理请求,避免线程被阻塞。

在Feign中使用Hystrix熔断

基本流程:

1.开启Hystrix熔断

2.开启Feign Client功能

3.修改项目中接口

4.项目测试

(1)开启Hystrix熔断

Feign自带熔断功能,默认情况下,熔断功能是关闭的。如果要开启熔断,只需在配置文件中将hystrix.enabled设置为true即可。

开启Hystrix熔断功能。在eureka-hystrix-client项目的配置文件application.yml中添加开启熔断的配置

feign:
  hystrix:
    enabled: true

注:因为在Feign的起步依赖中引入了Hystrix依赖,所以在Feign中使用Hystrix不需要引入任何的依赖,只需要在配置文件中开启即可。

(2)开启Feign Client功能

在eureka-hystrix-client的启动类中添加@EnableFeignClients开启Feign Client功能。

@SpringBootApplication
@EnableHystrix
@EnableFeignClients  #开启feign功能
@EnableEurekaClient
public class EurekaHystrixClientApplication {
     public static void main(String[] args) {
          SpringApplication.run
       (EurekaHystrixClientApplication.class, args);
   }
}

(3)修改项目中接口

进入项目eureka-hystrix-client,在LocalItemService接口上方的@FeignClient注解中,增加fallback属性配置,指定LocalItemServiceImpl类为失败逻辑处理类

@FeignClient (value = "hystrix-provider",
              configuration =HystrixConfig.class,
              fallback = LocalItemServiceImpl.class)
public interface LocalItemService {
     @RequestMapping(value = "/hi", method = RequestMethod.GET)
     public String hi(@RequestParam(value = "id")String id);
}  

注:注意的是,使用fallback属性指定的用于处理回退逻辑的类,必须实现@FeignClient注解修饰的接口。

在service包中新建失败逻辑处理类LocalItemServiceImpl,并实现被@FeignClient修饰的LocalItemService接口。

@Component
public class LocalItemServiceImpl implements LocalItemService
 {
    @Override
    public String hi(String id) {
        return String.format("Hi,your message is : %s but request 
        bad.",id);
    }
}

LocalItemServiceImpl实现了LocalItemService接口,并定义了一个hi()方法用于编写处理熔断的具体逻辑。这里,我们使用@Component注解修饰类LocalItemServiceImpl,其目的是将该类交给Spring容器管理。

结论

   当服务提供者hystrix-provider不可用的情况下,如果服务消费者eureka-hystrix-client调用服务提供者hystrix-provider的“/hi”方法,就会失败,此时会开启熔断器。熔断器打开后,请求会直接执行fallback属性指定的用于处理回退逻辑的类,通过快速失败,做到及时处理请求,避免线程被阻塞。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值