专注于 JetBrains IDEA 全家桶,永久激活,教程
持续更新 PyCharm,IDEA,WebStorm,PhpStorm,DataGrip,RubyMine,CLion,AppCode 永久激活教程

SpringCloud(二)Hystrix服务容错

35_1.png

1.Hystrix基本概念

Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.

What does it do?

1、 Latency and Fault Tolerance Stop cascading failures. Fallbacks and graceful degradation. Fail fast and rapid recovery.

Thread and semaphore isolation with circuit breakers.

1、 Realtime Operations Realtime monitoring and configuration changes. Watch service and property changes take effect immediately as they spread across a fleet.

Be alerted, make decisions, affect change and see results in seconds.

1、 Concurrency Parallel execution. Concurrency aware request caching. Automated batching through request collapsing.

⚠️注意:Hystrix官方已停止对其的维护

2.基于Hystrix构建服务

我们这里构建两个服务,书店服务BookStore,提供图书借阅服务;读者服务Reading,远程调用书店的借阅服务,使用Hystrix对Reading的远程调用进行包装。(是的,Hystrix是在上游的调用方进行配置)

2.1.构建BookStore服务

BookstoreApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

@RestController
@SpringBootApplication
public class BookstoreApplication {

  @RequestMapping(value = "/recommended")
  public String readingList(){
    return "BookStore 调用成功,祝阅读愉快";
  }

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

BookStore/resources/application.properties

server.port=8090

2.2.构建读者服务

ReadingApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.web.client.RestTemplate;

@EnableCircuitBreaker   #启用hystrix进行熔断
@RestController
@SpringBootApplication
public class ReadingApplication {

  @Autowired
  private BookService bookService;

  @Bean
  public RestTemplate rest(RestTemplateBuilder builder) {
    return builder.build();
  }

  @RequestMapping("/to-read")
  public String toRead() {
    return bookService.readingList();
  }

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

最关键的一步,使用Hystrix包装远程调用方法 BookService.java

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.net.URI;

@Service
public class BookService {

  private final RestTemplate restTemplate;

  public BookService(RestTemplate rest) {
    this.restTemplate = rest;
  }

  @HystrixCommand(fallbackMethod = "reliable")  #远程服务失败,则回调fallbackMethod
  public String readingList() {
    URI uri = URI.create("http://localhost:8090/recommended");

    return this.restTemplate.getForObject(uri, String.class);
  }

  public String reliable() {
    return "BookStore 调用失败";
  }

}

Reading/resources/application.properties

server.port=8080

2.3.测试

分别启动BookstoreApplication和ReadingApplication

访问 localhost:8080/to-read

35_2.png

停止BookstoreApplication,再次访问localhost:8080/to-read,可以看到,hystrix已启用了熔断

35_3.png

3.Hystrix Dashboard

同时,Hystrix还有一套自我监控体系——Hystrix Dashboard。Hystrix Dashboard可以对Hystrix进行监控,并提供可视化监测,方便开发、运维人员随时查看当前服务将康状态。接下来我们来构建Hystrix DashBoard。

3.1.修改Reading服务

配置Reading服务暴露actuator,同时配置hystrix.stream,使得其可以被Hystrix Dashboard监测到。(同时在pom.xml添加相应依赖)

Reading/resources/application.properties

server.port=8080
management.server.port=8888
management.endpoint.health.show-details=always
management.endpoints.web.exposure.exclude='*'
management.endpoints.web.exposure.include='hystrix.stream'

3.2.构建Hystrix Dashboard服务

新建一个Spring工程,HystrixDashboard

HystrixDashboardApplication.java

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

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

建立HystrixIndexController,提供一个对外接口 HystrixIndexController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HystrixIndexController {
  @GetMapping("")
  public String index() {
    return "forward:/hystrix";
  }
}

HystrixDashboard/resources/application.properties

server.port=8030

3.3. 测试

启动BookstoreApplication和ReadingApplication

访问一次ReadingApplication的to-read接口,即localhost:8090/to-read

访问localhost:8888/actuator/hytrix.stream,可以看到已经有Hytrix的信息了。

35_4.png

光是这样还不行,这么多日志不方便查看,接下来启动可视化面板。

启动HystrixDashboardApplication,访问localhost:8030,可以看到Dashboard页面如下。 把刚才的Hystrix服务链接和title填入,点击Monitor Stream

35_5.png

就可以看到可视化的Hystrix Dashborad了。 停止BookStore服务,再调用几次to-read接口,可以看到面板中已经做出了相应的警告。

35_6.png

面板参数说明如下

35_7.png

4. Hystrix主要配置

本文仅对Hystrix熔断降级进行了简单的实验,Hystrix功能丰富,支持各种配置,感兴趣的同学可以参考以下链接

github.com/Netflix/Hys… github.com/Netflix/Hys…

参考:

github.com/Netflix/Hys…

Spring-Guides: Circuit Breaker

Hystrix快速入门(6) hystrix-dashboard

跟我学Spring Cloud(Finchley版)-15-Hystrix监控详解

文章永久链接:https://tech.souyunku.com/37974

未经允许不得转载:搜云库技术团队 » SpringCloud(二)Hystrix服务容错

JetBrains 全家桶,激活、破解、教程

提供 JetBrains 全家桶激活码、注册码、破解补丁下载及详细激活教程,支持 IntelliJ IDEA、PyCharm、WebStorm 等工具的永久激活。无论是破解教程,还是最新激活码,均可免费获得,帮助开发者解决常见激活问题,确保轻松破解并快速使用 JetBrains 软件。获取免费的破解补丁和激活码,快速解决激活难题,全面覆盖 2024/2025 版本!

联系我们联系我们