微服务学习笔记八 Spring Cloud Hystrix容错机制

本文涉及的产品
注册配置 MSE Nacos/ZooKeeper,118元/月
云原生网关 MSE Higress,422元/月
服务治理 MSE Sentinel/OpenSergo,Agent数量 不受限
简介: 微服务学习笔记八 Spring Cloud Hystrix容错机制

**Hystrix 容错机制**

在不改变各个服务器调用关系的前提下,针对错误情况进行预先处理。

设计原则:

1)服务隔离机制

2)服务降级机制

3)熔断机制

4)提供实时的监控和报警功能

5)提供实时的配置修改功能

Hystrix数据监控需要结合Spring Cloud Actuator来使用,Actuator提供了

对服务的健康监控、数据统计,可以通过hystrix.stream节点获取监控的请求数据,

提供了可视化的监控界面。

创建maven模块

pom.xml


```yaml

<dependencies>

   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>


   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-starter-openfeign</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>


   <dependency>

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-actuator</artifactId>

       <version>2.0.7.RELEASE</version>

   </dependency>


   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>


   <dependency>

       <groupId>org.springframework.cloud</groupId>

       <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>

       <version>2.0.2.RELEASE</version>

   </dependency>

</dependencies>

```

创建配置文件 application.yml


```yaml

server:

 port: 8060

spring:

 application:

   name: hystrix

eureka:

 client:

   service-url:

     defaultZone: http://localhost:8761/eureka/

 instance:

   prefer-ip-address: true

feign:

 hystrix:

   enabled: true

management:

 endpoints:

   web:

     exposure:

       include: 'hystrix.stream'

```

创建启动类


```java

package com.shuang;



import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;

import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;

import org.springframework.cloud.openfeign.EnableFeignClients;


@SpringBootApplication

@EnableFeignClients

@EnableCircuitBreaker

@EnableHystrixDashboard

public class HystrixApplication {

   public static void main(String[] args) {

       SpringApplication.run(HystrixApplication.class,args);

   }

}

```

注解说明:

@EnableCircuitBreaker:声明启用数据监控

@EnableHystrixDashboard:声明启用可视化数据监控

Handler


```java

package com.shuang.controller;


import com.shuang.entity.Student;

import com.shuang.feign.FeignProviderClient;

import org.springframework.beans.factory.annotation.Autowired;

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

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;


import java.util.Collection;


@RestController

@RequestMapping("/hystrix")

public class HystrixHandler {

   @Autowired

   private FeignProviderClient feignProviderClient;


   @GetMapping("/findAll")

   public Collection<Student> findAll(){

       return feignProviderClient.findAll();

   }


   @GetMapping("/index")

   public String index(){

       return feignProviderClient.index();

   }

}

```

启动成功后,访问http://localhost:8060/actuator/hystrix.stream 可以监控到请求数据

访问 http://localhost:8060/hystrix,可以看到可视化的监控界面,输入要监控的地址节点,

即可看到该节点的可视化数据监控。


![在这里插入图片描述](https://ucc.alicdn.com/images/user-upload-01/20200714185225301.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ0OTY5NjQz,size_16,color_FFFFFF,t_70)

feign提供了服务熔断,与笔记7相同,这里就不啰嗦了

目录
相关文章
|
1月前
|
监控 负载均衡 Java
深入理解Spring Cloud中的服务网关
深入理解Spring Cloud中的服务网关
|
1月前
|
Java 开发工具 git
实现基于Spring Cloud的配置中心
实现基于Spring Cloud的配置中心
|
1月前
|
设计模式 监控 Java
解析Spring Cloud中的断路器模式原理
解析Spring Cloud中的断路器模式原理
|
5天前
|
Java 开发工具 Spring
Spring的Factories机制介绍
Spring的Factories机制介绍
10 1
|
23天前
|
负载均衡 Java Spring
Spring cloud gateway 如何在路由时进行负载均衡
Spring cloud gateway 如何在路由时进行负载均衡
148 15
|
29天前
|
资源调度 Java 调度
Spring Cloud Alibaba 集成分布式定时任务调度功能
Spring Cloud Alibaba 发布了 Scheduling 任务调度模块 [#3732]提供了一套开源、轻量级、高可用的定时任务解决方案,帮助您快速开发微服务体系下的分布式定时任务。
14229 19
|
22天前
|
Java Spring
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
spring cloud gateway在使用 zookeeper 注册中心时,配置https 进行服务转发
41 3
|
1月前
|
消息中间件 Java 开发者
Spring Cloud微服务框架:构建高可用、分布式系统的现代架构
Spring Cloud是一个开源的微服务框架,旨在帮助开发者快速构建在分布式系统环境中运行的服务。它提供了一系列工具,用于在分布式系统中配置、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导选举、分布式会话、集群状态等领域的支持。
119 5
|
1月前
|
Java API 开发工具
Spring Boot与Spring Cloud Config的集成
Spring Boot与Spring Cloud Config的集成

热门文章

最新文章