FF4J不适用于Hystrix。例如,在MyRest中,我尝试在某种条件下翻转实现,而在@HystrixCommand中没有Hytrix注释MyIntegrationImpl的情况下,它可以很好地工作。但是,当我在@HystrixCommand中使用MyIntegrationImpl时,它将无法正常工作,并且spring仅注入默认实现MyIntegrationImpl

我的代码.....

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>

最佳答案

FF4J的1.5版以前存在错误。 FeatureAdvisor没有正确处理cglib增强类(动态代理)。

我为您提供了ff4j 1.6+的完整工作示例HERE(顺便说一下,使用新控制台)。

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

关于java - “FF4J”(翻转)不适用于“Hystrix”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40161243/

10-11 04:12