断路器:https://martinfowler.com/bliki/CircutiBreaker.htmlhtml
核心思想:java
在断路器对象中封装受保护的方法调用。web
该断路器监控调用和断路状况spring
调用失败触发阈值后,后续调用直接由短路器返回错误,再也不执行实际调用。服务器
理解:网络
客户端经过circuit breaker调用服务提供者,正常的时候能够调用。若是服务提供方出现了问题,发生了超时, 前几回能够超时处理, 到达一个阀值能够经过断路器进行处理, 就再也不向服务方发起请求。app
import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; @Aspect @Component @Slf4j public class CircuitBreakerAspect { // 阀值 private static final Integer THRESHOLD = 3; //记录失败的次数 private Map<String, AtomicInteger> counter = new ConcurrentHashMap<>(); // 记录被保护的次数 private Map<String, AtomicInteger> breakCounter = new ConcurrentHashMap<>(); /** * * @param pjp 程序链接点 * @return * @throws Throwable */ @Around("execution(* 拦截的区域") public Object doWithCircuitBreaker(ProceedingJoinPoint pjp) throws Throwable { // 获取当前执行的方法 String signature = pjp.getSignature().toLongString(); log.info("Invoke {}", signature); Object retVal; try { if (counter.containsKey(signature)) { // 失败次数达到预制,若是保护次数没到,返回null if (counter.get(signature).get() > THRESHOLD && breakCounter.get(signature).get() < THRESHOLD) { log.warn("Circuit breaker return null, break {} times.", breakCounter.get(signature).incrementAndGet()); return null; } } else { counter.put(signature, new AtomicInteger(0)); breakCounter.put(signature, new AtomicInteger(0)); } retVal = pjp.proceed(); counter.get(signature).set(0); breakCounter.get(signature).set(0); } catch (Throwable t) { log.warn("Circuit breaker counter: {}, Throwable {}", counter.get(signature).incrementAndGet(), t.getMessage()); breakCounter.get(signature).set(0); throw t; } return retVal; } }
Hystrix [hɪst'rɪks],中文含义是豪猪,因其背上长满棘刺,从而拥有了自我保护的能力。本文所说的Hystrix是Netflix开源的一款容错框架,一样具备自我保护能力。为了实现容错和自我保护,下面咱们看看Hystrix如何设计和实现的。框架
Hystrix设计目标:ide
实现了断路服务器模式ui
在须要服务熔断的方法上添加@HystrixCommand注解, fallbackMethod指定熔断的地址,默认状况下@HystrixCommand是在另一个线程执行的。能够作一些超时的处理。
@HystrixProperty(name="excution.isolation.strategy",value="SEMAPHORE")设置为信号
Hystrix配置项参考:
Spring cloud 支持
spring-cloud-starter-netfixi-hystrix
@EnableCircuitBreaker
Feign支持
feign.hystrix.enable=true
@FeignClient
fallback / fallbackFactory
简单示例:
pom引入
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
application.properties
feign.client.config.default.connect-timeout=500
feign.client.config.default.read-timeout=500
#开启feign支持 feign.hystrix.enabled=true
#cousul链接配置
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.cloud.consul.discovery.prefer-ip-address=true
bootstarp.properties
spring.application.name=name-service
开启注解:
@EnableDiscoveryClient // 注册发现服务
@EnableFeignClients // feign的支持
@EnableCircuitBreaker // feignClient的演示
//Spring cloud 支持 @PostMapping("/order") @HystrixCommand(fallbackMethod = "fallbackCreateOrder") public CoffeeOrder createOrder() { /*业务代码*/ return order; } public CoffeeOrder fallbackCreateOrder() { log.warn("Fallback to NULL order."); return null; }
//FeignClient 的支持 import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestParam; import java.util.List; @FeignClient(name = "waiter-service", contextId = "coffee", qualifier = "coffeeService", path="/coffee", fallback = FallbackCoffeeService.class) // 若是用了Fallback,不要在接口上加@RequestMapping,path能够用在这里 public interface TestService { @GetMapping("/{id}") Product getById(@PathVariable Long id); } /*实现TestService*/ import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import java.util.Collections; import java.util.List; @Slf4j @Component public class FallbackTestService implements TestService{ @Override public Product getById(Long id) { /**发送了垄断的逻辑代码*/ return null; } } /**Controller调用*/ @GetMapping("testGetById") public String testGetById() { TestService.getById((long) 1); return ""; }