本文整理匯總了Java中com.twitter.thrift.ServiceInstance類的典型用法代碼示例。如果您正苦於以下問題:Java ServiceInstance類的具體用法?Java ServiceInstance怎麽用?Java ServiceInstance使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ServiceInstance類屬於com.twitter.thrift包,在下文中一共展示了ServiceInstance類的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: watch
import com.twitter.thrift.ServiceInstance; //導入依賴的package包/類
/**
* Registers a monitor to receive change notices for this server set as long as this jvm process
* is alive. Blocks until the initial server set can be gathered and delivered to the monitor.
* The monitor will be notified if the membership set or parameters of existing members have
* changed.
*
* @param monitor the server set monitor to call back when the host set changes
* @throws MonitorException if there is a problem monitoring the host set
*/
public void watch(final ServerSetMonitor monitor)
throws MonitorException {
try {
serverSet.watch(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
@Override
public void onChange(ImmutableSet<ServiceInstance> serviceInstances) {
Set<DLSocketAddress> dlServers = Sets.newHashSet();
for (ServiceInstance serviceInstance : serviceInstances) {
Endpoint endpoint = serviceInstance.getAdditionalEndpoints().get("thrift");
InetSocketAddress inetAddr =
new InetSocketAddress(endpoint.getHost(), endpoint.getPort());
int shardId = resolvedFromName ? -1 : serviceInstance.getShard();
DLSocketAddress address = new DLSocketAddress(shardId, inetAddr);
dlServers.add(address);
}
monitor.onChange(ImmutableSet.copyOf(dlServers));
}
});
} catch (DynamicHostSet.MonitorException me) {
throw new MonitorException("Failed to monitor server set : ", me);
}
}
示例2: endpointAddressToServiceInstance
import com.twitter.thrift.ServiceInstance; //導入依賴的package包/類
private ServiceInstance endpointAddressToServiceInstance(Address endpointAddress) {
if (endpointAddress instanceof Address.Inet) {
InetSocketAddress inetSocketAddress = ((Address.Inet) endpointAddress).addr();
Endpoint endpoint = new Endpoint(inetSocketAddress.getHostString(), inetSocketAddress.getPort());
HashMap<String, Endpoint> map = new HashMap<String, Endpoint>();
map.put("thrift", endpoint);
return new ServiceInstance(
endpoint,
map,
Status.ALIVE);
} else {
logger.error("We expect InetSocketAddress while the resolved address {} was {}",
endpointAddress, endpointAddress.getClass());
throw new UnsupportedOperationException("invalid endpoint address: " + endpointAddress);
}
}
示例3: buildClient
import com.twitter.thrift.ServiceInstance; //導入依賴的package包/類
private LindenService.ServiceIface buildClient(String parent, String node) {
String nodePath = FilenameUtils.separatorsToUnix(FilenameUtils.concat(parent, node));
byte[] bytes = zkClient.readData(nodePath);
ServiceInstance serviceInstance = JSONObject.parseObject(new String(bytes), ServiceInstance.class);
String schema = String.format("%s:%s",
serviceInstance.getServiceEndpoint().getHost(),
serviceInstance.getServiceEndpoint().getPort());
return Thrift.newIface(schema, LindenService.ServiceIface.class);
}
示例4: hostSetToString
import com.twitter.thrift.ServiceInstance; //導入依賴的package包/類
private String hostSetToString(ImmutableSet<ServiceInstance> hostSet) {
StringBuilder result = new StringBuilder();
result.append("(");
for (ServiceInstance serviceInstance : hostSet) {
Endpoint endpoint = serviceInstance.getServiceEndpoint();
result.append(String.format(" %s:%d", endpoint.getHost(), endpoint.getPort()));
}
result.append(" )");
return result.toString();
}
示例5: watch
import com.twitter.thrift.ServiceInstance; //導入依賴的package包/類
/**
* Registers a monitor to receive change notices for this server set as long as this jvm process
* is alive. Blocks until the initial server set can be gathered and delivered to the monitor.
* The monitor will be notified if the membership set or parameters of existing members have
* changed.
*
* @param monitor the server set monitor to call back when the host set changes
* @return A command which, when executed, will stop monitoring the host set.
* @throws com.twitter.common.net.pool.DynamicHostSet.MonitorException if there is a problem monitoring the host set
*/
@Override
public Command watch(HostChangeMonitor<ServiceInstance> monitor) throws MonitorException {
// First add the monitor to the watchers so that it does not miss any changes and invoke
// the onChange method
synchronized (watchers) {
watchers.add(monitor);
}
if(resolutionPending.compareAndSet(false, false)) {
monitor.onChange(hostSet);
}
return Commands.NOOP; // Return value is not used
}
示例6: testInetNameResolution
import com.twitter.thrift.ServiceInstance; //導入依賴的package包/類
@Test(timeout = 10000)
public void testInetNameResolution() throws Exception {
String nameStr = "inet!127.0.0.1:3181";
final CountDownLatch resolved = new CountDownLatch(1);
final AtomicBoolean validationFailed = new AtomicBoolean(false);
NameServerSet serverSet = new NameServerSet(nameStr);
serverSet.watch(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
@Override
public void onChange(ImmutableSet<ServiceInstance> hostSet) {
if (hostSet.size() > 1) {
logger.error("HostSet has more elements than expected {}", hostSet);
validationFailed.set(true);
resolved.countDown();
} else if (hostSet.size() == 1) {
ServiceInstance serviceInstance = hostSet.iterator().next();
Endpoint endpoint = serviceInstance.getAdditionalEndpoints().get("thrift");
InetSocketAddress address = new InetSocketAddress(endpoint.getHost(), endpoint.getPort());
if (endpoint.getPort() != 3181) {
logger.error("Port does not match the expected port {}", endpoint.getPort());
validationFailed.set(true);
} else if (!address.getAddress().getHostAddress().equals("127.0.0.1")) {
logger.error("Host address does not match the expected address {}", address.getAddress().getHostAddress());
validationFailed.set(true);
}
resolved.countDown();
}
}
});
resolved.await();
Assert.assertEquals(false, validationFailed.get());
}
示例7: readServerSet
import com.twitter.thrift.ServiceInstance; //導入依賴的package包/類
protected static ImmutableSet<ServiceInstance> readServerSet(byte[] fileContent)
throws IOException {
ImmutableSet.Builder<ServiceInstance> builder = new ImmutableSet.Builder<ServiceInstance>();
InputStream stream = new ByteArrayInputStream(fileContent);
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
while (true) {
String line = reader.readLine();
if (line == null) {
// EOF.
break;
} else if (line.isEmpty()) {
// Skip empty lines.
continue;
}
// We expect each line to be of the form "hostname:port". Note that host names can
// contain ':' themselves (e.g. ipv6 addresses).
int index = line.lastIndexOf(':');
Preconditions.checkArgument(index > 0 && index < line.length() - 1);
String host = line.substring(0, index);
int port = Integer.parseInt(line.substring(index + 1));
builder.add(new ServiceInstance(
new Endpoint(host, port), // endpoint
Collections.<String, Endpoint>emptyMap(), // additional endpoints
Status.ALIVE)); // status
}
return builder.build();
}
示例8: respondToChanges
import com.twitter.thrift.ServiceInstance; //導入依賴的package包/類
private BoxedUnit respondToChanges(Addr addr) {
ImmutableSet<ServiceInstance> oldHostSet = ImmutableSet.copyOf(hostSet);
ImmutableSet<ServiceInstance> newHostSet = oldHostSet;
if (addr instanceof Addr.Bound) {
scala.collection.immutable.Set<Address> endpointAddresses = ((Addr.Bound)addr).addrs();
scala.collection.Iterator<Address> endpointAddressesIterator = endpointAddresses.toIterator();
HashSet<ServiceInstance> serviceInstances = new HashSet<ServiceInstance>();
while (endpointAddressesIterator.hasNext()) {
serviceInstances.add(endpointAddressToServiceInstance(endpointAddressesIterator.next()));
}
newHostSet = ImmutableSet.copyOf(serviceInstances);
} else if (addr instanceof Addr.Failed) {
logger.error("Name resolution failed", ((Addr.Failed)addr).cause());
newHostSet = ImmutableSet.of();
} else if (addr.toString().equals("Pending")) {
logger.info("Name resolution pending");
newHostSet = oldHostSet;
} else if (addr.toString().equals("Neg")) {
newHostSet = ImmutableSet.of();
} else {
logger.error("Invalid Addr type: {}", addr.getClass().getName());
throw new UnsupportedOperationException("Invalid Addr type:" + addr.getClass().getName());
}
// Reference comparison is valid as the sets are immutable
if (oldHostSet != newHostSet) {
logger.info("NameServerSet updated: {} -> {}", hostSetToString(oldHostSet), hostSetToString(newHostSet));
resolutionPending.set(false);
hostSet = newHostSet;
synchronized (watchers) {
for (HostChangeMonitor<ServiceInstance> watcher: watchers) {
watcher.onChange(newHostSet);
}
}
}
return BoxedUnit.UNIT;
}
示例9: watch
import com.twitter.thrift.ServiceInstance; //導入依賴的package包/類
@Override
public Command watch(final HostChangeMonitor<ServiceInstance> monitor) throws MonitorException {
monitor(monitor);
return null;
}
示例10: initialize
import com.twitter.thrift.ServiceInstance; //導入依賴的package包/類
public void initialize() throws Exception {
// Check if use of serverset is enabled, and if so, register a change monitor so we
// can find out how many PinLater servers are active. We use this to compute the
// per-server rate limit.
if (pinlaterServerSetEnabled) {
MorePreconditions.checkNotBlank(pinlaterServerSetPath);
LOG.info("Monitoring pinlater serverset: {}", pinlaterServerSetPath);
String fullServerSetPath = getClass().getResource("/" + pinlaterServerSetPath).getPath();
ServerSet serverSet = new ConfigFileServerSet(fullServerSetPath);
serverSet.monitor(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
@Override
public void onChange(ImmutableSet<ServiceInstance> hostSet) {
int oldSize = numPinLaterServers.get();
int newSize = hostSet.size();
if (newSize == 0) {
LOG.error("PinLater serverset is empty, ignoring and keeping old size: {}", oldSize);
return;
}
if (oldSize == newSize) {
LOG.info("PinLater serverset update, size unchanged: {}", oldSize);
return;
}
LOG.info("PinLater serverset update, old size: {}, new size: {}", oldSize, newSize);
numPinLaterServers.set(newSize);
rebuild();
}
});
} else {
LOG.info("PinLater server set is disabled; rate limits will be applied per server.");
}
if (queueConfigFilePath == null || queueConfigFilePath.isEmpty()) {
LOG.info("Queue config zookeeper path not specified, using defaults.");
return;
}
LOG.info("Registering watch on queue config: {}", queueConfigFilePath);
String fullQueueConfigFilePath = getClass().getResource("/" + queueConfigFilePath).getPath();
ConfigFileWatcher.defaultInstance().addWatch(
fullQueueConfigFilePath, new ExceptionalFunction<byte[], Void>() {
@Override
public Void applyE(byte[] bytes) throws Exception {
QueueConfigSchema queueConfigSchema = QueueConfigSchema.load(bytes);
LOG.info("Queue config update, new value: {}", queueConfigSchema);
queueConfigSchemaRef.set(queueConfigSchema);
rebuild();
return null;
}
});
}