CQRS With Axon Tutorial: Part 8 – Setup Spring Profiles

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).

We would cover the following

  1. Add the Profiles Annotations
  2. Modify the application.properties file
  3. Setup the Run Configuration
  4. Test the Application

 

1. Add the Profiles() Annotations

Follow the steps below

Step 1: Open the GiftCard class.

Step 2: Add the @Profile(“command”) just below the @Aggregate annotation

Step 3: Open the SummaryProjection class and add the @Profile(“query”) annotation as well

Step 4: Open the GiftCardGUI class and add the @Profile(“client”) annotation

 

2. Modify the application.properties File

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.

Next, we would tell spring to set the application name based on the active profile

Finally, we would tell spring to run the application as a servlet. To achieve all this, take the step below

Step 1: Copy and paste the code below into the application.properties file

spring.application.name=gc-${spring.profiles.active}
spring.main.web-application-type=servlet
server.port=0

 

3. Configure the Run/Debug Configuration

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

Step 1: Click on Run in the menu bar, and Select Run Configuration. This is shown below

The Run/Debug configuration window opens up as shown below:

 

Step 2: Click on the plus sign on the upper left side and choose Application. A configuration will be create as ‘unnamed’

Step 3: In the Name field, replace ‘unnamed’ with gcdemo – command

Step 4: In the VM Option field, enter -Dspring.profiles.active=command

Step 5: Repeat Steps 2 to 4 to create two more profiles for query and client

Step 6: For each of them. under the ‘Before launch, Activate tool window’, click on the plus(+) sign and select build

Step 7: Click on Apply and then click ok to exit  the wizard

At this point, you have completed setting up the profiles

Now if you click on the dropdown at the upper right corner, you will see the three configurations as shown below

 

4. Test the Application

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 – How to Start Axon Server

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.

Select the next profile and run. The do same for all

At this point, check the AxonServer dashboard. You’ll see that the three profiles all started and registered with AxonServer. This is shown below:

AxonDashboard with Running Profiles

AxonDashboard with Running Profiles

Finally, check the port where the client is running. You can get this from the console window. Look for message like:

Tomcat started on port(s): xxxxx (http) with context path ‘

Now visit http://localhost:xxxxx

You can now test the application  as before to make sure it works perfectly.

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.