/spring-s3-properties-loader

Spring component to load external properties from AWS S3

Primary LanguageJavaMIT LicenseMIT

Build Status

Spring S3 Property Loader

S3 Property Loader has the aim of allowing loading of Spring property files from S3 bucket, in order to guarantee stateless machine configuration.

Spring PropertyConfigurer uses PropertiesFactoryBean to load property files from AWS S3 bucket.

Install

Gradle:

repositories {  
   jcenter()  
}
compile "com.spring.loader:s3-loader:2.2.2"

Maven:

<dependency>
  <groupId>com.spring.loader</groupId>
  <artifactId>s3-loader</artifactId>
  <version>2.2.2</version>
  <type>pom</type>
</dependency>

How to use

  • Adding this annotation to any spring managed bean
@S3PropertiesLocation("my-bucket/my-folder/my-properties.properties")
  • Using a specific profile to only load properties if the app is running with that profile
@S3PropertiesLocation(value = "my-bucket/my-folder/my-properties.properties", profiles = "production")
  • Load from a System env variable
@S3PropertiesLocation(value = "${AWS_S3_LOCATION}", profiles = "developer")
or
@S3PropertiesLocation(value = "${AWS_S3_BUCKET}/application/my.properties", profiles = "developer")

Refreshing properties in runtime

You can force your application to load properties from S3 again without restart. S3 Properties Loader uses a Spring Cloud feature that allows the spring beans annotated with @RefreshScope to reload properties. To work, it is only necessary to inject the S3PropertiesContext bean and call refresh() method. After this, S3 Properties Loader will get properties again from s3 bucket defined previously and refresh your beans annotated with @RefreshScope.

tip: You can create a endpoint that calls this class and refresh your application via endpoint or create a @Scheduled class which updates from time to time.

Example:

@RestController
public SomeController {

   @Autowired
   private S3PropertiesContext s3PropertiesContext;
    
   @PostMapping("/refresh-properties")
   public void refresh() {
       s3PropertiesContext.refresh();
   }
}

Requisites

Official spring aws sdk lib.

Problems and Issues

Found some bug? Have some enhancement ? Open a Issue here