springcloud+Hystrix断路器

2024-08-28 02:38

本文主要是介绍springcloud+Hystrix断路器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

springcloud+Hystrix断路器

1.Hystrix简介及相关概念

1.1简介

Hystrix是一个用于处理分布式系统的延迟和容错的开源库,在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等;
Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。

1.2相关概念

1.服务雪崩:

​ 一个服务,依赖于另一个功能服务的,如果这个功能服务挂掉了,那么依赖的服务就不能再用了,这种级联的失败, 我们可以称之为雪崩。

2.服务降级:

​ 当服务提供者因为某些原因响应过慢,服务提供者主动停掉一些不太重要的业务,释放服务器资源,提高响应速度。

​ 当服务提供者因为某些原因不可用,服务消费者调用本地降级逻辑,迅速返回给客户,避免卡顿。

3.服务熔断

​ 请求错误率达到某一阈值,熔断器全开,产生熔断(熔断期间会对所有请求采用降级处理);

​ 到熔断时间窗口之后,熔断器会进入半开状态,此时会放过试验性请求 ;

​ 如果该试验性请求成功,熔断器进入关闭状态 ;

​ 如果该试验性请求失败,熔断器重新进入全开状态 。

2.Ribbon中使用Hystrix

2.1前置工作:

1.需要搭建Eureka server,我们以一台Eureka Server为例:

https://blog.csdn.net/u013071014/article/details/111031306

2.Ribbon负载均衡

https://blog.csdn.net/u013071014/article/details/111361176

2.2改造服务消费者项目

1.在pom.xml中新增依赖

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

2.在主启动类上增加@EnableHystrix注解,开启Hystrix断路器功能

package com.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;import com.springcloud.myRule.myRule;@SpringBootApplication
@EnableEurekaClient
@RibbonClient(name = "springcloud-consumer1", configuration = myRule.class)
@EnableHystrix
public class SpringCloudConsumerApplication {public static void main(String[] args) {// TODO Auto-generated method stubSpringApplication.run(SpringCloudConsumerApplication.class, args);}@Bean@LoadBalanced	//开启负载均衡public RestTemplate restTemplate() {return new RestTemplate();}}

3.在controller中需要有熔断机制的方法上添加@HystrixCommand注解,属性fallbackMethod是发生熔断时返回的方法。

package com.springcloud.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;@RestController
@RequestMapping(path = "/consumer")
public class ConsumerController {@Autowiredprivate RestTemplate restTemp;@HystrixCommand(fallbackMethod = "getDefaultResp") //发生熔断时,调用getDefaultResp方法@RequestMapping(path = "/getConsumer", method = RequestMethod.GET)public String getConsumer() {String result = restTemp.getForObject("http://springcloud-provider1/provider/getProvider", String.class);return result;}public String getDefaultResp() {return "发生熔断!!!";}
}

4.启动eureka server、provider以及consumer。

正常访问时:
在这里插入图片描述

此时关闭服务提供者,再次访问consumer的API
在这里插入图片描述

3.HystrixDashboard服务监控

在Hystrix中提供Hystrix Dashboard来进行微服务监控工作

3.1项目搭建步骤

1.在parent项目右键,New—> Maven Module,名称命名为springcloud-hystrix-dashboard

2.在pom.xml文件中引入依赖

<packaging>jar</packaging><dependencies><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>
</dependencies>

3.在需要被监控的服务的pom.xml文件中引入监控服务依赖库

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

4.在springcloud-hystrix-dashboard配置application.yml文件

server:port: 9999hystrix:dashboard:proxy-stream-allow-list: "*"# Actuator默认只启动了 health 和 info 端点,如下配置打开全部端点
management:endpoints:web:exposure:include: "*"

5.在springcloud-hystrix-dashboard添加主启动类,并使用@EnableHystrixDashboard注解

package com.springcloud;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);}
}

6.启动springcloud-hystrix-dashboard,访问http://localhost:9999/hystrix测试
在这里插入图片描述

7.在需要被监控的服务的主启动类中新增如下方法

@Bean
public ServletRegistrationBean getServlet(){HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);registrationBean.setLoadOnStartup(1);registrationBean.addUrlMappings("/actuator/hystrix.stream");registrationBean.setName("HystrixMetricsStreamServlet");return registrationBean;
}

8.启动eureka server、provider、consumer和dashboard,输入需要监控的url
在这里插入图片描述

3.2 dashboard数据解读

在这里插入图片描述

4.源码地址

https://github.com/DamonLiu666/springcloud_test

5.Feign + Hystrix使用

见下一篇博客:
https://blog.csdn.net/u013071014/article/details/111627988

这篇关于springcloud+Hystrix断路器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1113496

相关文章

Java反转字符串的五种方法总结

《Java反转字符串的五种方法总结》:本文主要介绍五种在Java中反转字符串的方法,包括使用StringBuilder的reverse()方法、字符数组、自定义StringBuilder方法、直接... 目录前言方法一:使用StringBuilder的reverse()方法方法二:使用字符数组方法三:使用自

JAVA封装多线程实现的方式及原理

《JAVA封装多线程实现的方式及原理》:本文主要介绍Java中封装多线程的原理和常见方式,通过封装可以简化多线程的使用,提高安全性,并增强代码的可维护性和可扩展性,需要的朋友可以参考下... 目录前言一、封装的目标二、常见的封装方式及原理总结前言在 Java 中,封装多线程的原理主要围绕着将多线程相关的操

Java进阶学习之如何开启远程调式

《Java进阶学习之如何开启远程调式》Java开发中的远程调试是一项至关重要的技能,特别是在处理生产环境的问题或者协作开发时,:本文主要介绍Java进阶学习之如何开启远程调式的相关资料,需要的朋友... 目录概述Java远程调试的开启与底层原理开启Java远程调试底层原理JVM参数总结&nbsMbKKXJx

Spring Cloud之注册中心Nacos的使用详解

《SpringCloud之注册中心Nacos的使用详解》本文介绍SpringCloudAlibaba中的Nacos组件,对比了Nacos与Eureka的区别,展示了如何在项目中引入SpringClo... 目录Naacos服务注册/服务发现引⼊Spring Cloud Alibaba依赖引入Naco编程s依

java导出pdf文件的详细实现方法

《java导出pdf文件的详细实现方法》:本文主要介绍java导出pdf文件的详细实现方法,包括制作模板、获取中文字体文件、实现后端服务以及前端发起请求并生成下载链接,需要的朋友可以参考下... 目录使用注意点包含内容1、制作pdf模板2、获取pdf导出中文需要的文件3、实现4、前端发起请求并生成下载链接使

Java springBoot初步使用websocket的代码示例

《JavaspringBoot初步使用websocket的代码示例》:本文主要介绍JavaspringBoot初步使用websocket的相关资料,WebSocket是一种实现实时双向通信的协... 目录一、什么是websocket二、依赖坐标地址1.springBoot父级依赖2.springBoot依赖

如何用java对接微信小程序下单后的发货接口

《如何用java对接微信小程序下单后的发货接口》:本文主要介绍在微信小程序后台实现发货通知的步骤,包括获取Access_token、使用RestTemplate调用发货接口、处理AccessTok... 目录配置参数 调用代码获取Access_token调用发货的接口类注意点总结配置参数 首先需要获取Ac

Java逻辑运算符之&&、|| 与&、 |的区别及应用

《Java逻辑运算符之&&、||与&、|的区别及应用》:本文主要介绍Java逻辑运算符之&&、||与&、|的区别及应用的相关资料,分别是&&、||与&、|,并探讨了它们在不同应用场景中... 目录前言一、基本概念与运算符介绍二、短路与与非短路与:&& 与 & 的区别1. &&:短路与(AND)2. &:非短

Java的volatile和sychronized底层实现原理解析

《Java的volatile和sychronized底层实现原理解析》文章详细介绍了Java中的synchronized和volatile关键字的底层实现原理,包括字节码层面、JVM层面的实现细节,以... 目录1. 概览2. Synchronized2.1 字节码层面2.2 JVM层面2.2.1 ente

什么是 Java 的 CyclicBarrier(代码示例)

《什么是Java的CyclicBarrier(代码示例)》CyclicBarrier是多线程协同的利器,适合需要多次同步的场景,本文通过代码示例讲解什么是Java的CyclicBarrier,感... 你的回答(口语化,面试场景)面试官:什么是 Java 的 CyclicBarrier?你:好的,我来举个例