SpringCloud 2020.0.5 搭建项目
Spring Boot2.5.3集成Hystrix实现服务熔断
fallbackMethod 声明一个失败回滚处理函数
创建OrderApi项目(Consumer)
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
@EnableHystrix // 包含 @EnableCircuitBreaker
@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class ApiApplication {
public static void main(String[] args){
SpringApplication.run(ApiApplication.class, args);
}
}
application.yml
server:
port: 8092
spring:
application:
name: api-local
main:
allow-bean-definition-overriding: true
eureka:
instance:
appname: api-local
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:9000/eureka
# 开启Feign的熔断功能 #feign 调用服务启用hystrix
feign:
circuitbreaker: #断路器
enabled: true
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<version>3.0.6</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
OrderFeignController.java
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@DefaultProperties(defaultFallback = "orderFeignError")
public class OrderFeignController {
@Autowired
private OrderFeignClientService orderFeignClientService;
public String orderFeignError(){
return "[Fallback] orderFeignError";
}
@HystrixCommand
@PostMapping("/order/detail")
public String findDetail(@RequestBody Map<String, Object> params) throws Exception{
log.info("detail, get request params:" + params);
orderFeignClientService.findDetail(params);
int id = Integer.parseInt((String) params.getOrDefault("id", 0));
if(id == 0){
throw new NullPointerException();
}
return "[Controller] findDetail ok";
}
}
二、Fiegn接口熔断
一.fallback方式
OrderFeignClientService.java
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestBody;
@FeignClient(value = "server-local", fallback = OrderFeignClientServiceFallBack.class)
public interface OrderFeignClientService {
@PostMapping("/order/detail")
public String findDetail(@RequestBody Map<String, Object> params);
}
OrderFeignClientServiceFallBack.java
@Service
public class OrderFeignClientServiceFallBack implements OrderFeignClientService {
@Override
public String findDetail(@RequestBody Map<String, Object> params){
return "ERR OrderFeignClientService fallBack";
}
}
创建OrderServer项目(Provider)
@EnableEurekaClient
@SpringBootApplication
public class ServerApplication {
public static void main(String[] args){
SpringApplication.run(ServerApplication.class, args);
}
}
application.yml 暂时没什么复杂配置
server:
port: 8095
spring:
application:
name: server-local
eureka:
instance:
appname: server-local
prefer-ip-address: true
client:
service-url:
defaultZone: http://127.0.0.1:9000/eureka
创建Eureka 服务
启动类 EurekaServerApplication.java
@EnableEurekaServer
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
application.yml
server:
port: 9000
spring:
application:
name: eureka-server
eureka:
instance:
appname: eureka-server
# hostname: eureka9000.com # eureka服务端的实例名称,eureka8501.com需要修改host文件
client:
fetch-registry: false #自己不需要注册自己
#是否从Eureka获取注册信息 一般情况下,Eureka服务端是不需要的 由于注册中心的职责是维护服务实例,它并不需要去检索服务
register-with-eureka: false #不向自己注册
service-url:
defaultZone: http://localhost:9000/eureka
server:
enable-self-preservation: false #关闭自我保护机制,保证不可用服务被及时移除
eviction-interval-timer-in-ms: 3000 #3秒后移除
关于 hystrix 失效原因
1、添加 openfegin 的依赖;在启动类添加@EnableFeignClients注解;
2、使用@FeignClient的fallback参数;
3、熔断处理的实现类要被Spring管控
# 开启Feign的熔断功能 #feign 调用服务启用hystrix
feign:
circuitbreaker:
enabled: true
Spring Boot2.5.3集成Hystrix实现服务熔断
https://blog.csdn.net/weixin_46041797/article/details/119220545
#无效
spring:
cloud:
circuitbreaker:
hystrix:
enabled: true
参考资料
SpringCloud-Feign开启Hystrix
https://blog.csdn.net/qq_38173650/article/details/120008186
在Feign中使用断路器 Hystrix
https://blog.csdn.net/bbj12345678/article/details/108948298
熔断和降级的关系,图文并茂
https://blog.csdn.net/qq_27184497/article/details/119993725