從零搭建微服務項目Pro(第0章——微服務項目腳手架搭建)

前言:

? ? ? ? 在本專欄Base第0章曾介紹一種入門級的微服務項目搭建,盡管后續基于此框架上實現了Nacos、Eureka服務注冊發現、配置管理、Feign調用、網關模塊、OSS文件存儲、JSR參數校驗、LogBack日志配置,鑒權模塊、定時任務模塊等,但由于之前的博客更側重于功能的實現而非整體架構的和諧統一,隨著模塊和功能的增多,原先框架設計層面上的不足也逐漸暴露出來,代碼層面比如實體類存放位置、Exception處理方式難以統一,各層級耦合度高等,依賴方面如循環依賴、依賴沖突等問題也時有發生。因此,筆者通過參考實習公司代碼,使用企業級的腳手架搭建方式,最終重構了原有項目,并實現了絕大部分功能。本章將結合代碼以及一些個人見解具體介紹,并會介紹一些微服務相關知識幫助快速上手。

? ? ? ? 最終實現完整項目代碼鏈接如下,后續會逐步迭代并最終完成完整的后端代碼,(可能還會包括Vue網頁端和OpenHarmony手機端的前端代碼),以及詳盡的配置文檔方便快速入手以及設計文檔方便快速了解業務,歡迎Star和Follow。

wlf728050719/BitGoPlushttps://github.com/wlf728050719/BitGoPlus如果沒有接觸過微服務項目或者對上述功能模塊感興趣的小伙伴可收藏下面專欄目錄鏈接,專欄主要內容為筆者從0接觸微服務的全部學習心得和記錄。以及建議按時間順序看循序漸進。

從零搭建微服務項目(全)-CSDN博客https://blog.csdn.net/wlf2030/article/details/145799620?spm=1001.2014.3001.5501


一、前置知識:

為降低本章門檻還是做一些前置知識講解。如果有一定微服務項目開發基礎這部分就可以直接跳過。以及筆者有誤的地方也歡迎評論區指出。

1.前端、后端、數據庫

前端(客戶端):B/S架構下前端主要指網頁,C/S架構下主要指手機或電腦上的應用程序,當然也有微信小程序這種混合架構下的前端,前端主要用于和用戶交互,并將用戶指定通過http請求發送給后端。并展示后端返回數據

后端(服務器):接收前端請求,處理業務邏輯(如驗證、計算),并與數據庫交互,并將數據作為響應返回前端。

數據庫:結構化存儲數據(如用戶信息、訂單記錄)。

這里拿BS架構舉例,當搜索一次4399后按f12抓包能夠看見其發起了一次http請求,請求地址是www.baidu.com以及將輸入的值4399和編碼格式utf8以及上次搜索的值43999一并發送(請求)給了對應域名的服務器,并成功獲取了其返回的數據xml形式數據(響應)。

(注意不是有服務器就是后端,筆者大二學數據庫時曾以為安裝了數據庫的云服務器就是后端,做的課設實際前端既負責處理遠程服務器傳來的數據又展示界面,當時信誓旦旦的和老師說是CS架構項目,現在看來可能叫CD結構更合適)

2.后端分層架構

(學習過Servlet開發或者Spring MVC/Spring Boot的小伙伴應該對這張圖不會陌生。個人所有編程語言最喜歡Java很大程度上在于它把分層解耦的思想發揮到了極致。)

后端按照各自負責功能劃分為五層,可以選擇將Manager層合并到Service層,筆者更傾向五層架構。圖中默認上層依賴于下層,箭頭關系表示可直接依賴,如:開放接口層可以依賴于 Web 層,也可以直接依賴于 Service 層,依此類推:

開放接口層:可直接封裝 Service 方法暴露成 RPC 接口;通過 Web 封裝成 http 接口;網關控制層等。
終端顯示層:各個端的模板渲染并執行顯示的層。當前主要是 velocity 渲染,JS 渲染,JSP 渲染,移動端展示等。
Web 層:主要是對訪問控制進行轉發,各類基本參數校驗,或者不復用的業務簡單處理等。
Service 層:相對具體的業務邏輯服務層。
Manager 層:通用業務處理層,它有如下特征:
(1)對第三方平臺封裝的層,預處理返回結果及轉化異常信息。
(2)對 Service 層通用能力的下沉,如緩存方案、中間件通用處理。
(3)與 DAO 層交互,對多個 DAO 的組合復用。
DAO 層:數據訪問層,與底層 MySQL、Oracle、Hbase、OB 等進行數據交互。
外部接口或第三方平臺:包括其它部門 RPC 開放接口,基礎平臺,其它公司的 HTTP 接口。
?
拿項目代碼舉例:
PermissionMapper屬于DAO層,沒接觸過mybatis的小伙伴也沒關系,只需要知道這個類的這個方法是執行下面的sql并返回拿到的數據即可。

UserManagerImpl屬于Manager層,其作用在于對插入數據庫的主鍵進行處理,而非使用數據庫生成主鍵。(個人傾向五層架構的原因也在于如果使用了mybatis plus,在遇到需要對與數據庫直接交互數據特殊處理時,放dto中需要手寫本來已經實現的crud操作費時費力,放service中與其他業務函數相比比如register、login忽然出現一個insert,update顯得不倫不類)

UserServiceImpl屬于Service層,能夠看到其一個業務函數中使用了多個Manager層數據對象,實現了功能的復用。

UserController屬于Web層,其將service層提供的功能封裝為一個個接口,供前端訪問。比如登錄功能,前端只需要發送https://ip地址:port號/user/register并附帶上注冊所需要的數據即可在數據庫中新增一個用戶。

終端顯示層負責將后端返回的數據(如JSON、XML)轉換為用戶可理解的界面(如表格、圖表、文字),其實理解為前端也行,但一般都采用前后端分離的方式,所以一般不會直接在后端返回一個html頁面。

3.微服務架構

服務調用:

微服務架構是一種?將單體應用拆分為多個小型、獨立服務?的架構風格,每個服務負責不同的業務功能,比如A服務負責用戶,B服務負責訂單,各個服務內部可以相互調用,比如訂單表中只有用戶id,但訂單服務需要返回用戶完整信息,此時B服務就需要遠程調用(RPC)A服務,傳給它用戶id,并拿取其返回信息拼裝返回給前端。

服務調用可參考下面鏈接:從零搭建微服務項目Base(第1章——微服務模塊間調用接口)_搭建一個微服務項目-CSDN博客

從零搭建微服務項目Base(第5章——SpringBoot項目LogBack日志配置+Feign使用)_springboot feign 日志-CSDN博客

從零搭建微服務項目Base(第6章——Feign性能優化以及模塊抽取)-CSDN博客

服務注冊、網關路由:

當服務從單體拆分成多個后,前端人員編寫接口時需要把哪個服務對應哪個ip端口寫入程序中,然后每次發請求均需要查詢對應接口,這顯然不切實際,一旦接口增加,豈不是用戶還得升級以下前端配置,注冊中心加網關即可很好解決這個問題,每個服務啟動時,將自己的服務名和ip端口注冊到注冊中心,前端只需要記住網關服務的地址即可,所有請求都發向網關,網關中配置哪些請求路徑匹配哪些服務,并從注冊中心拿到這些服務的地址,網關進行轉發即可。同時注冊中心會告訴網關哪些服務實例掛了,或者哪些服務現在很忙,通過負載均衡決定到底路由到哪個服務。

服務注冊可參考下面鏈接:

從零搭建微服務項目Base(第2章——Eureka服務注冊和發現)_微服務開發實例 服務發現與注冊-CSDN博客

從零搭建微服務項目Base(第3章——Nacos服務注冊和發現)_nacos服務從零搭建-CSDN博客

網關路由可參考下面鏈接:

從零搭建微服務項目Base(第7章——微服務網關模塊基礎實現)-CSDN博客

鑒權:

但此時又出現一個問題,要是我知道你某個服務真實地址,我直接訪問它不過你的網關,我直接訪問敏感接口猛猛爬用戶數據該怎么辦,此時就需要為每個服務引入鑒權,你繞過我網關沒關系,但你只要訪問我的敏感接口就必須提供身份證明(jwt生成的token),同時為了讓其他服務能夠正常相互調用也需要進一步處理,這里不再細講。

鑒權可參考下面鏈接:

從零搭建微服務項目Pro(第6-1章——Spring Security+JWT實現用戶鑒權訪問與token刷新)_微服務springboot+security+jwt實現刷新token-CSDN博客

配置管理:

既然服務名和ip對應關系已經存放在注冊中心,不如多存點,將各服務配置也同樣存儲,這樣就不用將配置也打包,否則一旦配置發生變化豈不是還得重新打包上線。因此nacos,eureka等(注冊中心)順便也做了配置管理。

配置管理可參考下面鏈接:

從零搭建微服務項目Base(第4章——Nacos環境隔離和配置拉取)_微服務項目搭建 nacos-CSDN博客

分庫分表:

既然服務都拆分了,那自然數據庫也得拆,否則相當于把熱點從服務器轉移到了數據庫上。分庫顯然能夠減輕單個數據庫壓力,那分表呢?垂直分表將熱點數據集中減小每次數據吞吐量,水平分布則能通過主鍵id定位使用哪張表,如果分了100張表,則單表的數據量除以100,查詢表的速率顯然會漲一大截。當然由于分布式系統加分表后如何確保主鍵唯一以及具體如何使用也需要解決。

分庫分表可參考下面鏈接:

從零搭建微服務項目Pro(第7-1章——分布式雪花算法)_如果當前時間戳對于用例來說是異常的,那么深入到指標,找出時間戳中有高度異-CSDN博客

從零搭建微服務項目Pro(第8-1章——Sharding-Jdbc+MybatisPlus)_mybatis-plus 整合 sharding-jdbc-CSDN博客

緩存:

學習過計組的小伙伴都知道cache的速度遠大于磁盤,當然此cache非彼cache,單拿最常用的緩存redis舉例,其會將數據以key-value的形式存儲,搜索效率遠快于數據庫逐條查詢,因此一般常將不那么頻繁變動的數據存在緩存,后端優先使用緩存數據。當然如果sql數據變動一定要及時更改緩存,否則就會出現經典面試題目,數據庫和緩存不一致怎么辦(延遲雙刪)。

緩存可參考下面鏈接:

(-----------------------占位符----------------------還沒做------------------------后續更新------------------------)


二、代碼約定:

對于代碼的某種實現,不同時間可能會有不同的做法,為了統一代碼,做了以下約定(其實只是單純怕自己忘記變成左右手互博),該部分內容在項目readme.md中,會隨著項目推進不斷迭代(可能會吃書,僅供參考)

(1)表現層業務成功返回R.ok,可預見錯誤返回R.failed。用戶異常操作時(如未授權訪問/參數校驗錯誤)均拋出異常(前后端統一)
(2)返回R中不帶額外數據時,使用R<Boolean>,需要明確設置true,失敗使用false(前后端統一)
(3)除了業務異常以及繼承類msg使用中文,其余使用英文(用戶友好)
(4)持久層類均與PO或DictItem結尾,對應同名Manager類(統一命名規范)
(5)Manager層負責可復用業務,Service層實現具體業務,原則上每個服務只能有一個service,且其函數與對應接口同名(分層解耦)
(6)所有接口傳入對象只能為dto中定義對象,且只有dto中對象添加jsr校驗(使參數校驗部分統一由Controller負責)
(7)所有dto對象轉po對象方法必須定義在dto對象中,且方法固定為newXX明確為兩個不同對象(使po專注于與數據庫交互)
(8)manager層不對mapper的異常進行特殊處理,拋出統一捕獲(便于Service層回滾)
(9)每個服務RPC接口均命名為APIController且統一以/api路徑開頭(安全配置開放為內部端點,統一鑒權)
(10)通過@Cacheable注解value為命名空間,統一使用KeyGenerator進行管理。使用RedisTemplate則需要統一使用定義在RedisKey文件中的String.format/String形式(防止魔法值,且相同類型緩存使用類似鍵生成方式方便查詢)
(11)所有service層涉及操作manager的方法均需要添加@Transactional(防止數據庫臟數據)
(12)各模塊內部exception定義在各自模塊中,繼承BizException或SysException(方便ExceptionHandler統一處理)
(13) 所有實體類必須定義在common-core模塊中(否則容易出現循環依賴問題)


三、項目結構

.sql存儲建庫sql文件

.style存儲代碼風格規范文件(可參考下面鏈接)Java靜態代碼分析工具安裝及使用(FindBugs、SpotBugs、SonarQube)以及使用CheckStyle規范阿里代碼風格_spotbugs怎么用-CSDN博客

.yaml存儲放在nacos上的配置文件,方便配置文件版本管理

auth-service鑒權服務模塊

common公共模塊定義

common-bom約定依賴版本

common-cache緩存相關模塊

common-core存放所有模塊pojo,異常聲明處理,以及jsr參數校驗(可參考下面鏈接)

從零搭建微服務項目Pro(第2-1章——JSR303自定義參數校驗+異常處理)-CSDN博客

從零搭建微服務項目Pro(第2-2章——JSR303自定義文件校驗+整合至微服務公共模塊)-CSDN博客

common-data數據庫模塊,上文提到分庫分表在此配置

common-feign服務調用模塊,上文提到服務調用在此配置

common-security鑒權模塊,上文提到鑒權在此配置

gateway網關模塊,上文提到網關模塊在此配置

order-service訂單服務

task-service定時任務服務(可參考下面鏈接)

從零搭建微服務項目Pro(第1-1章——Quartz實現定時任務模塊)_微服務如何處理定時任務-CSDN博客

從零搭建微服務項目Pro(第1-2章——Quartz實現定時任務模塊優化)_springcloud quartz模塊-CSDN博客

從零搭建微服務項目Pro(第1-3章——Quartz定時任務模塊整合)_創建quarz服務和原有服務的交互邏輯-CSDN博客

user-service用戶服務


四、Pom文件

雖然此處會展示,但仍然建議在項目文件中查看。

<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>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version><name>${project.artifactId}</name><packaging>pom</packaging><properties><spring-boot.version>2.3.9.RELEASE</spring-boot.version><spring-cloud.version>2.2.5.RELEASE</spring-cloud.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><maven.compiler.version>3.8.1</maven.compiler.version><checkstyle.skip>false</checkstyle.skip></properties><modules><module>user-service</module><module>order-service</module><module>common</module><module>gateway</module><module>task-service</module><module>auth-service</module></modules><dependencyManagement><dependencies><!-- 公共版本定義 --><dependency><groupId>cn.bit</groupId><artifactId>common-bom</artifactId><version>${project.version}</version><type>pom</type><scope>import</scope></dependency><!-- spring boot --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>${spring-boot.version}</version><type>pom</type><scope>import</scope></dependency><!--spring cloud--><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><scope>compile</scope><version>1.18.20</version><optional>true</optional></dependency></dependencies><build><resources><!--允許src/main/resources下配置文件讀取pom中配置--><resource><directory>src/main/resources</directory><filtering>true</filtering></resource></resources><pluginManagement><plugins><!--checkstyle插件--><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-checkstyle-plugin</artifactId><version>3.6.0</version><dependencies><dependency><groupId>com.puppycrawl.tools</groupId><artifactId>checkstyle</artifactId><version>9.3</version></dependency></dependencies><configuration><outputEncoding>UTF-8</outputEncoding><skip>${checkstyle.skip}</skip><configLocation>.style/alibaba.xml</configLocation><consoleOutput>true</consoleOutput><failsOnError>false</failsOnError><linkXRef>false</linkXRef><includeTestSourceDirectory>false</includeTestSourceDirectory><sourceDirectories>${project.build.sourceDirectory}</sourceDirectories></configuration><executions><execution><id>validate</id><phase>validate</phase><goals><goal>check</goal></goals></execution></executions></plugin><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>${spring-boot.version}</version><executions><execution><goals><goal>repackage</goal></goals></execution></executions></plugin></plugins></pluginManagement><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>${maven.compiler.version}</version><configuration><target>${maven.compiler.target}</target><source>${maven.compiler.source}</source><encoding>UTF-8</encoding><skip>true</skip></configuration></plugin></plugins></build><!--多環境配置--><profiles><profile><id>dev</id><properties><profile.active>dev</profile.active><nacos.username>nacos</nacos.username><nacos.password>nacos</nacos.password><nacos.server-addr>192.168.200.1:8848</nacos.server-addr><nacos.cluster-name>BJ</nacos.cluster-name><nacos.namespace>5dadfe94-237d-4367-998e-b13d172ddcfc</nacos.namespace><profiles.auth-service.port>1232</profiles.auth-service.port><profiles.gateway.port>1233</profiles.gateway.port><profiles.user-service.port>1234</profiles.user-service.port><profiles.order-service.port>1235</profiles.order-service.port><profiles.quartz-service.port>1236</profiles.quartz-service.port></properties></profile><profile><id>test</id><properties><profile.active>test</profile.active><nacos.username>nacos</nacos.username><nacos.password>nacos</nacos.password><nacos.server-addr>192.168.200.1:8848</nacos.server-addr><nacos.cluster-name>BJ</nacos.cluster-name><nacos.namespace>5dadfe94-237d-4367-998e-b13d172ddcfc</nacos.namespace><profiles.auth-service.port>2232</profiles.auth-service.port><profiles.gateway.port>2233</profiles.gateway.port><profiles.user-service.port>2234</profiles.user-service.port><profiles.order-service.port>2235</profiles.order-service.port><profiles.quartz-service.port>2236</profiles.quartz-service.port></properties></profile><profile><id>prod</id><properties><profile.active>prod</profile.active><nacos.username>nacos</nacos.username><nacos.password>nacos</nacos.password><nacos.server-addr>192.168.200.1:8848</nacos.server-addr><nacos.cluster-name>BJ</nacos.cluster-name><nacos.namespace>5dadfe94-237d-4367-998e-b13d172ddcfc</nacos.namespace><profiles.auth-service.port>3232</profiles.auth-service.port><profiles.gateway.port>3233</profiles.gateway.port><profiles.user-service.port>3234</profiles.user-service.port><profiles.order-service.port>3235</profiles.order-service.port><profiles.quartz-service.port>3236</profiles.quartz-service.port></properties></profile></profiles></project>

common

<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><parent><groupId>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version></parent><artifactId>common</artifactId><packaging>pom</packaging><name>common</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><modules><module>common-bom</module><module>common-core</module><module>common-cache</module><module>common-feign</module><module>common-data</module><module>common-security</module></modules>
</project>

common-bom

<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>cn.bit</groupId><artifactId>common-bom</artifactId><version>1.0.0</version><packaging>pom</packaging><name>common-bom</name><url>http://maven.apache.org</url><properties><bitgo.version>1.0.0</bitgo.version><mybatis-plus.version>3.5.1</mybatis-plus.version><sharding-jdbc.version>4.1.1</sharding-jdbc.version><jjwt.version>0.9.1</jjwt.version><jaxb.version>2.3.1</jaxb.version><mysql.connector.version>8.0.28</mysql.connector.version><openfeign.version>2.2.5.RELEASE</openfeign.version><gateway.version>2.2.5.RELEASE</gateway.version></properties><dependencyManagement><dependencies><!-- jwt --><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>${jjwt.version}</version></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId><version>${jaxb.version}</version></dependency><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId><version>${jaxb.version}</version></dependency><!-- sharding jdbc --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId><version>${sharding-jdbc.version}</version></dependency><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>${mybatis-plus.version}</version></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql.connector.version}</version></dependency><!-- open feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId><version>${openfeign.version}</version></dependency><!-- gateway --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId><version>${gateway.version}</version></dependency><!-- =============內部依賴版本號============== --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId><version>${bitgo.version}</version></dependency><dependency><groupId>cn.bit</groupId><artifactId>common-cache</artifactId><version>${bitgo.version}</version></dependency><dependency><groupId>cn.bit</groupId><artifactId>common-feign</artifactId><version>${bitgo.version}</version></dependency><dependency><groupId>cn.bit</groupId><artifactId>common-data</artifactId><version>${bitgo.version}</version></dependency><dependency><groupId>cn.bit</groupId><artifactId>common-security</artifactId><version>${bitgo.version}</version></dependency></dependencies></dependencyManagement>
</project>

common-cache

<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><parent><groupId>cn.bit</groupId><artifactId>common</artifactId><version>1.0.0</version></parent><artifactId>common-cache</artifactId><packaging>jar</packaging><name>common-redis</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- redis --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!-- cache --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency></dependencies>
</project>

common-core

<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><parent><groupId>cn.bit</groupId><artifactId>common</artifactId><version>1.0.0</version></parent><artifactId>common-core</artifactId><packaging>jar</packaging><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- validation --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-validation</artifactId></dependency><!-- web --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId></dependency><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- security --><dependency><groupId>org.springframework.security</groupId><artifactId>spring-security-core</artifactId></dependency></dependencies>
</project>

common-data

<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><parent><groupId>cn.bit</groupId><artifactId>common</artifactId><version>1.0.0</version></parent><artifactId>common-data</artifactId><packaging>jar</packaging><name>common-mybatis</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- =======內部依賴======= --><!-- common cache --><dependency><groupId>cn.bit</groupId><artifactId>common-cache</artifactId></dependency></dependencies>
</project>

common-feign

<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><parent><groupId>cn.bit</groupId><artifactId>common</artifactId><version>1.0.0</version></parent><artifactId>common-feign</artifactId><packaging>jar</packaging><name>common-feign</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- open feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- =======內部依賴======= --><!-- common-core --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId></dependency></dependencies>
</project>

common-security

<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><parent><groupId>cn.bit</groupId><artifactId>common</artifactId><version>1.0.0</version></parent><artifactId>common-security</artifactId><packaging>jar</packaging><name>common-security</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><!-- security --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- jwt --><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId></dependency><dependency><groupId>javax.xml.bind</groupId><artifactId>jaxb-api</artifactId></dependency><dependency><groupId>org.glassfish.jaxb</groupId><artifactId>jaxb-runtime</artifactId></dependency><!-- =======內部依賴======= --><!-- common-core --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId></dependency><!-- common-feign --><dependency><groupId>cn.bit</groupId><artifactId>common-feign</artifactId></dependency><!-- =======內部依賴======= --></dependencies>
</project>

gateway

<?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>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version></parent><artifactId>gateway</artifactId><name>gateway</name><description>gateway</description><dependencies><!-- gateway --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><!-- =======公共依賴======= --><!-- nacos discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- nacos config --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- =======內部依賴======= --></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

order-service

<?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>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version></parent><artifactId>order-service</artifactId><name>order-service</name><description>order-service</description><dependencies><!-- =======公共依賴======= --><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- nacos discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- nacos config --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- open feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- sharding jdbc --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- =======內部依賴======= --><!-- common-core --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId></dependency><!-- common-data --><dependency><groupId>cn.bit</groupId><artifactId>common-data</artifactId></dependency><!-- common-cache --><dependency><groupId>cn.bit</groupId><artifactId>common-cache</artifactId></dependency><!-- common-feign --><dependency><groupId>cn.bit</groupId><artifactId>common-feign</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

task-service

<?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>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version></parent><artifactId>task-service</artifactId><name>task-service</name><description>task-service</description><dependencies><!-- quartz--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-quartz</artifactId></dependency><!-- =======公共依賴======= --><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- nacos discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- nacos config --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- open feign  --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- sharding jdbc --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- =======內部依賴======= --><!-- common-core --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId></dependency><!-- common-data --><dependency><groupId>cn.bit</groupId><artifactId>common-data</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

user-service

<?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>cn.bit</groupId><artifactId>BitGoPlus</artifactId><version>1.0.0</version></parent><artifactId>user-service</artifactId><name>user-service</name><description>user-service</description><dependencies><!-- =======公共依賴======= --><!-- web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- nacos discovery --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId></dependency><!-- nacos config --><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId></dependency><!-- open feign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- sharding jdbc --><dependency><groupId>org.apache.shardingsphere</groupId><artifactId>sharding-jdbc-spring-boot-starter</artifactId></dependency><!-- mysql --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- mybatis plus --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></dependency><!-- =======內部依賴======= --><!-- common-core --><dependency><groupId>cn.bit</groupId><artifactId>common-core</artifactId></dependency><!-- common-data --><dependency><groupId>cn.bit</groupId><artifactId>common-data</artifactId></dependency><!-- common-security --><dependency><groupId>cn.bit</groupId><artifactId>common-security</artifactId></dependency><!-- common-cache --><dependency><groupId>cn.bit</groupId><artifactId>common-cache</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

核心為將配置文件定義在根pom中更具不同開發環境選擇不同配置,比如nacos地址和各服務端口,并用common-bom統一管理用到所有依賴以及內部common模塊的版本信息,再由根pom將其導入通過<dependencyManagement>進而實現對整個項目所用依賴統一管理。


五、服務bootstrap配置文件

server:port: @profiles.order-service.port@spring:profiles:active: @profile.active@application:name: @project.artifactId@cloud:nacos:server-addr: @nacos.server-addr@username: @nacos.username@password: @nacos.password@discovery:cluster-name: @nacos.cluster-name@namespace: @nacos.namespace@group: ${spring.profiles.active}config:cluster-name: ${spring.cloud.nacos.discovery.cluster-name}namespace: ${spring.cloud.nacos.discovery.namespace}group: ${spring.cloud.nacos.discovery.group}file-extension: yamlshared-configs:- data-id: application.yamlgroup: ${spring.profiles.active}refresh: true
snowflake:service-name: ${spring.application.name}

所有服務均使用此bootstrap.yaml文件,不同的配置使用在nacos服務器上的yaml文件,同時各服務公共配置文件為application.yaml。


總結:

理論上使用項目提供的所有配置文件和sql以及代碼是能夠運行所有服務的,不過由于本章重點在于微服務腳手架的搭建,所以不再具體講解如何正確配置,后續發布release版本代碼會同步更新博客,感興趣可以start或者follow,基本每周都會有較大新增和改動,當然能自己正確配置的是這個(大拇指)

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/75552.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/75552.shtml
英文地址,請注明出處:http://en.pswp.cn/web/75552.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

VS Code下開發FPGA——FPGA開發體驗提升__下

上一篇&#xff1a;IntelliJ IDEA下開發FPGA-CSDN博客 Type&#xff1a;Quartus 一、安裝插件 在應用商店先安裝Digtal IDE插件 安裝后&#xff0c;把其他相關的Verilog插件禁用&#xff0c;避免可能的沖突。重啟后&#xff0c;可能會彈出下面提示 這是插件默認要求的工具鏈&a…

使用Python從零開始構建端到端文本到圖像 Transformer大模型

簡介&#xff1a;通過特征向量從文本生成圖像 回顧&#xff1a;多模態 Transformer 在使用Python從零實現一個端到端多模態 Transformer大模型中&#xff0c;我們調整了字符級 Transformer 以處理圖像&#xff08;通過 ResNet 特征&#xff09;和文本提示&#xff0c;用于視覺…

Webpack中的文件指紋:給資源戴上個“名牌”

你是否想過&#xff0c;當你修改代碼后&#xff0c;瀏覽器為什么仍然拿著舊版資源不放&#xff1f;秘密就在于——文件指紋&#xff01;簡單來說&#xff0c;文件指紋就像給每個構建出來的文件貼上獨一無二的“姓名牌”&#xff0c;告訴瀏覽器&#xff1a;“嘿&#xff0c;我更…

python可變對象與不可變對象

文章目錄 Python 中的可變對象與不可變對象不可變對象(Immutable Objects)可變對象(Mutable Objects)重要區別 Python 中的可變對象與不可變對象 在 Python 中&#xff0c;對象可以分為可變對象(mutable)和不可變對象(immutable)&#xff0c;這是 Python 中非常重要的概念&…

DeepSeek私有化部署性能怎么樣?企業級AI落地實戰解析!

1. 私有化部署是什么&#xff1f;為什么企業需要它&#xff1f; 很多公司在考慮用AI時都會問&#xff1a;“DeepSeek私有化部署性能怎么樣&#xff1f;能不能在我們自己的服務器上跑&#xff1f;” 私有化部署的意思就是把AI模型裝在你自己的機房或者云服務器上&#xff0c;而…

SQL學習--基礎語法學習

SQL和excle對比 學習目標 單表查詢 項目背景 SQL 練習環境 SQL Online Compiler - Next gen SQL Editor 商品信息表&#xff1a;https://study-zhibo.oss-cn-shanghai.aliyuncs.com/test/%E5%95%86%E5%93%81%E4%BF%A1%E6%81%AF%E8%A1%A8.csv 訂單明細表&#xff1a;https://…

【Docker基礎-網絡】--查閱筆記4

目錄 Docker 網絡網絡類型none 網絡host 網絡bridge 網絡自定義網絡 容器間通信IP 通信Docker DNS Serverjoined 容器 容器與外部通信容器訪問外部外部訪問容器 Docker 網絡 學習Docker提供的幾種原生網絡如何創建自定義網絡容器間通信&#xff0c;容器于外界交互 Docker 安裝…

GPT模型架構與文本生成技術深度解析

核心發現概述 本文通過系統分析OpenAI的GPT系列模型架構&#xff0c;揭示其基于Transformer解碼器的核心設計原理與文本生成機制。研究顯示&#xff0c;GPT模型通過自回歸機制實現上下文感知的序列生成&#xff0c;其堆疊式解碼器結構配合創新的位置編碼方案&#xff0c;可有效…

AWTK-MVVM 如何讓多個View復用一個Model記錄+關于app_conf的踩坑

前言 有這么一個業務&#xff0c;主界面點擊應用窗口進入聲納顯示界面&#xff0c;聲納顯示界面再通過按鈕進入菜單界面&#xff0c;菜單界面有很多關于該聲納顯示界面的設置項&#xff0c;比如量程&#xff0c;增益&#xff0c;時間顯示&#xff0c;亮度&#xff0c;對比度等…

CrystalDiskInfo電腦硬盤監控工具 v9.6.0中文綠色便攜版

前言 CrystalDiskInfo是一個不用花錢的硬盤小幫手軟件&#xff0c;它可以幫你看看你的電腦硬盤工作得怎么樣&#xff0c;健不健康。這個軟件能顯示硬盤的溫度高不高、還有多少地方沒用、傳輸東西快不快等等好多信息。用了它&#xff0c;你就能很容易地知道硬盤現在是什么情況&…

數據分析-數據預處理

數據分析-數據預處理 處理重復值 duplicated( )查找重復值 import pandas as pd apd.DataFrame(data[[A,19],[B,19],[C,20],[A,19],[C,20]],columns[name,age]) print(a) print(--------------------------) aa.duplicated() print(a)只判斷全局不判斷每個 any() import p…

如何用海倫公式快速判斷點在直線的哪一側

一、海倫公式的定義與推導 1. 海倫公式的定義 海倫公式&#xff08;Heron’s Formula&#xff09;是用于計算三角形面積的一種方法&#xff0c;適用于已知三角形三邊長度的情況。公式如下&#xff1a; S s ( s ? a ) ( s ? b ) ( s ? c ) S \sqrt{s(s - a)(s - b)(s - c…

python推箱子游戲

,--^----------,--------,-----,-------^--,-------- 作者 yty---------------------------^----------_,-------, _________________________XXXXXX XXXXXX XXXXXX ______(XXXXXXXXXXXX(________(------ 0 [[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1], [1,0,0,0,0,0,0,0,0,0,0,0,…

使用Python建模量子隧穿

引言 量子隧穿是量子力學中的一個非常有趣且令人神往的現象。在經典物理學中,我們通常認為粒子必須克服一個勢壘才能通過它。但是,在量子力學中,粒子有時可以“穿越”一個勢壘,即使它的能量不足以克服這個勢壘。這種現象被稱為“量子隧穿”。今天,我們將通過 Python 來建…

Vuex Actions 多參數傳遞的解決方案及介紹

Vuex Actions 多參數傳遞的解決方案及介紹 引言 在Vuex狀態管理模式中&#xff0c;Actions 扮演著至關重要的角色。它主要用于處理異步操作&#xff0c;并且可以提交 Mutations 來修改全局狀態。然而&#xff0c;在實際開發中&#xff0c;我們常常會遇到需要向 Actions 傳遞多…

設計模式 --- 策略模式

?策略模式&#xff08;Strategy Pattern&#xff09;是一種 ??行為型設計模式??&#xff0c;用于動態切換算法或策略??&#xff0c;使得算法可以獨立于客戶端變化。它通過封裝算法策略并使其可互換&#xff0c;提升了系統的靈活性和擴展性&#xff0c;尤其適用于需要多種…

【論文閱讀】RMA: Rapid Motor Adaptation for Legged Robots

Paper: https://arxiv.org/abs/2107.04034Project: https://ashish-kmr.github.io/rma-legged-robots/Code: https://github.com/antonilo/rl_locomotion訓練環境&#xff1a;Raisim 1.方法 RMA&#xff08;Rapid Motor Adaptation&#xff09;算法通過兩階段訓練實現四足機器…

QQ風格客服聊天窗口

QQ風格客服聊天窗口 展示引入方式 展示 引入方式 <!DOCTYPE html> <html lang"zh-CN"> <head><meta charset"UTF-8"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title&g…

【家政平臺開發(37)】家政平臺蛻變記:性能優化與代碼重構揭秘

本【家政平臺開發】專欄聚焦家政平臺從 0 到 1 的全流程打造。從前期需求分析,剖析家政行業現狀、挖掘用戶需求與梳理功能要點,到系統設計階段的架構選型、數據庫構建,再到開發階段各模塊逐一實現。涵蓋移動與 PC 端設計、接口開發及性能優化,測試階段多維度保障平臺質量,…