1

FF4J不适用于 Hystrix。例如,在MyRest我尝试在某些条件下翻转实现时,如果没有 Hytrix 注释,它可以正常@HystrixCommand 工作MyIntegrationImpl。但是当我使用它时@HystrixCommandMyIntegrationImpl它就不起作用了,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/*");
    }

}

应用程序.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>
4

1 回答 1

1

对于 1.5 之前的版本, FF4J中存在一个错误。FeatureAdvisor没有正确处理 cglib 增强类(动态代理)。

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

@HystrixCommand(groupKey = HystrixHelper.MY_GROUP, commandKey = "myCommandKey")
public ResponseDTO doIntegration(String myDTO) {
    return new ResponseDTO("doIntegration: ok with " + myDTO);
}
于 2016-10-27T14:19:22.820 回答