{"id":107,"date":"2019-11-24T01:39:27","date_gmt":"2019-11-24T01:39:27","guid":{"rendered":"https:\/\/www.kindsonthegenius.com\/microservices\/?p=107"},"modified":"2019-11-24T01:40:49","modified_gmt":"2019-11-24T01:40:49","slug":"cqrs-with-axon-tutorial-part-8-setup-spring-profiles","status":"publish","type":"post","link":"https:\/\/www.kindsonthegenius.com\/microservices\/cqrs-with-axon-tutorial-part-8-setup-spring-profiles\/","title":{"rendered":"CQRS With Axon Tutorial: Part 8 &#8211; Setup Spring Profiles"},"content":{"rendered":"<p>In this part, we would split out application into three parts: command, query and client. So these would correspond to three microservices. To achieve this, we would have to setup spring profiles.(you can also do the same thing by separating into three different applications).<\/p>\n<p>We would cover the following<\/p>\n<ol>\n<li><a href=\"#t1\">Add the Profiles Annotations<\/a><\/li>\n<li><a href=\"#t2\">Modify the application.properties file<\/a><\/li>\n<li><a href=\"#t3\">Setup the Run Configuration<\/a><\/li>\n<li><a href=\"#t4\">Test the Application<\/a><\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t1\">1. Add the Profiles() Annotations<\/strong><\/h5>\n<p>Follow the steps below<\/p>\n<p><strong>Step 1<\/strong>: Open the GiftCard class.<\/p>\n<p><strong>Step 2<\/strong>: Add the @Profile(&#8220;command&#8221;) just below the @Aggregate annotation<\/p>\n<p><strong>Step 3<\/strong>: Open the SummaryProjection class and add the @Profile(&#8220;query&#8221;) annotation as well<\/p>\n<p><strong>Step 4<\/strong>: Open the GiftCardGUI class and add the @Profile(&#8220;client&#8221;) annotation<\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t2\">2. Modify the application.properties File<\/strong><\/h5>\n<p>One change we need to make is to specify the port each of the profile will be mapped to. At this point, we cannot allow spring to use the default port 8080. So we would tell spring to choose random ports when each profile runs.<\/p>\n<p>Next, we would tell spring to set the application name based on the active profile<\/p>\n<p>Finally, we would tell spring to run the application as a servlet. To achieve all this, take the step below<\/p>\n<p><strong>Step 1<\/strong>: Copy and paste the code below into the application.properties file<\/p>\n<pre style=\"margin: 0; line-height: 125%;\">spring.application.name=gc-${spring.profiles.active}\r\nspring.main.web-application-type=servlet\r\nserver.port=0\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t3\">3. Configure the Run\/Debug Configuration<\/strong><\/h5>\n<p>What we would do here is to set up the configuration for each of the profiles we created. Follow the steps below to achieve this<\/p>\n<p><strong>Step 1:<\/strong> Click on Run in the menu bar, and Select Run Configuration. This is shown below<br \/>\n<a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Select-Run-Configuration.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-109 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Select-Run-Configuration-283x300.jpg\" alt=\"\" width=\"283\" height=\"300\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Select-Run-Configuration-283x300.jpg 283w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Select-Run-Configuration.jpg 616w\" sizes=\"auto, (max-width: 283px) 100vw, 283px\" \/><\/a><\/p>\n<p>The Run\/Debug configuration window opens up as shown below:<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-111 size-large aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles1-1024x613.jpg\" alt=\"\" width=\"640\" height=\"383\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles1-1024x613.jpg 1024w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles1-300x180.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles1-768x460.jpg 768w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles1-240x145.jpg 240w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles1.jpg 1034w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Step 2<\/strong>: Click on the plus sign on the upper left side and choose Application. A configuration will be create as &#8216;unnamed&#8217;<\/p>\n<p><strong>Step 3:<\/strong> In the Name field, replace &#8216;unnamed&#8217; with gcdemo &#8211; command<\/p>\n<p><strong>Step 4<\/strong>: In the VM Option field, enter -Dspring.profiles.active=command<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-110 size-large aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles-1024x613.jpg\" alt=\"\" width=\"640\" height=\"383\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles-1024x613.jpg 1024w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles-300x180.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles-768x460.jpg 768w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles-240x145.jpg 240w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Run-and-Debug-Configuration-for-Profiles.jpg 1034w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><\/p>\n<p><strong>Step 5<\/strong>: Repeat Steps 2 to 4 to create two more profiles for query and client<\/p>\n<p><strong>Step 6<\/strong>: For each of them. under the &#8216;Before launch, Activate tool window&#8217;, click on the plus(+) sign and select build<\/p>\n<p><strong>Step 7<\/strong>: Click on Apply and then click ok to exit\u00a0 the wizard<\/p>\n<p>At this point, you have completed setting up the profiles<\/p>\n<p>Now if you click on the dropdown at the upper right corner, you will see the three configurations as shown below<\/p>\n<p><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Configurations.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-112 aligncenter\" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Configurations-300x281.jpg\" alt=\"\" width=\"219\" height=\"205\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Configurations-300x281.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/Configurations.jpg 359w\" sizes=\"auto, (max-width: 219px) 100vw, 219px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<h5><strong id=\"t4\">4. Test the Application<\/strong><\/h5>\n<p>Now you need to start Axon Server. I guess you already know how to do this from Step 1. Just in case, this is it &#8211; <a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/cqrs-with-axon-tutorial-part-1-introduction-and-setup-in-intellij\/#t5\">How to Start Axon Server<\/a><\/p>\n<p>To run each of the profile, select a configuration and click on the green run icon. Wait for few seconds and make sure that it runs successfully.<\/p>\n<p>Select the next profile and run. The do same for all<\/p>\n<p>At this point, check the AxonServer dashboard. You&#8217;ll see that the three profiles all started and registered with AxonServer. This is shown below:<\/p>\n<figure id=\"attachment_114\" aria-describedby=\"caption-attachment-114\" style=\"width: 640px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/AxonDashboard-with-Running-Profiles.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-114 size-large\" src=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/AxonDashboard-with-Running-Profiles-1024x775.jpg\" alt=\"AxonDashboard with Running Profiles\" width=\"640\" height=\"484\" srcset=\"https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/AxonDashboard-with-Running-Profiles-1024x775.jpg 1024w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/AxonDashboard-with-Running-Profiles-300x227.jpg 300w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/AxonDashboard-with-Running-Profiles-768x582.jpg 768w, https:\/\/www.kindsonthegenius.com\/microservices\/wp-content\/uploads\/sites\/18\/2019\/11\/AxonDashboard-with-Running-Profiles.jpg 1149w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/a><figcaption id=\"caption-attachment-114\" class=\"wp-caption-text\">AxonDashboard with Running Profiles<\/figcaption><\/figure>\n<p>Finally, check the port where the client is running. You can get this from the console window. Look for message like:<\/p>\n<p style=\"text-align: center;\"><em>Tomcat started on port(s): xxxxx (http) with context path &#8216;<\/em><\/p>\n<p>Now visit http:\/\/localhost:xxxxx<\/p>\n<p>You can now test the application\u00a0 as before to make sure it works perfectly.<\/p>\n<p>At this point, i would like to say, thumbs up to you for coming this far! Besides, it gets more interesting. In Part 9, we then see how containerize our app using docker, set up kubernetes cluster on Google Cloud and deploy our application to the cloud.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this part, we would split out application into three parts: command, query and client. So these would correspond to three microservices. To achieve this, &hellip; <\/p>\n","protected":false},"author":1,"featured_media":115,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,27,2],"tags":[37,39,6,40,8,38,22,36],"class_list":["post-107","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-axon","category-axonframework","category-microservices","tag-axonserver","tag-container","tag-cqrs","tag-docker-images","tag-event-sourcing","tag-google-cloud","tag-kubernetes","tag-spring-profiles"],"_links":{"self":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/107","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=107"}],"version-history":[{"count":5,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/107\/revisions"}],"predecessor-version":[{"id":118,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/posts\/107\/revisions\/118"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media\/115"}],"wp:attachment":[{"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/media?parent=107"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/categories?post=107"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kindsonthegenius.com\/microservices\/wp-json\/wp\/v2\/tags?post=107"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}