jeudi 3 octobre 2019

Can FeignClient use default fallback?

I want to using default fallback that retry other server. But feign&hystrix support fallback and fallback factory to implements fallback logic each method. What I want to do in fallback logic is just replace host(or port) of request url to other server. Can I configure default fallback strategy for achieve above purpose?

This is my client.

@FeignClient(value = "stores", url = "localhost:9000" ,fallbackFactory = FallbackFactoryImp.class)
public interface StoreClient {

    @RequestMapping(method = RequestMethod.GET, value = "/stores")
    List<Long> getStores();

    @RequestMapping(method = RequestMethod.GET, value = "/storesOthers")
    List<Long> getStoresOther();
}

And this is my fallback factory.

@Component
public class FallbackFactoryImp implements FallbackFactory<StoreClient> {

    @Override
    public StoreClient create(Throwable throwable) {
        return new StoreClient() {
            @Override
            public List<Long> getStores() {
                return (List<Long>) new HystrixCommand<Object>(null, null) {
                    @Override
                    protected Object run() throws Exception {
                        // retry to other server ex) GET localhost:8080/stores
                        return null;
                    }

                    @Override
                    protected Object getFallback() {
                        // return static data;
                        return null;
                    }
                }.execute();
            }

            @Override
            public List<Long> getStoresOther() {
                // duplicate logic
                return (List<Long>) new HystrixCommand<Object>(null, null) {
                    @Override
                    protected Object run() throws Exception {
                        // retry to other server ex) GET localhost:8080/storeOthers
                        return null;
                    }

                    @Override
                    protected Object getFallback() {
                        // return static data;
                        return null;
                    }
                }.execute();
            }
        };
    }
}

Aucun commentaire:

Enregistrer un commentaire