标签:template com 保险 erro stream 获取 label 包含 []
较低级别的服务中的服务故障可能导致级联故障一直到用户。 当对特定服务的调用超过circuitBreaker.requestVolumeThreshold(默认值:20个请求)且失败百分比大于circuit.rolllingStats.timeInMilliseconds定义的滚动窗口中的circuitBreaker.errorThresholdPercentage(默认值:> 50%)时(默认值:10秒)
设计原则:
功能特性:
1、基于之前的服务,引入Hystrix依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
2、开启断路器功能
@EnableCircuitBreaker:加载断路器配置
@EnableCircuitBreaker
也可以使用@SpringCloudApplication注解:三合一
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
public @interface SpringCloudApplication {
}
3、创建fallback方法,指定为某个接口的降级
@SpringCloudApplication
public class ConsumerRibbonApplication {
public static void main(String[] args) {
SpringApplication.run(ConsumerRibbonApplication.class, args);
}
@Bean
@LoadBalanced
public RestTemplate restTemplate() {
return new RestTemplate();
}
@RestController
@RequestMapping("/api/v1/consumer")
class ProviderController {
private static final String SERVICE_NAME = "provider-server";
private static final String GET_PORT = "/api/v1/provider/port";
@Resource
private RestTemplate restTemplate;
@GetMapping
@HystrixCommand(fallbackMethod = "consumerFallback")
public String consumer() {
ResponseEntity<String> forEntity = restTemplate.getForEntity("http://" + SERVICE_NAME + GET_PORT, String.class, (Object) null);
return forEntity.getBody();
}
public String consumerFallback() {
return "provider error";
}
}
}
hystrix:配置
hystrix:
command:
default:
execution:
isolation:
strategy: SEMAPHORE
thread:
timeoutInMilliseconds: 60000
shareSecurityContext: true
验证:
访问消费者:http://localhost:3000/api/v1/consumer
获得响应:2000
引入依赖和开启配置和Ribbon一样
1、在@FeignClient上声明
@FeignClient(name = "provider-server", fallback = ProviderClientFallback.class)
@RequestMapping("/api/v1/provider")
public interface ProviderClient {
@GetMapping("/port")
String port();
}
2、开启配置feign.hystrix.enabled=true
server:
port: 3001
spring:
application:
name: consumer-server-feign
profiles:
active: dev
cloud:
config:
label: master
profile: ${spring.profiles.active}
discovery:
service-id: config-server
enabled: true
feign:
hystrix:
enabled: true
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:8761/eureka/
3、编写fallback类
public class ProviderClientFallback implements ProviderClient {
@Override
public String port() {
return "provider error";
}
}
4、在声明类@FeignClient上指定fallback属性
@FeignClient(name = "provider-server", fallback = ProviderClientFallback.class)
@RequestMapping("/api/v1/provider")
public interface ProviderClient {
@GetMapping("/port")
String port();
}
验证:
访问消费者:http://localhost:3000/api/v1/consumer
获得响应:2000
关闭生产者服务:http://localhost:3000/api/v1/consumer
获得响应:provider error
5、换成指定FallbackFactory,可以获取到异常对象
@FeignClient(name = "provider-server",
fallbackFactory = HystrixClient.HystrixClientFallback.class,
path = "/api/v1/provider")
public interface HystrixClient {
@GetMapping("/port")
String port();
@Component
class HystrixClientFallback implements FallbackFactory<HystrixClient> {
@Override
public HystrixClient create(Throwable throwable) {
return new HystrixClient() {
@Override
public String port() {
return "provider error: " + throwable;
}
};
}
}
}
验证:
访问消费者:http://localhost:3000/api/v1/consumer
获得响应:2000
关闭生产者服务:http://localhost:3000/api/v1/consumer
获得响应:provider error: java.lang.RuntimeException: com.netflix.client.ClientException: Load balancer does not have available server for client: provider-server
包含依赖关系spring-boot-starter-actuator
,设置 management.endpoints.web.exposure.include: hystrix.stream
。这样做会将/actuator/hystrix.stream
管理端点公开
依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
配置:
# 全部开放监控端点
management:
endpoints:
web:
exposure:
include: "*"
验证:
GET /actuator/metrics
GET /actuator/metrics/{requiredMetricName}
{[/actuator/metrics/{requiredMetricName}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}"
{[/actuator/metrics],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}"
1、添加依赖项:spring-cloud-starter-netflix-hystrix-dashboard
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
2、使用@EnableHystrixDashboard注解开启仪表盘
@EnableFeignClients
@SpringCloudApplication
@EnableHystrixDashboard
public class ConsumerFeignApplication {
}
3、访问/hystrix
并将仪表板指向/hystrix.stream
Hystrix客户端应用程序中的单个实例的端点。
(五)springcloud 断路器-Spring Cloud Netflix Hystrix
标签:template com 保险 erro stream 获取 label 包含 []
原文地址:https://www.cnblogs.com/zuier/p/10768075.html