为什么我会将Hystrix的熔断器换成resilience4j,因为尝试了半天发现虽然Hystrix的熔断器可以正常使服务降级,单个api接口可以返回fallback错误信息,但是不能使得服务熔断,具体原因可能就是注解失效,被放弃了,所以后面边将熔断器换成了当前的熔断器resilience4j
具体可以看看这篇博客,这篇博客详细写了如何使用resilience4j熔断器去使服务下降和服务熔断。OpenFeign熔断与降级_openfeign熔断降级-优快云博客
紧接上文的熔断器使服务降级
resilience4j的依赖下载:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
</dependency>
架构如图所示:
IProductClientServiceFallbackFactory
package com.gaolang.fallback;
import com.gaolang.FeignClient.OrderFeignClient;
import com.gaolang.entities.CommonResult;
import com.gaolang.entities.Payment;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;
public class IProductClientServiceFallbackFactory implements FallbackFactory<OrderFeignClient> {
@Override
public OrderFeignClient create(Throwable cause) {
return new OrderFeignClient() {
@Override
public CommonResult create(Payment payment) {
return new CommonResult<>(444,"服务降级返回,---PaymentFallbackService");
}
@Override
public CommonResult getPaymentById(Long id) {
return new CommonResult<>(444,"服务降级返回,---PaymentFallbackService");
}
};
}
}
OrderFeignClient
package com.gaolang.FeignClient;
import com.gaolang.entities.CommonResult;
import com.gaolang.entities.Payment;
import com.gaolang.fallback.IProductClientServiceFallbackFactory;
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.PostMapping;
@FeignClient(value = "cloud-provider-payment8001", fallbackFactory = IProductClientServiceFallbackFactory.class) // 指定调用的服务名称
public interface OrderFeignClient {
@PostMapping(value = "/payment/create")
CommonResult create(Payment payment); // 创建支付记录
@GetMapping(value = "/payment/{id}")
CommonResult getPaymentById(@PathVariable("id") Long id); // 根据 ID 获取支付记录
}
FallbackConfig
package com.gaolang.config;
import com.gaolang.fallback.IProductClientServiceFallbackFactory;
import org.springframework.context.annotation.Bean;
public class FallbackConfig {
@Bean
public IProductClientServiceFallbackFactory iArticleClientFallbackFactory() {
return new IProductClientServiceFallbackFactory();
}
}
OrderMain80
package com.gaolang;
import com.gaolang.config.FallbackConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
//import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableFeignClients(basePackages = "com.gaolang", defaultConfiguration = FallbackConfig.class)
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
@LoadBalanced // 使 RestTemplate 自动支持 Ribbon 负载均衡
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
单启动该模块后,对api进行测试http://localhost:80/customer/payment/1后的结果如图所示:
成功使得服务熔断。