in the previous post sprig config server we have seen how to start a spring config server with a sample application config.
Let us see some more variations that spring allows us with spring config server.
The spring config server allows for using placeholders {application} and {profile} .
With this flexibility we can configure one repository per application against one repository for all the applications as we did in the previous post, let us see how. We will change what we did in earlier post so I will make changes to the same project to demonstrate the feature.
Let us first create two repositories for two applications “my-app-1” and “my-app-2”
Below I have created and initialized the first repo
add two files into this directory as application.yml and application-dev.yml
application.yml
app:
name: my-app-1
application-dev.yml
app:
name: my-app-1-dev
commit this file into the git repo with “git add .” and “git commit”
Now, similarity create another repo in the same root directory with name “my-app-2”, in my case it was “C:\Users\<user>\git”
So we have two repositories for the two apps now, one with name “my-app-1” and another with “my-app-2”. Now lets add this config to the server application config.
Navigate to the configuration of the config-server and update the git url to use application placeholder like below.
With the placeholder “{profile}” we can also create various other combinations. For e.g. we can create directories within the application repo for each of different profile instead of the suffix used in our example or in case of single repository for all the applications, we can use profile for dev and test or any folder of your choice within the repository.
You can also use the placeholder {application} or {profile} in searchpath config which basically is instruction to the spring config server on the whereabouts of your app config.
I have created two directories under the single repository configuration from my previous post and instead of having the config with the app name into the root directory, now I have created 2 directories for each app within the same root directory like below
I will have to also update the config-server application configuration to instruct on this new application locations.
Spring config server is one of the very important module of spring cloud to basically externalize application configuration. In a microservice architecture, spring config server provides a central place where all the microservices can look for the application configuration. It has support for multiple applications and environments.
We will look into a sample spring server application supported by git as storage for the application config. Although we can use a different form of storage but then it will require some additional changes.
Navigate to spring https://start.spring.io/ and add config server to dependencies, provide the artifact details as per the screenshot below or choose one of your own and click on generate
This will download a zip to you local filesystem. Unzip the directory and import/open the project into your favorite IDE. I am using Intellij.
The imported project will look like below.
Take a look at the pom.xml, it will have dependency for spring cloud config server apart from the spring boot starter dependency. Also it will import spring cloud dependencies using the maven import pom.
let us create a git repository on our local and then migrate it on to the github.
create a directory app-config and navigate into the directory on command line and fire command “git init”
create a file inside the app-config directory. lets say myapp.yml and add a property like below
app:
name: my-first-app
Now add this file to the git repo using git add and then git commit as below
let us add reference to this repo in our spring boot application. To do this, just open the application.yml file use the spring.cloud.config.git.url to the repository we created like below
Here the spring has assumed the default profile as master or assumed that the branch is the default profile.
Now lets add another file to the git repo with name “my-first-app-dev.yml” and add the following property to the config
app:
name: my-first-app-dev
lets go back to the browser and now instead of master lets use dev as the profile in the url and check what the server responds with ( “http://localhost:8001/my-first-app/dev“) .
Now our spring config server has done the magic trick. It has returned us the dev profile for our application. if you see the app.name is now my-first-app-dev and also we did not restart the server. Spring has picked up the change and responded with new profile we just added.
In the next blog, we will see various other configurations that we can use with spring config server.
NOTE: Kindly select java version according to your project need ..I am using java 1.8 else you will get below exception when you run mvn clean install
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Maven: Compilation failure: Compilation failure
Dependencies will look like below
Kindly add required dependencies as shown in above screenshot and down load project by clicking on “GENERATE” button. ZIP will get downloaded.
NOTE: if you are downloading on office laptop then you might need to unblock zip before unzipping. for doing this right click on zip file and go to properties and unblock it. so you will able to unzip it completely.
After unzip you will see folder generate like below screenshot
4 It will show pop up to select folder of your project. Kindly select folder which you unzipped.
5 Select pom.xml and click on Finish button
6. Now your project will get created in workspace and all dependencies will get downloaded by maven.Make sure maven is installed on your machine.
In below screenshot and in highlighted part you can see its showing “boot” project in bracket because spring tool suit will automatically detect it as spring project. For this in eclipse you need to download plugins.
Your project will be visible in workspace now
7. Right click on project and go to run and do maven install as shown in below screen shot
On console you will see below logs
8. Once build is successful then add below code in DemoApplication.java and add below configurations to application.properties file as well
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("Welcome to spring boot");
}
}
Above code will have datasource related to H2 DB and it will tell spring boot on which path database whould be accessible in browser. Your application will run on 8888 port
Add below files in resources folder
Add data.sql file as shown below
data.sql
DROP TABLE IF EXISTS employee;
CREATE TABLE employee (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(250) NOT NULL,
last_name VARCHAR(250) NOT NULL
);
INSERT INTO employee (first_name, last_name) VALUES
('James', 'mill')
schema.sql
DROP TABLE IF EXISTS NATION;
CREATE TABLE NATION (
id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
PRIMARY KEY (id)
);
9. Now Run application as spring boot app shown in below screenshot
10. Congrats now your application is running successfully and your H2 Db is running
Here in both classes as you see we have used annotation like @Table(name=”country”) ,@Entity,@Id and @Column ,They will help spring JPA to understand that its entity class and with which table its associated
@Id marks a field in a model class as the primary key:
@Table: Specifies the primary table for the annotated entity
@Column :Specifies the mapped column for a persistent property or field. If no Column annotation is specified, the default values apply.
@Entity:Specifies that the class is an entity. This annotation is applied to the entity class.
@GeneratedValue :Provides for the specification of generation strategies for the values of primary keys
Below is Screen shot to make you familiar with spring initializer
Add dependency and generate project
Spring Web
Kindly add required dependencies as shown in above screenshot and down load project by clicking on “GENERATE” button. ZIP will get downloaded.
NOTE: Kindly select java version according to your project need ..I am using java 1.8 else you will get below exception when you run mvn clean install
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Maven: Compilation failure: Compilation failure
NOTE: if you are downloading on office laptop then you might need to unblock zip before unzipping. for doing this right click on zip file and go to properties and unblock it. so you will able to unzip it completely.
After unzip you will see folder generate like below screenshot
Go to your editor and import project.
For eclipse or STS Users..
1 GO to file
2 click on import
3 type maven and select Existing maven project
4 It will show pop up to select folder of your project. Kindly select folder which you unzipped.
5 Select pom.xml and click on Finish button
6. Now your project will get created in workspace and all dependencies will get downloaded by maven.Make sure maven is installed on your machine.
In below screenshot and in highlighted part you can see its showing “boot” project in bracket because spring tool suit will automatically detect it as spring project. For this in eclipse you need to download plugins.
Your project will be visible in workspace now
7. Right click on project and go to run and do maven install as shown in below screen shot
On console you will see below logs
8. Once build is successful then add below code in DemoApplication.java
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("Welcome to spring boot");
}
}
NOTE: Kindly select java version according to your project need ..I am using java 1.8 else you will get below exception when you run mvn clean install
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project Maven: Compilation failure: Compilation failure
Dependencies will look like below
Kindly add required dependencies as shown in above screenshot and down load project by clicking on “GENERATE” button. ZIP will get downloaded.
NOTE: if you are downloading on office laptop then you might need to unblock zip before unzipping. for doing this right click on zip file and go to properties and unblock it. so you will able to unzip it completely.
After unzip you will see folder generate like below screenshot
4 It will show pop up to select folder of your project. Kindly select folder which you unzipped.
5 Select pom.xml and click on Finish button
6. Now your project will get created in workspace and all dependencies will get downloaded by maven.Make sure maven is installed on your machine.
In below screenshot and in highlighted part you can see its showing “boot” project in bracket because spring tool suit will automatically detect it as spring project. For this in eclipse you need to download plugins.
Your project will be visible in workspace now
7. Right click on project and go to run and do maven install as shown in below screen shot
On console you will see below logs
8. Once build is successful then add below code in DemoApplication.java and add below configurations to application.properties file as well
DemoApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
System.out.println("Welcome to spring boot");
}
}
Above code will have datasource related to H2 DB and it will tell spring boot on which path database whould be accessible in browser. Your application will run on 8888 port
Add below files in resources folder
Add data.sql file as shown below
data.sql
DROP TABLE IF EXISTS employee;
CREATE TABLE employee (
id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(250) NOT NULL,
last_name VARCHAR(250) NOT NULL
);
INSERT INTO employee (first_name, last_name) VALUES
('James', 'mill')
schema.sql
DROP TABLE IF EXISTS NATION;
CREATE TABLE NATION (
id INTEGER NOT NULL AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
PRIMARY KEY (id)
);
9. Now Run application as spring boot app shown in below screenshot
10. Congrats now your application is running successfully and your H2 Db is running