原文地址:http://www.maoyupeng.com/springboot-cloud-register-and-found.html
簡單演示使用idea搭建Spring boot模塊化項(xiàng)目結(jié)構(gòu)搭建及Spring boot服務(wù)發(fā)現(xiàn)與注冊 (該文章為作者筆記,只供參考,不要學(xué)習(xí))
開發(fā)環(huán)境
操作系統(tǒng) | Mac OS |
---|---|
JDK版本 | 1.8 |
Maven | 3.0.5 |
IDE | IntelliJ IDEA |
項(xiàng)目結(jié)構(gòu)規(guī)劃

w400
項(xiàng)目結(jié)構(gòu)規(guī)劃
假設(shè)我們這個(gè)項(xiàng)目有客戶模塊與管理員模塊. 則項(xiàng)目結(jié)構(gòu)劃分為如上圖:
名稱 | 描述 |
---|---|
mpicloud | 項(xiàng)目名稱 |
mpi-admin-provide | 例: mpi項(xiàng)目下,管理員模塊的提供方 |
mpi-admin-service | 例: mpi項(xiàng)目下,管理員模塊的消費(fèi)者 |
mpi-client-provide | mpi項(xiàng)目下,客戶模塊的提供方 |
mpi-client-service | mpi項(xiàng)目下,客戶模塊的消費(fèi)者 |
mpi-cloud | 注冊中心 (通俗的說法) |
新建父模塊
步驟截圖


配置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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.maoyupeng.test</groupId>
<artifactId>mpicloud</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
修改后
<?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.maoyupeng.test</groupId>
<artifactId>mpicloud</artifactId>
<version>${mpicloid.version}</version>
<!--修改打包方式為pom-->
<packaging>pom</packaging>
<!--項(xiàng)目名稱-->
<name>mpicloud</name>
<!--項(xiàng)目描述-->
<description>mpicloud for spring-boot</description>
<!--
子模塊引入
-->
<modules>
</modules>
<!--
定義屬性變量
使用方法例如: <version>${mpicloid.version}</version>
這樣version里面的值為0.0.1-SNAPSHOT
-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--main version-->
<mpicloid.version>0.0.1-SNAPSHOT</mpicloid.version>
<!--cloud服務(wù)jar版本-->
<mpi-cloud.version>0.0.1-SNAPSHOT</mpi-cloud.version>
<!--管理員服務(wù)jar版本-->
<mpi-admin-service.version>0.0.1-SNAPSHOT</mpi-admin-service.version>
<!--客戶端服務(wù)jar版本-->
<mpi-client-service.version>0.0.1-SNAPSHOT</mpi-client-service.version>
<!--管理員提供方j(luò)ar版本-->
<mpi-admin-provide.version>0.0.1-SNAPSHOT</mpi-admin-provide.version>
<!--客戶端提供方j(luò)ar版本-->
<mpi-client-provide.version>0.0.1-SNAPSHOT</mpi-client-provide.version>
</properties>
<!--引入spring boot 核心包-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--引入依賴包-->
<dependencies>
<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.restdocs</groupId>
<artifactId>spring-restdocs-mockmvc</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<!--插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<!--指定spring鏡像倉庫,有的時(shí)候依賴包會報(bào)錯(cuò),如果引入了該倉庫,就會解決問題-->
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
</project>
新建子模塊
步驟截圖


修改子模塊(mpi-client-service)的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>mpicloud</artifactId>
<groupId>com.maoyupeng.test</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mpi-client-service</artifactId>
</project>
修改后
<?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>mpicloud</artifactId>
<groupId>com.maoyupeng.test</groupId>
<!--修改: 引用父模塊中的properties, 引入成功,則配置生效-->
<version>${mpicloid.version}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>mpi-client-service</artifactId>
</project>
驗(yàn)證

由上圖可知,在mpi-client-service(pom.xml)里面沒有引入任何的依賴包,但是在Maven Dependencies 里面,則顯示出了與父模塊一樣的引用依賴.
最后的項(xiàng)目結(jié)構(gòu)
其他子模塊的新建步驟雷同,所以省略...

w400
Spring-boot服務(wù)的注冊與發(fā)現(xiàn)demo(二):http://www.maoyupeng.com/springboot-cloud-register-and-found-2.html