dubbo Reference 注解属性 version

以前没有怎么关注Reference注解的version字段,理解也就是个service版本号。

但是当在service多实现的时候version的重要就体现出来了,version就是区别不同实现的key,因为dubbo内部远程服务的缓存key就是由version构成。(以下代码都来自重新更新的阿里维护的dubbo版本)

private Object buildReferenceBean(Reference reference, Class<?> referenceClass) throws Exception {

    String referenceBeanCacheKey = generateReferenceBeanCacheKey(reference, referenceClass);

    ReferenceBean<?> referenceBean = referenceBeansCache.get(referenceBeanCacheKey);

    if (referenceBean == null) {

        ReferenceBeanBuilder beanBuilder = ReferenceBeanBuilder
                .create(reference, classLoader, applicationContext)
                .interfaceClass(referenceClass);

        referenceBean = beanBuilder.build();

        referenceBeansCache.putIfAbsent(referenceBeanCacheKey, referenceBean);

    }

    return referenceBean.get();
}


/**
 * Generate a cache key of {@link ReferenceBean}
 *
 * @param reference {@link Reference}
 * @param beanClass {@link Class}
 * @return
 */
private static String generateReferenceBeanCacheKey(Reference reference, Class<?> beanClass) {

    String interfaceName = resolveInterfaceName(reference, beanClass);

    String key = reference.group() + "/" + interfaceName + ":" + reference.version();

    return key;

}

从代码可以看出用做ReferenceBean缓存的map可以组成是由group,service名称,和版本号组成的,当然从url也可以看出。

所以当service多个实现启用dubbo的时候要么区分版本号要么给实现加名称(Service :value)区分。

如果不加以区分,从ConcurrentMap源码可以看到putIfAbsent方法的返回注释说明如果key对应的value存在则会返回该值,所以不区分会一直调用的是第一次获取到的ReferenceBean,多个service实现也就不存在了。

/**
 * If the specified key is not already associated
 * with a value, associate it with the given value.
 * This is equivalent to
 *  <pre> {@code
 * if (!map.containsKey(key))
 *   return map.put(key, value);
 * else
 *   return map.get(key);
 * }</pre>
 *
 * except that the action is performed atomically.
 *
 * @implNote This implementation intentionally re-abstracts the
 * inappropriate default provided in {@code Map}.
 *
 * @param key key with which the specified value is to be associated
 * @param value value to be associated with the specified key
 * @return the previous value associated with the specified key, or
 *         {@code null} if there was no mapping for the key.
 *         (A {@code null} return can also indicate that the map
 *         previously associated {@code null} with the key,
 *         if the implementation supports null values.)
 * @throws UnsupportedOperationException if the {@code put} operation
 *         is not supported by this map
 * @throws ClassCastException if the class of the specified key or value
 *         prevents it from being stored in this map
 * @throws NullPointerException if the specified key or value is null,
 *         and this map does not permit null keys or values
 * @throws IllegalArgumentException if some property of the specified key
 *         or value prevents it from being stored in this map
 */
 V putIfAbsent(K key, V value);

 

  
BugHome版权所有丨转载请注明出处:https://minei.me/archives/247.html
  

发表评论

电子邮件地址不会被公开。 必填项已用*标注