1. 程式人生 > >springcloud-eureka server

springcloud-eureka server

一、背景

eureka分為server端和client端,本文主要來總結server端。

二、功能

1、自身啟動

2、服務發現

3、給客戶端傳送服務提供者的資訊

4、服務續約

5、剔除服務

6、叢集

在微服務的架構中,為了提供高可用,在生產環境中可以部署多個註冊中心(Eureka server),也就是將自己作為服務註冊到其他的註冊中心上,這樣形成一組互相註冊的服務註冊中心,以實現服務清單的互相同步,達到高可用的效果。

示例:

註冊中心1:

server.port=8760
eureka.instance.appname=eurekaone
eureka.instance.hostname=eurekaone
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/
eureka.client.register-with-eureka=true

註冊中心2:

spring.application.name=eurekatwo
server.port=8761
eureka.instance.hostname=eurekatwo
eureka.client.service-url.defaultZone=http://localhost:8760/eureka/
eureka.client.register-with-eureka=true

服務1:

spring.application.name=servera
eureka.instance.hostname=localhost
eureka.client.service-url.defaultZone=http://localhost:8760/eureka/
server.port=8080

服務1註冊到註冊中心1之後,可同步到註冊中心2

PS:

服務註冊資訊不會被二次傳播:註冊中心1註冊到註冊中心2,註冊中心2註冊到註冊中心3,註冊中心3註冊到註冊中心1;服務1註冊到註冊中心1上,註冊中心1和註冊中心2可以看到新註冊上來的服務1,但是服務1不會註冊到註冊中心3上。

註冊中心1:

註冊中心二:

註冊中心三:

如果想讓服務1註冊到這三個節點上,則將註冊中心1需要註冊到其他2個註冊中心上。

三、原始碼分析

1、啟動流程

在spring-cloud-netflix-eureka-server模組的resources下的META-INF中有一個spring.factories

檔案,檔案內容為:

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
  org.springframework.cloud.netflix.eureka.server.EurekaServerAutoConfiguration

即當裝配spring-cloud-netflix-eureka-server模組時,會通過該配置檔案找到具體的實現類名——EurekaServerAutoConfiguration,並裝載例項化,完成模組的注入。

EurekaServerAutoConfiguration類

用於EurekaServer往beanfactory中新增與eureka server有關的bean

@Configuration
@Import(EurekaServerInitializerConfiguration.class)
@ConditionalOnBean(EurekaServerMarkerConfiguration.Marker.class)
@EnableConfigurationProperties({ EurekaDashboardProperties.class,
		InstanceRegistryProperties.class })
@PropertySource("classpath:/eureka/server.properties")
public class EurekaServerAutoConfiguration extends WebMvcConfigurerAdapter {

(原來有@EnableDiscoveryClient後來沒有了,不知道自己的猜測對不對,先保留)

①、@Import:匯入EurekaServerInitializerConfiguration.class—spring啟動eureka server的bean

@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();
	}

②、@ConditionalOnBean: 當且僅當指定的EurekaServerMarkerConfiguration.Marker.class類在當前容器中,才建立標記上該註解的類(EurekaServerAutoConfiguration類)的例項,那EurekaServerMarkerConfiguration.Marker.class類是何時建立的呢?

建立eureka服務模組的application類中:

@EnableEurekaServer
@SpringBootApplication
public class EurekademoApplication {
	public static void main(String[] args) {
		SpringApplication.run(EurekademoApplication.class, args);
	}
}

@EnableEurekaServer註解:

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

}

EurekaServerMarkerConfiguration.class

@Configuration
public class EurekaServerMarkerConfiguration {

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

	class Marker {
	}
}

可以發現當啟動eureka server服務後,最終EurekaServerMarkerConfiguration.class類建立了一個Marker例項

③、

2、eureka叢集

3、接收註冊資訊

4、續約

5、服務下線

6、監測客戶端心跳

四、與zk的對比

五、與consul的對比