Java systems configuration - how to remove pain

Read configs via Java interface
Read configs via Java interface

Problem

If you have developed and maintained a large system in Java at least once, then, for sure, you have encountered the inconvenience of setting up certain processes in the system. 





Namely: the sizes of the various buffers; mailbox parameters; host, port, login, password for calling external services; all sorts of timeouts and much more.





Each time it is not comme il faut to change all this in the code, rebuild and re-roll it to real.





Naturally, all these parameters need to be transferred to the config files and read from there - everyone does this.





In Java, out of the box, there is a certain Properties for this. But using it is extremely inconvenient. Firstly, UTF-8 does not work there, and secondly, if you change any parameter in the config, then the application must be restarted for the new value to get into the system. And if you don’t want to restart it, or it’s not possible at 11 am - peak hour. And postponing until later is not an option - you need it urgently. What to do? It is necessary that the configs be re-read "hot", that is, without restarting the system.





And it is also very important: you need to somehow come up with so that the names of the config parameters in the program code correspond to those in the file. That is, to make it difficult to make a mistake. Usually they use constants for this - it helps, but I would like something more convenient, simpler and more flexible.





: , . . ? ? , ? ? ? ? β€” ? , ? , - …





.





- , , β€” , . .





β€” - , - - ( IDE ) β€” β€” .





, - (), - β€” - , .





greetgo! , β€” .





Java- , , , . β€” . , β€” β€” .





updateTimeout batchSize β€” . Java-:





public interface MyMigrationConfig {
  long updateTimeoutMs();
  int  batchSize();
}
      
      



:





@Description("    ");
public interface MyMigrationConfig {
  @Description("      ."
               + "    ,    "
               + "   ")
  @DefaultLongValue(30000)
  long updateTimeoutMs();

  @Description("     ")
  @DefaultIntValue(150)
  int batchSize();
}
      
      



- , , , , :





@Autowire
private MyMigrationConfig config;
 
public void migrate() {
  // ...
  System.out.println(" updateTimeoutMs = " + config.updateTimeoutMs());
  System.out.println(" batchSize       = " + config.bachSize());
  // ...
}
      
      



, .





, . . , production ready. β€” MIT.





greetgo conf β€” maven github: https://github.com/greetgo/greetgo.conf





, :





public class MyConfigFactory extends FileConfigFactory {
  @Override
  public Path getBaseDir() {
    return Paths.get("/path/to/directory/where/config/files/are/located");
  }

  @Override
  protected String getConfigFileExt() {
    return ".conf";
  }
}
      
      



createConfig:   





MyConfigFactory confFactory = new MyConfigFactory();

MyMigrationConfig config =  confFactory.createConfig(MyMigrationConfig.class);
      
      



. MyMigrationConfig.conf. , , :





# Created at 2021-01-29 11:03:21
#     

#    
batchSize=150

#       .   
# ,       
updateTimeoutMs=30000
      
      



. β€” , , ( ), .





kubernetes. , , Zookeeper, FileConfigFactory AbstractZookeeperConfigFactory, Zookeeper.





JdbcConfigFactory β€” .





, -.





@FirstReadEnv β€” , β€” kubernetes .





That's all. Thank you for attention.












All Articles