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 stub
		SpringApplication.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 {
	
	@Autowired
	private 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: 9999
  
hystrix:
  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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值