Hystrix之服务熔断与服务降级

高并发处理的两种方式

  1. 线程池隔离
  2. 信号量隔离

在这里插入图片描述

1、对RestTemplete的支持(略)

2、对Feign的支持

2.1 引入依赖

feign中已经继承Hystrix。

2.2 在Feign中开启Hystrix

在调用方的微服务的application.properties中开启。

#开启Hystrix的支持
feign.hystrix.enabled=true

2.3 自定义一个接口实现类

这个实现类就是熔断触发的降级逻辑。

package com.yan.order.feign;

import org.springframework.stereotype.Component;

import com.yan.order.entity.Product;
@Component
public class ProductFeignClientCallBack implements ProductFeignClient {

	/**
	 * 熔断降级的方法
	 */
	@Override
	public Product findById(Long id) {
		Product product = new Product();
		product.setProduct_name("调用熔断降级的方法");
		return product;
	}

}

2.4 修改FeignClient接口添加降级方法支持

package com.yan.order.feign;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yan.order.entity.Product;

/**
 * 声明需要调用微服务的名称
 * @FeignClient
 *   name : 服务提供者的名称
 *   fallback : 熔断降级的实现类
 * @author Administrator
 *
 */
@FeignClient(name="product-service",fallback=ProductFeignClientCallBack.class)
public interface ProductFeignClient {
	
	@RequestMapping(value="/product/{id}")
	public Product findById(@PathVariable("id")Long id);
}

3、Hystrix的超时时间配置

在这里插入图片描述

4、Hystrix的监控平台

在这里插入图片描述
引入依赖

<!-- actuator健康检查 -->
		<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>
		<!-- 监控平台 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
		</dependency>

在启动类上配置

// 激活hystrix
@EnableCircuitBreaker

暴露所有的hystrix监控的端口

#开放所有端口
management.endpoints.web.exposure.include=*

在浏览器上访问

http://localhost:9002/actuator/hystrix.stream

4.1、搭建Hystrix的dashboard监控平台

4.1.1 引入依赖
<!-- actuator健康检查 -->
		<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>
		<!-- 监控平台 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
		</dependency>
4.1.2 在启动类上激活dashboard仪表盘
//激活hystrix的web监控平台
@EnableHystrixDashboard
4.1.3 访问监控平台
http://localhost:9002/hystrix

将监控地址(http://localhost:9002/actuator/hystrix.stream)输入平台进行监控。

4.1.4 监控页面

在这里插入图片描述

5、Turbine的监控平台

Turbine

5.1、创建hystrix-turbine工程

5.2、引入依赖

<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-turbine</artifactId>
		</dependency>
		<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>

5.3、application配置

server.port=8031
spring.application.name=hystrix-turbine

#配置eurekaClient
eureka.client.service-url.defaultZone=http://localhost:9000/eureka/
#使用IP地址注册
eureka.instance.prefer-ip-address=true

##turbine配置
#要监控的微服务列表,多个用,分隔
turbine.appConfig=order-service
turbine.clusterNameExpression='default'

5.4、激活turbine

package com.yan.turbine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
@SpringBootApplication
//配置turbine
@EnableTurbine
@EnableHystrixDashboard
public class TurbineApplication {
	
	public static void main(String[] args) {
		SpringApplication.run(TurbineApplication.class, args);
	}
}

5.5、页面测试

先在页面输入地址

http://localhost:8031/hystrix

再输入监控地址

http://localhost:8031/turbine.stream

在这里插入图片描述

6、断路器

在这里插入图片描述
在这里插入图片描述
代码编写
在这里插入图片描述
配置文件
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值