拆分原則
什么時候拆分
- 大多數小型項目: 一般是先采用單體架構,隨著用戶規模擴大、業務復雜后再逐漸拆分為微服務架構(前易后難)。
- 確定的大型項目: 資金充足,目標明確,可以直接選擇微服務架構,避免后續拆分的麻煩(前難后易)。
怎么拆分
拆分目標:
- 高內聚:每個微服務的職責要盡量單一,包含的業務相互關聯度高、完整度高。
- 低耦合:每個微服務的功能要相對獨立,盡量減少對其它微服務的依賴,或者依賴接口的穩定性要強。
拆分方式:
- 縱向拆分:按照業務模塊來拆分。
- 橫向拆分:抽取公共服務,提高復用性。
當然,由于黑馬商城并不是一個完整的項目,其中的短信發送、風控管理并沒有實現,這里就不再考慮了。而其它的業務按照縱向拆分,可以分為以下幾個微服務:
- 用戶服務
- 商品服務
- 訂單服務
- 購物車服務
- 支付服務
拆分服務
工程結構有兩種:
- 獨立Project
- Maven聚合(使用最多)
拆分操作
新建一個module,命名為item-service
- 由于我已經新建過了這個module,所以會顯示存在,這是正常的。
- 將hm-service中的controller domain mapper service還有resource中的配置文件拷貝到item-service中,注意只需要拷貝跟item-service中的文件就行,如果關聯其他文件,那就一并拷貝下來。注意:如果import包爆紅的話,那可能是正常的。只需要把包刪了,重新導入一下就行。
application.yaml:
server:port: 8081
spring:application:name: item-serviceprofiles:active: devdatasource:url: jdbc:mysql://${hm.db.host}:3306/hm-item?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghaidriver-class-name: com.mysql.cj.jdbc.Driverusername: rootpassword: ${hm.db.pw}
mybatis-plus:configuration:default-enum-type-handler: com.baomidou.mybatisplus.core.handlers.MybatisEnumTypeHandlerglobal-config:db-config:update-strategy: not_nullid-type: auto
logging:level:com.hmall: debugpattern:dateformat: HH:mm:ss:SSSfile:path: "logs/${spring.application.name}"
knife4j:enable: trueopenapi:title: 黑馬商城接口文檔description: "黑馬商城服務接口文檔"email: zhanghuyi@itcast.cnconcat: 虎哥url: https://www.itcast.cnversion: v1.0.0group:default:group-name: defaultapi-rule: packageapi-rule-resources:- com.hmall.item.controller
hm:jwt:location: classpath:hmall.jksalias: hmallpassword: hmall123tokenTTL: 30mauth:excludePaths:- /search/**- /users/login- /items/**- /hi
# keytool -genkeypair -alias hmall -keyalg RSA -keypass hmall123 -keystore hmall.jks -storepass hmall123
application-dev.yaml
hm:db:host: mysqlpw: 123
application-local.yaml
hm:db:host: 10.105.2.124 # 修改為你自己的虛擬機IP地址pw: 123 # 修改為docker中的MySQL密碼
- 操作虛擬機中的數據庫
參考這兩篇博客:
如何在docker中的mysql容器內執行命令與執行SQL文件
先把要執行的sql文件導入/root根目錄下,然后進行以下操作:
將idea與虛擬機docker中的mysql數據庫連接
我的密碼是123(mark一下,我怕我忘了) - 測試一下