11.Curator扩展库
1.curator-x-discovery介绍
base path
|_______ service A name
|__________ instance 1 id --> (serialized ServiceInstance)
|__________ instance 2 id --> (serialized ServiceInstance)
|__________ ...
|_______ service B name
|__________ instance 1 id --> (serialized ServiceInstance)
|__________ instance 2 id --> (serialized ServiceInstance)
|__________ ...
|_______ ...

/**
* Return an instance for a single use. <b>IMPORTANT: </b> users
* should not hold on to the instance returned. They should always get a fresh instance.
*
* @return the instance to use
* @throws Exception any errors
*/
public ServiceInstance<T> getInstance() throws Exception;
/**
* Return the current available set of instances <b>IMPORTANT: </b> users
* should not hold on to the instance returned. They should always get a fresh list.
*
* @return all known instances
* @throws Exception any errors
*/
public Collection<ServiceInstance<T>> getAllInstances() throws Exception;


2.低级别的API介绍
/**
* Register/re-register a service 注册服务
*
* @param service service to add
* @throws Exception errors
*/
public void registerService(ServiceInstance<T> service) throws Exception;
/**
* Unregister/remove a service instance 取消注册服务
*
* @param service the service
* @throws Exception errors
*/
public void unregisterService(ServiceInstance<T> service) throws Exception;
/**
* Return the names of all known services
*
* @return list of service names
* @throws Exception errors
*/
public Collection<String> queryForNames() throws Exception;
/**
* Return all known instances for the given service
*
* @param name name of the service
* @return list of instances (or an empty list)
* @throws Exception errors
*/
public Collection<ServiceInstance<T>> queryForInstances(String name) throws Exception;
/**
* Return a service instance POJO
*
* @param name name of the service
* @param id ID of the instance
* @return the instance or <code>null</code> if not found
* @throws Exception errors
*/
public ServiceInstance<T> queryForInstance(String name, String id) throws Exception;
/**
* Return the current list of instances. NOTE: there is no guarantee of freshness. This is
* merely the last known list of instances. However, the list is updated via a ZooKeeper watcher
* so it should be fresh within a window of a second or two.
*
* @return the list
*/
public List<ServiceInstance<T>> getInstances();
/**
* Listener for changes to a service cache
*/
public interface ServiceCacheListener extends ConnectionStateListener
{
/**
* Called when the cache has changed (instances added/deleted, etc.)
*/
public void cacheChanged();
}
3.curator-x-discovery使用实例
@JsonRootName("details")
public class InstanceDetails
{
/** 服务说明信息 */
private String description;
public InstanceDetails(String description)
{
this.description = description;
}
public void setDescription(String description)
{
this.description = description;
}
public String getDescription()
{
return description;
}
@Override
public String toString()
{
return "InstanceDetails [description=" + description + "]";
}
}
public class ExampleServer implements Closeable
{
private final ServiceDiscovery<InstanceDetails> serviceDiscovery;//发现服务的实例类
private final ServiceInstance<InstanceDetails> thisInstance;//服务注册信息实例
public ExampleServer(CuratorFramework client, String path, String serviceName, String description) throws Exception
{
UriSpec uriSpec = new UriSpec("{scheme}://foo.com:{port}");
thisInstance = ServiceInstance.<InstanceDetails>builder()
.name(serviceName)
.payload(new InstanceDetails(description))
.port(12345)
.uriSpec(uriSpec)
.build();
JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class);
serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class)
.client(client)
.basePath(path)
.serializer(serializer)
.thisInstance(thisInstance)
.build();
}
public ServiceInstance<InstanceDetails> getThisInstance()
{
return thisInstance;
}
public void start() throws Exception
{
serviceDiscovery.start();
}
@Override
public void close() throws IOException
{
CloseableUtils.closeQuietly(serviceDiscovery);
}
}
public class DiscoveryExample
{
public static void main(String[] args) throws Exception
{
String basePath = "/discoverys";
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new RetryNTimes(10, 5000));
client.start();
ExampleServer exampleServer = new ExampleServer(client, basePath, "serviceName01", "服务说明");
exampleServer.start();
getAllService();
System.out.println("服务已启动!输入回车关闭服务:");
in.readLine();
exampleServer.close();
getAllService();
System.out.println("服务已关闭!输入回车关闭连接:");
in.readLine();
client.close();
System.out.println("连接已关闭!");
System.out.println("OK!");
}
//遍历所有服务实例
private static void getAllService() throws Exception
{
CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", new RetryNTimes(10, 5000));
client.start();
JsonInstanceSerializer<InstanceDetails> serializer = new JsonInstanceSerializer<InstanceDetails>(InstanceDetails.class);
ServiceDiscovery<InstanceDetails> serviceDiscovery = ServiceDiscoveryBuilder.builder(InstanceDetails.class)
.client(client)
.basePath("/discoverys")
.serializer(serializer)
.build();
serviceDiscovery.start();
Collection<String> serviceNames = serviceDiscovery.queryForNames();// 获取所有的服务名称
for(String serviceName : serviceNames)
{
// 获取所有相同服务名称的所有服务实例
Collection<ServiceInstance<InstanceDetails>> instances = serviceDiscovery.queryForInstances(serviceName);
System.out.println(serviceName);
for (ServiceInstance<InstanceDetails> instance : instances)
{
System.out.println("\t" + instance);
}
}
serviceDiscovery.close();
client.close();
}
}
serviceName01
ServiceInstance{name='serviceName01', id='bb766181-4e7a-423a-974a-8ac840a9ea08', address='192.168.4.232', port=12345, sslPort=null, payload=InstanceDetails [description=服务说明], ...}
ServiceInstance{name='serviceName01', id='298c5dc6-c431-41cb-8e74-6abbefecd2ee', address='192.168.4.232', port=12345, sslPort=null, payload=InstanceDetails [description=服务说明], ...}
服务已启动!输入回车关闭服务:
serviceName01
ServiceInstance{name='serviceName01', id='298c5dc6-c431-41cb-8e74-6abbefecd2ee', address='192.168.4.232', port=12345, sslPort=null, payload=InstanceDetails [description=服务说明], ...}
服务已关闭!输入回车关闭连接:
连接已关闭!
OK!
base path
|_______ service A name
|__________ instance 1 id --> (serialized ServiceInstance)
|__________ instance 2 id --> (serialized ServiceInstance)
|__________ ...
|_______ service B name
|__________ instance 1 id --> (serialized ServiceInstance)
|__________ instance 2 id --> (serialized ServiceInstance)
|__________ ...
|_______ ...
4.其他扩展介绍
-------------------------------------------------------------------------------------------------------------------------------
11.Curator扩展库的更多相关文章
- PHP mysqli 扩展库(面向对象/数据库操作封装/事务控制/预编译)
1.和mysql扩展库的区别: (1 安全性.稳定性更高 (2 提供了面向对象和面向过程两种风格 2.php.ini 中的 extension=php_mysqli.dll 解除封印 3.面 ...
- PHP基础Mysql扩展库
mysql扩展库操作步骤如下: 1.连接数据库 2.选择数据库 3.设置操作编码 4.发送指令sql,并返回结果集 ddl:数据定义语句 dml:数据操作语句 dql:数据查询 ...
- centos下不重装php——给PHP添加新扩展库
装完php.发现需要一些新扩展库比如常见的mysqli之类的.在不重装php安装新扩展,以一个不常用的库xsl为例. 环境:centos6.8,php5.3.29 ,osx10.11.6 我的php相 ...
- php mysqli扩展库之预处理操作
分享下php使用mysqli扩展库进行预处理操作的二个例子,有意研究mysqli用法的朋友,可以参考学习下,一定会有所帮助的. 例1.使用mysqli扩展库的预处理技术 mysqli stmt 向数据 ...
- 树莓派安装opencv3及其扩展库
https://www.cnblogs.com/Pyrokine/p/8921285.html 目标编译针对python的opencv以及扩展库 环境树莓派4和3B+都可以python3.7.3 py ...
- Layman 使用ffmpeg-php扩展库实现视频截图(默认图)
这几天做项目,其中一个需求是用户上传视频文件到服务器,然后服务器自动截取该视频的一帧作为该视频对应的缩略图,服务器端语言采用php编写,找了半天资料,发现ffmpeg-php可以满足该需求,所以下面简 ...
- PHP的SPL扩展库(一)数据结构
SPL 库也叫做 PHP 标准库,主要就是用于解决典型问题的一组接口或类的集合.这些典型问题包括什么呢?比如我们今天要讲的数据结构,还有一些设计模式的实现,就像我们之前讲过的观察者模式相关的接口在 S ...
- Z.ExtensionMethods 一个强大的开源扩展库
今天有意的在博客园里面搜索了一下 Z.ExtensionMethods 这个扩展类库,确发现只搜到跟这个真正相关的才两篇博文而已,我都点进去看了一下,也都只是提到而已,没有专门介绍,才引起我写这篇文档 ...
- PHP使用mysqli扩展库实现增删改查(面向对象版)
mysqli扩展库是mysql扩展库的改进版本,在mysql扩展库的基础上提高了稳定性和效率,mysqli扩展库有两套东西,一套就是面向过程的mysqli另一套是面向对象的mysqli.操作方式大体和 ...
随机推荐
- 常用音频软件:Cool edit pro
作者:桂. 时间:2017-06-02 11:51:08 链接:http://www.cnblogs.com/xingshansi/p/6932671.html 这里只涉及Cool edit pro ...
- django 使用post方法出现403错误的解决办法
当采用客户端象django的服务器提交post请求时.会得到403,权限异常.因为django针对提交的请教,有校验.所以会如此. 解决办法: 导入模块:from django.views.decor ...
- Makefile学习之路6——让编译环境更加有序
在大多项目中都会合理设计目录结构来提高维护性,在编译一个项目时会产生大量中间文件,如果中间文件直接和源文件放在一起,就显得杂乱而不利于维护.在为现在这个complicated项目编写makefile之 ...
- Unity3D学习(十一):关于UI销毁后图集仍然无法释放问题的解决办法
前言 最近进行项目性能优化的时候发现的问题. 问题 从大厅进到单局的过程中,会经过选择英雄和加载两个流程,这两个流程对应的UI界面都会有一张几mb左右的贴图作为背景,在进入单局游戏后这两个UI已经销毁 ...
- Java compiler level does not match the version of the installed Java project facet 的解决方案
今天将MyEclipse升级到 9.1 后,打开原来的工作空间,原来所有的项目都前面都显示了一个小叉叉,代码中却没有任何错误.于从 problems 视图中查看错误信息,错误信息的"D ...
- python学习笔记(1)--遍历txt文件,正则匹配替换文字
遍历一个文件夹,把里面所有txt文件里的[]里的朗读时间删除,也就是替换为空. import os import re import shutil #os文件操作,re正则,shutil复制粘贴 pa ...
- 在ORACLE中如何将一个表中某字段值合计与另一个表的某字段值相减
现在有两个表,A表字段AMOUNT为发票金额,B表字段REV为收款金额,两表通过字段id关联,需将A表的字段AMOUNT与B表的字段REV相减, 但是A表表示的发票可能对应多个B表的收款金额,如何将A ...
- FragmentTabHost的应用
原创)FragmentTabHost的应用(fragment学习系列文章之二) 时间 2014-04-14 00:11:46 CSDN博客 原文 http://blog.csdn.net/flyi ...
- C语言 · 大数加法
算法提高 大数加法 时间限制:1.0s 内存限制:256.0MB 问题描述 输入两个正整数a,b,输出a+b的值. 输入格式 两行,第一行a,第二行b.a和b的长度均小于1000位. ...
- 离线 + 位优化 - SGU 108 Self-numbers 2
SGU 108 Self-numbers 2 Problem's Link Mean: 略有这样一种数字:对于任意正整数n,定义d(n)为n加上n的各个位上的数字(d是数字的意思,Kaprekar发明 ...