服务发现:Eureka Server

搭建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;

其他

  1. 如果Eureka Server项目中使用了Thymeleaf模版引擎,那么Eureka Server本身自带的Freemarker 模版引擎可能无法正常加载。要解决这个问题,可以添加如下配置,但是一般不会这样做。
spring:
  freemarker:
    template-loader-path: classpath:/templates/
    prefer-file-system-access: false
  1. Eureka server没有后端存储,但是注册到eureka的服务实例必须发送心跳来维持他们注册的最新状态,注册表是在eureka server的内存中维护的。客户端也有注册表的缓存(因此他们不必为每个服务请求都访问注册表)。
  2. 默认情况下,每个Eureka服务器也是Eureka客户端,需要(至少一个)service-url来定位对等端(自己或者其他的Eureka Server实例)。如果Eureka Server中没有配置service-url,项目也能正常启动,但在日志中充满很多关于无法向对等体注册的报错。
  3. 在单机模式下,一般想要关闭客户端的行为(比如上文中的自注册、获取注册表信息等)。

官方文档

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值