Modular development overview

Updated at:

The main goal of SOFABoot modular development is how to isolate the context of each module and communicate with each other. The main implementation principles are as follows:

  • Each SOFABoot module contains a separate context. Modules and modules cannot interact directly through beans, which is a manifestation of modular isolation.

  • A root context is generated from the startup class as the parent context for each module.

  • Each module discovers the root context through the starter, ensuring that each module can discover the bean in the root context and realize the communication of each module.

The overall design of SOFABoot modular development is as follows: 模块化开发

Modular development is further explained below.

Module structure and function

Module structure

A SOFABoot project can contain multiple modules, each of which contains an independent context. The SOFABoot module consists of the following two parts:

  • A common JAR package. Mainly include:

    • Java code

    • Spring configuration file

    • SOFABoot submodule identification

  • Configuration specific to SOFABoot. Mainly include:

    • properties configuration file

    • Spring configuration file

For more information about how to configure SOFABoot modules, see Module configuration files.

Modular features

SOFABoot modular development is modularization based on Spring context isolation. The modular mode with SOFABoot modules as units provides developers with the following features:

  • run time the application, the Spring context of each SOFABoot module is isolated from each other, and the beans defined between modules do not affect each other.

  • Each SOFABoot module is fully functional and self-contained. You can easily migrate and reuse modules among different SOFABoot applications. You only need to copy all SOFABoot modules to the target application and adjust Maven dependencies to run them.

Modular development

Background overview

There are two types of SOFABoot projects that can be created according to the Create a project documentation on the official website:

  • SOFABoot Web Project

  • SOFABoot Core project

The startup class is typically placed in a deployable module. Deployable modules are modules that are packaged using spring-boot-maven-plugin. The deployable modules and corresponding startup classes of the preceding two project types are:

  • SOFABoot Web Project's Web Module: SOFABootWebApplication

  • Service module of SOFABoot Core project: SOFABootApplication

Enable modularization

To enable SOFABoot modularization, you must add isle-alipay-sofa-boot-starter dependencies to the pom.xml file of the deployable module. You do not need to specify the dependency version number. Example:

<dependency>
     <groupId>com.alipay.sofa</groupId>
     <artifactId>isle-sofa-boot-starter</artifactId>
</dependency>

Modular implementation

The main contexts involved in modular implementation include:

  • Root ApplicationContext: This article is referred to as rt_appc, which refers to the Spring context started by the running SpringApplication.run when the startup class of the project is started.

  • applicationContext: This article is referred to as appc, which refers to a Spring context that each module has.

In the SOFABoot project, the main implementation principles are described as follows:

  • The SOFABoot project is started and a context rt_appc is generated.

  • Each module in the project also has a context appc, which is independent and isolated from each other.

  • rt_appc is the parent of appc in each module.

  • After modularization is enabled, the following functions can be implemented:

    • After the rt_appc refresh of the startup class is completed, the isle-alipay-sofa-boot-starter will be responsible for finding each module in the project and then starting the independent appc in each module.

    • You can ensure that the bean created in rt_appc can be found in the appc of each module.

    • When an application adds Starter, not only can rt_appc use the beans added in Starter, but the appc of each module can also use these beans.

Inter-module communication

After context isolation, beans between modules cannot be directly injected. Modules need to communicate with each other through the SOFA service. Currently, SOFABoot provides the following two forms of service publishing and reference to implement calls between different levels of modules:

  • Publish and reference JVM services: This topic describes how to publish and reference JVM services in a SOFABoot project. For more information, see Publish and reference JVM services.

  • Publish and reference RPC services: Publish a SOFARPC service to resolve remote calls between multiple SOFABoot projects.

Module parallelization startup

Each SOFABoot module is an independent Spring context, and multiple SOFABoot modules support parallel startup. Compared with the single Spring context mode of Spring Boot, the module parallel startup can speed up the startup of the application.