前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >聊聊nacos的DelegateConsistencyServiceImpl

聊聊nacos的DelegateConsistencyServiceImpl

原创
作者头像
code4it
修改2019-09-09 14:13:49
7100
修改2019-09-09 14:13:49
举报
文章被收录于专栏:码匠的流水账码匠的流水账

本文主要研究一下nacos的DelegateConsistencyServiceImpl

ConsistencyService

nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/ConsistencyService.java

代码语言:javascript
复制
public interface ConsistencyService {
​
    /**
     * Put a data related to a key to Nacos cluster
     *
     * @param key   key of data, this key should be globally unique
     * @param value value of data
     * @throws NacosException
     * @see
     */
    void put(String key, Record value) throws NacosException;
​
    /**
     * Remove a data from Nacos cluster
     *
     * @param key key of data
     * @throws NacosException
     */
    void remove(String key) throws NacosException;
​
    /**
     * Get a data from Nacos cluster
     *
     * @param key key of data
     * @return data related to the key
     * @throws NacosException
     */
    Datum get(String key) throws NacosException;
​
    /**
     * Listen for changes of a data
     *
     * @param key      key of data
     * @param listener callback of data change
     * @throws NacosException
     */
    void listen(String key, RecordListener listener) throws NacosException;
​
    /**
     * Cancel listening of a data
     *
     * @param key      key of data
     * @param listener callback of data change
     * @throws NacosException
     */
    void unlisten(String key, RecordListener listener) throws NacosException;
​
    /**
     * Tell the status of this consistency service
     *
     * @return true if available
     */
    boolean isAvailable();
}
  • ConsistencyService定义了put、remove、get、listen、unlisten、isAvailable方法

DelegateConsistencyServiceImpl

nacos-1.1.3/naming/src/main/java/com/alibaba/nacos/naming/consistency/DelegateConsistencyServiceImpl.java

代码语言:javascript
复制
@Service("consistencyDelegate")
public class DelegateConsistencyServiceImpl implements ConsistencyService {
​
    @Autowired
    private PersistentConsistencyService persistentConsistencyService;
​
    @Autowired
    private EphemeralConsistencyService ephemeralConsistencyService;
​
    @Override
    public void put(String key, Record value) throws NacosException {
        mapConsistencyService(key).put(key, value);
    }
​
    @Override
    public void remove(String key) throws NacosException {
        mapConsistencyService(key).remove(key);
    }
​
    @Override
    public Datum get(String key) throws NacosException {
        return mapConsistencyService(key).get(key);
    }
​
    @Override
    public void listen(String key, RecordListener listener) throws NacosException {
​
        // this special key is listened by both:
        if (KeyBuilder.SERVICE_META_KEY_PREFIX.equals(key)) {
            persistentConsistencyService.listen(key, listener);
            ephemeralConsistencyService.listen(key, listener);
            return;
        }
​
        mapConsistencyService(key).listen(key, listener);
    }
​
    @Override
    public void unlisten(String key, RecordListener listener) throws NacosException {
        mapConsistencyService(key).unlisten(key, listener);
    }
​
    @Override
    public boolean isAvailable() {
        return ephemeralConsistencyService.isAvailable() && persistentConsistencyService.isAvailable();
    }
​
    private ConsistencyService mapConsistencyService(String key) {
        return KeyBuilder.matchEphemeralKey(key) ? ephemeralConsistencyService : persistentConsistencyService;
    }
}
  • DelegateConsistencyServiceImpl实现了ConsistencyService接口;其put、remove、get、listen、unlisten方法内部都使用了mapConsistencyService来判断是使用ephemeralConsistencyService还persistentConsistencyService;其isAvailable方法要求ephemeralConsistencyService及persistentConsistencyService都是available

小结

ConsistencyService定义了put、remove、get、listen、unlisten、isAvailable方法;DelegateConsistencyServiceImpl实现了ConsistencyService接口;其put、remove、get、listen、unlisten方法内部都使用了mapConsistencyService来判断是使用ephemeralConsistencyService还persistentConsistencyService;其isAvailable方法要求ephemeralConsistencyService及persistentConsistencyService都是available

doc

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ConsistencyService
  • DelegateConsistencyServiceImpl
  • 小结
  • doc
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档