The Lakeside Mutual project is a fictitious insurance company which serves as a sample application to demonstrate microservices and Domain-driven Design (DDD).
We use the project here to illustrate how the Context Mapper discovery library can be used to reverse engineer a Context Mapping DSL (CML) model from existing source code. The project's backend services are based on Spring Boot and it provides a Docker Compose configuration to start all microservices. Thus, our discovery strategies support to reverse engineer a Context Map for the Lakeside Mutual project.
The class src/main/java/org/contextmapper/lakesidemutual/example/LakesideMutualContextMapDiscoverer.java contains the code needed to discover the Context Map for the Lakeside Mutual project:
public class LakesideMutualContextMapDiscoverer {
public static void main(String[] args) throws IOException {
// configure the discoverer
ContextMapDiscoverer discoverer = new ContextMapDiscoverer()
.usingBoundedContextDiscoveryStrategies(
new SpringBootBoundedContextDiscoveryStrategy("com.lakesidemutual"))
.usingRelationshipDiscoveryStrategies(
new DockerComposeRelationshipDiscoveryStrategy(new File(System.getProperty("user.home") + "/source/LakesideMutual/")))
.usingBoundedContextNameMappingStrategies(
new SeparatorToCamelCaseBoundedContextNameMappingStrategy("-") {
@Override
public String mapBoundedContextName(String s) {
// remove the "Backend" part of the Docker service names to map correctly...
String name = super.mapBoundedContextName(s);
return name.endsWith("Backend") ? name.substring(0, name.length() - 7) : name;
}
});
// run the discovery process to get the Context Map
ContextMap contextmap = discoverer.discoverContextMap();
// serialize the Context Map to CML
new ContextMapSerializer().serializeContextMap(contextmap, new File("./src-gen/lakesidemutual.cml"));
}
}
Spring Boot Bounded Context Discovery Strategy:
The Spring Boot Context Map discovery strategy simply needs to know the package in which it shall search for the @SpringBootApplication
annotation. For this example, we scan com.lakesidemutual
.
Docker Compose Relationship Discovery Strategy: The Docker Compose strategy needs to know the root directory of the cloned Lakeside Mutual project. Within this directory it will search for docker-compose.yml files to analyze the relationships (dependencies).
Bounded Context Name Mapping Strategy: In our example illustrated above we use an adjusted SeparatorToCamelCaseBoundedContextNameMappingStrategy strategy, since the services in the docker-compose.yml files are named in the format customer-core while the Spring Boot discovery strategy will use the application class name in the format CustomerCore. The adjusted name mapping strategy illustrated above further removes the "Backend" strings at the end, since the Spring Boot discovery strategy will detect the Bounded Contexts without this ending.
Resulting Model:
The example code above finally stores the discovered Context Map under src-gen/lakesidemutual.cml.
-
Clone the Lakeside Mutual project.
-
Build all backend (Java) projects with the provided Maven build and publish them to your local Maven repository:
mvn clean install
Note: Spring Boot works with it's own special class loader and the JAR's produced by the given Maven Builds are not scannable for the reflections library (classes located within BOOT-INF). In order to build real JAR archives, you first have to disable the spring-boot-maven-plugin in all these backend projects (one solution is to simply comment the section out in the pom.xml files).
-
Clone this example project and import it within your favorite IDE (import Gradle project).
-
Adjust the source root path in the LakesideMutualContextMapDiscoverer class to the location where you cloned the Lakeside Mutual project (DockerComposeRelationshipDiscoveryStrategy).
-
Run the LakesideMutualContextMapDiscoverer main method to generate the Context Map.
Note: Of course there are other ways how to run the example main method. You just have to ensure that your classpath contains our discovery library (find Maven or Gradle include snippets here) and all Lakeside Mutual microservices you want to discover (for the Spring Boot discovery strategy). We added all the projects to the classpath with Gradle here (see build.gradle).