1.

Where do you define properties in Spring Boot Application? Explain different property formats supported.

Answer»

By default, application properties will sit inside src/main/resources of your Spring Boot Application. You can have multiple properties FILE, one per environment and one default properties file. 

For example, you may have :

application.properties           application-dev.properites application-int.properites          application-perf.properites application-uat.properites application-prod.properites

Now, depending on the value of Spring profile, the appropriate file would be selected for reference (along with with environment independent file, application.properties ). So what you get at any environment is a merger of application.properties and environment-specific properties. It means if the same property exists in the application.properties file, then the one from an environment-specific file will get higher precedence hence later overrides the former.

Spring boot supports properties, YAML or XML format. Spring Boot advocates 12-factor app principle of EXTERNALIZING the configuration. What it means, you can have these key-value pairs in external property repository and Spring Boot supports not only fetching the properties from those repositories but also refreshing the values when value changes at the repository.

There are 3 ways Spring Boot supports this:

1. Pull Approach 

In this approach, let’s CONSIDER your properties file are residing in the git repository ( True source of Properties file).  You can mark a property ready for a dynamic refresh ( without restarting the application) by using @RefreshScope.

(org.springframework.cloud.context.scope.refresh.RefreshScope).

You can use spring-cloud-config-server dependency to host these files to a server using a simple Spring Boot Application. In this spring boot application, you can use the following property to connect to the git repository:

spring.cloud.config.server.git.uri=https://github.com/your-repo/config-server-repo.git

And in actual application, make use of spring-cloud-starter-config dependency to connect your application to the server where these files are HOSTED. Your service will read these files from the hosted server. In your application, you need to provide the URL where these property files are   for example:

spring.cloud.config.uri=http://host1:8888

Whenever the value of any property goes, you can hit /refresh endpoint (provided by the actuator) on the application, this will be a POST request. Once the request is executed, the application will refresh all those properties marked for dynamic change will be refreshed.

2. Using a Push Approach 

In this approach, you use Spring-Cloud-Bus dependency and message broker ( let’s say RabbitMQ). Your config-server still points to the git repository. However, you use spring-cloud-starter-bus-amqp dependency to establish the communication between the config server & your application. This dependency NEEDS to be included in both, your application & Config Server.



Discussion

No Comment Found