@EnableEurekaServer

定义:@EnableEurekaServer注解是Spring Cloud中的一个注解,用于将Spring Boot应用程序指定为Eureka服务器。

Eureka服务器是一个服务注册中心,也被称为发现服务器,管理和协调微服务。保存有关所有客户端服务应用程序的信息。

每个微服务都会注册到Eureka服务器,然后Eureka服务器就知道每个端口和IP地址上运行的所有客户端应用程序。

使用:

在Spring Boot应用程序的主类(启动类)中添加@EnableEurekaServer注解,就可以启动一个Eureka服务器。默认端口号:8761

注解的源代码:

java 复制代码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.cloud.netflix.eureka.server;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.context.annotation.Import;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Import({EurekaServerMarkerConfiguration.class})
public @interface EnableEurekaServer {
}

EurekaServerMarkerConfiguration.class源码:

java 复制代码
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.cloud.netflix.eureka.server;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration(
    proxyBeanMethods = false
)
public class EurekaServerMarkerConfiguration {
    public EurekaServerMarkerConfiguration() {
    }

    @Bean
    public EurekaServerMarkerConfiguration.Marker eurekaServerMarkerBean() {
        return new EurekaServerMarkerConfiguration.Marker();
    }

    class Marker {
        Marker() {
        }
    }
}

源码分析:

1.@EnableEurekaServer 注解直接导入了配置类EurekaServerMarkerConfiguration,而这个配置类中向spring容器中注册了一个EurekaServerMarkerConfiguration的Bean。

这个注解可以看作是一个开关,开启时,会激活相关配置去作为注册中心。

2.EurekaServerMarkerConfiguration配置类中,向容器中注入了一个类EurekaServerMarkerConfiguration.Marker,用来激活配置类。

总结:在Spring Cloud中,当你需要使用Eureka注册中心的时候,需要在Eureka的服务端启动类上添加@EnableEurekaServer注解

【用于激活Eureka服务器相关配置,将项目作为Spring Cloud中的注册中心,让Eureka服务器能正常运行,提供服务发现的功能】

相关推荐
jay神6 小时前
【计算机毕业设计】基于SpringBoot的程序教学辅助系统
java·前端·vue.js·spring boot·后端·毕业设计·课程设计
谢亮_vipxieliang10 小时前
ValidX分组验证详解:不同场景使用不同验证规则
java·spring boot·后端·spring cloud·eclipse·hibernate
凤山老林11 小时前
Spring Boot 集成 Spring Vault:集中式密钥管理与动态凭证轮换
spring boot·后端·spring·vault·集中式秘钥管理
2601_9620711711 小时前
八. Spring Boot2 整合连接 Redis(超详细剖析)
spring boot·redis·后端
AIGC小尼13 小时前
2026 最新实战|SpringCloud 微服务整合 SpringAI 完整落地教程(RAG 知识库 + 限流降级 + 跨服务调用)
spring·spring cloud·微服务
钱栈up14 小时前
VSCode 调试多模块 Maven + Spring Boot 后端:从启动失败到一次跑通
java·spring boot·maven
2601_9621896015 小时前
【SpringBoot】使用IDEA创建SpringBoot项目
java·spring boot·intellij-idea
步行cgn15 小时前
Spring Boot 启动报错:MissingServletWebServerFactoryBean 的排查与解决
java·spring boot·后端
步行cgn16 小时前
Spring Boot 项目打包部署详解
java·spring boot·后端
pnoker16 小时前
拆解 IoT DC3:六层微服务架构
java·物联网·spring cloud·微服务·架构