feign icon indicating copy to clipboard operation
feign copied to clipboard

Should I use EnableCircuitBreaker?

Open ZhouYixun opened this issue 5 years ago • 4 comments

I only add these in my pom.xml

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!--    openfeign    -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-openfeign</artifactId>
    </dependency>

I can use fallback now, it looks work.

@FeignClient(value = "xxxx-server-controller", fallback = TestDataFeignClientFallBack.class) public interface TestDataFeignClient { @GetMapping("/xxxx/xxxx") JSONObject xxxxx(@RequestParam(name = "id") int id); }

but I found CircuitBreaker not work.

@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class TransportApplication { public static void main(String[] args) { SpringApplication.run(TransportApplication.class, args); } }

feign: hystrix: enabled: true client: config: default: connectTimeout: 10000 readTimeout: 10000

hystrix: command: default: execution: timeout: enabled: true isolation: thread: timeoutInMilliseconds: 12000 circuitBreaker: enabled: true forceOpen: false forceClosed: false requestVolumeThreshold: 4 errorThresholdPercentage: 50 sleepWindowInMilliseconds: 10000

but I want to use CircuitBreaker,should I add this dependency? org.springframework.cloud spring-cloud-starter-netflix-hystrix

ZhouYixun avatar Jun 11 '21 02:06 ZhouYixun

@Felfan I need your help

ZhouYixun avatar Jun 21 '21 14:06 ZhouYixun

Questions like this are more appropriate for Stack Overflow. Issues in this repository are for bug fixes and feature requests for the project.

kdavisk6 avatar Jul 19 '21 13:07 kdavisk6

这样的问题更适合Stack Overflow。此存储库中的问题是针对项目的错误修复和功能请求。

ok

ZhouYixun avatar Sep 13 '21 05:09 ZhouYixun

首先要看你的版本,应该是从3.x版本之后, 1 对于Netflix来说所需要引用依赖 org.springframework.cloud spring-cloud-starter-netflix-hystrix 2 对于alibaba sentinel来说所需要引用依赖 com.alibaba.cloud spring-cloud-starter-alibaba-sentinel 3 如果不想引入依赖需要自己写断路器 @Component public class MyCircuitBreakerFactory extends CircuitBreakerFactory{ @Override public CircuitBreaker create(String id) { return new MyCircuitBreaker(); }

@Override
protected ConfigBuilder configBuilder(String id) {
    return new ConfigBuilder() {
        @Override
        public Object build() {
            return null;
        }
    };
}

@Override
public void configureDefault(Function defaultConfiguration) {

}

}

public class MyCircuitBreaker implements CircuitBreaker {

@Override
public <T> T run(Supplier<T> toRun, Function<Throwable, T> fallback) {
    try {
        return toRun.get();
    } catch (Exception ex) {
        return fallback.apply(ex);
    }

}

}

312527814 avatar Sep 13 '23 06:09 312527814