@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服务器能正常运行,提供服务发现的功能】

相关推荐
William Dawson24 分钟前
【一文吃透 Spring Boot 面向切面编程(AOP):实例\+实现\+注意事项】
java·spring boot
mldlds1 小时前
SpringBoot项目如何导入外部jar包:详细指南
spring boot·后端·jar
Devin~Y2 小时前
大厂Java面试实战:Spring Boot/Cloud + Redis/Kafka + K8s + RAG/Agent 追问全流程(小Y翻车记)
java·spring boot·redis·spring cloud·kafka·kubernetes·micrometer
Counter-Strike大牛2 小时前
SpringBoot中使用POI+EasyExcel批量导出主子表信息,以箱单为例
windows·spring boot·后端
weiwen14084 小时前
快递100 API 工具类封装实践:签名、请求与缓存防锁单
spring boot·spring·缓存
码农阿豪4 小时前
群晖部署Moodist配内网穿透穿透,把白噪音服务搬到公网上
数据库·spring boot·后端
MuzySuntree5 小时前
Ubuntu 下 Maven 构建 Spring Boot 项目报错 release version 17 not supported 解决方案
spring boot·ubuntu·maven
九转苍翎5 小时前
微服务学习笔记(3)——基于SpringCloud OpenFeign实现远程调用
spring cloud·openfeign
想不明白的过度思考者5 小时前
一个叫Swagger的工具,让写接口文档变成享受
java·spring boot·接口·swagger
Devin~Y15 小时前
大厂Java面试实录:Spring Boot/Cloud、Kafka、Redis、K8s 与 Spring AI(RAG/Agent)三轮连环问
java·spring boot·redis·mysql·spring cloud·kafka·kubernetes