Spring boot switch API calls

is there any spring way to configure switching between different API calls using the properties set. Any suggestions are welcomed. Thanks

1 answer

  • answered 2020-02-17 04:58 Mark Bramnik

    IMO, Your question is somewhat vague, an example of what you're trying to achieve could be helpful.

    Anyway, in spring boot "properties set" can be implemented in configurations and spring profiles:

    1. create file src/main/resources/application-p1.propreties (p1 stands for profile, give it a meaningful name instead):
    api1.host=host_a
    api1.port=port_a
    api2.host=host_b
    api2.port=port_b
    ... // and so forth
    
    1. create file src/main/resources/application-p2.properties that will denote a different profile of execution:
    api1.host=host_c
    api1.port=port_c
    api2.host=host_d
    api2.port=port_d
    ... // and so forth
    
    1. Note, the keys are the same, but the values are different between (1) and (2) Now, create beans "responsible for API calls". Inject the values with the given names (api1.host, api1.port, ...) to the corresponding beans

    2. Run the spring boot application with a profile:

    java -jar myapp.jar --spring.profiles.active=p1 // or alternatively p2

    Now different sets of properties values will be loaded depending on profiles.

    Side notes:

    1. Its also possible to use yaml files for configuration, in yaml file you can also define all profiles in one file if you wish or create different yamls

    2. If you want some "default" set of properties that would be resolved the application is running without --spring.profiles.active flag - you can put these key/value pairs into application.properties which is resolved automatically.

    3. If you specify the same key/value pairs in both application.properties and application-p1.properties and run with p1 profiles the values in application-p1.properties (more specific) will take precedence over the default values.

    Update 1 based on OP's comment:

    You still can use profiles in this case:

    For example: in application-p1.properties create a key/value like calls.shouldCallB=true application-p2.properties will contain key/value like calls.shouldCallB=false

    Then you can inject this value into your service of API where you decide where to call A, B, C, D beans:

    
    public class MyService {
    
       private final boolean shouldCallB;
    
       public MyService(boolean shouldCallB) {
          this.shouldCallB = shouldCallB;
       }
    
       public void execute() {
    
          callA();
          if(shouldCallB) {
              callB();
          }
          callC();
          callD();
       }
    }
    

    In Java Configuration you can map the configuration property with @Value or @ConfigurationProperties (spring boot supports both, configuration properties is better IMO):

    @ConfigurationProperties(prefix="calls")
    public class CallsConfigProps {
       private boolean shouldCallB;
       // getters, setters
    }
    
    @Configuration
    @EnableConfigurationProperties(CallsConfigProps.class)
    public class MyAppConfig {
    
        @Bean
        public MyService myService(CallsConfigProps config) {
          return new MyService(config.getShouldCallB());
        }
    }
    

    Depending on the loaded profile the switch will be turned on or off