Still crud? Come and learn architecture design --- dependency management under microservices (maven article)

14939609:

I. Introduction

In the first year when masks are released in 2023, most people want to start over, seize the good opportunity of gold, silver and gold, find a job they like and talents who meet the job requirements, and set off together. Our ideal situation is that the economy is already recovering, so the job market should also recover. However, the reality is cruel, and Haitou’s lack of response is still the norm. According to the statistics of the third-party Internet platform, the demand for enterprise employment increases by 100,000+ per month, and the Java talent gap is as high as 425,000, and it is growing at a rate of about 20% per year. However, the recruitment market is severely fragmented. HR complains that it is difficult to recruit people, and programmers complain that they cannot get offers. Why? Put aside the phenomenon to see the essence: it is nothing more than the dislocation of the needs of both parties.
In the initial stage of the development of the IT industry, junior Java programmers were sufficient to meet the needs of enterprises. However, since the development of the IT industry, the industry has become more and more refined, and the needs of enterprises have changed at any time. Programmers are required to "use it out of the box" and have more skills to solve complex problems. Ability.

The most intuitive manifestation is that the recruitment requirements of enterprises have once again increased, and the salary has continued to increase.

The following is the recruitment notice for Java programmers released by Byte in 2020 and 2022:
insert image description here
From the recruitment notice alone, it can be seen that if you want to find a job in 2022 with a salary and level similar to that in 2020, Java programmers need more abundant skills . (It is impossible to find a good job if you only know curd facial expression)
The Internet is not what it used to be, and the iteration is extremely fast. In the process, the company's business may undergo subversive adjustments, and the technology stack will change accordingly. This is just a company, the Internet industry is all-encompassing, and the overall product innovation and iteration speed are extremely fast. With the emergence of new industries, new technologies, and new products, corresponding business needs will continue to be derived.

The Internet industry " retreats if you don't advance, and retreats if you advance slowly" , and the same is true for programmers. For Internet companies whose employment requirements continue to increase, senior Java programmers are the most scarce in the current market. Only crud programmers are likely to be replaced by the current GPTs (chatGPT, GPT4.0). You can take a look at the python program I asked chatGPT to write for me before, I have to say it is too powerful. What's the use of chatGPT for a python beginner?
It's a little long before the shake facial expression, and then we start to get to the point. Maven must have been used by everyone, but generally when we are working on a project, the architect has set up the framework, and we go in to crud and add some business logic. If things go on like this, our architecture design ability will be useless. When we want to There is no competitiveness when changing jobs and raising salaries, so we need to learn some architecture design-related things after work, such as our most commonly used dependency tool maven, which is very simple to use. But it still needs some learning if you want to use it well. This article will start from the current popular microservice framework springcloud dependency management, and deeply analyze what the package management of some big factory microservices should be like.

Two, actual combat

The best way to learn a technology is to look at the official documents. For example, if we want to learn SpringCloud, we can look at the following resources:

In the overview of the official springcloud document, we can see how spring officials recommend us to use springcloud:
Figure 1:
insert image description here
As shown in the above figure, let us first introduce the pom that the springcloud package management depends on . This way of writing will not put the springcloud All package dependencies are imported into our project, which is equivalent to a declaration. When you need to use a certain dependency in your project, you only need to write <groupId>the and <artifactId>.
insert image description here
Some friends may be a little curious that we have not specified the version here, can maven successfully import it? facial expression, In fact, it can be introduced successfully, because we springcloud包管理依赖的pomhave <version> .

2.1 Create the version management of the unified dependency of the parent project

Here is an introduction to a project I wrote. This is a suggested mall project. The source code address: https://gitee.com/T-OPEN/skywalking-demo

  • The public package of the ratel-skywalking-common project is used to store common tool classes and entity classes that need to be passed between services.
  • ratel-skywalking-gateway Gateway.
  • ratel-skywalking-goods Goods services.
  • ratel-skywalking-order order service. The order service will call the product service to get the details of the products contained in an order.
  • ratel-skywalking-notice notification service.
    insert image description here

First of all, we need to create an empty parent project to manage the pom. Here, dependencyManagement is used to manage the dependency versions of the submodules in a unified way. The version number uses the acquisition method to facilitate later modification and maintenance:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.ratel.skywalking</groupId>
    <artifactId>ratel-skywalking-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>ratel-skywalking-parent</name>
    <description>Demo of Spring Cloud project for skywalking</description>

    <packaging>pom</packaging>

    <modules>
        <module>ratel-skywalking-common</module>
        <module>ratel-skywalking-gateway</module>
        <module>ratel-skywalking-goods</module>
        <module>ratel-skywalking-order</module>
        <module>ratel-skywalking-notice</module>
    </modules>

    <properties>
        <skywalking.demo.verion>0.0.1-SNAPSHOT</skywalking.demo.verion>
        <java.version>1.8</java.version>
        <spring.boot.version>2.3.12.RELEASE</spring.boot.version>
        <spring.cloud.version>Hoxton.SR12</spring.cloud.version>
        <spring.cloud.alibaba.version>2.2.8.RELEASE</spring.cloud.alibaba.version>
        <lombok.version>1.18.20</lombok.version>
        <hutool.version>5.8.11</hutool.version>
        <skywalking.version>8.9.0</skywalking.version>
        <knife4j.version>3.0.3</knife4j.version>
        <ratel-skywalking.verion>0.0.1-SNAPSHOT</ratel-skywalking.verion>
    </properties>

    <dependencyManagement>

        <dependencies>
            <!-- 统一依赖管理 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring.boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring.cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring.cloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <dependency>
                <groupId>org.apache.skywalking</groupId>
                <artifactId>apm-toolkit-logback-1.x</artifactId>
                <version>${skywalking.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.skywalking</groupId>
                <artifactId>apm-toolkit-trace</artifactId>
                <version>${skywalking.version}</version>
            </dependency>
            <dependency>
                <groupId>org.apache.skywalking</groupId>
                <artifactId>apm-toolkit-opentracing</artifactId>
                <version>${skywalking.version}</version>
            </dependency>

            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>${hutool.version}</version>
            </dependency>

            <dependency>
                <groupId>com.github.xiaoymin</groupId>
                <artifactId>knife4j-spring-boot-starter</artifactId>
                <version>${knife4j.version}</version>
            </dependency>

            <dependency>
                <groupId>com.ratel.skywalking</groupId>
                <artifactId>ratel-skywalking-common</artifactId>
                <version>${ratel-skywalking.verion}</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2.2 Create a common project for public use

ratel-skywalking-common It is a public class of the entire project. Its pom content is as follows. Here we prefer to introduce parent project dependencies to facilitate dependency management. We introduce them for the convenience of development lombok.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <!--引入父工程依赖 方便依赖管理-->
    <parent>
        <groupId>com.ratel.skywalking</groupId>
        <artifactId>ratel-skywalking-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>ratel-skywalking-common</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <!--lombok相关-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>none</mainClass>     <!-- 取消查找本项目下的Main方法:为了解决Unable to find main class的问题 -->
                    <classifier>execute</classifier>    <!-- 为了解决依赖模块找不到此模块中的类或属性 -->
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

You can notice that I did not specify it lombokwhen <version> , but looking at the dependency management, lombok has been successfully introduced, and the version number is 1.18.20, which is also the version of lombok we are declaring in the parent project pom.

insert image description here

Then create GoodsEntity in the project to facilitate the transfer of GoodsEntity when ratel-skywalking-order calls ratel-skywalking-goods, instead of defining it twice in two projects. In the future, you only need to introduce the ratel-skywalking-common project into the two projects.

insert image description here

2.3 Create a sub-project and introduce the dependencies of the parent project and the public project

Here we take creating ratel-skywalking-ordera project as an example, and the rest ratel-skywalking-gateway,ratel-skywalking-goods, ratel-skywalking-noticeare similar. pom content is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.ratel.skywalking</groupId>
        <artifactId>ratel-skywalking-parent</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>ratel-skywalking-order</artifactId>
    <name>ratel-skywalking-order</name>
    <description>ratel-skywalking-order</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

       <!--springboot相关-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--nacos相关-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

       <!--openfegin相关-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!--lombok相关-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--skywalking相关-->
        <dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>apm-toolkit-logback-1.x</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>apm-toolkit-trace</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.skywalking</groupId>
            <artifactId>apm-toolkit-opentracing</artifactId>
        </dependency>

        <!--hutool工具包-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
        </dependency>

        <!--knife4j接口文档-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
        </dependency>

       <!--本项目的公共实体类,公共utils等 -->
        <dependency>
            <groupId>com.ratel.skywalking</groupId>
            <artifactId>ratel-skywalking-common</artifactId>
        </dependency>

        <dependency>
            <groupId>com.ratel.skywalking</groupId>
            <artifactId>ratel-skywalking-goods-api</artifactId>
            <version>${skywalking.demo.verion}</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Here we prefer to introduce parent project dependencies ratel-skywalking-parentto facilitate dependency management, and then introduce public projects: ratel-skywalking-commonand some other dependencies. In order to facilitate the display of the effect, here we only need to deal with creating a new one in the same way ratel-skywalking-gateway,ratel-skywalking-goods,, and ratel-skywalking-noticewe can ignore it if it is not used in this demonstration. Friends who want to be lazy can also directly download the source code I mentioned above, and compile and start it directly.facial expression

2.4 Build the startup environment

Since our service is a microservice, we also need a nacos. For the specific environment construction, please refer to skywalking from entry to proficiency (1) - environment construction ,
startup script path: \nacos-server-2.0.1\nacos\bin\startup- standalone.cmd
We can use the stand-alone mode of nacos for local testing.
insert image description here
After startup, open the browser and visit http://localhost:8848/nacos/#/login to log in. Default account password: nacos / nacos
insert image description here

2.5 Start the program to start verification

We can start it first ratel-skywalking-gateway, and then start it ratel-skywalking-order. ratel-skywalking-goodsAfter the startup is complete,
we can see on nacos that our three services have all started successfully.
insert image description here
Next, we can access the interface: http://localhost:9010/ratel-order/order/getOrder for testing.

3. Summary

The above series of explanations are relatively basic dependency management methods under microservices. I hope you can gain something. If you have any questions, you can leave a comment and let me know. I will answer your questions in time.
insert image description here

Guess you like

Origin blog.csdn.net/weter_drop/article/details/130186558