SpringCloud-微服务拆分之购物车微服务

一、拆分购物车微服务

step1). 创建模块.

在scmall下创建一个新的module,起名cart-service:

step2).添加依赖.

打开cart-service工程 \pom.xml 文件,添加依赖如下:

xml 复制代码
<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>
    
    <artifactId>cart-service</artifactId>
    <packaging>jar</packaging>

    <name>cart-service</name>
    <url>http://maven.apache.org</url>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

    <dependencies>
        <!--common-->
        <dependency>
            <groupId>com.heima</groupId>
            <artifactId>hm-common</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!--web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--数据库-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!--mybatis-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

    </dependencies>
    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

step3). 修改启动引导类,添加注解。

CartApplication.java 添加如下注解:

java 复制代码
@MapperScan("com.scmall.cart.mapper")

step4). 准备配置文件。

item-service 项目的的 resources目录下,准备application.yml、application-local.yml、application-dev.yml 文件:

其中 application.yml 文件如下:

yml 复制代码
server:
  port: 8082
spring:
  application:
    name: cart-service
  profiles:
    active: dev
  datasource:
    url: jdbc:mysql://${hm.db.host}:3306/hm-cart?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: ${hm.db.pw}
mybatis-plus:
  configuration:
    default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandler
  global-config:
    db-config:
      update-strategy: not_null
      id-type: auto
logging:
  level:
    com.hmall: debug
  pattern:
    dateformat: HH:mm:ss:SSS
  file:
    path: "logs/${spring.application.name}"
knife4j:
  enable: true
  openapi:
    title: 购物车接口文档
    description: "购物车接口文档"
    email: it@163.com
    concat: admin
    url: https://www.it.com
    version: v1.2.0
    group:
      default:
        group-name: default
        api-rule: package
        api-rule-resources:
          - com.scmall.cart.controller

step5). 代码迁移

项目 中与购物车有关的代码迁移到 cart-service 中。

  • 要获取登录用户信息。
  • 查询购物车时需要查询商品信息。

step6). 创建数据库表

在docker数据库中执行 SQL文件:

step7). 测试。

测试:

启动CartApplication,访问swagger文档:http://localhost:8082/doc.html

结果如下:

相关推荐
方便面不加香菜14 小时前
Docker Compose 文件 -- docker-compose.yml
spring cloud·docker·容器
威嵌神州1 天前
NXP P2020核心板技术解析:PowerPC架构与VxWorks存量项目迁移指南
嵌入式硬件·微服务·云原生·架构
真上帝的左手2 天前
31. 占位-14
分布式·spring cloud·架构·系统架构
captain3763 天前
SpringCloud(12)-Gateway
spring boot·spring cloud·gateway
行百里er4 天前
【Spring Insight 技术分享】如何让 AI 解释失败也不拖垮控制台
后端·spring cloud·开源
YOU OU5 天前
项目实战知识点—在线oj项目
微服务
行百里er5 天前
【Spring Insight 技术分享】如何给 AI 接口调用做滑动窗口限流
spring boot·后端·spring cloud
万里侯5 天前
日志聚类之后别把 noise 扔掉:降噪应该改变视图,不抹掉证据
人工智能·微服务·ai·容器·k8s
Flynn Liang6 天前
流量治理不是限流那么简单:I/O 密集型 Java 服务的 6 层防线
java·微服务·高并发·限流·熔断·系统稳定性·resilience4j
草莓熊Lotso6 天前
【C++ 基于微服务的即时通讯系统】ElasticSearch 用户数据管理实现
linux·数据库·c++·elasticsearch·微服务·架构