Search code examples
javaspringspring-cloudhystrixff4j

"FF4J"(flip) not work with "Hystrix"


FF4J doesn't work with Hystrix. For example in MyRest I try flip the implementation with some condition, it's work fine without Hytrix annotation @HystrixCommand in MyIntegrationImpl. But when I use @HystrixCommand in MyIntegrationImpl it's not work more and spring only injects the default implementation MyIntegrationImpl

My code.....

MyRest.java

@RequestMapping("/myrest")
public class MyRest {

    @Autowired
    private MyService myService;

    @Autowired
    private FF4j ff4j;


    @ResponseBody
    @RequestMapping(method=RequestMethod.POST, 
        consumes=MediaType.APPLICATION_JSON_VALUE,
        produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseDTO cancellationRequest(@RequestBody MyDTO myDTO) {


        //Just for test
        if(myDTO.getId() != null){
            ff4j.enable(FF4JHelper.MY_INTEGRATION);
        }else{
            ff4j.disable(FF4JHelper.MY_INTEGRATION);
        }



        ResponseDTO responseDTO = myService.doSomething(myDTO);

        return responseDTO;
    }
}

MyServiceImpl.java

@Service
public class MyServiceImpl implements MyService {

    @Autowired
    @Qualifier(FF4JHelper.MY_INTEGRATION)
    MyIntegration myIntegration;

    @Override
    public ResponseDTO doSomething(MyDTO myDTO) {
        ResponseDTO response = myIntegration.doIntegration(myDTO);
        return response;
    }
}

MyIntegrationImpl.java

@Service(FF4JHelper.MY_INTEGRATION)
public class MyIntegrationImpl implements MyIntegration{

    @Override
    @HystrixCommand(groupKey = HystrixHelper.MY_GROUP, commandKey = "myCommandKey")
    public ResponseDTO doIntegration(MyDTO myDTO) {
        ResponseDTO responseDTO = null;   //TODO: .... doIntegration
        return responseDTO;
    }

    public ResponseDTO doIntegrationFallback(MyDTO myDTO){
        ResponseDTO responseDTO = null; //TODO: .... doIntegration Fallback
        return responseDTO;
    }
}

MyIntegrationMock.java

@Service(FF4JHelper.MY_INTEGRATION_ALTERNATIVE)
public class MyIntegrationMock implements MyIntegration{

    @Override
    public ResponseDTO doIntegration(MyDTO myDTO) {
        ResponseDTO responseDTO = null;   //TODO: .... doIntegration MOCK
        return responseDTO;
    }
}

FF4jConfiguration.java

@Configuration
@ComponentScan(basePackages ="org.ff4j.aop")
public class FF4jConfiguration implements FF4JProvider {

    @Autowired
    private Mongo mongo;

    @Value("${ff4j.webapi.authentication}")
    private boolean authentication;

    @Value("${ff4j.webapi.authorization}")
    private boolean authorization;

    @Bean
    public FF4j getFF4j() {
        FF4j ff4j = new FF4j();

        //MongoDB
        DBCollection ff4jCollection = mongo.getDB(FF4JHelper.FF4J_DATABASE_NAME).getCollection(FF4JHelper.FF4J_COLLECTION);
        FeatureStore featureStore = new FeatureStoreMongoDB(ff4jCollection);
        ff4j.setFeatureStore(featureStore);

        //Create Feature
        createFeatureStore(FF4JHelper.MY_INTEGRATION, ff4j, featureStore);

        return ff4j;
    }

    private void createFeatureStore(final String nameFeatureStore, final FF4j ff4j, final FeatureStore featureStoreMongoDB) {
        if(!ff4j.getFeatureStore().exist(nameFeatureStore)) {
            featureStoreMongoDB.create(new Feature(nameFeatureStore));
        }
    }

    @Bean
    public ApiConfig getApiConfig(FF4j ff4j) {
        ApiConfig apiConfig = new ApiConfig();
        apiConfig.setAuthenticate(authentication);
        apiConfig.setAutorize(authorization);
        apiConfig.setFF4j(ff4j);
        return apiConfig;
    }

    @Bean
    public ConsoleServlet getFF4JServlet(FF4j ff4j) {
        ConsoleServlet consoleServlet = new ConsoleServlet();
        consoleServlet.setFf4j(ff4j);
        return consoleServlet;
    }

    @Bean
    public ServletRegistrationBean servletRegistrationBean(ConsoleServlet consoleServlet){
        return new ServletRegistrationBean(consoleServlet, "/ff4j-console/*");
    }

}

application.yml

server:
    port: 7300
    display-name: my-project

ff4j:
  webapi:
    authentication: false
    authorization: false

hystrix:
    command:
        default:
            execution:
                timeout:
                    enabled: true
                isolation:
                    strategy: THREAD
                    thread:
                        interruptOnCancel: true
                        interruptOnTimeout: true
                        timeoutInMilliseconds: 10000
            fallback:
                enabled: true
            circuitBreaker:
                enabled: true
                requestVolumeThreshold: 20
                sleepWindowInMilliseconds: 5000
                errorThresholdPercentage: 50
                forceOpen: false
                forceClosed: true
            metrics:
                rollingStats:
                    timeInMilliseconds: 10000
                    numBuckets: 10
                rollingPercentile:
                    enabled: true
                    timeInMilliseconds: 60000
                    numBuckets: 6

pom.xml

<dependencies>

    <!-- Spring -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Brixton.SR5</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>


    <!-- FF4J -->
    <dependency>
        <groupId>org.ff4j</groupId>
        <artifactId>ff4j-core</artifactId>
        <version>1.5</version>
    </dependency>
    <dependency>
        <artifactId>ff4j-web</artifactId>
        <groupId>org.ff4j</groupId>
        <version>1.5</version>
    </dependency>
    <dependency>
        <groupId>org.ff4j</groupId>
        <artifactId>ff4j-store-mongodb</artifactId>
        <version>1.5</version>
    </dependency>
    <dependency>
        <artifactId>ff4j-aop</artifactId>
        <groupId>org.ff4j</groupId>
        <version>1.5</version>
    </dependency>

    <!-- Hystrix -->
    <dependency>
        <groupId>com.netflix.hystrix</groupId>
        <artifactId>hystrix-javanica</artifactId>
        <version>1.5.6</version>
    </dependency>

</dependencies>

Solution

  • There were a bug in FF4J for versions previous 1.5. The FeatureAdvisor did not properly handled the cglib enhanced classes (dynamic proxy).

    I provide you a full working sample HERE with ff4j 1.6+ (by the way, with the new console).

    @HystrixCommand(groupKey = HystrixHelper.MY_GROUP, commandKey = "myCommandKey")
    public ResponseDTO doIntegration(String myDTO) {
        return new ResponseDTO("doIntegration: ok with " + myDTO);
    }