创建Eureka服务模块
1. 新建模块
在项目名上右键,依次点击 新建 > 新模块
在左侧选择Spring Initializr
在右侧输入模块名称server
,语言选择Java
,构建系统选Maven
,打包选择Jar
,然后点击“下一步”
然后按照下图所示,从左侧选择依赖:Spring Web
、 Eureka Server
新建完server
模块后,在pom.xml
上右键,选择添加为 Maven 项目
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>server</name>
<description>server</description>
<properties>
<java.version>17</java.version>
<spring-cloud.version>2023.0.1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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>
2. 修改启动类
打开ServerApplication
,在启动类上添加注解@EnableEurekaServer
3. 修改配置
打开application.properties
,按照下图修改
4. 构建模块
点击server
模块下的maven install
,构建模块
5. 测试模块
点击调试
按钮,启动服务
打开浏览器,在地址栏输入http://localhost:8761/
如果浏览器,出现如下图所示的页面,说明Eureka服务模块配置成功