赞
踩
服务器忙,请稍后重试,不让客户端等待立马返回一个友好提示,fallback
主启动类上加:@EnableCircuitBreaker
@SpringBootApplication
@EnableEurekaClient
@MapperScan("springcloud.mapper")
@EnableCircuitBreaker
public class HystrixPay8005Application {
public static void main(String[] args) {
SpringApplication.run(HystrixPay8005Application.class, args);
}
}
@GetMapping("queryPort")
@HystrixCommand(fallbackMethod = "queryPortFallBack",commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000") //3秒钟以内就是正常的业务逻辑
})
public ResponResult<String> queryPort(
) {
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
return new ResponResult<>(ResponEnum.SUCCESS, serverPort + "\t\t" + UUID.randomUUID().toString());
}
public ResponResult<String> queryPortFallBack(){
return new ResponResult<>(ResponEnum.SUCCESS, "服务繁忙,请稍后重试");
}
第一步: 主启动类上加 @EnableHystrix
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class HystrixOrder80Application {
public static void main(String[] args) {
SpringApplication.run(HystrixOrder80Application.class, args);
}
}
第二步: controller层建立 兜底方法
@GetMapping("queryPort")
@HystrixCommand(fallbackMethod = "queryPortFallBack",commandProperties = {@HystrixProperty(name = "execution" +
".isolation.thread.timeoutInMilliseconds",value = "1500") //1.5秒钟以内就是正常的业务逻辑
})
public ResponResult<String> queryPort(
) {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return orderservices.queryPort();
}
public ResponResult<String> queryPortFallBack(){
return new ResponResult<>(ResponEnum.SUCCESS, "我是调用方我等不及了");
}
第三步,yml文件加配置
feign:
hystrix:
enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样
代码冗余
每个方法都需要配置一个服务降级方法
逻辑混乱
第一步: 自定义是实现类实现接口
@Component
public class OrderServiceFallBack implements Orderservices {
@Override
public ResponResult<String> queryPort() {
return new ResponResult<>(ResponEnum.SUCCESS, "┭┮﹏┭┮");
}
}
第二步: 在OPenfeign的接口上写回滚方法
@FeignClient(value = "cloud-hystrix-payment-service",fallback = OrderServiceFallBack.class)
public interface Orderservices {
@GetMapping("queryPort")
ResponResult<String> queryPort();
}
第三步:主启动类开启注解
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrix
public class HystrixOrder80Application {
public static void main(String[] args) {
SpringApplication.run(HystrixOrder80Application.class, args);
}
}
第四步: yml配置
feign:
hystrix:
enabled: true #如果处理自身的容错就开启。开启方式与生产端不一样
注意:此服务降级无法处理方法内部发生异常问题,比如
类比保险丝达到最大服务访问后,直接拒绝访问,拉闸限电,然后调用服务降级的方法返回友好提示;
服务熔断-》进而熔断-》恢复调用链路
大神的结论
地址
//服务熔断
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled",value = "true"), //是否开启断路器
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"), //请求次数
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"), //时间范围
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60"), //失败率达到多少后跳闸
})
public String paymentCircuitBreaker(@PathVariable("id") Integer id){
if (id < 0){
throw new RuntimeException("*****id 不能负数");
}
String serialNumber = IdUtil.simpleUUID();
return Thread.currentThread().getName()+"\t"+"调用成功,流水号:"+serialNumber;
}
public String paymentCircuitBreaker_fallback(@PathVariable("id") Integer id){
return "id 不能负数,请稍候再试,(┬_┬)/~~ id: " +id;
}
涉及到断路器的三个重要参数:快照时间窗
、请求总数阀值
、错误百分比阀值
。
1.新建一个模块,导入依赖,注意下面两个依赖必须要有
<!--新增hystrix dashboard-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.主启动类上加注解
@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashBoard9001Application {
public static void main(String[] args) {
SpringApplication.run(HystrixDashBoard9001Application.class,args);
}
}
3.假设该模块端口为9001 则启动项目 ,访问 localhost:9001/hystrix即可
4.9001为监控模块,将需要监控的服务,加入到里面去
<!--监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.在主启动类上加上指定监控路径,也必须要有@EnableCircuitBreak注解开启
@SpringBootApplication
@EnableEurekaClient
@MapperScan("springcloud.mapper")
@EnableCircuitBreaker
public class HystrixPay8005Application {
public static void main(String[] args) {
SpringApplication.run(HystrixPay8005Application.class, args);
}
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
3.配置监控模块对提供服务的模块的配置,并取好名字
实心圆:共有两种含义。它通过颜色的变化代表了实例的健康程度,它的健康度从绿色<黄色<橙色<红色递减。
该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大该实心圆就越大。所以通过该实心圆的展示,就可以在大量的实例中快速的发现故障实例和高压力实例。
曲线:用来记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。