原文:https://xsxy007.github.io/2019/10/SpringCloud-Eureka/
Eureka 是Netflix开源的一款提供服务注册和发现的产品,提供了完整的Service Registery和Service Discovery实现,也是SpringCloud体系中最核心的组件之一java
由上边的图能够看出,Eureka
由客户端和服务端组成,服务用用于服务的注册服务器,客户端用做服务的提供者和发现者git
git@github.com:xsxy007/springcloud-demo.gitgithub
springboot 已经很好的支持了Eureka,只须要在pom中加入Eureka依赖,并在Application启动类中加上相关注解便可spring
一、pom依赖添加springboot
本次使用的springboot版本为2.1.9;springcloud版本为Greenwich.RELEASE服务器
如下列出了全部的依赖(ps:一开始由于springcloud和springboot的版本依赖问题,致使项目一直启动报错)app
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.RELEASE</spring-cloud.version> </properties> <dependencies> <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>
二、在启动类上加注解@EnableEurekaServer
注解maven
@SpringBootApplication @EnableEurekaServer public class SpringcloudDemoApplication { public static void main(String[] args) { SpringApplication.run(SpringcloudDemoApplication.class, args); } }
三、配置文件ide
在默认配置下,该注册中心会将本身做为客户端尝试注册本身,所以在单机的注册中心时,须要将此机制关闭掉spring-boot
server: port: 8761 # 使用的是默认的端口 spring: application: name: eureka-server eureka: client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://localhost:${server.port}/eureka/
eureka.client.register-with-eureka
:表示是否将本身注册的eureka server,默认为true
eureka.client.fetch-registry
:表示是否从eureka server上拉取注册信息,默认为true
eureka.client.service-url.defaultZone
:表示注册中心的地址(默认为localhost:8761/eureka/)
四、启动项目
启动项目后访问localhost:8761
便可显示以下页面,此时 Instances current Registers里边并无内容
简单的集群,只要将各个注册中心相互配置便可
eureka.client.serviceUrl.defaultZone=http://域名:/${server.port}/eureka/,http://域名:/${server.port}/eureka/
请其余注册中心的地址配置的defaultZone中
EurekaServerAutoConfiguration
,该类就是EurekaServer的自动配置类/** * Annotation to activate Eureka Server related configuration {@link EurekaServerAutoConfiguration} * @author Dave Syer * @author Biju Kunjummen */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(EurekaServerMarkerConfiguration.class) public @interface EnableEurekaServer { }
// 能够看到,该类并无什么实现,该类的做用:是为了控制 `EurekaServerAutoConfiguration` 类是否加载 @Configuration public class EurekaServerMarkerConfiguration { @Bean public Marker eurekaServerMarkerBean() { return new Marker(); } class Marker { } }
EurekaServerAutoConfiguration
(该类是在经过Springboot的@EnableAutoconfiguration 来加载spring.factories中的类的)// 省略了部分代码 @Configuration @Import(EurekaServerInitializerConfiguration.class) @ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class) // 当存在 EurekaServerMarkerConfiguration 类是才初始化当前文件 @EnableConfigurationProperties({ EurekaDashboardProperties.class, InstanceRegistryProperties.class }) @PropertySource("classpath:/eureka/server.properties") public class EurekaServerAutoConfiguration extends WebMvcConfigurerAdapter { // 实例化一个配置类,该配置类在 EurekaServerInitializerConfiguration 中会使用到 @Configuration protected static class EurekaServerConfigBeanConfiguration { @Bean @ConditionalOnMissingBean public EurekaServerConfig eurekaServerConfig(EurekaClientConfig clientConfig) { EurekaServerConfigBean server = new EurekaServerConfigBean(); if (clientConfig.shouldRegisterWithEureka()) { // Set a sensible default if we are supposed to replicate server.setRegistrySyncRetries(5); } return server; } } // eurekaServer的上下文 @Bean public EurekaServerContext eurekaServerContext(ServerCodecs serverCodecs, PeerAwareInstanceRegistry registry, PeerEurekaNodes peerEurekaNodes) { return new DefaultEurekaServerContext(this.eurekaServerConfig, serverCodecs, registry, peerEurekaNodes, this.applicationInfoManager); } // eurekaServer的启动 @Bean public EurekaServerBootstrap eurekaServerBootstrap(PeerAwareInstanceRegistry registry, EurekaServerContext serverContext) { return new EurekaServerBootstrap(this.applicationInfoManager, this.eurekaClientConfig, this.eurekaServerConfig, registry, serverContext); } }
EurekaServerInitializerConfiguration
类,该类实现了lifecycle接口,因此会被spring容器毁掉start方法@Override public void start() { new Thread(new Runnable() { @Override public void run() { try { //TODO: is this class even needed now? eurekaServerBootstrap.contextInitialized( EurekaServerInitializerConfiguration.this.servletContext); log.info("Started Eureka Server"); publish(new EurekaRegistryAvailableEvent(getEurekaServerConfig())); EurekaServerInitializerConfiguration.this.running = true; publish(new EurekaServerStartedEvent(getEurekaServerConfig())); } catch (Exception ex) { // Help! log.error("Could not initialize Eureka servlet context", ex); } } }).start(); // 调用 start 方法启动线程 }
参考: https://blog.csdn.net/cuixhao110/article/details/88353714