SpringCloud4-HyStrix

版权声明:fromZjy QQ1045152332 https://blog.csdn.net/qq_36762677/article/details/84851800

官网: https://springcloud.cc/spring-cloud-dalston.html#_circuit_breaker_hystrix_clients
springcloud版本: 【Finchley 版】


服务雪崩效应:
是一种因 服务提供者 的不可用导致 服务调用者 的不可用,并将不可用 逐渐放大 的过程。

Hystrix

在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。

Hystrix提供了熔断、降级、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。

  • 熔断
    一般由某个服务鼓掌或异常引起,当某个异常被触发,直接熔断整个服务,而不是等待服务超时
  • 降级
    降级从整体负荷考虑,虽然服务熔断,但仍给返回值(客户端完成-消费者)
熔断简单应用
  1. 生产者pom文件
   <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
   </dependency>
  1. 生产者启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableCircuitBreaker//<<----------
public class UserHystrixApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserHystrixApplication.class, args);
    }
}
  1. 生产者controller层
    在这里插入图片描述
  2. 成果演示:

此示例缺点:

  • 高耦合,处理方法在controller
  • 每个请求都要带一个处理方法
服务降级应用

当整个微服务架构整体的负载超出了预设的上限阈值或即将到来的流量预计将会超过预设的阈值时,为了保证重要或基本的服务能正常运行,我们可以将一些 不重要或 不紧急 的服务或任务进行服务的 延迟使用 或 暂停使用

更多参考:[纯洁的微笑]https://baijiahao.baidu.com/s?id=1607017969929896292&wfr=spider&for=pc

HyStrix上下文传递

如果没有调用成功,则进入errorHandler方法,但是这是两个线程

可以使用某些配置或直接在注释中使用与使用相同的线程来调用Hystrix,方法是要求使用不同的“隔离策略”
,但是不建议修改"execution.isolation.strategy"

@RestController
public class HelloController {

    @Autowired
    private FeignClientOne feignClientOne;

    @RequestMapping("/movie")
    @HystrixCommand(fallbackMethod = "errorHandler", commandProperties = {
            @HystrixProperty(name="execution.isolation.strategy", value="SEMAPHORE")
    })
    public String hello() {

        return "feign movie:<br/>"+feignClientOne.getUser();
    }

    public String errorHandler(){
        return "Hystrix is working!<br> <b>404</b>";
    }
}
Hystrix Dashboard 仪表盘

点击进入官网
连接断路器的状态也暴露在呼叫应用程序的/health端点中。
http://192.168.0.101:8765/actuator/health

  <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>

http://localhost:8765/hystrix.stream 此路径如果不能访问,则需要加如下配置

@Configuration
public class HystrixConfig {
    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registration.addUrlMappings("/hystrix.stream");
        return registration;
    }
}

准实时的调用监控,记录执行信息,并以统计报表和图形的可视化界面展现给用户

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

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

启动类添加注解

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

访问地址: http://localhost:9003/hystrix

参考文章: https://blog.csdn.net/u013739073/article/details/80627087


feign的hystrix支持

在这里插入图片描述

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_36762677/article/details/84851800
今日推荐