搭建Eureka Server
1. 基础环境
JDK21
Spring boot 3.5.3
Spring cloud 2025.0.0
2. 引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
完整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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>21</java.version>
<spring-cloud.version>2025.0.0</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>
3. @EnableEurekaServer
在spring boot的主启动类上标注 @EnableEurekaServer
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
4. application配置文件(单机模式)
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
上面的配置是官网给出的,配置名是驼峰命名。如果自己手写配置的话,是中划线,两种都可以。
spring:
application:
name: eureka-server
server:
port: 8761
eureka:
instance:
hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.client.register-with-eureka:指示此实例是否应向Eureka Server注册其信息以供其服务发现。在某些情况下,Eureka Server不希望自己的实例被发现,而只想发现其他实例。默认值为true;一般情况下,Eureka Server服务本身不需要作为业务服务注册。
通俗来讲,其他业务服务不会通过feign client来调用eureka server的接口,eureka server也不会提供接口给业务服务调用。
eureka.client.fetch-registry:指示此客户端是否应从Eureka Server获取Eureka注册表信息。默认值为true;一般情况下,Eureka Server服务本身不需要获取注册信息。除非是要做一些扩展?
eureka.client.service-url.defaultZone:Eureka服务器的地址。单机模式下,这里写Eureka Server自己的地址。集群模式下,这里写其他Eureka Server的地址,多个地址使用逗号分隔。
总结:
如果服务要提供接口工其他服务通过feign调用,则需要配置eureka.client.register-with-eureka;
如果服务通过feign调用其他服务的接口,则需要配置eureka.client.fetch-registry;
其他
- 如果Eureka Server项目中使用了Thymeleaf模版引擎,那么Eureka Server本身自带的Freemarker 模版引擎可能无法正常加载。要解决这个问题,可以添加如下配置,但是一般不会这样做。
spring:
freemarker:
template-loader-path: classpath:/templates/
prefer-file-system-access: false
- Eureka server没有后端存储,但是注册到eureka的服务实例必须发送心跳来维持他们注册的最新状态,注册表是在eureka server的内存中维护的。客户端也有注册表的缓存(因此他们不必为每个服务请求都访问注册表)。
- 默认情况下,每个Eureka服务器也是Eureka客户端,需要(至少一个)service-url来定位对等端(自己或者其他的Eureka Server实例)。如果Eureka Server中没有配置service-url,项目也能正常启动,但在日志中充满很多关于无法向对等体注册的报错。
- 在单机模式下,一般想要关闭客户端的行为(比如上文中的自注册、获取注册表信息等)。
官方文档
springcloud:https://docs.spring.io/spring-cloud-netflix/reference/spring-cloud-netflix.html#spring-cloud-eureka-server
eureka配置:https://docs.spring.io/spring-cloud-netflix/reference/configprops.html
1945

被折叠的 条评论
为什么被折叠?



