在IDEA 中创建项目,首先创建一个空项目。
一、创建空项目
-
点击New Project,选择 Empty Project,点击Next 。
image.png -
填写项目名称及项目地址,点击finish。
image.png
3.添加父模块,选择Modules ,点击 + 号,如图:
-
选择Maven,点击下一步。
image.png -
填写 Groupld 和 ArtifactId ,然后点击Next。
image.png -
填写 模块名称和 选择项目路径,点击 Finish。
image.png -
点击 Apply 和 OK
image.png -
删除多余目录,如下:
选择 src 目录,右键选择删除。
image.png
9.修改父 pom 文件
在 pom 文件中 添加如下依赖。
<packaging>pom</packaging>
<!--父模块-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/>
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!--版本管理-->
<junit.version>4.12</junit.version>
<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<!--依赖包管理-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
</dependency>
<!--SpringCloud 的版本-->
<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>
二、创建 子模块
-
点击项目右键 --> 新建模块
image.png
2.在New Module 中选择Spring Initializr ,点击Next 。
-
填写GroupId以及ArtifactId,以及版本号等
image.png
-
选择CloudDiscovery -> Eureka Server ,点击Next 。
image.png -
填写 模块名称,模块路径,点击Finish 。
注:模块名称要一致。
image.png 修改 pom 文件
a . 修改 父模块的 pom 文件,在父 pom 文件中引入子模块。
<!--引入子模块-->
<modules>
<module>cloud_eureka_01</module>
</modules>
b. 修改 子模块的 pom 文件
<!--引入父模块依赖-->
<parent>
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-parent</artifactId>-->
<!--<version>2.1.3.RELEASE</version>-->
<!--<relativePath/>-->
<groupId>com.yao</groupId>
<artifactId>SpringCloudParent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
完整子模块的 pom 文件
<?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>
<groupId>com.yaogx</groupId>
<artifactId>cloud_eureka_01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cloud_eureka_01</name>
<description>Demo project for Spring Boot</description>
<!--引入父模块依赖-->
<parent>
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-parent</artifactId>-->
<!--<version>2.1.3.RELEASE</version>-->
<!--<relativePath/>-->
<groupId>com.yao</groupId>
<artifactId>SpringCloudParent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<properties>
<!--<spring-cloud.version>Finchley.RELEASE</spring-cloud.version>-->
</properties>
<dependencies>
<!--eureka 的 服务依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</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.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
7.删除 子模块中多余的文件夹
-
最终项目结构如下:
image.png
三、遇到的问题
-
如果启动类报错Cannot resolve method 'run(java.lang.Class,String[])',如下:
image.png
原因:Project SDK 可能没有引入JDK1.8
解决:Project SDK 引入JDK1.8
-
如果现在直接启动,可能会报错:
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused: connect
image.png
原因:因为 eureka默认情况下,也会把自己当做客户端来注册自己(不知道为什么会这样设计==!)因此我们要禁用它的注册;
解决:在application.properties 配置文件中添加
#表示是否将自己注册到Eureka Server上,默认为true
eureka.client.registerWithEureka=false
#表示是否从Eureka Server上获取注册信息,默认为true
eureka.client.fetchRegistry=false
![image.png](https://upload-images.jianshu.io/upload_images/19949786-a7f6d150fdf49626.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
或者 在 application.yml 文件中添加
server:
port: 9090
### eureka 基本配置
eureka:
client:
register-with-eureka: false
fetch-registry: false