我是靠谱客的博主 斯文棒球,最近开发中收集的这篇文章主要介绍springboot eureka 注册服务探索,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

运行时日志
2018-03-09 13:16:42.132  INFO 13652 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-03-09 13:17:42.141  INFO 13652 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 7ms
2018-03-09 13:18:42.147  INFO 13652 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 5ms
2018-03-09 13:19:42.155  INFO 13652 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 6ms
2018-03-09 13:20:03.562  INFO 13652 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration
2018-03-09 13:20:42.159  INFO 13652 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 2ms
2018-03-09 13:21:42.160  INFO 13652 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-03-09 13:22:42.163  INFO 13652 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 1ms
2018-03-09 13:23:42.165  INFO 13652 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-03-09 13:24:42.165  INFO 13652 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
2018-03-09 13:25:03.584  INFO 13652 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver      : Resolving eureka endpoints via configuration

注册服务日志
2018-03-09 13:25:42.169  INFO 13652 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 2ms
2018-03-09 13:26:37.169  INFO 13652 --- [ XNIO-2 task-32] c.n.e.registry.AbstractInstanceRegistry  : Registered instance EES-HOTEL/16101164-15-PC:ees-hotel:8119 with status UP (replication=[true/false])
2018-03-09 13:26:42.169  INFO 13652 --- [a-EvictionTimer] c.n.e.registry.AbstractInstanceRegistry  : Running the evict task with compensationTime 0ms
PATH:
AbstractInstanceRegistry : com.netflix.eureka:eureka-core 1.6.2
#跟代码
com.netflix.eureka.registry.AbstractInstanceRegistry.register(InstanceInfo registrant, int leaseDuration, boolean isReplication)//注册信息,注册信息持续时间 默认120,是否复制
1.加同步锁读取锁
2.在该类的常量registry(ConcurrentHashMap<String, Map<String, Lease<InstanceInfo>>>)根据取服务名(spring:  application:  name: eureka )获取所有注册机( rtn:Map<String, Lease<InstanceInfo>> gMap)
3.=====(监听)EurekaMonitors.REGISTER.increment(isReplication);  监听注册服务的个数,增加服务计数(EurekaMonitors的myZoneCounter属性),isReplication为falses时才进行计数增长。//主要是对 AtomicLong[extends Number implements java.io.Serializable]内的unsafe常量
4.判断2是否获取到有效eureka服务,若没有则
if (gMap == null) {
    ConcurrentHashMap<String, Lease<InstanceInfo>> gNewMap = new ConcurrentHashMap();
    gMap = (Map)this.registry.putIfAbsent(registrant.getAppName(), gNewMap);
    if (gMap == null) {
        gMap = gNewMap;
    }
}
5.获取existingLease(生存周期)信息,从2返回的map内,根据注册信息(instanceId[16101164-15-PC:eureka:8001]或eureka.instance.hostName[eureka1]),Lease<InstanceInfo> existingLease = (Lease)((Map)gMap).get(registrant.getId());
if (existingLease != null && existingLease.getHolder() != null) {
    Long existingLastDirtyTimestamp = ((InstanceInfo)existingLease.getHolder()).getLastDirtyTimestamp();
    Long registrationLastDirtyTimestamp = registrant.getLastDirtyTimestamp();
    logger.debug("Existing lease found (existing={}, provided={}", existingLastDirtyTimestamp, registrationLastDirtyTimestamp);
    if (existingLastDirtyTimestamp > registrationLastDirtyTimestamp) {
        logger.warn("There is an existing lease and the existing lease's dirty timestamp {} is greater than the one that is being registered {}", existingLastDirtyTimestamp, registrationLastDirtyTimestamp);
        logger.warn("Using the existing instanceInfo instead of the new instanceInfo as the registrant");
        registrant = (InstanceInfo)existingLease.getHolder();
    }
} else {
    Object var6 = this.lock;
    synchronized(this.lock) {
        if (this.expectedNumberOfRenewsPerMin > 0) {
            this.expectedNumberOfRenewsPerMin += 2;
            this.numberOfRenewsPerMinThreshold = (int)((double)this.expectedNumberOfRenewsPerMin * this.serverConfig.getRenewalPercentThreshold());
        }
    }

    logger.debug("No previous lease information found; it is new registration");
}
Lease<InstanceInfo> lease = new Lease(registrant, leaseDuration);
if (existingLease != null) {
        lease.setServiceUpTimestamp(existingLease.getServiceUpTimestamp());
}
6.获取最近注册队列recentRegisteredQueue【AbstractInstanceRegistry.CircularQueue<Pair<Long, String>>】,
对队列加锁,将待注册的服务信息注册(当前系统时间戳 ,当前待注册服务名eg:ees-hotel + id(instanceId/hostName)),
synchronized(this.recentRegisteredQueue) {
    this.recentRegisteredQueue.add(new Pair(System.currentTimeMillis(), registrant.getAppName() + "(" + registrant.getId() + ")"));
}

7.注册信息的overriddenStatus状态不是InstanceStatus.UNKNOWN时,将当前服务注册信息内的此状态维护到overriddenInstanceStatusMap,key为id.
8.将overriddenInstanceStatusMap内的OverriddenStatus同步到注册信息registrant内,当overriddenStatusFromMap内存在此状态时。
9.当注册信息registrant的状态为InstanceStatus.UP时,更新lease的存活【租约】时间,服务开始时间为当前时间戳。
10.注册信息的actionType设置为在发现服务中添加,将当前注册的服务添加到服务发现内。
11.将新注册(更改)的服务信息(lease)添加到最近更改队列(recentlyChangedQueue)
12.设置新注册的服务的最后更新时间
13.this.invalidateCache(registrant.getAppName(), registrant.getVIPAddress(), registrant.getSecureVipAddress());  使缓存无效
缓存使用的是Google的private final LoadingCache<Key, ResponseCacheImpl.Value> readWriteCacheMap; 该接口继承Cache,存在invalidate抽象方法,实际调用LocalCache.LocalManualCache.invalidate,如下
public void invalidate(Object key) {
    Preconditions.checkNotNull(key);
    this.localCache.remove(key);
}
其中使用Preconditions的checkNotNull做非空校验
最后在final LocalCache<K, V> localCache;内删除缓存的信息。
LoadingCache extends Cache,Function 
LocalCache implements Cache
LocalManualCache implements Cache
Manual内操作

此处删除缓存是循环操作,在初次启动时会删除
this.invalidate(new Key(EntityType.Application, appName, type, v, EurekaAccept.full), new Key(EntityType.Application, appName, type, v, EurekaAccept.compact), new Key(EntityType.Application, "ALL_APPS", type, v, EurekaAccept.full), new Key(EntityType.Application, "ALL_APPS", type, v, EurekaAccept.compact), new Key(EntityType.Application, "ALL_APPS_DELTA", type, v, EurekaAccept.full), new Key(EntityType.Application, "ALL_APPS_DELTA", type, v, EurekaAccept.compact));

ResponseCacheImpl构造方法会初始化readWriteCacheMap,initialCapacity=1000
this.readWriteCacheMap = CacheBuilder.newBuilder().initialCapacity(1000).expireAfterWrite(serverConfig.getResponseCacheAutoExpirationInSeconds(), TimeUnit.SECONDS).removalListener(new RemovalListener<Key, ResponseCacheImpl.Value>() {
    public void onRemoval(RemovalNotification<Key, ResponseCacheImpl.Value> notification) {
        Key removedKey = (Key)notification.getKey();
        if (removedKey.hasRegions()) {
            Key cloneWithNoRegions = removedKey.cloneWithoutRegions();
            ResponseCacheImpl.this.regionSpecificKeys.remove(cloneWithNoRegions, removedKey);
        }

    }
}).build(new CacheLoader<Key, ResponseCacheImpl.Value>() {
    public ResponseCacheImpl.Value load(Key key) throws Exception {
        if (key.hasRegions()) {
            Key cloneWithNoRegions = key.cloneWithoutRegions();
            ResponseCacheImpl.this.regionSpecificKeys.put(cloneWithNoRegions, key);
        }

        ResponseCacheImpl.Value value = ResponseCacheImpl.this.generatePayload(key);
        return value;
    }
});

未完待续

最后

以上就是斯文棒球为你收集整理的springboot eureka 注册服务探索的全部内容,希望文章能够帮你解决springboot eureka 注册服务探索所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(99)

评论列表共有 0 条评论

立即
投稿
返回
顶部