Skip to main content

How to create Spring boot cloud config server


What is Spring boot cloud config server?

Spring Cloud Config Server provides an HTTP resource-based API for external configuration (name-value pairs or equivalent YAML content). The server is embeddable in a Spring Boot application, by using the @EnableConfigServer annotation. Consequently, the following application is a config server
In Simple terms, Spring boot cloud config server takes your application.yml or application.properties from your spring boot application and serve it over HTTP, and spring application which need to use it just need to delete the application.yml or application.properties and create one bootstrap.yml/properties and defined 2 simple properties spring.application.name and spring.cloud.config.uri



  • Create New java project using one of following instructions.
  1. Create project with Java, maven and Intellij

  • Edit POM.xml, add following content in pom.xml
First specify the packaging

jar
also add parent of this pom to be
NOTE: You can lookup the latest version for parent from here, I will advise to use latest version if possible at the time of writing this article its 2.0.4.RELEASE.


Add Rest of dependencies Right Click on Java folder and create new Java file com.next.config.SpringCloudConfigServer
NOTE : the annotation to enable config server



Add application.yml file in resources


Create Directory /tmp/config (if you are on Mac or Linux), on windows you can create directory like c:/tmp/config and update the application.yml with this path.

create a file application.yml in directory /tmp/config or c:/tmp/config
I am adding only one property for simplicity, but feel free to add as many as you like.

once file is created, initialise the git on this folder by running following commands.
Now the server is ready with minimum capabilities, that it can serve this application.yml from /tmp/config folder via http.

Start the server by right clicking on java file SpringCloudConfigServer and select Run,
once started visit http://localhost:8888/application/profile (basically url structure is http://localhost:8888/<application name>/<profile name(s)> and this will show you the content of /tmp/config/application.yml in format something like this

We have just seen simplest implementation of Spring Cloud Config server, this server doesn't care what application name or profile you passed to it, it will always return content of /tmp/config/application.yml which is not suitable for a production setup, as you may want to run multiple application in multiple environments(profiles).

I will explain such scenario in next section.

Comments

Popular posts from this blog

Validate HTML as XHTML in Intelij

When working in intelij with HTML, sometimes we need to make sure our HTML is XHTML compliance, no un ened tags etc.
i.e. we should not have

<img src="myimage.jpg> If You put above code in an HTML and run it in browser it will work fine, but if you are using some server side framework like Thymeleaf with Spring, it will generate error saying <img> tag must be terminated.

By Default intelij will not complaint about this and when you are writing HTML you may ignore the error and will get the error when you actually run it with in server. Or worst some HTML developer is writing the code HTML code for you and then you will have to fix all these errors.

Intelij can help you to make sure you write a valid HTML(XHTML). You can enable XHTML validation for HTML files by following below steps.

Go to Settings(Windows)/Preference(mac) in Intelij.Search for "File Types"Find HTML as shown in image below. You will see HTML has some extension associated with like *.html, *…

Understanding Equals and hashCode contract,and what can go wrong

SO what about Equals and hashcode?
Most people will say these are the two basic function available in Object class and could be called from any Class object created in Java. Well thats true but there is more then this.
First take a look at HashCode:
Every JVM provides a good hashCode implementation mostly based on the object's memory location and for two different object(by location) it produce different hashCode.
And at the same time Equal method also consider two different memory location object as Not Equal. So at core two are in a contract and works great.

But as soon as start writing code and write a class and create a state of a class(means create few data variables in a class), the core contract of hashCode and Equal doesnt work any more.
e.g.
Class MyFirstCLass{ int value; public MyFirstCLass(int value){ this.value = value; } } MyFistCLass obj1 = new MyFirstCLass(10); MyFistCLass obj2 = new MyFirstCLass(10);
Now obj1 and obj2, theoratically should be equal as both holds …

Difference between ArrayList and LinkedList

What is the difference between ArrayList and LinkedList

First of all lets see what Interfaces they implement.
ArrayList (See on Oracle/sun site)SerializableCloneableIterable<E>Collection<E>List<E>RandomAccessLinkedList(See on Oracle/Sun site)SerializableCloneableIterable<E>Collection<E>Deque<E>List<E>Queue<E>By looking at interface being implemented by these two collection, we could see RandomAccess is implemented by ArrayList and Queue<E>,Deque<E> implemented by LinkedList only. So all the differences between these two collections are because of these three Interfaces and their implementation.

RandomAccess : ArrayList can be accessed randomly, means we could access nth element of an ArrayList directly without affecting search/seek performance. e.g. list.get(n). Means get performance of nth element will never depend on how large is the ArrayList.It will remain constant.
While LinkedList cant be accessed randomly(though it does …