<!-- hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
Eureka正常启动
服务端注册进Eureka
服务器端
1.单个方法()一对一)降级
controller
@RestController
@RequestMapping("/payment/hystrix")
@Slf4j
public class PaymentController {
@Autowired
private PaymentService paymentService;
@GetMapping("/selectPaymentById/{id}")
public String selectPaymentById(@PathVariable String id){
// http://localhost:8001/payment/hystrix/selectPaymentById/1
return paymentService.selectPaymentById(id);
}
@GetMapping("/timeOut/{id}")
public String timeOut(@PathVariable String id){
return paymentService.timeOut(id);
}
}
service
public interface PaymentService {
String selectPaymentById(String id);
String timeOut(String id);
}
@Service
public class PaymentServiceImpl implements PaymentService {
@Override
@HystrixCommand(fallbackMethod = "test_fallback")
public String selectPaymentById(String id) {
return "server 8001 : selectPaymentById"+ Thread.currentThread().getName() + "ok~";
}
@Override
@HystrixCommand(fallbackMethod = "test_fallback", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
})
public String timeOut(String id) {
try {
long s = 3;
TimeUnit.SECONDS.sleep(s);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "server 8001 : timeOut"+ Thread.currentThread().getName() + "ok~";
}
//服务降级方法
public String test_fallback(String id){
return "test_fallback 请重试!" + id;
}
}
2.全局(独立于共享)降级
controller
@RestController
@RequestMapping("/payment/hystrix")
@Slf4j
public class PaymentController {
@Autowired
private PaymentService_1 paymentService_1;
@GetMapping("/selectPaymentById1/{id}")
public String selectPaymentById_1(@PathVariable String id){
return paymentService_1.selectPaymentById(id);
}
@GetMapping("/timeOut1/{id}")
public String timeOut_1(@PathVariable String id){
return paymentService_1.timeOut(id);
}
}
service
public interface PaymentService_1 {
String selectPaymentById(String id);
String timeOut(String id);
}
@Service
@DefaultProperties(defaultFallback = "test_fallback")
public class PaymentServiceImpl_1 implements PaymentService_1 {
//通用
@Override
@HystrixCommand //没有特别指明,使用公用降级方法test_fallback
public String selectPaymentById(String id) {
return "server 8001 : selectPaymentById"+ Thread.currentThread().getName() + "ok~";
}
@Override
@HystrixCommand(fallbackMethod = "test_fallback_timeout", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "5000")
})
public String timeOut(String id) {
try {
long s = 6;
TimeUnit.SECONDS.sleep(s);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "server 8001 : timeOut"+ Thread.currentThread().getName() + "ok~";
}
//服务降级方法
public String test_fallback(){
return "test_fallback 请重试!";
}
public String test_fallback_timeout(String id){
return "test_fallback_timeout 请重试!" + id;
}
}
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class) @EnableCircuitBreaker //开启熔断器 @EnableHystrix public class ApplicationHystrix8001 { public static void main(String[] args) { SpringApplication.run(ApplicationHystrix8001.class, args); } }
客户端
1.服务降级处理实现类
controller
@RestController
@RequestMapping("/order/hystrix")
public class OrderController {
@Autowired
private OrderService orderService;
@GetMapping("/consumer/selectPaymentById/{id}")
public String selectPaymentById(@PathVariable String id){
return orderService.selectPaymentById(id);
}
@GetMapping("/consumer/timeOut/{id}")
public String timeOut(@PathVariable String id){
return orderService.timeOut(id);
}
}
service
@FeignClient(value = "CLOUD-PAYMENT-HYSTRIX-SERVICE8001", fallback = OrderServiceHystrix.class)
@Component
public interface OrderService {
@GetMapping("/payment/hystrix/selectPaymentById/{id}")
public String selectPaymentById(@PathVariable String id);
@GetMapping("/payment/hystrix/timeOut/{id}")
public String timeOut(@PathVariable String id);
}
/**
* 降级处理
*/
@Component
public class OrderServiceHystrix implements OrderService {
@Override
public String selectPaymentById(String id) {
return "selectPaymentById" + "服务不可用";
}
@Override
public String timeOut(String id) {
return "timeOut 服务不可用";
}
}
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@EnableEurekaClient
@EnableFeignClients
public class ApplicationHystrix8083 {
public static void main(String[] args) {
SpringApplication.run(ApplicationHystrix8083.class, args);
}
}
server:
port: 8083
spring:
application:
name: cloud-order-service-openfeign-hystrix
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka #单机版
# defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群
ribbon:
ReadTimeout: 5000 # 超时配置
ConnectTimeout: 5000
feign:
hystrix:
enabled: true
pom加入
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
自己看的笔记,不喜勿喷!