SpringCloud搭建微服务框架(二)- Hystrix断路器

Spring Cloud Hystrix 实现了断路器、线路隔离等一系列服务保护功能。它是基于 Netflix 的开源框架 Hystrix 实现的,该框架的目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix 具备服务降级、服务熔断、线程和信号隔离、请求缓存、请求合并以及服务监控等强大功能。

由于微服务框架肯定会涉及服务间调用,当由于网络原因或微服务故障不可用,导致调用方大量线程堆积可能会导致调用方微服务不可用,所以需使用Hystrix熔断机制,当调用服务超时时,执行熔断后的方法。

这部分的内容需要以之前的部分内容为基础(即上一篇博客https://blog.csdn.net/weixin_38823568/article/details/81980472中的服务提供方和服务消费方(服务消费方需调用服务提供方的接口)),主要添加服务消费方的配置。

项目结构如下(相较未使用Hystrix断路器时,多了红色框中的部分):

1.在pom.xml文件中添加Hystrix断路器相关jar包(刚刚看上一篇博客发现已经添加了相关jar包,若直接复制上篇博客相应的pom.xml文件,可不用再添加)

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
 
<dependency>
    <groupId>com.netflix.hystrix</groupId>
    <artifactId>hystrix-javanica</artifactId>
    <version>1.5.12</version>
</dependency>

2.在application.yml中添加如下内容:

# 启用熔断
feign:
  hystrix:
    enabled: true
 
# 设置熔断超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 2000 # 单位毫秒

添加断路器配置之后的完整配置文件如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
server:
  port: 8781
spring:
  application:
    name: parameter-consumer
 
# 启用熔断
feign:
  hystrix:
    enabled: true
 
# 设置熔断超时时间
hystrix:
  command:
    default:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 2000 # 单位毫秒

3.在启动类上添加注解,允许断路器

添加的注解如下:

//允许断路器
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker

添加注解后的启动类代码如下:

package com.example.serviceconsumer;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
 
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
//允许断路器
@EnableHystrix
@EnableHystrixDashboard
@EnableCircuitBreaker
public class ServiceConsumerApplication {
 
    public static void main(String[] args) {
        SpringApplication.run(ServiceConsumerApplication.class, args);
    }
}

4.添加调用超时熔断时执行的类HelloServiceImpl.java(不要漏掉@Component注解,否则项目启动时会报错

package com.example.serviceconsumer.service.impl;
 
import com.example.serviceconsumer.service.HelloService;
import org.springframework.stereotype.Component;
 
@Component
public class HelloServiceImpl implements HelloService {
 
    @Override
    public String sayHello(String name){
        return "调用服务超时,请稍后再试。";
    }
}

5.修改HelloService.java,配置熔断时的执行类HelloServiceImpl.class(在@FeignClient注解中添加fallback属性)

package com.example.serviceconsumer.service;
 
import com.example.serviceconsumer.service.impl.HelloServiceImpl;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
 
@Component
@EnableFeignClients
//注册在服务中心的application name
@FeignClient(value = "parameter-module", fallback = HelloServiceImpl.class)
public interface HelloService {
 
    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    String sayHello(@RequestParam(value = "name") String name);
}

配置完成后,重启服务消费方应用程序,访问http://localhost:8781/hello?name=world,停掉服务提供方(或者在Intelli IDEA中debug模式下打断点),会看到如下画面,可见当调用超时时,确实到fallback属性指定的类中执行相应方法:

发布了184 篇原创文章 · 获赞 73 · 访问量 37万+

猜你喜欢

转载自blog.csdn.net/qq_32521313/article/details/102822464