当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个错误响应,而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延
spring cloud hystrix 实现了断路器,线程隔离等一系列服务保护功能。它也是基于netflix 的开源框架hystrix实现的,该框架的目标在于通过控制那些访问远程系统,服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。hystrix具备服务降级,服务熔断,线程和信号隔离,请求缓存,请求合并以及服务监控等强大功能
版本 spring boot 1.5.4
简单实用
配置文件 |
spring.application.name=ribbon-consumer server.port=9000 eureka.client.region=gd eureka.client.availability-zones.gd=gz,hz
eureka.client.serviceUrl.gz=http://peer1:1111/eureka/ eureka.client.serviceUrl.hz=http://peer2:1112/eureka/ eureka.instance.metadata-map.zone=gz |
service 层 |
package springcloud.hystrix; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate;
@Service public class HelloService { @Autowired RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "helloFallback") public String helloService(){ return restTemplate.getForEntity("http://HELLO-SERVICE/hello",String.class).getBody(); }
public String helloFallback(){ return "error"; }
} |
controller 层 |
package springcloud.hystrix;
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;
@RestController public class ConsumerController { @Autowired protected HelloService helloService;
@RequestMapping(value = "/ribbon-consumer",method = RequestMethod.GET) public String helloConsumer(){ return helloService.helloService(); }
} |
启动类 |
package springcloud.hystrix;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;
@EnableCircuitBreaker // 注解开启断路器功能 @SpringBootApplication @EnableDiscoveryClient public class HystrixApplication {
public static void main(String[] args) { SpringApplication.run(HystrixApplication.class, args); }
@Bean @LoadBalanced RestTemplate restTemplate(){ return new RestTemplate(); }
} |
相比java其他的实现方式
java原生 并发 实现
package springcloud.hystrix.future; import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit;
public class FutureDemo {
public static void main(String[] args) { Random random = new Random();
ExecutorService executorService = Executors.newFixedThreadPool(1);
Future<String> future = executorService.submit(()->{ // 如果随机时间大于100,那么触发容错 int value = random.nextInt(100);
System.out.println("helloWorld() costs " + value + " ms.");
Thread.sleep(value);
return "Hello,World"; });
try { future.get(10, TimeUnit.MILLISECONDS); }catch (Exception e){ System.out.println("超时保护!"); }
executorService.shutdown();
}
} |
rxjava 实现:
package springcloud.hystrix.rx;
import rx.Observable; import rx.Subscriber;
import java.util.Random;
public class RxJavaDemo {
public static void main(String[] args) {
//创建一个事件源 observable Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>(){ @Override public void call(Subscriber<? super String> subscriber) { subscriber.onNext("hello rxjava"); // subscriber.onNext("hello sd"); subscriber.onCompleted(); } }); Random random = new Random(); // 创建订阅者 subscriber Subscriber<String> subscriber = new Subscriber<String>() { @Override public void onCompleted() {
}
@Override public void onError(Throwable e) { System.out.println("熔断保护!"); }
@Override public void onNext(String s) { // 如果随机时间 大于 100 ,那么触发容错 int value = random.nextInt(200);
if (value > 100) { throw new RuntimeException("Timeout!"); }
System.out.println("helloWorld() costs " + value + " ms.");
System.out.println("subscriber: "+s); } };
// 订阅 observable.subscribe(subscriber);
}
} |
参考:
小马哥spring cloud 微服务教程
spring cloud 微服务实战