Wednesday, September 11, 2019

Spring Configuration server and client

In microservice concept where in we divide our big application into small services many time it happens that when we need to update any small service which has its own properties files in that case to load this individual microservice property files on deployment we need to restart the server on which this microservice is deployed. This make the production in downtime for this service. For application where in downtime is not preferred for microservice this architecture is good fit on the same.

To achieve this need Spring Boot configuration server, and we will have Spring Boot configuration client
Spring Boot configuration server :- This will store provide the ability for the spring boot microservice to have properties files of its client to be stored centralized in it. i.e. depending on the client name we store the different properties files inside this Spring Boot configuration server and at run time when Spring Boot configuration client run it take its respective file from this server. Only the thumb rule is the name of the property files is kept as similar to client service. Lets say we have client microservice as sid-client.jar then its respective property files can be named as sid-client.properties or sid-client.yml file.
In addition to individual client properties files we can also have one common application.properties in server which will be common for all the client.

Let us build simple Spring boot configuration server as shown below
Image1

Image2Image3Image4
You will able to see belwo code in pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
Add following tag to your mail spring boot applications to indicate this microservice as a configuration server.
@SpringBootApplication
@Configuration
@EnableAutoConfiguration
@EnableConfigServer

Image10

Please add follwing enteries in the properties files
Image5
#indicate the port number on which our configuration server is running
server.port=8889
#indicate the location of the properties files for the spring boot microservice client
spring.cloud.config.server.native.searchLocations=file:///C:/siddhu/configproperties/
#SPRING_PROFILES_ACTIVE=native
#spring.cloud.config.server.bootstrap=true
spring.application.name=siddhuconfigurationserver
#indicate profiles are active and it is stored in local system. We can store the configuration properties directly on GIT, SVN, Consul etc
spring.profiles.active=native
Inside C:/siddhu/configproperties/ we have created a property files names as sid-client.properties which will server our client microserver sid-client. for simple side we had just kept following text in that property file
first.message = This is first message for Sid-Client Microservice from Spring Boot Configuration server
Now compile the spring boot configuration server and run the jar file and see the browser text on hitting this url

Image6Image7Image8
Now hit the URL http://localhost:8889/sid-client/default/master and you will be see your client properties
Image9

Now lets create one Spring boot simple client that will refer to our above create spring boot configuration server for fetching its respective properties values i.e. from sid-client.
Note:- We need to make sure when we create the spring boot client we should name the client as sid-client so that it can fetched its properties from server.
Image11Image12
Make sure we have following entry in our pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Add following annotation in our mail class
@RefreshScope
Image14
We are using Spring thymeleaf java based library to creat web application hence we will also need spring-boot-starter-web package in the xml.
Now create static folder inside our resource folder along with CSS and templates folder as shown below
Image15Image16
Now compile the client application and run the jar as shown in below screen using different port
java -Dserver.port=8890 -jar sid-client-0.0.1-SNAPSHOT.jar

Image17
Now hit the url http://localhost:8890/ and you will be able to see the value from the text that is described in server property files.
Image18

No comments: