博客详情

springcloud(七)--服务消费方Hystrix断路器 (原创)

作者: 朝如青丝暮成雪
发布时间:2018-08-18 23:22:52  文章分类:springcloud   阅读(940)  评论(0)

如题,本篇我们介绍下服务消费方Hystrix断路器的使用。


注意,前面我们介绍的ribbon、feign都是基于客户端代理(服务消费方)的负载均衡器,而服务器端的负载均衡器在实际中可能会采用硬件负载,如F5或 LVS等设备,本篇中介绍的Hystrix也是基于客户端的技术,hystrix中实现了断路器、线程隔离、信号隔离等功能,使用Hystirx可以有效地避免因某一个服务器故障而导致所有依赖该服务的服务都发生阻塞的"雪崩"效应。


好了,现在回到正题,本篇主要介绍下hystrix在Ribbon消费方和Feign消费方中的使用,以及hystrix-dashboard的使用


一、Ribbon消费方中使用hystrix
1、在消费方工程中,本例是sim-consumer工程中,pom.xml中引入hystrix断路器起步依赖


<!-- hystrix 断路器 起步依赖 -->
        <dependency>
		    <groupId>org.springframework.cloud</groupId>
		    <artifactId>spring-cloud-starter-hystrix</artifactId>
		</dependency>


2、在springboot启动类中添加@EnableHystrix注解启动Hystrix客户端。 


package com.tingcream.simConsumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;



@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan
@ImportResource({"classpath:/spring.xml"})

 
@EnableDiscoveryClient
@EnableFeignClients //启用Feign客户端

@EnableHystrix//启动hystrix客户端

@EnableHystrixDashboard //启动hystrixDashbord 仪表板
public class SimConsumerApp extends SpringBootServletInitializer{
  
    @Override
	protected SpringApplicationBuilder configure(
			SpringApplicationBuilder builder) {
    	 return builder.sources(SimConsumerApp.class);
	}

	public static void main(String[] args) {
        SpringApplication.run(SimConsumerApp.class, args);
    }
	
}
3、在消费方接口方法中,使用@HystrixCommand(fallbackMethod = "xxx") 标记为一个hystrix方法,并指定失败回退的掉用方法,如


在 HelloConsumerController 类中使用@HystrixCommand(fallbackMethod = "xxx") 标记方法。


@GetMapping("/hello")
	@HystrixCommand(fallbackMethod = "helloError")
	public String  hello(){
	  return 	restTemplate.getForObject(PRE_URL+"/hello", String.class);
	}
	public String  helloError(){
		 return "抱歉,服务器访问中断了!";
	}
	
	
	@GetMapping("/hello2")
	@HystrixCommand(fallbackMethod = "helloError2")
	public String  hello2(String name){
		return 	restTemplate.getForObject(PRE_URL+"/hello?name="+name, String.class);
	}
	public String  hello2Error(String name){
		return "你好:"+name+",抱歉,服务器访问中断了!";
	}
配置完成后,消费方通过hystrix访问服务提供方,当服务提供方服务不可用时,就会执行快速失败(而不是等待线程、阻塞线程)的回退方法,即fallbackMethod 所指定的方法。


服务不可用时,访问

二、Feign消费方中使用hystrix

1、 application.yml中配置 feign.hystrix.enabled=true

Feign消费方中使用hystrix 比Ribbon的更简单,因为Feign中本身就内置了hystrix断路器,我们只需要使用feign.hystrix.enabled=true 开启之即可。

#feign客户端启动hystrix断路保护
feign:
  hystrix:
    enabled: true


2、跟在Ribbon中一样,在springboot启动类上标记 @EnableHystrix注解,启用hystrix 。

3、在消费方接口方法中,使用@FeignClient注解标记为一个hystrix方法,并指定value服务应用名称、fallback失败回退的调用的实现类。如

HelloFeignApi.java 和HelloFeignApiError.java 


package com.tingcream.simConsumer.clientApi;

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

@FeignClient(value = "sim-provider",fallback =HelloFeignApiError.class)
public interface HelloFeignApi {
	
	@GetMapping( "/hello")
	public String hello();
	
	@GetMapping( "/hello2")
	public String hello2(@RequestParam("name") String name);

}




package com.tingcream.simConsumer.clientApi;

import org.springframework.stereotype.Component;

import com.tingcream.simConsumer.clientApi.HelloFeignApi;

/**
 * HelloFeignApi 失败退回的调用方法
 * @author jelly
 *
 */
@Component
public class HelloFeignApiError implements HelloFeignApi {

	@Override
	public String hello() {
		 return "FeignApi 抱歉,服务器访问中断了!";
	}

	@Override
	public String hello2(String name) {
		return "FeignApi 你好:"+name+",抱歉,服务器访问中断了!";
	}

}



测试访问的效果与Ribbon类似,当依赖的服务不可用时,会自动调用指定的接口实现类中的实现的方法。


三、hystrix-dashboard仪表板

1、在访问消费方pom.xml中加入hystrix-dashboard的起步依赖


<!-- hystrix-dashboard 起步依赖   -->
		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
        </dependency>


2、在springboot启动类中,添加 @EnableHystrixDashboard 注解,启动hystrixDashbord 仪表板


3、访问消费方地址/hystrix ,本例中是http://localhost/hystrix/ 即可看到仪表板首页。如,





关键字:  springcloud  hystrix
评论信息
暂无评论
发表评论

亲,您还没有登陆,暂不能评论哦! 去 登陆 | 注册

博主信息
   
数据加载中,请稍候...
文章分类
   
数据加载中,请稍候...
阅读排行
 
数据加载中,请稍候...
评论排行
 
数据加载中,请稍候...

Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1

鄂公网安备 42011102000739号