# discovery **Repository Path**: bertli/discovery ## Basic Information - **Project Name**: discovery - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2016-05-13 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## disco 源码分析 源码来自:https://github.com/librato/disco-java.git discovery 服务端向zk注册自己的信息(包括ip等),客户端能够动态干知道变化 最好的抽象是: key=/xx/ip,value=json字符串,具体的model可以由自己发挥。 ## model分析 ### common 1. Node,描述一个节点,以及你想描述的数据 ### for client 1. SelectorStrategy, 客户端选取一个节点的策略 BackoffSelectorStrategy,RandomSelectorStrategy,RoundRobinSelectorStrategy 2. IExpireStrategy 过期策略 3. Decoder 数据解码,由具体的使用者自己实现 4. DiscoClient 客户端,DiscoClientFactory 5. ILevel2CacheStrategy ### for server 1. DiscoService As long as the service is running, this configuration will be associated with the Zookeeper node `/services/myservice/nodes/hostname:4321` and the byte[] payload as the node's data. ## 思路 他这个zkclient包的比较多,client输入很多东西(包括缓存,selector策略等),然后getServiceNode 从getServiceNode开始倒推,getServiceNode直观上获取一个childdata对象(curator),将其转换为Node{host,port,payload}对象返回。(这个转换过程,作者也用guavacache加了一个缓存,缓存转换结果(值得学习)) 要花时间学习下curator,http://macrochen.iteye.com/blog/1366136 那么现在问题变成了getServiceNode通过nextChildData拿到下一个节点,逻辑 1. 先从缓存中取出当前所有的节点列表 2. 根据选择策略选一个 通过curator提供的PathChildrenCache,可以得到一个List,返回/services/myservice/nodes目录下的所有子节点 但这个cache直接拿到后,作者好像不打算直接用,提供了一个Level2StateCache(当然可以设置一下不用这个level2StateCache) ## curator Path Cache Path Cache用于监听ZNode的子节点的变化, 当add, update, remove子节点时将改变Path Cache state, 同时返回所有子节点的data和state. Curator中采用了PathChildrenCache类来处理Path Cache, 状态的变化则采用PathChildrenCacheListener来监听. 相关用法参见TestPathChildrenCache测试类 ## guava 学习 其中一些技巧guava 1. optional,用来包装一个对象,强迫你考虑这个对象是否可以是null值。或者说`re method(a,b)`,你返回的re可能为null,也可能不为null。如果`Optional method(a,b)`,那么调用者`option.get()`拿返回值,如果返回值为空时会发生异常。如果调用这可以忍受这个值为null,那么可以事先`optional.isPresent`判断下。也就说,optional逼着用户显式的确定这个值是否可以为null http://www.cnblogs.com/peida/archive/2013/06/14/Guava_Optional.html 2. function,提供一个将一个对象转成成另一个对象的接口 3. Preconditions,优雅的检验参数,类似于checknotnull 1.Guava学习笔记:Google Guava 类库简介 2.Guava学习笔记:Optional优雅的使用null 3.Guava学习笔记:Preconditions优雅的检验参数 4.Guava学习笔记:复写的Object常用方法 5.Guava学习笔记:简化异常处理的Throwables类 6.Guava学习笔记:Immutable(不可变)集合 7.Guava学习笔记:Guava新增集合类型-Multiset 8.Guava学习笔记:Guava新增集合类型-Multimap 9.Guava学习笔记:Guava新增集合类型-Bimap 10.Guava学习笔记:Guava新集合-Table等 11.Guava学习笔记:Guava cache 12.Guava学习笔记:EventBus 13.Guava学习笔记:Range