首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“FF4J”(倒装)不适用于"Hystrix“

“FF4J”(倒装)不适用于"Hystrix“
EN

Stack Overflow用户
提问于 2016-10-20 17:51:00
回答 1查看 356关注 0票数 1

FF4J不适用于Hystrix。例如,在MyRest中,我尝试在一些条件下翻转实现,没有MyIntegrationImpl中的Hytrix注释@HystrixCommand,它可以很好地工作。但是,当我在@HystrixCommand中使用MyIntegrationImpl时,它不会工作得更多,spring只会注入默认的实现MyIntegrationImpl

我的密码..。

MyRest.java

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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

代码语言:javascript
复制
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

代码语言:javascript
复制
<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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-27 14:19:22

对于先前的1.5版本,FF4J中有一个bug。FeatureAdvisor没有正确地处理cglib增强类(动态代理)。

我为您提供了一个完整的工作示例这里和ff4j 1.6+ (顺便提一句,还有新的控制台)。

代码语言:javascript
复制
@HystrixCommand(groupKey = HystrixHelper.MY_GROUP, commandKey = "myCommandKey")
public ResponseDTO doIntegration(String myDTO) {
    return new ResponseDTO("doIntegration: ok with " + myDTO);
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40161243

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档