SpringCloud | 单体商城项目拆分(微服务)

2024-07-16 1778阅读

为什么要进行微服务拆分?

在平常的商城项目中,我们一般的项目结构模块都是将各种业务放在同一个项目文件夹,比如像:

SpringCloud | 单体商城项目拆分(微服务)

用户,购物车,商品,订单,支付等业务都是放在一起,这样很容易一个文件改动造成多个文件也要变动,而且在团队项目中也不容易维护,所以可以进行微服务拆分,来解决这个问题。

怎么拆分?

从拆分目标来说,要做到:

  • 高内聚:每个微服务的职责要尽量单一,包含的业务相互关联度高、完整度高。
  • 低耦合:每个微服务的功能要相对独立,尽量减少对其它微服务的依赖。

    从拆分方式来说,一般包含两种方式:

    • 纵向拆分:按照业务模块来拆分
    • 横向拆分:抽取公共服务,提高复用性

      对于hmall商城项目,它分为5大模块:

      1. 用户模块
      2. 商品模块
      3. 购物车模块
      4. 订单模块
      5. 支付模块

      我这里采用的是横向拆分,把它们公共的服务提取出来放在hm-api里面

      比如在购物车模块里面,它使用到了商品模块里面的服务,

      SpringCloud | 单体商城项目拆分(微服务)

      那么就可以把购物车模块里面用到的商品模块里面的服务抽取出来。

      实现微服务拆分

      前提:

      IDEA(2021以上版本),JDK11,VMware Workstation Pro,MobaXterm

      会使用docker,涉及到服务的远程调用(这里使用的是nacos注册中心)

      项目架构:

      SpringCloud | 单体商城项目拆分(微服务)

      hm-api:抽取出来的公共服务

      SpringCloud | 单体商城项目拆分(微服务)

      用户业务

      新建项目:

      SpringCloud | 单体商城项目拆分(微服务)

      SpringCloud | 单体商城项目拆分(微服务)

      从原本的单体商城项目中,把用户模块的内容复制过来,如图:

      SpringCloud | 单体商城项目拆分(微服务)

      这里还有很重要的是配置yaml文件

      application.yaml

      SpringCloud | 单体商城项目拆分(微服务)

      application-dev.yaml

      SpringCloud | 单体商城项目拆分(微服务)

      application-local.yaml

      SpringCloud | 单体商城项目拆分(微服务)

      在运行前,先配置一下UserApplication

      SpringCloud | 单体商城项目拆分(微服务)

      连接上虚拟机,开启MySQL和nacos

      SpringCloud | 单体商城项目拆分(微服务)

      SpringCloud | 单体商城项目拆分(微服务)

      一些命令:

      # 设置开机自启
      systemctl enable docker
      #查看
      docker ps
      #启动数据库
      docker start mysql
      #访问nacos
      docker log -f nacos

      运行成功:

      SpringCloud | 单体商城项目拆分(微服务)

      SpringCloud | 单体商城项目拆分(微服务)

      同理,剩下的4个业务也是这样拆分,其实公共服务就是把各个业务交织的部分,抽取出来,这样就只需要在hm-api里面去调用就可以,

      并且pom.xml里面要引入这个公共服务api

              
                  com.heima
                  hm-api
                  1.0.0
              

      hm-api

      项目结构:

      SpringCloud | 单体商城项目拆分(微服务)

      client:

      package com.hmall.api.client;
      import org.springframework.cloud.openfeign.FeignClient;
      import org.springframework.web.bind.annotation.DeleteMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import java.util.List;
      import java.util.Set;
      @FeignClient("cart-service")
      public interface CartClient {
          @DeleteMapping("/carts")
          void deleteCartItemByIds(@RequestParam("ids") Set ids);
      }
      
      package com.hmall.api.client;
      import org.springframework.cloud.openfeign.FeignClient;
      import org.springframework.web.bind.annotation.DeleteMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      import java.util.List;
      import java.util.Set;
      @FeignClient("cart-service")
      public interface CartClient {
          @DeleteMapping("/carts")
          void deleteCartItemByIds(@RequestParam("ids") Set ids);
      }
      
      package com.hmall.api.client;
      import io.swagger.annotations.ApiImplicitParam;
      import org.springframework.cloud.openfeign.FeignClient;
      import org.springframework.web.bind.annotation.PathVariable;
      import org.springframework.web.bind.annotation.PutMapping;
      @FeignClient("trade-service")
      public interface TradeClient {
          @ApiImplicitParam(name = "orderId", value = "订单id", paramType = "path")
          @PutMapping("/orders/{orderId}")
          void markOrderPaySuccess(@PathVariable("orderId") Long orderId);
      }
      
      package com.hmall.api.client;
      import org.springframework.cloud.openfeign.FeignClient;
      import org.springframework.web.bind.annotation.PutMapping;
      import org.springframework.web.bind.annotation.RequestParam;
      @FeignClient("user-service")
      public interface UserClient {
          @PutMapping("/users/money/deduct")
          public void deductMoney(@RequestParam("pw") String pw, @RequestParam("amount") Integer amount);
      }
      

      总结:

      微服务架构,首先是服务化,就是将单体架构中的功能模块从单体应用中拆分出来,独立部署为多个服务。同时要满足下面的一些特点:

      • 单一职责:一个微服务负责一部分业务功能,并且其核心数据不依赖于其它模块。

      • 团队自治:每个微服务都有自己独立的开发、测试、发布、运维人员,团队人员规模不超过10人

      • 服务自治:每个微服务都独立打包部署,访问自己独立的数据库。并且要做好服务隔离,避免对其它服务产生影响

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]