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

相关推荐
终将老去的穷苦程序员8 小时前
基于SpringBoot的餐饮管理系统
java·spring boot·后端
心之伊始8 小时前
Spring AI Tool Calling 实战:让 Java Agent 调用本地 Bean 工具方法
java·spring boot·agent·spring ai·tool calling
熠熠仔8 小时前
Spring Boot 与 MyBatis-Plus 空间几何数据集成指南
spring boot·后端·mybatis
天青色等烟雨..9 小时前
智慧农林核心遥感技术99个案例实践
运维·人工智能·spring boot·后端·自动化
雪宫街道11 小时前
SpringBoot 向 IOC 容器注册组件的两种姿势:@Configuration 与 @Import
java·spring boot·后端·spring
接着奏乐接着舞12 小时前
springboot mp mybatis plaus
windows·spring boot·mybatis
金融支付架构实战指南12 小时前
Milvus 向量检索服务 + SpringBoot 实战:电商商品语义检索与相似商品推荐
spring boot·后端·milvus·向量检索
愛~杦辷个訾13 小时前
Java Springboot使用阿里云oss对图片进行等质量压缩,转换成webp格式的压缩图。
java·spring boot·阿里云·oss
霸道流氓气质13 小时前
Spring Boot Multipart 表单中文乱码问题全解析
java·spring boot·后端
RuoyiOffice13 小时前
从 0 到 1 搭建 RuoyiOffice:30 分钟跑通后端+前端+移动端
前端·spring boot·uni-app·开源·oa·ruoyioffice·hrm