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

相关推荐
懒虫虫~19 分钟前
基于SpringBoot利用死信队列解决RabbitMQ业务队列故障重试无效场景问题
spring boot·rabbitmq
Chan165 小时前
【 SpringCloud | 微服务 MQ基础 】
java·spring·spring cloud·微服务·云原生·rabbitmq
面朝大海,春不暖,花不开6 小时前
自定义Spring Boot Starter的全面指南
java·spring boot·后端
HelloWord~6 小时前
SpringSecurity+vue通用权限系统
vue.js·spring boot
斯普信云原生组6 小时前
Docker构建自定义的镜像
java·spring cloud·docker
wangjinjin1806 小时前
使用 IntelliJ IDEA 安装通义灵码(TONGYI Lingma)插件,进行后端 Java Spring Boot 项目的用户用例生成及常见问题处理
java·spring boot·intellij-idea
白宇横流学长7 小时前
基于SpringBoot实现的大创管理系统设计与实现【源码+文档】
java·spring boot·后端
考虑考虑9 小时前
Springboot3.5.x结构化日志新属性
spring boot·后端·spring
TTDreamTT9 小时前
SpringBoot十二、SpringBoot系列web篇之过滤器Filte详解
spring boot
一只爱撸猫的程序猿13 小时前
构建一个简单的智能文档问答系统实例
数据库·spring boot·aigc