本文整理汇总了Java中org.apache.curator.x.discovery.ServiceInstance类的典型用法代码示例。如果您正苦于以下问题:Java ServiceInstance类的具体用法?Java ServiceInstance怎么用?Java ServiceInstance使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ServiceInstance类属于org.apache.curator.x.discovery包,在下文中一共展示了ServiceInstance类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: onInstanceAdded
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
@Override
protected void onInstanceAdded(ServiceInstance<RpcPayload> instance) {
if (!this.instances.contains(instance)) {
if (instance.getPayload() == null) {
this.instances.offer(instance);
} else {
int count = (int) instance.getPayload().getMaxQps() / BASE_NUM;
count++;
LOG.info("Max qps :" + instance.getPayload().getMaxQps()
+ " Count" + count);
for (int i = 0; i < count; i++) {
this.instances.offer(instance);
}
}
LOG.info("Add an instance to pool: " + instance);
this.failedCount.put(instance, new AtomicInteger(0));
}
}
示例2: start
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
@Override
public void start() throws Exception {
this.instances = new ArrayBlockingQueue<ServiceInstance<RpcPayload>>(
capacity);
this.failedCount = CacheBuilder.newBuilder().concurrencyLevel(4)
.weakKeys().maximumSize(MAX_SERVICE_INSTANCES)
.expireAfterWrite(failedTestSpan, TimeUnit.MILLISECONDS)
.build();
this.failedCountCallable = new Callable<AtomicInteger>() {
@Override
public AtomicInteger call() throws Exception {
return new AtomicInteger(0);
}
};
super.start();
}
示例3: create
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
@Override
public TTransport create(ServiceInstance<RpcPayload> instance)
throws Exception {
TTransport transport = this.createNativeTransport(instance);
try {
transport.open();
} catch (TException ex) {
LOG.warn(
"Error when creating new transport on server: "
+ instance.getAddress() + ":" + instance.getPort(),
ex);
markError(instance);
throw ex;
}
return new ManagedTransport(transport, instance);
}
示例4: createNativeTransport
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
/**
* 根据rc的设置来确定创建什么类型的transport;
*
* @param instance
* @return
*/
protected TTransport createNativeTransport(
ServiceInstance<RpcPayload> instance) {
TSocket socket = new TSocket(instance.getAddress(), instance.getPort());
socket.setTimeout(socketTimeout);
RpcPayload server = instance.getPayload();
if ((server == null) || (server.getTransport() == null)
|| (server.getTransport().equals("socket"))) {
return socket;
} else if ("framed-transport".equals(server.getTransport())) {
return new TFramedTransport(socket);
}
// for default, use TSocket;
return socket;
}
示例5: registerResource
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
private ServiceInstance<String> registerResource(final RegisteredService registeredService,
final ServiceDiscovery<String> serviceDiscovery) {
try {
UriSpec uriSpec = new UriSpec("{scheme}://{address}:{port}" +
ResourcePaths.ROOT_PATH +
registeredService.getVersionedPath());
ServiceInstance<String> serviceInstance = ServiceInstance.<String>builder()
.serviceType(ServiceType.DYNAMIC) //==ephemeral zk nodes so instance will disappear if we lose zk conn
.uriSpec(uriSpec)
.name(registeredService.getVersionedServiceName(stroomPropertyService))
.address(advertisedHostNameOrIpAddress)
.port(advertisedPort)
.build();
LOGGER.info("Attempting to register '{}' with service discovery at {}",
registeredService.getVersionedServiceName(stroomPropertyService), serviceInstance.buildUriSpec());
Preconditions.checkNotNull(serviceDiscovery).registerService(serviceInstance);
LOGGER.info("Successfully registered '{}' service.", registeredService.getVersionedServiceName(stroomPropertyService));
return serviceInstance;
} catch (Exception e) {
throw new RuntimeException("Failed to register service " + registeredService.getVersionedServiceName(stroomPropertyService), e);
}
}
示例6: getServiceInstance
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
private static ServiceInstance<MetaData> getServiceInstance(
String serviceName,
int servicePort,
String serviceAddress,
Map<String, String> parameters) throws Exception {
ServiceInstanceBuilder<MetaData> builder = ServiceInstance.builder();
// Address is optional. The Curator library will automatically use the IP from the first
// ethernet device
String registerAddress = (serviceAddress == null) ? builder.build().getAddress() : serviceAddress;
MetaData metadata = new MetaData(UUID.randomUUID(), registerAddress, servicePort, serviceName);
metadata.setParameters(parameters);
builder.name(serviceName).payload(metadata).id(registerAddress + ":" +
String.valueOf(servicePort)).serviceType(ServiceType.DYNAMIC).address(registerAddress).port(servicePort);
return builder.build();
}
示例7: unregister
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
@Override
public void unregister(RegistrationHandle handle) {
if (!(handle instanceof ZKRegistrationHandle)) {
throw new UnsupportedOperationException("Unknown handle type: " + handle.getClass().getName());
}
// when Drillbit is unregistered, clean all the listeners registered in CC.
this.listeners.clear();
ZKRegistrationHandle h = (ZKRegistrationHandle) handle;
try {
ServiceInstance<DrillbitEndpoint> serviceInstance = ServiceInstance.<DrillbitEndpoint>builder()
.address("")
.port(0)
.id(h.id)
.name(serviceName)
.build();
discovery.unregisterService(serviceInstance);
} catch (Exception e) {
propagate(e);
}
}
示例8: eventFromClient
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
private void eventFromClient(List<String> paths, List<ServiceInstance<T>> serviceInstanceList, Action type) {
log.info("{} for {} ", type, paths);
switch (type) {
case INITIALIZED:
syncBackup(paths);
break;
default:
String path = paths.get(0);
String IPv6 = null;
String weight = null;
int nameIndex = path.lastIndexOf(RedirectorConstants.DELIMETER);
String id = StringUtils.substring(path, nameIndex + 1);
for (ServiceInstance<T> instance : serviceInstanceList) {
if (instance.getId().equals(id)) {
IPv6 = gedIPV6FromInstance(instance);
weight = getWeightFromInstance(instance);
break;
}
}
writeBackup(path, IPv6, type, weight);
}
}
示例9: getCountOfHostsForPath
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
@Override
public int getCountOfHostsForPath(String path, ServerLookupMode mode){
IFilteredServiceProvider provider = getServiceProvider(path);
if (provider != null) {
FilterMode filterMode = mode.getFilterMode();
Collection<ServiceInstance<MetaData>> instances;
if (filterMode == FilterMode.NO_FILTER) {
instances = provider.getAllInstances();
} else {
instances = provider.getAllFilteredInstances(filterMode, whiteListedStacksManager);
}
if (instances != null) {
return StackServiceProvider.UniqueInstancesExtractor.extract(instances).size();
}
}
return 0;
}
示例10: getTransportClient
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
@Override
TransportClient getTransportClient(RpcRequest request) {
try {
Collection<ServiceInstance<MetaInfo>> servers = discovery.queryForInstances(serviceName);
if(servers != null && servers.size() > 0){
ServerInfo serverInfo = select(servers, request);
if(serverInfo == null){
throw new RpcException("no provider service selected");
}
return pool.getObject(serverInfo);
}else{
throw new RpcException("no provider services");
}
} catch (Exception e) {
logger.error("get transport client error:", e);
throw new RpcException(e);
}
}
示例11: getStackBackup
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
public static StackBackup getStackBackup(List<IFilteredServiceProvider> allProviders, int version) {
List<StackSnapshot> snapshot = new ArrayList<>(allProviders.size());
for (IFilteredServiceProvider provider : allProviders) {
List<StackSnapshot.Host> hosts = new ArrayList<>();
for (ServiceInstance<MetaData> instance : provider.getAllUniqueInstances()) {
StackSnapshot.Host host = new StackSnapshot.Host(
getAddress(instance, IpProtocolVersion.IPV4),
getAddress(instance, IpProtocolVersion.IPV6),
getWeightFromPayload(instance));
hosts.add(host);
}
snapshot.add(new StackSnapshot(provider.getStack().getPath(), hosts));
}
return new StackBackup(version, snapshot);
}
示例12: testGetInstance
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
@Test
public void testGetInstance() {
ServiceInstance<MetaData> serviceProvider;
Map<String, AtomicLong> result = new HashMap<>();
for (int i = 0; i <= 10001; i++) {
serviceProvider = provider.getInstance();
if (result.containsKey(serviceProvider.getId())) {
result.get(serviceProvider.getId()).incrementAndGet();
} else {
result.put(serviceProvider.getId(), new AtomicLong(0));
}
}
long firstIp = result.get("10.10.20.10").get();
Assert.assertTrue("10.10.20.10", firstIp >= 4990 && firstIp <= 5010);
long secondIp = result.get("10.10.20.20").get();
Assert.assertTrue("10.10.20.20", secondIp >= 4990 && secondIp <= 5010);
Assert.assertEquals(2, provider.getAllUniqueInstances().size());
}
示例13: serialize
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
@Override
public byte[] serialize(ServiceInstance<RpcPayload> instance)
throws Exception {
Map<String, Object> map = PropertyUtils.describe(instance);
map.put("uriSpec", this.serializeUriSpec(instance.getUriSpec()));
map.remove("class");
String jsonStr = this.toJson(map);
return jsonStr.getBytes();
}
示例14: onInstanceRemoved
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
@Override
protected void onInstanceRemoved(ServiceInstance<RpcPayload> instance) {
while (instances.contains(instance)) {
this.instances.remove(instance);
LOG.info("Instance removed from pool : " + instance);
}
this.failedCount.invalidate(instance);
}
示例15: exportService
import org.apache.curator.x.discovery.ServiceInstance; //导入依赖的package包/类
public void exportService(Class<?> interfaceClass, Object impl, int port){
providers.put(interfaceClass.getName(), new DefaultServiceProvider(interfaceClass, impl));
if(serviceDiscovery != null){
try {
ServiceInstance<MetaInfo> serviceInstance = ServiceInstance.<MetaInfo>builder()
.name(interfaceClass.getName())
.address(NetUtils.getLocalAddress().getHostAddress())
.port(port)
.build();
serviceDiscovery.registerService(serviceInstance);
} catch (Exception e) {
throw new RpcException("注册服务失败");
}
}
}