gpt4 book ai didi

java - "FF4J"(翻转)不适用于 "Hystrix"

转载 作者:行者123 更新时间:2023-11-30 07:05:45 25 4
gpt4 key购买 nike

FF4J不适用于 Hystrix。例如,在MyRest中,我尝试在某些条件下翻转实现,在​​MyIntegrationImpl中没有Hytrix注释@HystrixCommand的情况下它可以正常工作。但是,当我在 MyIntegrationImpl 中使用 @HystrixCommand 时,它不再起作用,spring 只注入(inject)默认实现 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 增强类(动态代理)。

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

@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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com