SpringCloud之断路器Hystrix

版权声明: https://blog.csdn.net/qq_24313635/article/details/84144382

断路器Hystrix简介

由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应
雪崩应对策略:

  • 流量控制:控制的方式有很多种,类似队列,令牌,漏桶等等。
  • 网关限流: 因为Nginx的高性能, 目前一线互联网公司大量采用Nginx+Lua的网关进行流量控制, 由此而来的OpenResty也越来越热门. 使用OpenResty,其是由Nginx核心加很多第三方模块组成,其最大的亮点是默认集成了Lua开发环境,使得Nginx可以作为一个Web Server使用。 借助于Nginx事件驱动模型非阻塞IO,可以实现高性能的Web应用程序。
    而且OpenResty提供了大量组件如Mysql、Redis、Memcached等等,使在Nginx上开发Web应用更方便更简单。目前在京东如实时价格、秒杀、动态服务、单品页、列表页等都在使用Nginx+Lua架构,其他公司如淘宝、去哪儿网等。
  • 用户交互限流:友好的提示,从源端限制流量流入。

基于Netflix的开源框架 Hystrix实现的 框架目标在于通过控制那些访问远程系统、服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。Hystrix具备了服务降级、服务熔断、线程隔离、请求缓存、请求合并以及服务监控等强大功能。

image

断路器Hystrix实践

一、在Ribbon中使用Hystrix

①pom文件添加依赖

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

②启动类上面,添加注解@EnableHystrix

③RibbonService

package com.yj.hystrix.ribbon.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

@Service
public class RibbonService {

	@Autowired
	private RestTemplate restTemplate;

	@HystrixCommand(fallbackMethod = "ribbonError")
	public String hiRibbon(String name) {
		return restTemplate.getForObject("http://EUREKA-CLIENT/hiEureka?name=" + name, String.class);
	}

	public String ribbonError(String name) {
		return "sorry " + name;
	}
}

二、在Feign中使用断路器

①Feign是自带断路器的,在D版本的Spring Cloud之后,它没有默认打开。需要在application.properties配置文件中配置开启:

feign.hystrix.enabled=true

②FeignService

package com.yj.hystrix.feign.service;

import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@FeignClient(value = "eureka-client",fallback = FeignServiceHiHystric.class)
public interface FeignService {
	@RequestMapping(value = "/hiEureka", method = RequestMethod.GET)
	String hiFeign(@RequestParam(value = "name") String name);
}

③FeignServiceHiHystric

package com.yj.hystrix.feign.service;

import org.springframework.stereotype.Component;

@Component
public class FeignServiceHiHystric implements FeignService {
	@Override
	public String hiFeign(String name) {
		 return "sorry "+name;
	}
}

④HystrixController

package com.yj.hystrix.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.yj.hystrix.feign.service.FeignService;
import com.yj.hystrix.ribbon.service.RibbonService;

@RestController
public class HystrixController {

	@Autowired
	private FeignService feignService;
	
	@Autowired
	private RibbonService ribbonService;

	@GetMapping(value = "/hiHystrixFeign")
	public String hiFeign(@RequestParam String name) {
		return feignService.hiFeign(name);
	}
	
	@GetMapping(value = "/hiHystrixRibbon")
	public String hiRibbon(@RequestParam String name) {
		return ribbonService.hiRibbon(name);
	}
}

⑤部署启动,将Hystrix项目也注册到Eureka

当eureka-client项目正常启动时,访问 http://192.168.37.141:8007/hiHystrixRibbon?name=yj或者http://192.168.37.141:8007/hiHystrixFeign?name=yj,均显示正常 hi yj ,i am from port:8004

eureka-client项目关闭后,访问 http://192.168.37.141:8007/hiHystrixRibbon?name=yj或者http://192.168.37.141:8007/hiHystrixFeign?name=yj,显示 sorry yj,则可以验证Hystrix生效了

猜你喜欢

转载自blog.csdn.net/qq_24313635/article/details/84144382
今日推荐