【spring cloud】匯入一個新的spring boot專案作為spring cloud的一個子模組微服務,怎麼做/或者 每次匯入一個新的spring boot專案,IDEA不識別子module,啟動類無法啟動/右下角沒有藍色圖示
如題:匯入一個新的spring boot專案作為spring cloud的一個子模組微服務,怎麼做
或者說每次匯入一個新的spring boot專案,IDEA不識別,啟動類無法啟動,怎麼解決
下面一起來走一遍這個流程:
1.將一個spring boot服務匯入spring cloud中作為一個子模組
如圖:這裡有一個現成的spring cloud微服務叢集,【如何建立一個spring cloud微服務:https://www.cnblogs.com/sxdcgaq8080/p/9035724.html】
然後
開啟本服務根目錄在資源管理器下:
可以看到根目錄下的現有的兩個ms 和parent的pom檔案
然後將想要加入的外部的一個spring boot專案貼上到此處【將tx-manager整個目錄拷貝過來】
如下:
修該名字為統一的ms名字
這個時候,再開啟idea ,看到
2.解決上面匯入但是沒有被識別的問題
所以先解決這個問題:
現將java目錄設定為tx-manager這個微服務的跟路徑
同理,設定resource目錄為根資源路徑
ok。現在就剩最後一個pom.xml檔案的轉變了
3.整理pom.xml檔案內容,建立和最外層parent的pom之間的父子依賴關係
先看一下最外層的parent的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>View Code4.0.0</modelVersion> <groupId>com.swapping</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> <!-- 注意 最後打包 最外層的pom 需要更改為 pom而不是jar <packaging>jar</packaging> --> <packaging>pom</packaging> <name>springcloud</name> <description>Demo project for Spring cloud</description> <modules> <module>springcloud-ms-eureka</module> <module>springcloud-ms-member</module> </modules> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.5.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> <spring-cloud.version>2.0.0.RELEASE</spring-cloud.version> <lcn.last.version>4.1.0</lcn.last.version> </properties> <dependencies> <!--========================spring boot 基礎依賴 start ==============================--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!--========================spring boot 基礎依賴 start ==============================--> <!--最外層 主專案 新增spring cloud依賴 https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter</artifactId> <version>${spring-cloud.version}</version> </dependency> <!-- ========================LCN 分散式事務 start============================--> <dependency> <groupId>com.codingapi</groupId> <artifactId>transaction-springcloud</artifactId> <version>${lcn.last.version}</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.codingapi</groupId> <artifactId>tx-plugins-db</artifactId> <version>${lcn.last.version}</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <!-- ========================LCN 分散式事務 END============================--> <!-- ======================== DB相關所有jar start =============================--> <!-- 資料庫連線池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- 使用mysql資料庫 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- 假設 每一個子模組都會 使用 jpa 和 mybatis 操作資料庫 才將相關jar放在父級pom中 --> <!-- jpa --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!-- mybatis 分頁外掛 --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> <!-- ======================== DB相關所有jar end =============================--> <!-- ======================== redis start ======================== --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- ======================== redis end ======================== --> <!-- ======================== 公用 工具包 start ==============================--> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.8.1</version> </dependency> <dependency> <groupId>com.xiaoleilu</groupId> <artifactId>hutool-all</artifactId> <version>3.3.2</version> </dependency> <dependency> <groupId>com.belerweb</groupId> <artifactId>pinyin4j</artifactId> <version>2.5.1</version> </dependency> <!-- base64加密解密 --> <dependency> <groupId>org.apache.ws.commons.util</groupId> <artifactId>ws-commons-util</artifactId> <version>1.0.2</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>22.0</version> </dependency> <!-- ======================== 公用 工具包 end ==============================--> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
再看一下現在的ms-member這個現成的子module的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.swapping</groupId> <artifactId>springcloud-ms-member</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-ms-member</name> <description>ms-member is consumer ,transfer ms-order and ms-integral</description> <parent> <groupId>com.swapping</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> </project>View Code
最後看一下現在的tx-manage的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.codingapi</groupId> <artifactId>tx-manager</artifactId> <version>4.2.0-SNAPSHOT</version> <packaging>jar</packaging> <name>tx-manager</name> <description>tx-manager</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.4.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> <maven.compile.source>1.7</maven.compile.source> <maven.compile.target>1.7</maven.compile.target> <java.version>1.7</java.version> <guava.version>19.0</guava.version> <spring-cloud.version>Dalston.SR1</spring-cloud.version> <maven.deploy.skip>true</maven.deploy.skip> <maven.install.skip>true</maven.install.skip> </properties> <dependencies> <dependency> <groupId>com.github.1991wangliang</groupId> <artifactId>lorne_core</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.12.Final</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> <exclusions> <exclusion> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </exclusion> </exclusions> </dependency> <!--<dependency>--> <!--<groupId>org.springframework.cloud</groupId>--> <!--<artifactId>spring-cloud-starter-zookeeper-discovery</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-redis</artifactId> <version>1.3.8.RELEASE</version> </dependency> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>${guava.version}</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>${maven-compiler-plugin.version}</version> <configuration> <source>${maven.compile.source}</source> <target>${maven.compile.target}</target> <encoding>${project.build.sourceEncoding}</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-deploy-plugin</artifactId> <version>2.8.2</version> <configuration> <skip>true</skip> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <archive> <manifest> <mainClass>com.codingapi.tm.TxManagerApplication</mainClass> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> </manifest> <manifestEntries> <Class-Path>./</Class-Path> </manifestEntries> </archive> <excludes> <exclude>config/**</exclude> </excludes> </configuration> </plugin> <plugin> <artifactId>maven-assembly-plugin</artifactId> <configuration> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/build/package.xml</descriptor> </descriptors> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>View Code
然後將tx-manager的pom按照member的更改:
<?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.codingapi</groupId> <artifactId>springcloud-ms-tx-manager</artifactId> <version>4.2.0-SNAPSHOT</version> <packaging>jar</packaging> <name>springcloud-ms-tx-manager</name> <description>tx-manager, LCN's distributed transaction coordinator</description> <parent> <groupId>com.swapping</groupId> <artifactId>springcloud</artifactId> <version>0.0.1-SNAPSHOT</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>com.github.1991wangliang</groupId> <artifactId>lorne_core</artifactId> <version>1.0.0</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>4.1.12.Final</version> </dependency> </dependencies> </project>View Code
並且在parent的pom.xml檔案新增tx-manager子模組
最後匯入:點選import changes
最後可以看到 這個子模組的pom檔案終於被識別,並且父子依賴關係已經建立起來:
4.最後將tx-manager真正設立成子module