This is Part 2 of an ongoing series of blogs explaining Axon Framework. This blog is going to take you a step ahead and help you know more about the application directory structure to be followed. Also we’ll understand the spring boot integration for Axon. Additionally, this blog will have a quick Axon Server Setup Guide.
Prerequisites:
- Go through the Axon Part 1 – An Introduction to Axon Framework (Highly recommended)
- Micro-services
- Command Query Responsibility Segregation (CQRS)
- Commands, Events and Queries
- Domain Driven Design
- Event Sourcing
- SAGA Design Pattern
In Axon Part 1- An Introduction to Axon Framework, we already saw how to quickly set up an Axon application. So, in this blog we will go a little further and see the package/directory structure of Axon application, quick setup of Axon Server and integration of Axon with SpringBoot.
The structure of an Axon Application
First lets dive into all the dependencies needed in the pom.xml-
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-spring-boot-starter</artifactId>
<version>${axon.version}</version>
</dependency>
<dependency>
<groupId>org.axonframework</groupId>
<artifactId>axon-test</artifactId>
<version>${axon.version}</version>
<scope>test</scope>
</dependency>
Above are the main dependencies that are required with respect to Axon in the pom.xml file.
Directory Structure
For setting up an Axon application, a certain directory structure needs to be followed which is-
So as we are doing CQRS, we will segregate commands and create a slide so we’ll have a dedicated command package.
We’ll have a dedicated query package too.
Additionally, when we are doing CQRS, the easiest way to communicate with both components is through dedicated messages, commands, queries and events. As they are the sole things which go in and out of your application, there are coreapi of application. We’re also going to dedicate that in a different package because this is the package we want to share with everybody because it is the API.
Spring Boot Integration
Axon framework provides support for Spring as well. Spring can be configured programmatically. Hence doesn’t require spring on the class path. Spring annotations play a major part in easing out the spring configuration effort. Axon provides spring boot starters. Hence no hassle in the auto-configuration.
Auto-configuration
Axon’s Spring Boot auto-configuration is by far the coolest selection to get in progress configuring your Axon gears. By merely stating dependency to axon-spring-boot-starter
. Axon will automatically configure the infrastructure gears (command bus, event bus, query bus), as well as any module required to run and stock up aggregates and sagas.
axon-spring-boot-starter
trails universal Spring boot pact in arranging the starter. It depends on axon-spring-boot-autoconfigure
which holds concrete implementation of Axon auto-configuration. Once Axon Spring Boot application starts up, it looks for a file named spring.factories
in the classpath
. This file is located in the META-INF
directory of axon-spring-boot-autoconfigure
module.
Quick Axon Server Setup Guide
What is Axon Server?
Axon server is the product of AxonIQ. This comes in two categories, one is Axon Server Standard Edition and the other one is Axon Serve Enterprise Addition. The standard edition is available under AxonIQ open source license. Whereas, the enterprise version is the commercial edition with all the features and functionality.
Installation
There are two ways of installation:
- Install Locally
- Use docker to install
How to install locally?
Pre-requisite:
The machine must have java 8 or 11.
Steps to download Axon Server SE/EE
Step 1
Download Binaries for Axon Server available at link.
Step 2
Copy the below command to a directory
axonserver.jar/axonserver-cli.jar
Step 3
Run the Axon Server SE by going to the directory where the files have been extracted. Run the below command:
$ ./axonserver.jar
_ ____
/ \ __ _____ _ __ / ___| ___ _ ____ _____ _ __
/ _ \ \ \/ / _ \| '_ \\___ \ / _ \ '__\ \ / / _ \ '__|
/ ___ \ > < (_) | | | |___) | __/ | \ V / __/ |
/_/ \_\/_/\_\___/|_| |_|____/ \___|_| \_/ \___|_|
Standard Edition Powered by AxonIQ
NOTE: This will start Axon Server SE using the default ports - 8024 for HTTP / 8124 for gRPC.
Additionally, Axon server can be opened in development mode also, just use the below command:
axoniq.axonserver.devmode.enabled=true
How to run Axon Server as Docker Images?
Axon server provides us with a functionality to be used as a docker image and hence we don’t have to download it locally.
Steps to run Axon Server SE as Docker Image
So, Axon Server comes with a ready to use docker image. To run the Axon Server as a docker image, below command should be executed:
$ docker run -d --name -p 8024:8024 -p 8124:8124 axoniq/axonserver
Another way to run Axon Server is by Docker Compose
Below is the docker compose file for Axon Server SE:
version: '3.3'
services:
axonserver:
image: axoniq/axonserver
hostname: axonserver
volumes:
- axonserver-data:/data
- axonserver-events:/eventdata
- axonserver-config:/config:ro
ports:
- '8024:8024'
- '8124:8124'
- '8224:8224'
networks:
- axon-demo
volumes:
axonserver-data:
driver: local
driver_opts:
o: bind
type: none
device: ${PWD}/axonserverse/data
axonserver-events:
driver: local
driver_opts:
o: bind
type: none
device: ${PWD}/axonserverse/events
axonserver-config:
driver: local
driver_opts:
o: bind
type: none
device: ${PWD}/axonserverse/config
networks:
axon-demo:
After getting done with the Docker Compose file, just run the below command:
$ docker-compose up
Creating network "docker-compose_axon-demo" with the default driver
Creating volume "docker-compose_axonserver-data" with local driver
Creating volume "docker-compose_axonserver-events" with local driver
Creating volume "docker-compose_axonserver-config" with local driver
Creating docker-compose_axonserver_1 ... done
Attaching to docker-compose_axonserver_1
axonserver_1 | _ ____
axonserver_1 | / \ __ _____ _ __ / ___| ___ _ ____ _____ _ __
axonserver_1 | / _ \ \ \/ / _ \| '_ \\___ \ / _ \ '__\ \ / / _ \ '__|
axonserver_1 | / ___ \ > < (_) | | | |___) | __/ | \ V / __/ |
axonserver_1 | /_/ \_\/_/\_\___/|_| |_|____/ \___|_| \_/ \___|_|
axonserver_1 | Standard Edition Powered by AxonIQ
For deeper knowledge about Axon Framework, Structure and Integrations visit Axon Documentation Guide.
For more interesting tech blogs, visit knoldus blogs.