springCloud学习记录
Hystrix(服务降级)
Hystrix用于服务出现问题时的处理
关键注解
启动类:
@EnableHystrix //开启 Hystrix
@EnableCircuitBreaker // 开启熔断器
实现类:
@HystrixCommand // Hystrix规则
@HystrixProperty 具体规则
例:
@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"),// 失败率到多少后跳闸
})
controller:
@DefaultProperties // 全局配置使用的回调操作
pom
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
application.yml
server:
port: 80
eureka:
client:
register-with-eureka: false
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka
feign:
hystrix:
# 在feign中开启Hystrix
enabled: true
启动类
@SpringBootApplication
@EnableFeignClients
//@EnableHystrix 开启 Hystrix
@EnableCircuitBreaker // 开启熔断器
public class OrderHystrixMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderHystrixMain80.class, args);
}
}
controller
package com.atguigu.springcloud.controller;
import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@Slf4j
// 全局的降级配置
@DefaultProperties(defaultFallback = "payment_Global_FullbackMethod")
public class OrderHystrixController {
@Resource
private PaymentHystrixService paymentHystrixService;
@GetMapping("/consumer/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id){
String result = paymentHystrixService.paymentInfo_OK(id);
return result;
}
@GetMapping("/consumer/payment/hystrix/timeout/{id}")
// 指定的使用降级内容,用自己的特殊的不用全局的
// @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler",commandProperties = {
// @HystrixProperty(name="execution.isolation.thread.timeoutInMilliseconds", value = "1500")
// })
// 表示使用降级
@HystrixCommand
public String paymentInfo_TIMEOUT(@PathVariable("id") Integer id){
String result = paymentHystrixService.paymentInfo_TIMEOUT(id);
return result;
}
public String paymentInfo_TimeOutHandler(Integer id){
return "80:线程池:"+ Thread.currentThread().getName()+" paymentInfo_TimeOutHandler,id: "+id +";┭┮﹏┭┮";
}
// 全局fallback
public String payment_Global_FullbackMethod(){
return "Global异常";
}
}
Service
@Component
// fallback 如果对应接口服务挂掉,后续操作指向
@FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT", fallback = PaymentFallbackService.class)
public interface PaymentHystrixService {
@GetMapping("/payment/hystrix/ok/{id}")
public String paymentInfo_OK(@PathVariable("id") Integer id);
@GetMapping("/payment/hystrix/timeout/{id}")
public String paymentInfo_TIMEOUT(@PathVariable("id") Integer id);
}
package com.atguigu.springcloud.service;
import org.springframework.stereotype.Component;
@Component
public class PaymentFallbackService implements PaymentHystrixService{
@Override
public String paymentInfo_OK(Integer id) {
return "--------PaymentFallbackService fall back-paymentInfo_OK, o(╥﹏╥)o";
}
@Override
public String paymentInfo_TIMEOUT(Integer id) {
return "--------PaymentFallbackService fall back-paymentInfo_TIMEOUT, o(╥﹏╥)o";
}
}