新建Module

该Module为Maven项目,命名为microservice-eureka-server-2001

修改pom.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?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/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>

<!--继承microservice依赖版本-->
<parent>
<groupId>org.example</groupId>
<artifactId>microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<name>microservice-eureka-server-2001</name>
<artifactId>microservice-eureka-server-2001</artifactId>

<dependencies>
<!--Eureka服务-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>

</project>

application.yml配置

在resources中新建application.yml

1
2
3
4
5
6
7
8
9
10
11
12
server:
port: 2001
context-path: /

eureka:
instance:
hostname: localhost #eureka注册中心实例名称
client:
register-with-eureka: false #false 由于该应用为注册中心,所以设置为false,代表不向注册中心注册自己。
fetch-registry: false #false 由于注册中心的职责就是维护服务实例,它并不需要去检索服务,所以也设置为false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与Eureka注册中心交互的地址,查询服务和注册服务用到

Eureka服务启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.ledao;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

/**
* Eureka服务启动类
*
* @author LeDao
* @company
* @create 2021-08-11 16:29
*/
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication_2001 {

public static void main(String[] args) {
SpringApplication.run(EurekaApplication_2001.class, args);
}
}

运行

运行Eureka服务启动类后,在浏览器地址栏输入:http://localhost:2001/