简体   繁体   English

功能翻转REST端点的注释

[英]Annotations for feature flipping REST end points

I have spring controller with several (REST) endpoints. 我有带有几个(REST)端点的spring控制器。 I want to bring up multiple instances of this controller where each instance would have few endpoints selectively enabled/disabled. 我想提出这个控制器的多个实例,其中每个实例都有几个端点有选择地启用/禁用。

Based on my reading so far, togglz provides feature flipping, but it doesnt enable/disable the REST endpoints (togglz provides API so that caller code can check if a feature is enabled); 根据我到目前为止的阅读, togglz提供了功能翻转,但它不启用/禁用REST端点(togglz提供API,以便调用者代码可以检查是否启用了功能); ff4j seems to be another alternative, but it was not very obvious from the documentation if it can enable/disable REST end points ff4j似乎是另一种选择,但是如果它可以启用/禁用REST端点,则从文档中不是很明显

I read the thread Feature Toggling Java Annotations but it is a longer implementation. 我阅读了线程功能切换Java注释,但它是一个更长的实现。 Is there any package that I can use to specify the endpoints that need to be enabled/disabled in a configuration file and use annotation on REST endpoints to disable/enable them (this way the logic in my method stays untouched and minimizes the testing) 是否有任何软件包可用于指定需要在配置文件中启用/禁用的端点,并在REST端点上使用注释来禁用/启用它们(这样我的方法中的逻辑保持不变并最小化测试)

A class with the @Bean or @Component will be loaded by spring on startup through the bean visitor mechanism. 具有@Bean@Component将在启动时通过bean访问器机制在春季加载。 To exclude this bean from the Spring context at startup you can create a BeanPostProcessor ( here ) and check for dedicated annotation BUT as far as I understand, you cannot put the bean back to the context at runtime. 要在启动时从Spring上下文中排除这个bean,你可以创建一个BeanPostProcessor这里 )并检查专用注释,但据我所知,你不能在运行时将bean放回上下文。

As a consequence, you must make this bean 'intelligent' to perform the correct operation/mock (or send 503 HTTP code) when requests come in. 因此,当请求进入时,您必须使此bean“智能”以执行正确的操作/模拟(或发送503 HTTP代码)。

FF4j can indeed help you implementing this behaviour but not with a single annotation on top of your REST Controller. FF4j确实可以帮助您实现此行为,但不能在REST控制器上使用单个注释。 What you could do : 你能做什么:

  • Create an interface, annotate the interface with the dedicated FF4J annotation 创建一个接口,使用专用的FF4J注释注释该接口
  • Create 2 implementations of the interface, each time with a different name 创建2个接口实现,每次使用不同的名称
  • Use FF4J to choose an implementation or another at runtime. 使用FF4J在运行时选择实现或其他实现。

Here some code snippet to get the idea : 这里有一些代码片段可以实现这个想法:

public interface GreetingService {
  @Flip(name = "theFeatureIDToToggle", alterBean = "greeting.french")
  String sayHello(String name);
}

@Component("greeting.french")
public class GreetingServiceFrenchImpl implements GreetingService {
    public String sayHello(String name) {return "Bonjour " + name;
}

@Component("greeting.english")
public class GreetingServiceEnglishImpl implements GreetingService {
    public String sayHello(String name) {return "Hello " + name;
}

//... import
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-ff4j-aop-test.xml")
public class FeatureAdvisorTest {

   @Autowired
   private FF4j ff4j;

   @Autowired
   @Qualifier("greeting.english")
   private GreetingService greeting

   @Test
   public void testAnnotatedFlipping_with_alterBean() {
      ff4j.disable("theFeatureIDToToggle");
      Assert.assertTrue(greeting.sayHello("CLU").startsWith("Hello"));

      ff4j.enable("theFeatureIDToToggle");
      Assert.assertTrue(greeting.sayHello("CLU").startsWith("Bonjour"));
   }
}

You can toggle a single method or the whole class, as you wish all samples are available here . 您可以切换单个方法或整个类,因为您希望所有样本都可在此处获得

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM