美文网首页
SpringCloud(一) —— Eureka

SpringCloud(一) —— Eureka

作者: 午后凉白开 | 来源:发表于2018-12-10 22:12 被阅读0次

一、代码

@EnableEurekaServer
@SpringBootApplication
public class CloudEurekaApplication {

    /**
     * The entry point of application.
     *
     * @param args the input arguments
     */
    public static void main(String[] args) {
        SpringApplication.run(CloudEurekaApplication.class, args);
    }
}

二、概念

三、源码阅读

EurekaClient继承LookupService,DiscoveryClient继承EurekaClient


/**
     * Register with the eureka service by making the appropriate REST call.
     */
    boolean register() throws Throwable {
        logger.info(PREFIX + "{}: registering service...", appPathIdentifier);
        EurekaHttpResponse<Void> httpResponse;
        try {
            httpResponse = eurekaTransport.registrationClient.register(instanceInfo);
        } catch (Exception e) {
            logger.warn(PREFIX + "{} - registration failed {}", appPathIdentifier, e.getMessage(), e);
            throw e;
        }
        if (logger.isInfoEnabled()) {
            logger.info(PREFIX + "{} - registration status: {}", appPathIdentifier, httpResponse.getStatusCode());
        }
        return httpResponse.getStatusCode() == 204;
    }

DiscoveryClient的register方法,被InstanceInfoReplicator类的run()方法调用。。InstanceInfoReplicator实现Runnable接口。


public void run() {
        try {
            discoveryClient.refreshInstanceInfo();

            Long dirtyTimestamp = instanceInfo.isDirtyWithTime();
            if (dirtyTimestamp != null) {
                discoveryClient.register();
                instanceInfo.unsetIsDirty(dirtyTimestamp);
            }
        } catch (Throwable t) {
            logger.warn("There was a problem with the instance info replicator", t);
        } finally {
            Future next = scheduler.schedule(this, replicationIntervalSeconds, TimeUnit.SECONDS);
            scheduledPeriodicRef.set(next);
        }
    }

ApplicationResource类的addInstance()方法,服务注册的接口。

相关文章

网友评论

      本文标题:SpringCloud(一) —— Eureka

      本文链接:https://www.haomeiwen.com/subject/qccjhqtx.html