日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Cloud【Finchley】实战-04将订单微服务与商品微服务分别拆分为多模块

發布時間:2025/3/21 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Cloud【Finchley】实战-04将订单微服务与商品微服务分别拆分为多模块 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • Spring Cloud【Finchley】專欄
  • 概述
  • Product微服務功能分析及多模塊拆分
    • 拆分原則
    • Step1. 調整主(父)工程的工程類型 packaging為pom
    • Step2. 新建子模塊
    • 父pom
    • product common
    • product client
    • product server
  • Order微服務的改造
  • 測試
  • 代碼

Spring Cloud【Finchley】專欄

如果還沒有系統的學過Spring Cloud ,先到我的專欄去逛逛吧

Spring Cloud 【Finchley】手札


概述

上篇博文 Spring Cloud實戰-03訂單微服務與商品微服務之間的調用 我們雖然打通了訂單流程,但是還是有些問題需要去修改和完善下

  • 問題一:將數據表映射的實體類暴露出去了,建議最好再封裝一層


  • 問題二:同一個對象在不同微服務之間重復定義

我們在Order微服中使用Feign調用Product微服務提供的一些接口時 ,原本屬于product微服務的Product和CartDTO類為了不報錯卻不得不在Order為服務中重復定義,維護較為麻煩。 原則是:這些類該屬于哪個模塊就在哪個模塊定義。


  • 問題三: 將對方的服務定義到自己的服務中

order和product 可能是兩個小組來開發,而我們要在order微服中將product微服務的請求的url寫到自己的為服務中,將對方的服務定義到自己的服務中是不是不太合理? 應該自己管理自己的對外接口,暴露出去供外部調用者使用。

那如何解決上述問題呢? 答案就是利用maven的多模塊技術,將服務拆分為多個子模塊。

如何拆分SpringBoot項目,阿里小馬哥有個視頻講的挺好,移步:https://www.imooc.com/video/16354

或者參考我以前的博文 : Maven父子工程的搭建


Product微服務功能分析及多模塊拆分

拆分原則

我們將Product劃分為3個子模塊

  • product Server : 所有業務邏輯
  • product Client : 對外暴露的接口(目前有查詢商品和扣減庫存)
  • product Common : 公用的對象

三者的依賴關系,
product Server 依賴 product Common
product Client 依賴 product Common

那動手拆分起來吧


Step1. 調整主(父)工程的工程類型 packaging為pom

將packaging 由 jar 改為 pom

<packaging>jar</packaging>

調整為:

<packaging>pom</packaging>


Step2. 新建子模塊

選中artisan-product工程 右鍵 New — Module ,

一定要選Maven

啥都不用勾選,默認下一步

Next

確認下,默認即可,點擊 Finish結束 。


回頭查看父工程的pom.xml 已經新增了 modules節點如下信息

再看下新建的子模塊 product-server

未完待續…

2019-03-31續
經過了一周喪心病狂的加班,周末終于有時間了,那我們繼續吧

先建好3個子Module(方式同上),然后再將代碼按照模塊的劃分分別整合到各個子模塊中。

模塊已經建好,根據確定好的拆分原則那將代碼按照模塊的劃分分別整合到各個子模塊中吧。

幾個注意事項:

  • 凡是版本的定義,都建議放在最外層的dependencyManagement中,方便做到統一管理
  • 依賴的模塊,需要先mvn install ,其他模塊才可以正確的引用

  • 父pom

    <?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><groupId>com.artisan</groupId><artifactId>artisan-product</artifactId><version>0.0.1-SNAPSHOT</version><modules><module>product-server</module><module>product-client</module><module>product-common</module></modules><packaging>pom</packaging><name>artisan-product</name><description>Product</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><java.version>1.8</java.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><productcommon.version>0.0.1-SNAPSHOT</productcommon.version><springcloud.version>Finchley.RELEASE</springcloud.version></properties><!-- 凡是版本的定義,都建議放在最外層的dependencyManagement中,方便做到統一管理 --><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${springcloud.version}</version><type>pom</type><scope>import</scope></dependency><!-- product-server和product-client都需要依賴該模塊,在這里定義版本之后,在server和client模塊引用的話就無需再加上version了,方便統一管理 --><dependency><groupId>com.artisan</groupId><artifactId>product-common</artifactId><version>${productcommon.version}</version></dependency></dependencies></dependencyManagement></project>

    product common

    主要是解決問題一將數據表映射的實體類暴露出去了,建議最好再封裝一層

    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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>artisan-product</artifactId><groupId>com.artisan</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>product-common</artifactId><dependencies><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies></project>

    product client

    主要是解決問題二同一個對象在不同微服務之間重復定義以及問題三自己的服務寫到自己的工程中。

    那么我們就將屬于product微服務的ProductClient定義在該模塊中

    product client 依賴 product common

    <?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"><parent><artifactId>artisan-product</artifactId><groupId>com.artisan</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>product-client</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>com.artisan</groupId><artifactId>product-common</artifactId></dependency></dependencies></project> package com.artisan.product.client;import com.artisan.product.common.DecreaseStockInput; import com.artisan.product.common.ProductOutput; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.PostMapping;import java.util.List;@FeignClient(name="ARTISAN-PRODUCT") public interface ProductClient {@PostMapping("/product/productListForOrder")List<ProductOutput> getProductForOrder(List<String> productIdList);/*** 這里我們就不用CartDTO了,因為它屬于Order工程,我們這里自己在ProductCommon中自己維護一個DTO類* DecreaseStockInput* @param decreaseStockInputList*/@PostMapping("/product/decreseProduct")void decreseProduct(List<DecreaseStockInput> decreaseStockInputList); }

    product server

    product server也依賴 product common

    <?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"><parent><artifactId>artisan-product</artifactId><groupId>com.artisan</groupId><version>0.0.1-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>product-server</artifactId><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><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><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><optional>true</optional></dependency><dependency><groupId>com.artisan</groupId><artifactId>product-common</artifactId></dependency></dependencies><!--打包 放在product-server中--><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build> </project>

    ProductController

    ProductService 接口

    然后刪掉些之前的引用即可。


    Order微服務的改造

    同上,就不多贅述了,詳見Github,

    目前order-client 和 order-common沒有啥需求,先寫個空的吧,后續根據需求逐步完善,

    其中order-server模塊需要說明的是:

    因為要調用商品微服務來查詢商品和扣減庫存,所以要依賴product-client包

    <dependency><groupId>com.artisan</groupId><artifactId>product-client</artifactId></dependency>

    同時為了讓order server微服務啟動時實例化Feign接口,需要配置掃描基包 (因為他們不在一個工程的同一級或者子孫級目錄中)

    @EnableFeignClients(basePackages="com.artisan.product.client")


    測試

    啟動注冊中心,通過product-server和order-server中的main函數啟動倆微服務,

    下單

    select * from artisan_order a where a.order_id = '1554135137371873119'; select * from order_detail a where a.order_id = '1554135137371873119';


    代碼

    Github
    https://github.com/yangshangwei/springcloud-o2o/tree/master/artisan-product
    https://github.com/yangshangwei/springcloud-o2o/tree/master/artisan_order

    總結

    以上是生活随笔為你收集整理的Spring Cloud【Finchley】实战-04将订单微服务与商品微服务分别拆分为多模块的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。