Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand the split of WEB Engineering Module

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to understand WEB project module split", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let Xiaobian take you to learn "how to understand the WEB project module split"!

WEB Engineering module split idea

First of all, let's look at some of the development modules currently used, usually dao, api, service, task, base, web five kinds, the meaning of the above five kinds of content is as follows:

dao: stores database entities and mapper related content.

api: Store open feign related interfaces and provide services for SpringCloud.

service: Stores the code for the business process in the current project.

task: store code related to schedule tasks.

base: Store request parameters, return parameters, enumerations and other JavaBean objects, without advanced functions.

Web: store controller, interceptor, filter equals web-related code.

The above module subcontracting is a common way, but the above subcontracting has caused many problems in the author's development process, such as the transformation of quickly finding an object, the cache processing of quickly finding an object, etc. A new subcontracting model is proposed for this purpose.

domain model

First of all, starting from HTTP request, there will be two contents for a request, request object and request response object. Generally speaking, these two objects are a relatively independent object without special methods (i.e., an ordinary JavaBean). For this piece of content, the author divides it into domain-model. Objects in cache can also be placed in domain-model, and they basically do not have additional processing methods. Domain models store various types of model objects that do not have special methods.

Contents that can be attributed to domain model modules are:

Request object: request params, request body

Request response object: response

cache objects

exception object

Continuing back to HTTP requests, developers usually validate request parameters when they enter a web application. For validation, there are some simple validations that can improve coding efficiency by providing annotations in hibernate-validator. There are various groups in the use of such annotations. As an example, the UserAppViewReq class, as a request parameter, becomes the following code after validation related annotations are used:

public class UserAppViewReq { @NotBlank( message = "appId cannot be empty", groups = {First.class, Second.class}) private String appId;}

In the above code, there are two groups, First and Second. For this group, there may be as many as four. At the same time, when flipping through the code, such as Controller, the specific code is as follows:

@PostMapping("/addUserAppView")public ResultVO addUserAppView(@RequestBody @Validated(First.class) UserAppViewReq req) { userService.addUserAppView(req); return ResultVO.success();}

In this code, you also need to record the group tag First to correspond to some validation in the entity object. At the same time, these validations can only support some simple validations. In case of logical validation, such as database uniqueness, this validator has no way to provide direct help to developers. Developers still need to write a separate validation method. Therefore, for parameter validation, a new module domain-validator is extracted, in which developers need to write the content of domain model for data validation. In this module it needs to have database operations or other third-party data operations (such as HTTP requests).

After verifying the relevant content, in the previous example, HTTP request parameters have entered the backend application, assuming that all data processing is completed. Now it is time to return the object. At this time, developers often need to do some data conversion to convert the database object into the request return object. For example: username password, both can only return the username, password generally does not return, usually this part of the operation is carried out in Service, the author thinks this part of the operation should be independent, it can extract a new module domain model conversion (domain-convert). In this module, I hope it is a separate project with only domain model transformation. I don't want to have some database or cache operations. I just hope that developers can still perform some database or cache query operations in this module. However, RedisTemplate and DataSource cannot be manipulated directly.

Summary of Domain Models

From the previous description, we can extract some maven projects as follows

domain: Storage domain model related engineering

domain-convert: Domain model conversion engineering, responsible for domain model conversion.

domain-model: domain model storage engineering, responsible for storing various domain models.

domain-validator: domain model validation project, responsible for validating domain models.

Note: Domain events are not defined in the current version of the domain model. Domain related events can be supplemented with SpringEvent-related content

Controller

The following will introduce the classification related to Controller. For a WEB project, there are usually interfaces exposed to the Internet and interfaces limited to internal local area network access. There may also be some common interfaces between the two. For this, the following three types of Controller can be summarized:

Public Controller

Internet Controller

local area network Controller

Public Controller is referenced by Internet Controller and local area network Controller. According to such a design idea, the following maven project can be made:

controller: Storage controller related engineering

controller-common: stores the common Controller interface.

controller-inner: Stores the Controller interface accessible within the local area network.

controller-public-network: Stores Controller interfaces accessible on the Internet.

Note: In controller engineering, there are only controller interface definitions and some basic processes in the control layer, which do not have business processing capabilities.

service-related layering

A developer needs to use at least objects, databases, and caches (some projects may not) in a normal business process. The following will be based on the three commonly used operating objects for the division of engineering modules. Object this content you can score it to the domain model, not too much discussion here, the following look at the database level content.

DAO

ORM framework is usually used for database interaction. This paper takes MyBatis framework as an example to describe how to divide DAO layer. In Mybatis development may introduce some plug-ins These plug-ins usually need to be configured through Java classes or configuration files, etc., according to which you can establish database configuration project (db-config), in addition to Mybatis there are two key elements One is the database entity object and the other is the mapper, both of which need to be split independently, database entity object can i establish database entity (db-entity) project, mapper can establish mapper project.

Next, separate splitting of database entities is described. In the process of adding, deleting and checking a single table object, the request parameter object may be directly converted into a database object. Therefore, in order to avoid the introduction of redundant Java classes, only db-entity can be introduced in domain-convert project to achieve the principle of minimum dependence. The same is true for caching, so the database entity is split into a separate project.

According to the previous design ideas, you can create the following maven project

database-object: Store DAO related projects.

db-config: stores database configuration related content, mainly MyBatis plug-in configuration.

db-entity: stores database entities.

Mapper: Stores the mapper interface and mapperXML files.

cache

Next, we will introduce how the cache should be grouped into projects. First of all, we need to know what the cached objects may be. The two mentioned above. The first is cached objects stored in domain-models, and the second is database entity objects (db-entity projects). These two points confirm that caching relies on domain-model and db-entity engineering. Dependency explanation completed The following look at the specific project classification, first of all for the use of cache is usually redis, for redis there will be some RedisTemplate related configuration, this part of the configuration will be split into a separate configuration project (cache-configuration), in addition to cache interface and cache interface implementation of two projects, the main purpose of splitting these two projects is to directly provide external cache calls, with dubbo framework for use.

According to the previous design ideas, you can create the following maven project

cache: Storage cache related engineering

cache-api: storage cache interaction interface

cache-api-impl: implementation class for storing cache interaction interfaces

cache-configuration: stores cache-related configuration classes

business processing capacity

Business processing capability is the core of the project and provides all the functions of the project. There are only two subprojects in this project, the first is api and the second is api implementation. The content written in api implementation project is the code that needs to be written in general development. In this module, various dependencies mentioned above are mainly introduced.

The first dependencies to be introduced are two database-level dependencies (db-entity and mapper) that can provide database interaction (add, delete and change), the next dependencies to be introduced are three dependencies in domain engineering (domain-convert, domain-model, domain-validator), followed by cache-related dependencies (not introduced if there is no cache layer), and the ones to be introduced are cache-api and cache-api-impl. Introducing these dependencies allows business-related development.

According to the above ideas, the business processing capacity can be divided into the following mavne projects

infrastructure: storage service processing capability related projects

infrastructure-api: stores business-related interface definitions

infrastructure-api-impl: stores business-related interface implementation classes

Finally, the business processing capability should be integrated with the three projects in the controller project. This part of integration can be integrated according to the project requirements.

Note: When splitting projects related to business processing capacity, no distinction is made according to network environment.

Custom configuration within project

During the project development process, it is inevitable to define some configurations within the project. This part of the configuration is not SpringBoot configuration or SpringCloud-related configuration, but independent configuration in the project, such as some configurations pushed by the Alliance. This part of the configuration needs to have a separate place to store, because the configuration class is usually a simple Java object in SpringBoot, so the configuration project will not have too many sub-projects, the current configuration project maven project definition is as follows:

project-configuration: stores custom configuration classes within the project

Note: This part of custom configuration is usually introduced into the module of business processing capability, i.e. infrastructure-impl project.

startup class

Next, we will introduce the related contents of startup. From the above, we can deduce the following three startup classes:

Boot classes for internal local area network use

Start-up classes for Internet use

Timed Task Startup Class

It is said that there are three startup classes, but they should be an independent project. For web projects, he does not have his own independent interceptors, filters, etc. For timed tasks, it will be customized according to the timed task framework selected by the project.

According to the above ideas, the following maven projects can be made

start: storage start-up project

inner-web-start: store startup classes and content relate to internal local area network usage.

public-network-web-start: stores startup classes and content related to Internet usage.

task-start: stores the timed task startup class and related content.

To illustrate some dependencies here, it is necessary to introduce the corresponding controller project in both web projects. In addition, it is necessary to introduce two additional configuration projects: database configuration project (db-config) and cache configuration project (cache-configuration). Similarly, the project module required for scheduled tasks can be introduced as needed.

There is another situation is the case of internal RPC calls, which are basically web applications that need to be called through the http protocol. Sometimes http is not the optimal solution and there are other RPC methods, such as dubbo. In the development process encountered by the author, sometimes the cache layer may be called by other projects. At this time, a dubbo-starter project will be established in the start project to put the required content into this project and then publish a dubbo project. The other party can directly call the interface through dubbo. Other business processing layers can also open interfaces in this way.

provide external services

The following describes the external service, in general, the project is in the form of WEB project operation, external service provision has two modes: the first is HTTP-CLIENT and the other is OpenFeign, in HTTP-CLIENT can be subdivided httpclient and okhttp. According to this idea, we can make the following maven project

Open-api: store projects that provide services to the outside world.

open-api-httlclient: Store external services implemented based on httpClient.

open-api-okhttp: Store external services implemented based on okHttp.

with-http-client: Store httpClient-related projects.

with-open-feign: Store external interface invocation services based on OpenFeign.

At this point, I believe that everyone has a deeper understanding of "how to understand WEB engineering module splitting", so it is advisable to actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report