Easy to understand-the basic structure of the microservice SpringCloud Alibaba project

table of Contents

Understanding of microservice structure

Consumer: Service consumer

Provider: service provider

Build the project

1. Create a parent project

1.1. Import dependencies shared by subclasses (word classes do not need to lead to repeated dependencies)

1.2. Import SpringCloudAlibaba dependency (note the correspondence between versions)

2. Create an api sub-project (store entity classes common to Provider and Consumer)

2.1. Build the package structure

2.2. Package structure description

2.3. Define the classes in each package

2.4. Add api coordinates in the parent project pom file

3. Create Consumer Subproject

3.1. Import Discovery Discovery Service Dependency

3.1. Modify the configuration file

3.2. Create a package structure and write classes

4. Create a Provider subproject

4.1. Import database dependencies (mysql + mybatis)

4.2. Import nacos dependencies (used to register services)

4.3. Write configuration file (add database and nacos configuration)

4.4. Write mybatis configuration

4.5. Create a package structure and write classes

5. Registration Service

5.1. Start nacos

5.2. Start Provider

6. Service call

6.1. Start Consumer

6.2. Invoke the service


I recently learned about microservices, and then I looked at some microservice project structures, and have a general understanding of microservices.

Understanding of microservice structure

Consumer: Service consumer

1. Handling front-end requests
2. Call the service provided by the Provider according to the demand
3. Close interaction with the front end, no business operations involved

Provider: service provider

1. Controller layer: Provide a service call interface for Consumer
2. Service layer: contains all business logic and provides business operations to the Controller layer
3. Dao layer: Get data from the database and process it, and provide data to the Service layer ( data processing can only be done in the dao layer!!! Service layer only gets data )

Build the project

1. Create a parent project

1.1. Import dependencies shared by subclasses (word classes do not need to lead to repeated dependencies)

1.2. Import SpringCloudAlibaba dependency ( note the correspondence between versions )

<!--    导入Spring Cloud依赖    -->            <dependency>                <groupId>org.springframework.cloud</groupId>                <artifactId>spring-cloud-dependencies</artifactId>                <version>Hoxton.SR8</version>            </dependency>            <!--    导入Spring Cloud Alibaba依赖    -->            <dependency>                <groupId>com.alibaba.cloud</groupId>                <artifactId>spring-cloud-alibaba-dependencies</artifactId>                <version>2.2.1.RELEASE</version>            </dependency>

2. Create an api sub-project (store entity classes common to Provider and Consumer)

2.1. Build the package structure

2.2. Package structure description

entity: Persistence layer object, an object corresponds to a record in a table in the database; each attribute corresponds to the database table column
dto: Customize according to requirements, copy the required attributes in the entity class (not necessarily all attributes; there may be multiple dto classes corresponding to one entity class); for network transmission, serialization is required
vo: The object passed to the front end, which contains the dto object, return information, and status code; it is used for network transmission so it needs to be serialized

2.3. Define the classes in each package

The User class corresponds to the database table (I am a lazy dog ​​and do not want to build a new table, I use the previous one; please forgive me if the table structure is messy)

The UserInfDto class represents only basic user information (only some attributes are defined)

ResponseVo class

(①The set get method must be added, otherwise the front-end will not be able to parse ②The parameterless construction method must be added, otherwise the Consumer cannot be parsed );

I am a lazy dog! ! ! Too lazy to add the status code, use boolean to identify whether the processing is successful

2.4. Add api coordinates in the parent project pom file

3. Create Consumer Subproject

3.1. Import Discovery Discovery Service Dependency

<!--    nacos发现服务    -->        <dependency>            <groupId>com.alibaba.cloud</groupId>            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>            <version>2.2.5.RELEASE</version>        </dependency>

3.1. Modify the configuration file

Change to yml suffix is ​​not necessary, knowledge is clearer when adding configuration

3.2. Create a package structure and write classes

controller

configuration (configuration class)

RestTemplate 用于远程调用,用于Consumer调用Provider的服务
@LoadBalanced 该注解必须有,否则无法调用服务

4. Create a Provider subproject

4.1. Import database dependencies (mysql + mybatis)

4.2. Import nacos dependencies (used to register services)

<!--    nacos发现服务    -->        <dependency>            <groupId>com.alibaba.cloud</groupId>            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>            <version>2.2.1.RELEASE</version>        </dependency>

4.3. Write configuration file (add database and nacos configuration)

4.4. Write mybatis configuration

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>     <settings>        <!-- 全局映射器启用缓存 -->        <setting name="cacheEnabled" value="true"/>        <!-- 允许 JDBC 支持自动生成主键 -->        <setting name="useGeneratedKeys" value="true"/>        <!-- 配置默认的执行器 -->        <setting name="defaultExecutorType" value="REUSE"/>        <!-- 指定 MyBatis 所用日志的具体实现 -->        <setting name="logImpl" value="SLF4J"/>    </settings> </configuration>

4.5. Create a package structure and write classes

controller layer

service layer

dao layer

Added the mapper package to get data from the database
All kinds of full class names in the configuration file must be written correctly, otherwise you don’t know where the error is.
The repository performs data processing operations
Data processing can only be done here, absolutely not in the service layer

5. Registration Service

5.1. Start nacos

5.2. Start Provider

Note that there is no consumer at this time

6. Service call

6.1. Start Consumer

6.2. Invoke the service