{"id":124,"date":"2019-11-26T12:02:47","date_gmt":"2019-11-26T12:02:47","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/microservices\/?p=124"},"modified":"2019-11-27T08:46:51","modified_gmt":"2019-11-27T08:46:51","slug":"microservices-with-spring-profilesstep-by-step-tutorial","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/microservices\/microservices-with-spring-profilesstep-by-step-tutorial\/","title":{"rendered":"Microservices with Spring Profiles(Step by Step Tutorial)"},"content":{"rendered":"<p>In this step by step tutorial, I will teach you how to build microservices using Spring Profiles.<\/p>\n<p>Spring Profiles allow you to split a single application into microservices each running with a different profile(run configuration). Also in this tutorial, we would set up and start service discovery using Eureka Server.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>The Microservices we&#8217;ll Build<\/strong><\/p>\n<p>We would build microservices that represents a school system. I would have four services:<\/p>\n<ul>\n<li>Teachers service<\/li>\n<li>Students service<\/li>\n<li>Courses service<\/li>\n<li>Departments service<\/li>\n<\/ul>\n<p>The architecture for the system we are building is given below:<\/p>\n<figure id=\"attachment_125\" aria-describedby=\"caption-attachment-125\" style=\"width: 430px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Microservices-With-Spring-Profiles.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-125 \" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Microservices-With-Spring-Profiles.jpg\" alt=\"Microservices With Spring Profiles\" width=\"430\" height=\"261\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Microservices-With-Spring-Profiles.jpg 580w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Microservices-With-Spring-Profiles-300x182.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Microservices-With-Spring-Profiles-240x145.jpg 240w\" sizes=\"auto, (max-width: 430px) 100vw, 430px\" \/><\/a><figcaption id=\"caption-attachment-125\" class=\"wp-caption-text\">Microservices With Spring Profiles<\/figcaption><\/figure>\n<p><strong>Follow the Steps Below<\/strong><\/p>\n<p>For these steps, the actual details are provided in the Video here.<\/p>\n<p><strong>Step 1:<\/strong> Setup and start the Eureka Server<\/p>\n<p>You simply create a spring boot starter project and add the Eureka Server dependency.<\/p>\n<p>For some reason, I don&#8217;t see spring-boot-starter web dependency. In that case, you need to add the dependency to the pom.xml file.<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #007700;\">&lt;dependency&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;groupId&gt;<\/span>org.springframework.boot<span style=\"color: #007700;\">&lt;\/groupId&gt;<\/span>\r\n    <span style=\"color: #007700;\">&lt;artifactId&gt;<\/span>spring-boot-starter-web<span style=\"color: #007700;\">&lt;\/artifactId&gt;<\/span>\r\n<span style=\"color: #007700;\">&lt;\/dependency&gt;<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Also add the following to the application.properties file<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">server.port=8761\r\neureka.client.register-with-eureka=false\r\neureka.client.fetch-registry=false\r\n<\/pre>\n<p>These lines tell the server not to search for other servers (we just have one server). It also tell the server to run on port 8761 instead of default port 8080.<\/p>\n<p>Also remember to add the @EnableEurekaServer annotation to the main application class.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong>: Build a Eureka Client monolith application<\/p>\n<p>Create a spring boot starter project and add the Eureka Client dependency. Also add the spring-boot-starter-web dependency to to pom.xml.<\/p>\n<p>You can fire up the Eureka Server and visit<a href=\"#\"> http:\/\/localhost:8761<\/a> to make sure it&#8217;s up and running. The window is as shown below<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Eureka-Server-Window-2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-130 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Eureka-Server-Window-2-1024x735.jpg\" alt=\"\" width=\"572\" height=\"410\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Eureka-Server-Window-2-1024x735.jpg 1024w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Eureka-Server-Window-2-300x215.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Eureka-Server-Window-2-768x551.jpg 768w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Eureka-Server-Window-2.jpg 1266w\" sizes=\"auto, (max-width: 572px) 100vw, 572px\" \/><\/a><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Note<\/strong><\/span>: In your case, you&#8217;ll not see any services under the Application column. That would appear later.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 3<\/strong>: Create the four packages<\/p>\n<p>Create the following four packages: students, courses, teachers and departments<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 4<\/strong>: Add the controller class<\/p>\n<p>For each of the package, add the controller class with the following content:<br \/>\n<!-- HTML generated using hilite.me --><\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@GetMapping<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"\/students\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<span style=\"color: #008800; font-weight: bold;\">public<\/span> String <span style=\"color: #0066bb; font-weight: bold;\">Home<\/span><span style=\"color: #333333;\">()<\/span> <span style=\"color: #333333;\">{<\/span>\r\n\t<span style=\"color: #008800; font-weight: bold;\">return<\/span> <span style=\"background-color: #fff0f0;\">\"Welcome to Students Services\"<\/span><span style=\"color: #333333;\">;<\/span>\r\n<span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>Do the same for all the other packages\/controllers.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 5:<\/strong> Add the @EnableEurekaClient annotation to the the main application class<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 6:<\/strong> Add the @Profile annotation to all the Controller classes. For example, in the student controller, just below the @RestController annotation, add the annotation<\/p>\n<pre style=\"margin: 0; line-height: 125%;\"><span style=\"color: #555555; font-weight: bold;\">@Profile<\/span><span style=\"color: #333333;\">(<\/span><span style=\"background-color: #fff0f0;\">\"students\"<\/span><span style=\"color: #333333;\">)<\/span>\r\n<\/pre>\n<p>Repeat the same for the other services changing the name of the profile accordingly.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 7<\/strong>: Set the server.port=0 in the application.properties file.<\/p>\n<p>This setting tells spring boot to automatically assign a port (not the default 8080 port) to any of the service that starts up.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 8:<\/strong> Set the application name in the application.properties file<\/p>\n<p>We need to set the application name to correspond to the profile name. So at startup, the active profile name is used to generated the application name. In this way, we ensure the four profiles run as four different application.<\/p>\n<p>To do this, add the following code to the\u00a0 application.properties file<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">spring<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">application<\/span><span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">name<\/span> <span style=\"color: #333333;\">=<\/span> $<span style=\"color: #333333;\">{<\/span>spring.profiles<span style=\"color: #333333;\">.<\/span><span style=\"color: #0000cc;\">active<\/span><span style=\"color: #333333;\">}<\/span>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong>Step 9:<\/strong> Set up the run configuration.<\/p>\n<p>This is a very important part. To do that, click on run in the menu and select &#8216;Run Configuration&#8217;. The Run Configurations window opens up as shown below:<\/p>\n<figure id=\"attachment_127\" aria-describedby=\"caption-attachment-127\" style=\"width: 581px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Spring-Boot-Run-Configuration-Window-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-127 \" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Spring-Boot-Run-Configuration-Window-1.jpg\" alt=\"Spring Boot Run Configuration Window\" width=\"581\" height=\"407\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Spring-Boot-Run-Configuration-Window-1.jpg 862w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Spring-Boot-Run-Configuration-Window-1-300x210.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Spring-Boot-Run-Configuration-Window-1-768x538.jpg 768w\" sizes=\"auto, (max-width: 581px) 100vw, 581px\" \/><\/a><figcaption id=\"caption-attachment-127\" class=\"wp-caption-text\">Spring Boot Run Configuration Window<\/figcaption><\/figure>\n<p><strong>Step 10:<\/strong> Select Spring Boot App in the list and click on the Launch New Configuration button (circled in red)<\/p>\n<p><strong>Step 11<\/strong>: Give the configuration a name and click on Apply. In this case students-config<\/p>\n<p><strong>Step 12<\/strong>: Set the spring boot run configuration as shown in the figure below:<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-Configuration-Spring-Boot-Setting.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-128 \" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-Configuration-Spring-Boot-Setting.jpg\" alt=\"\" width=\"647\" height=\"478\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-Configuration-Spring-Boot-Setting.jpg 824w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-Configuration-Spring-Boot-Setting-300x222.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-Configuration-Spring-Boot-Setting-768x568.jpg 768w\" sizes=\"auto, (max-width: 647px) 100vw, 647px\" \/><\/a><\/p>\n<p><strong>Step 13<\/strong>: Click on the Arguments tab. In the VM arguments field, enter the following:<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">-Dspring.profiles.active=students\r\n<\/pre>\n<p>This is for the students service. The window is shown below:<\/p>\n<figure id=\"attachment_129\" aria-describedby=\"caption-attachment-129\" style=\"width: 605px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-Configuration-VM-arguments.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-129 \" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-Configuration-VM-arguments.jpg\" alt=\"Run Configuration VM arguments\" width=\"605\" height=\"447\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-Configuration-VM-arguments.jpg 811w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-Configuration-VM-arguments-300x222.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-Configuration-VM-arguments-768x567.jpg 768w\" sizes=\"auto, (max-width: 605px) 100vw, 605px\" \/><\/a><figcaption id=\"caption-attachment-129\" class=\"wp-caption-text\">Run Configuration VM arguments<\/figcaption><\/figure>\n<p><strong>Step 14<\/strong>: Repeat steps 10 to 13 for the other 3 profiles.<\/p>\n<p>At this point, we have completed setting up the services. So we now need to test them<\/p>\n<p><strong>Step 15<\/strong>: Fire up the students service.Go to Eureka Server page. Then refresh it to make sure the services shows up under Applications.<\/p>\n<p><strong>Step 16<\/strong>: Check the port for the student service. Then visit http:\/\/localhost:&lt;port&gt;\/students.<\/p>\n<p>You will see that the welcome message is displayed.<\/p>\n<p><strong>Step 17<\/strong>: Repeat step 15 and 16 for the other three services.<\/p>\n<p>&nbsp;<\/p>\n<p>I&#8217;ll like to say thumbs up to you if you successfully come this far!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this step by step tutorial, I will teach you how to build microservices using Spring Profiles. Spring Profiles allow you to split a single &hellip; <\/p>\n","protected":false},"author":1,"featured_media":131,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[41,42,25,43,36],"class_list":["post-124","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-microservices","tag-eureka","tag-service-discovery","tag-spring-boot","tag-spring-framework","tag-spring-profiles"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/comments?post=124"}],"version-history":[{"count":2,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/124\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/124\/revisions\/133"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media\/131"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media?parent=124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/categories?post=124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/tags?post=124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}