首页 > 其他分享 >Spring Cloud微服务项目公共类抽取

Spring Cloud微服务项目公共类抽取

时间:2024-07-26 12:00:44浏览次数:12  
标签:spring Spring 服务项目 common 模块 公共 Cloud

        在微服务架构中,代码的重用性和维护性是非常重要的。Spring Cloud 提供了丰富的工具和框架来帮助我们构建和管理微服务应用,但随着项目规模的扩大,我们会遇到大量的重复代码和相似的逻辑。在这种情况下,抽取公共类成为提高代码质量和开发效率的关键手段。本文将探讨如何在 Spring Cloud 微服务项目中有效地抽取公共类,并给出一些最佳实践。

前置条件

        在进行Spring Cloud微服务项目公共类抽取策略之前,博主希望你已经创建了一个微服务项目:Spring Cloud微服务项目搭建

1.创建公共模块

        在项目根路径下 ==> 右键 ==> 新建 ==> 模块:

        我们先创建一个Spring Boot 项目: 

因为这里的 common 只做依赖管理作用,所以并不需要Spring-Boot-Web 等依赖

         然后将除 pom.xml 外的文件删除,并在common模块下新建一个Maven空项目:

         新建完 Maven 模块后,common 部分的 pom.xml 文件看起来像这样:

<!-- 修改 pom.xml 文件 -->

<?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>3.3.3-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>common</groupId>
    <artifactId>common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>common</name>
    <description>common</description>

    <modules>
        <module>common_utils</module>
    </modules>

    <dependencies>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <name>Spring Snapshots</name>
            <url>https://repo.spring.io/snapshot</url>
            <releases>
                <enabled>false</enabled>
            </releases>
        </pluginRepository>
    </pluginRepositories>

</project>

        一般来说,微服务项公共模块以jar包的形式下载到本地Maven仓库,然后在其他子模块以依赖的形式引用,所以只需要有公共代码就可以了

        我们删除多余的启动类,然后创建新目录: 

2.创建公共类

        这里博主将响应体封装在公共模块 

数据响应体封装请参考:Spring Cloud微服务统一封装数据响应体

3.在子模块中引用

        在其他子模块引用公共模块时,一定得先 install 一下,否则本地Maven仓库中不会存在这个 jar

        这里博主在 user 模块进行引入:

<!-- user 模块的 pom.xml 文件 -->

<!-- 在 dependencies 节点中添加 common 依赖 -->
<dependency>
     <groupId>common</groupId>
     <artifactId>common_utils</artifactId>
     <version>0.0.1-SNAPSHOT</version>
</dependency>

        然后在控制器中直接编写测试代码: 

        到这一步,我们就算已经完成了Spring Cloud 微服务项目中,公共模类的抽取啦!!! 

标签:spring,Spring,服务项目,common,模块,公共,Cloud
From: https://blog.csdn.net/qq_56046190/article/details/140710052

相关文章