Spring-Cloud-Finchley | 熔断 Hystrix

简介

熔断机制产生的背景是“雪崩效应”,在分布式系统中,一个服务提供者的不可用会导致服务消费者的不可用,一个服务的异常,最终会影响其他服务,造成线程阻塞,资源耗尽,如果有大量的请求,会导致服务瘫痪,引蝴蝶效应造成系统宕机。

如果一个服务的错误过多,短时间内得不到修复,就可以开启熔断机制,防止多次没有意义的调用。服务调用方会定时重试,如果可用,就继续使用。

熔断有以下几种状态:

  • 闭合状态
    添加一个计数器,如果失败的次数在指定时间内超过一定阈值,则开启熔断,此时处于断开状态,此时开启一个计时器,如果过了指定时间,则切换到半断开,服务可被尝试调用。

  • 断开状态
    服务如果被调用会立即返回错误信息,减少资源损耗。

  • 半开状态
    允许一定数量的请求调用服务,如果请求调用成功,则关闭熔断,认为服务的错误已经修正。如果还是发生错误,则切换到断开状态。

实例

1、搭建 maven 环境

Hystrix 是知名视频网站 Netflix 开源的框架,完美解决了分布式系统中所面临的这个技术问题,解决了雪崩效应。

首先搭建 maven 环境,父 pom 如下

  
    UTF-8
    1.8
    1.8
    Finchley.RELEASE
  

  
    org.springframework.boot
    spring-boot-starter-parent
    2.0.5.RELEASE
    
  

  
    
      junit
      junit
      4.11
      test
    

    
      org.springframework.boot
      spring-boot-starter-test
      test
    

  

  
    
      
        org.springframework.cloud
        spring-cloud-dependencies
        ${spring-cloud.version}
        pom
        import
      
    
  
2、搭建服务注册中心 eureka-server

maven 依赖

	
		com.hly
		04-spring-cloud-hystrix
		1.0-SNAPSHOT
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-server
		

		
			org.springframework.boot
			spring-boot-starter-web
		
	

application.yml 配置文件

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

spring:
  application:
    name: service-server

SpringBoot 启动类

@SpringBootApplication
@EnableEurekaServer
public class ServiceServerApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceServerApplication.class, args);
	}
}
3、搭建服务提供者 client

maven 依赖

	
		com.hly
		04-spring-cloud-hystrix
		1.0-SNAPSHOT
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		

		
			org.springframework.boot
			spring-boot-starter-web
		
	

application.yml

server:
  port: 8762

spring:
  application:
    name: service-client

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

SpringBoot 启动类

@SpringBootApplication
@RestController
@EnableEurekaClient
public class ServiceClientApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceClientApplication.class, args);
	}

	@Value("${server.port}")
	String port;
	@RequestMapping("/hello")
	public String home(@RequestParam(value = "name", defaultValue = "hly") String name) {
		return "hi " + name + " ,i am from port:" + port;
	}
}
4、搭建服务消费者 feign 客户端

maven 依赖

	
		com.hly
		04-spring-cloud-hystrix
		1.0-SNAPSHOT
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
			org.springframework.cloud
			spring-cloud-starter-netflix-eureka-client
		

		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.cloud
			spring-cloud-starter-openfeign
		
	

application.yml

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
server:
  port: 8765
spring:
  application:
    name: service-feign

# 开启熔断
feign:
  hystrix:
    enabled : true

服务层接口

//服务名指定调用哪个服务
@FeignClient(value = "service-client",fallback =HiHystrixServiceImpl.class)
public interface HiHystrixService {
    //这里的映射名和需要调用的服务的映射名一样
    @RequestMapping(value = "/hello",method = RequestMethod.GET)
    String sayHiFromClientOne(@RequestParam(value = "name") String name);
}

指定实行快速失败的方法

@Component
public class HiHystrixServiceImpl implements HiHystrixService {
    @Override
    public String sayHiFromClientOne(String name) {
        return "sorry"+name;
    }
}

Controller 层

@RestController
public class HiController {

   @Autowired
   HiHystrixService hiHystrixService;

    /**
     * 消费服务
     * @param name
     * @return
     */
    @GetMapping(value = "/hi")
    public String sayHi(@RequestParam String name) {
        return hiHystrixService.sayHiFromClientOne( name );
    }
}

SpringBoot 启动类

@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
public class ServiceFeignHystrixApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceFeignHystrixApplication.class, args);
	}
}

演示

1、依次启动 注册中心 server ,启动两个 client(启动一个后,修改application.yml 配置文件的端口,再次启动一次),接着启动 Feign 客户端,访问:
http://localhost:8765/hi?name=hly
当关掉客户端后,将会执行快速失败策略。

代码下载

04-spring-cloud-hystrix:https://github.com/huangliangyun/Spring-Cloud-Finchley

关于

我的 Github:Github
CSDN: CSDN
个人网站: sirius 的博客
E-mail: [email protected]

推荐阅读
史上最全,最完美的 JAVA 技术体系思维导图总结,没有之一!

你可能感兴趣的:(秒懂,Spring,Cloud)