系統複雜了,抽離單一職責的模塊幾乎是必須的;若需維護多個項目,抽離公用包上傳私有倉庫管理也幾乎是必須的。其優勢無需贅述,如下將記錄操做過程。
java
在.NET 中因爲其統一性,實現上更天然一點。Spring Boot 經過 Maven 構建多模塊工程也不麻煩,假如個人項目中包含如下幾個包:
spring
我須要將他們分別拆分紅獨立模塊,首先要修改的是根目錄下的 pom.xml,packaging 類型改成 pom,並添加 modules 節點:apache
<?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.youclk.multi-package</groupId> <artifactId>parent</artifactId> <version>0.0.1-SNAPSHOT</version> <modules> <module>api</module> <module>service</module> <module>dao</module> </modules> <packaging>pom</packaging> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <lombok>1.16.20</lombok> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok}</version> </dependency> </dependencies> </project>
以後新建一個個 Module,將對應的代碼移植過去:
api
須要注意的是在啓動模塊的 pom.xml 中須要指定啓動類:負載均衡
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.youclk.multipackage.api.MultiApplication</mainClass> <layout>ZIP</layout> </configuration> </plugin> </plugins> </build>
統一升級版本命令:mvn versions:set -DnewVersion=0.0.1-SNAPSHOT
,到此差很少完成了,引用方式與普通的依賴包一致:maven
<dependency> <groupId>com.youclk.multi-package</groupId> <artifactId>service</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency>
Docker 時代一切都變得異常簡單,Compose 配置以下:spring-boot
version: '3.5' services: nexus: image: sonatype/nexus3:3.10.0 networks: - proxy - youclk volumes: - /mnt/nas/db/nexus-data:/nexus-data deploy: mode: replicated labels: - com.df.notify=true - com.df.port=8081 - com.df.serviceDomain=nexus.youclk.com restart_policy: condition: any max_attempts: 3 update_config: delay: 5s order: start-first resources: limits: cpus: '0.50' memory: 1g networks: proxy: external: true youclk: external: true
啓動過程須要一分鐘左右:
ui
須要注意的是若是你的 ssl 是在負載均衡或者其餘的反向代理之上,那麼必須在 HTTP 頭中指定 X-Forwarded-Proto 傳輸協議爲 HTTPS,而後,就能夠愉快地玩耍了。url
首先須要在 Nexus 建立私有倉庫,例如個人:
spa
其次在本地 maven 設置中添加 server 節點,默認在 ~/.m2/settings.xml
:
<servers> <server> <id>youclk</id> <username>admin</username> <password>youclk</password> </server> </servers>
pom.xml 中添加上傳地址:
<distributionManagement> <repository> <id>nexus</id> <name>Releases Repository</name> <url>https://nexus.youclk.com/repository/youclk-releases/</url> </repository> <snapshotRepository> <id>nexus</id> <name>Snapshot Repository</name> <url>https://nexus.youclk.com/repository/youclk-snapshots/</url> </snapshotRepository> </distributionManagement>
最後執行 mvn clean deploy
便會上傳至私有倉庫,單獨傳包命令以下:
mvn deploy:deploy-file -DgroupId=com.youclk -DartifactId=utils -Dversion=0.0.1-SNAPSHOT -Dpackaging=jar -Dfile=target/utils-0.0.1-SNAPSHOT.jar -Durl=https://nexus.youclk.com/repository/youclk/ -DrepositoryId=youclk
管理和查看:
Finally,最後的最後就是怎麼使用啦~ 若是須要全局引用的話須要在 settings.xml 添加和激活倉庫:
<?xml version="1.0" encoding="UTF-8"?> <settings> <mirrors> <mirror> <id>aliyun</id> <mirrorOf>central</mirrorOf> <name>central mirror</name> <url>http://maven.aliyun.com/mvn/repository</url> </mirror> <mirror> <id>nexus</id> <mirrorOf>maven-public</mirrorOf> <name>private mirror</name> <url>http://local-nexus.youclk.com/repository/maven-public/</url> </mirror> </mirrors> <servers> <server> <id>nexus</id> <username>admin</username> <password>youclk</password> </server> </servers> <profiles> <profile> <id>nexus</id> <repositories> <repository> <id>maven</id> <name>local private nexus</name> <url>http://local-nexus.youclk.com/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>maven</id> <name>local private nexus</name> <url>http://local-nexus.youclk.com/repository/maven-public/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> </pluginRepositories> </profile> </profiles> <activeProfiles> <activeProfile>nexus</activeProfile> </activeProfiles> </settings>
不過通常不推薦這麼寫,settings.xml 應該儘量保持簡潔,精簡配置,此處留下代理和權限認證便可,其他的能夠移植到 pom.xml 中:
<repositories> <repository> <id>aliyun</id> <url>http://maven.aliyun.com/nexus/content/groups/public/</url> </repository> <repository> <id>nexus</id> <url>http://local-nexus.youclk.com/repository/maven-public/</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>central</id> <url> http://maven.aliyun.com/nexus/content/groups/public/ </url> </pluginRepository> <pluginRepository> <id>maven-public</id> <url>http://local-nexus.youclk.com/repository/maven-public/</url> </pluginRepository> </pluginRepositories>