<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
# Eureka 客戶端配置 eureka: client: service-url: defaultZone: http://node2:10002/eureka/,http://node3:10003/eureka/ #本客戶端是否註冊至eureka register-with-eureka: false #本客戶端是否從eureka獲取服務列表 fetch-registry: false instance: # 配置經過主機名方式註冊 hostname: node1 # 配置實例編號 instance-id: ${eureka.instance.hostname}:${server.port}:@project.version@ # 集羣節點之間讀取超時時間。單位:毫秒 server: peer-node-read-timeout-ms: 1000 # 服務端口號 server: port: 10001node2配置:
# Eureka 客戶端配置 eureka: client: service-url: defaultZone: http://node1:10001/eureka/,http://node3:10003/eureka/ #本客戶端是否註冊至eureka register-with-eureka: false #本客戶端是否從eureka獲取服務列表 fetch-registry: false instance: # 配置經過主機名方式註冊 hostname: node2 # 配置實例編號 instance-id: ${eureka.instance.hostname}:${server.port}:@project.version@ # 集羣節點之間讀取超時時間。單位:毫秒 server: peer-node-read-timeout-ms: 1000 # 服務端口號 server: port: 10002node3配置:
# Eureka 客戶端配置 eureka: client: service-url: defaultZone: http://node1:10001/eureka/,http://node2:10002/eureka/ #本客戶端是否註冊至eureka register-with-eureka: false #本客戶端是否從eureka獲取服務列表 fetch-registry: false instance: # 配置經過主機名方式註冊 hostname: node3 # 配置實例編號 instance-id: ${eureka.instance.hostname}:${server.port}:@project.version@ # 集羣節點之間讀取超時時間。單位:毫秒 server: peer-node-read-timeout-ms: 1000 # 服務端口號 server: port: 10003
@EnableEurekaServer @SpringBootApplication public class EurekaServiceApplication { public static void main(String[] args) { SpringApplication.run(EurekaServiceApplication.class, args); } }補充說明:每個eureak服務,須要指定出其餘的服務節點名字,組成集羣組,通過以上配置咱們分別啓動三個服務的main函數,就能夠把這組有三個節點node一、node二、node3組成的集羣啓動起來,對外提供註冊服務
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>2.二、註冊中心配置
spring: application: name: orderService server: port: 8761 eureka: instance: hostname: myapp client: registerWithEureka: true fetchRegistry: true serviceUrl: defaultZone: http://node2:10002/eureka/,http://node3:10003/eureka/,http://node1:10001/eureka/2.三、啓動服務註冊
//@EnableDiscoveryClient //@EnableEurekaClient @SpringBootApplication public class OrderApplication { /** * @MethodName: main * @Description: TODO(這裏用一句話描述這個方法的做用) * @param @param args * @return void * @throws */ public static void main(String[] args) { //DiscoveryClient tt = null; SpringApplication.run(OrderApplication.class, args); } }
補充下,咱們在配置文件中指定了,該客戶端須要向server端進行註冊,並從server端獲取服務列表,因此運行的main函數中,能夠不須要作@EnableEurekaClient或@EnableDiscoveryClient的註解!java
@Bean(destroyMethod = "shutdown") @ConditionalOnMissingBean(value = EurekaClient.class, search = SearchStrategy.CURRENT) @org.springframework.cloud.context.config.annotation.RefreshScope @Lazy public EurekaClient eurekaClient(ApplicationInfoManager manager, EurekaClientConfig config, EurekaInstanceConfig instance) { manager.getInfo(); // force initialization return new CloudEurekaClient(manager, config, this.optionalArgs, this.context); }
二、CloudEurekaClient繼承自DiscoveryClient,在DiscoveryClient完成核心的註冊流程,以下node
@Inject DiscoveryClient(ApplicationInfoManager applicationInfoManager, EurekaClientConfig config, AbstractDiscoveryClientOptionalArgs args, Provider<BackupRegistry> backupRegistryProvider) { if (args != null) { this.healthCheckHandlerProvider = args.healthCheckHandlerProvider; this.healthCheckCallbackProvider = args.healthCheckCallbackProvider; this.eventListeners.addAll(args.getEventListeners()); this.preRegistrationHandler = args.preRegistrationHandler; } else { this.healthCheckCallbackProvider = null; this.healthCheckHandlerProvider = null; this.preRegistrationHandler = null; } this.applicationInfoManager = applicationInfoManager; InstanceInfo myInfo = applicationInfoManager.getInfo(); clientConfig = config; staticClientConfig = clientConfig; transportConfig = config.getTransportConfig(); instanceInfo = myInfo; if (myInfo != null) { appPathIdentifier = instanceInfo.getAppName() + "/" + instanceInfo.getId(); } else { logger.warn("Setting instanceInfo to a passed in null value"); } this.backupRegistryProvider = backupRegistryProvider; this.urlRandomizer = new EndpointUtils.InstanceInfoBasedUrlRandomizer(instanceInfo); localRegionApps.set(new Applications()); fetchRegistryGeneration = new AtomicLong(0); remoteRegionsToFetch = new AtomicReference<String>(clientConfig.fetchRegistryForRemoteRegions()); remoteRegionsRef = new AtomicReference<>(remoteRegionsToFetch.get() == null ? null : remoteRegionsToFetch.get().split(",")); if (config.shouldFetchRegistry()) { //若是開啓了從eureka獲取服務列表,建立列表更新的,健康監控 this.registryStalenessMonitor = new ThresholdLevelsMetric(this, METRIC_REGISTRY_PREFIX + "lastUpdateSec_", new long[]{15L, 30L, 60L, 120L, 240L, 480L}); } else { this.registryStalenessMonitor = ThresholdLevelsMetric.NO_OP_METRIC; } if (config.shouldRegisterWithEureka()) { //若是開啓了註冊功能,建立一個eureka之間心跳監控 this.heartbeatStalenessMonitor = new ThresholdLevelsMetric(this, METRIC_REGISTRATION_PREFIX + "lastHeartbeatSec_", new long[]{15L, 30L, 60L, 120L, 240L, 480L}); } else { this.heartbeatStalenessMonitor = ThresholdLevelsMetric.NO_OP_METRIC; } logger.info("Initializing Eureka in region {}", clientConfig.getRegion()); //沒有開啓獲取註冊列表和服務註冊功能,直接返回 if (!config.shouldRegisterWithEureka() && !config.shouldFetchRegistry()) { logger.info("Client configured to neither register nor query for data."); scheduler = null; heartbeatExecutor = null; cacheRefreshExecutor = null; eurekaTransport = null; instanceRegionChecker = new InstanceRegionChecker(new PropertyBasedAzToRegionMapper(config), clientConfig.getRegion()); // This is a bit of hack to allow for existing code using DiscoveryManager.getInstance() // to work with DI'd DiscoveryClient DiscoveryManager.getInstance().setDiscoveryClient(this); DiscoveryManager.getInstance().setEurekaClientConfig(config); initTimestampMs = System.currentTimeMillis(); logger.info("Discovery Client initialized at timestamp {} with initial instances count: {}", initTimestampMs, this.getApplications().size()); return; // no need to setup up an network tasks and we are done } //下面是開啓了服務註冊和列表查詢 try { // default size of 2 - 1 each for heartbeat and cacheRefresh //這是一個定時任務,分配了兩個調度任務,一個是給心跳維持的線程池,一個是給服務列表刷新的線程池 scheduler = Executors.newScheduledThreadPool(2, new ThreadFactoryBuilder() .setNameFormat("DiscoveryClient-%d") .setDaemon(true) .build()); //心跳維持線程池,經過線程池方式實現了隔離 heartbeatExecutor = new ThreadPoolExecutor( 1, clientConfig.getHeartbeatExecutorThreadPoolSize(), 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactoryBuilder() .setNameFormat("DiscoveryClient-HeartbeatExecutor-%d") .setDaemon(true) .build() ); // use direct handoff //列表刷新的線程池,經過線程池刷新了隔離 cacheRefreshExecutor = new ThreadPoolExecutor( 1, clientConfig.getCacheRefreshExecutorThreadPoolSize(), 0, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new ThreadFactoryBuilder() .setNameFormat("DiscoveryClient-CacheRefreshExecutor-%d") .setDaemon(true) .build() ); // use direct handoff eurekaTransport = new EurekaTransport(); scheduleServerEndpointTask(eurekaTransport, args); AzToRegionMapper azToRegionMapper; if (clientConfig.shouldUseDnsForFetchingServiceUrls()) { azToRegionMapper = new DNSBasedAzToRegionMapper(clientConfig); } else { azToRegionMapper = new PropertyBasedAzToRegionMapper(clientConfig); } if (null != remoteRegionsToFetch.get()) { azToRegionMapper.setRegionsToFetch(remoteRegionsToFetch.get().split(",")); } instanceRegionChecker = new InstanceRegionChecker(azToRegionMapper, clientConfig.getRegion()); } catch (Throwable e) { throw new RuntimeException("Failed to initialize DiscoveryClient!", e); } if (clientConfig.shouldFetchRegistry() && !fetchRegistry(false)) { fetchRegistryFromBackup(); } // call and execute the pre registration handler before all background tasks (inc registration) is started if (this.preRegistrationHandler != null) { this.preRegistrationHandler.beforeRegistration(); } if (clientConfig.shouldRegisterWithEureka() && clientConfig.shouldEnforceRegistrationAtInit()) { try {//這裏完成啓動的時候註冊,調用遠程的eureka server,如:http://node1:10001/eureka/apps/,經過jersey實現rest調用,詳細的註冊代碼,見下方 if (!register() ) { throw new IllegalStateException("Registration error at startup. Invalid server response."); } } catch (Throwable th) { logger.error("Registration error at startup: {}", th.getMessage()); throw new IllegalStateException(th); } } // finally, init the schedule tasks (e.g. cluster resolvers, heartbeat, instanceInfo replicator, fetch //完成調度任務的初始化,具體就是本地服務列表刷新任務、心跳監測任務的初始化 initScheduledTasks(); try { Monitors.registerObject(this); } catch (Throwable e) { logger.warn("Cannot register timers", e); } // This is a bit of hack to allow for existing code using DiscoveryManager.getInstance() // to work with DI'd DiscoveryClient DiscoveryManager.getInstance().setDiscoveryClient(this); DiscoveryManager.getInstance().setEurekaClientConfig(config); initTimestampMs = System.currentTimeMillis(); logger.info("Discovery Client initialized at timestamp {} with initial instances count: {}", initTimestampMs, this.getApplications().size()); }
三、註冊的具體動做:參見AbstractJerseyEurekaHttpClient類的register方法,其實就是發送的一個http請求,以下:spring
public EurekaHttpResponse<Void> register(InstanceInfo info) { String urlPath = "apps/" + info.getAppName(); ClientResponse response = null; try { Builder resourceBuilder = jerseyClient.resource(serviceUrl).path(urlPath).getRequestBuilder(); addExtraHeaders(resourceBuilder); response = resourceBuilder .header("Accept-Encoding", "gzip") .type(MediaType.APPLICATION_JSON_TYPE) .accept(MediaType.APPLICATION_JSON) .post(ClientResponse.class, info); return anEurekaHttpResponse(response.getStatus()).headers(headersOf(response)).build(); } finally { if (logger.isDebugEnabled()) { logger.debug("Jersey HTTP POST {}/{} with instance {}; statusCode={}", serviceUrl, urlPath, info.getId(), response == null ? "N/A" : response.getStatus()); } if (response != null) { response.close(); } } }
四、心跳維持和緩存刷新,這一步就是在initScheduleTasks完成的
心跳維持的線程池,首次註冊完成以後,如何保證這個註冊的’有效性‘,因此須要經過心跳維持的線程(HeartbeatThread)作續租,整個過程就是經過前面提到的定時任務,定時去和eureka server進行通訊,告訴它服務還在,服務是有效的!具體的’續租‘邏輯以下:緩存
boolean renew() { EurekaHttpResponse<InstanceInfo> httpResponse; try { httpResponse = eurekaTransport.registrationClient.sendHeartBeat(instanceInfo.getAppName(), instanceInfo.getId(), instanceInfo, null); logger.debug(PREFIX + "{} - Heartbeat status: {}", appPathIdentifier, httpResponse.getStatusCode()); if (httpResponse.getStatusCode() == 404) { REREGISTER_COUNTER.increment(); logger.info(PREFIX + "{} - Re-registering apps/{}", appPathIdentifier, instanceInfo.getAppName()); long timestamp = instanceInfo.setIsDirtyWithTime(); boolean success = register(); if (success) { instanceInfo.unsetIsDirty(timestamp); } return success; } return httpResponse.getStatusCode() == 200; } catch (Throwable e) { logger.error(PREFIX + "{} - was unable to send heartbeat!", appPathIdentifier, e); return false; } }
上面提到了服務列表刷新線程,如何保證,本地的服務列表是有效的:即以下場景
4.一、A服務首次啓動,註冊至eureka的server
4.二、eureka client經過CacheRefreshThread獲取服務列表
4.三、A服務宕機,未能及時經過HeartbeatThread向server作’續租‘
4.四、CacheRefreshThread獲取最新的服務列表,最新的服務列表不包含A服務
CacheRefreshThread實現服務列表刷新邏輯以下:app
private boolean fetchRegistry(boolean forceFullRegistryFetch) { Stopwatch tracer = FETCH_REGISTRY_TIMER.start(); try { // If the delta is disabled or if it is the first time, get all // applications Applications applications = getApplications(); if (clientConfig.shouldDisableDelta() || (!Strings.isNullOrEmpty(clientConfig.getRegistryRefreshSingleVipAddress())) || forceFullRegistryFetch || (applications == null) || (applications.getRegisteredApplications().size() == 0) || (applications.getVersion() == -1)) //Client application does not have latest library supporting delta { logger.info("Disable delta property : {}", clientConfig.shouldDisableDelta()); logger.info("Single vip registry refresh property : {}", clientConfig.getRegistryRefreshSingleVipAddress()); logger.info("Force full registry fetch : {}", forceFullRegistryFetch); logger.info("Application is null : {}", (applications == null)); logger.info("Registered Applications size is zero : {}", (applications.getRegisteredApplications().size() == 0)); logger.info("Application version is -1: {}", (applications.getVersion() == -1)); getAndStoreFullRegistry(); } else { getAndUpdateDelta(applications); } applications.setAppsHashCode(applications.getReconcileHashCode()); logTotalInstances(); } catch (Throwable e) { logger.error(PREFIX + "{} - was unable to refresh its cache! status = {}", appPathIdentifier, e.getMessage(), e); return false; } finally { if (tracer != null) { tracer.stop(); } } // Notify about cache refresh before updating the instance remote status onCacheRefreshed(); // Update remote status based on refreshed data held in the cache updateInstanceRemoteStatus(); // registry was fetched successfully, so return true return true; }