本文整理汇总了Java中org.springframework.cloud.client.DefaultServiceInstance类的典型用法代码示例。如果您正苦于以下问题:Java DefaultServiceInstance类的具体用法?Java DefaultServiceInstance怎么用?Java DefaultServiceInstance使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DefaultServiceInstance类属于org.springframework.cloud.client包,在下文中一共展示了DefaultServiceInstance类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: customDiscoveryClient
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Bean
@Order(1)
public DiscoveryClient customDiscoveryClient() {
return new DiscoveryClient() {
@Override
public String description() {
return "A custom discovery client";
}
@Override
public List<ServiceInstance> getInstances(String serviceId) {
if (serviceId.equals("custom")) {
ServiceInstance s1 = new DefaultServiceInstance("custom", "host",
123, false);
return Arrays.asList(s1);
}
return Collections.emptyList();
}
@Override
public List<String> getServices() {
return Arrays.asList("custom");
}
};
}
示例2: getInstances
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Override
public List<ServiceInstance> getInstances(final String serviceId) {
DiscoveryContext context = new DiscoveryContext();
context.setInputParameters(serviceId);
DiscoveryTree discoveryTree = discoveryTrees.computeIfAbsent(serviceId, key -> {
return new DiscoveryTree();
});
VersionedCache serversVersionedCache = discoveryTree.discovery(context,
RegistryUtils.getAppId(),
serviceId,
DefinitionConst.VERSION_RULE_ALL);
Map<String, MicroserviceInstance> servers = serversVersionedCache.data();
List<ServiceInstance> instances = new ArrayList<>(servers.size());
for (MicroserviceInstance s : servers.values()) {
for (String endpoint : s.getEndpoints()) {
URIEndpointObject uri = new URIEndpointObject(endpoint);
instances.add(new DefaultServiceInstance(serviceId, uri.getHostOrIp(), uri.getPort(), uri.isSslEnabled()));
}
}
return instances;
}
示例3: getInstances
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Override
public List<ServiceInstance> getInstances(String serviceId) {
String prefix = properties.getPrefix() + "/" + serviceId;
GetOption option = GetOption.newBuilder()
.withPrefix(fromString(prefix))
.withKeysOnly(true)
.build();
try {
GetResponse response = etcdClient.getKVClient().get(fromString(prefix), option)
.get();
return response.getKvs().stream()
.map(KeyValue::getKey)
.map(ByteSequence::toStringUtf8)
.map(key -> {
String address = key.replace(prefix, "").substring(1);
String[] ipAndPort = address.split(":");
return new DefaultServiceInstance(serviceId, ipAndPort[0], Integer.parseInt(ipAndPort[1]), false);
})
.collect(Collectors.toList());
} catch (InterruptedException | ExecutionException e) {
throw new EtcdOperationException(e);
}
}
示例4: getLocalServiceInstance
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Override
public ServiceInstance getLocalServiceInstance() {
String serviceName = properties.getServiceName();
String podName = System.getenv(HOSTNAME);
ServiceInstance defaultInstance = new DefaultServiceInstance(serviceName, "localhost", 8080, false);
Endpoints endpoints = client.endpoints().withName(serviceName).get();
if (Utils.isNullOrEmpty(podName) || endpoints == null) {
return defaultInstance;
}
try {
return endpoints.getSubsets()
.stream()
.filter(s -> s.getAddresses().get(0).getTargetRef().getName().equals(podName))
.map(s -> (ServiceInstance) new KubernetesServiceInstance(serviceName,
s.getAddresses().stream().findFirst().orElseThrow(IllegalStateException::new),
s.getPorts().stream().findFirst().orElseThrow(IllegalStateException::new),
false))
.findFirst().orElse(defaultInstance);
} catch (Throwable t) {
return defaultInstance;
}
}
示例5: testCombineHostPort
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Test
public void testCombineHostPort() {
turbineProperties.setCombineHostPort(true);
CommonsInstanceDiscovery discovery = createDiscovery();
String appName = "testAppName";
int port = 8080;
String hostName = "myhost";
DefaultServiceInstance serviceInstance = new DefaultServiceInstance(appName, hostName, port, false);
Instance instance = discovery.marshall(serviceInstance);
assertEquals("hostname is wrong", hostName+":"+port, instance.getHostname());
assertEquals("port is wrong", String.valueOf(port), instance.getAttributes().get("port"));
String urlPath = SpringClusterMonitor.ClusterConfigBasedUrlClosure.getUrlPath(instance);
assertEquals("url is wrong", "http://"+hostName+":"+port+"/hystrix.stream", urlPath);
String clusterName = discovery.getClusterName(serviceInstance);
assertEquals("clusterName is wrong", appName, clusterName);
}
示例6: test_convert_with_metadata
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Test
public void test_convert_with_metadata() {
ServiceInstance service = new DefaultServiceInstance("test", "localhost", 80, false);
Map<String, String> metadata = new HashMap<>();
metadata.put("health.path", "ping");
metadata.put("management.context-path", "mgmt");
metadata.put("management.port", "1234");
service.getMetadata().putAll(metadata);
Registration registration = new DefaultServiceInstanceConverter().convert(service);
assertThat(registration.getName()).isEqualTo("test");
assertThat(registration.getServiceUrl()).isEqualTo("http://localhost:80/");
assertThat(registration.getManagementUrl()).isEqualTo("http://localhost:1234/mgmt");
assertThat(registration.getHealthUrl()).isEqualTo("http://localhost:1234/mgmt/ping");
assertThat(registration.getMetadata()).isEqualTo(metadata);
}
示例7: test_matching_and_ignore_pattern
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Test
public void test_matching_and_ignore_pattern() {
when(discovery.getServices()).thenReturn(asList("service-1", "service", "rabbit-1", "rabbit-2"));
when(discovery.getInstances("service")).thenReturn(
singletonList(new DefaultServiceInstance("service", "localhost", 80, false)));
when(discovery.getInstances("service-1")).thenReturn(
singletonList(new DefaultServiceInstance("service-1", "localhost", 80, false)));
listener.setServices(singleton("ser*"));
listener.setIgnoredServices(singleton("service-*"));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
StepVerifier.create(registry.getInstances())
.assertNext(a -> assertThat(a.getRegistration().getName()).isEqualTo("service"))
.verifyComplete();
}
示例8: test_register_and_convert
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Test
public void test_register_and_convert() {
when(discovery.getServices()).thenReturn(singletonList("service"));
when(discovery.getInstances("service")).thenReturn(
singletonList(new DefaultServiceInstance("service", "localhost", 80, false)));
listener.onInstanceRegistered(new InstanceRegisteredEvent<>(new Object(), null));
StepVerifier.create(registry.getInstances()).assertNext(application -> {
Registration registration = application.getRegistration();
assertThat(registration.getHealthUrl()).isEqualTo("http://localhost:80/health");
assertThat(registration.getManagementUrl()).isEqualTo("http://localhost:80/");
assertThat(registration.getServiceUrl()).isEqualTo("http://localhost:80/");
assertThat(registration.getName()).isEqualTo("service");
}).verifyComplete();
}
示例9: single_discovery_for_same_heartbeat
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Test
public void single_discovery_for_same_heartbeat() {
Object heartbeat = new Object();
listener.onParentHeartbeat(new ParentHeartbeatEvent(new Object(), heartbeat));
when(discovery.getServices()).thenReturn(singletonList("service"));
when(discovery.getInstances("service")).thenReturn(
singletonList(new DefaultServiceInstance("service", "localhost", 80, false)));
listener.onApplicationEvent(new HeartbeatEvent(new Object(), heartbeat));
StepVerifier.create(registry.getInstances()).verifyComplete();
listener.onApplicationEvent(new HeartbeatEvent(new Object(), new Object()));
StepVerifier.create(registry.getInstances())
.assertNext(a -> assertThat(a.getRegistration().getName()).isEqualTo("service"))
.verifyComplete();
}
示例10: addInstancesToList
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
private void addInstancesToList(List<ServiceInstance> instances, String serviceId,
QueryParams queryParams) {
String aclToken = properties.getAclToken();
Response<List<HealthService>> services;
if (StringUtils.hasText(aclToken)) {
services = client.getHealthServices(serviceId,
this.properties.getDefaultQueryTag(),
this.properties.isQueryPassing(), queryParams, aclToken);
}
else {
services = client.getHealthServices(serviceId,
this.properties.getDefaultQueryTag(),
this.properties.isQueryPassing(), queryParams);
}
for (HealthService service : services.getValue()) {
String host = findHost(service);
instances.add(new DefaultServiceInstance(serviceId, host, service
.getService().getPort(), false, getMetadata(service)));
}
}
示例11: getInstances
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Override
public List<ServiceInstance> getInstances(String serviceId) {
return cloudFoundryService
.getApplicationInstances(serviceId)
.map(tuple -> {
ApplicationDetail applicationDetail = tuple.getT1();
InstanceDetail instanceDetail = tuple.getT2();
String applicationId = applicationDetail.getId();
String applicationIndex = instanceDetail.getIndex();
String name = applicationDetail.getName();
String url = applicationDetail.getUrls().size() > 0 ? applicationDetail.getUrls().get(0) : null;
boolean secure = (url + "").toLowerCase().startsWith("https");
HashMap<String, String> metadata = new HashMap<>();
metadata.put("applicationId", applicationId);
metadata.put("instanceId", applicationIndex);
return (ServiceInstance) new DefaultServiceInstance(name, url, 80, secure, metadata);
})
.collectList()
.blockOptional()
.orElse(new ArrayList<>());
}
示例12: getServiceInstances
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Override
protected List<ServerInstance> getServiceInstances(String serviceName) {
List<ServiceInstance> instances = new ArrayList<>();
Response<List<HealthService>> healthServices = consulClient.getHealthServices(serviceName, true, QueryParams.DEFAULT);
for (HealthService healthService : healthServices.getValue()) {
if (isPassingChecks(healthService)) {
String host = findHost(healthService);
instances.add(new DefaultServiceInstance(serviceName, host,
healthService.getService().getPort(), false, getMetadata(healthService)));
}
}
return convertToServerInstanceList(instances);
}
示例13: getInstances
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Override
public List<ServiceInstance> getInstances(String s) {
List<ServiceInstance> serviceInstances = new ArrayList<>();
try {
Document one = documentDao.findOne(Integer.parseInt(s));
URL url = getUrl(one);
DefaultServiceInstance instance = new DefaultServiceInstance("" + one.getId(), url.getHost(), url.getPort(), url.getHost().contains("https://"));
serviceInstances.add(instance);
} catch (IOException e) {
e.printStackTrace();
}
return serviceInstances;
}
示例14: extractServiceInstances
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
/**
* Extract instances of a service for a specific marathon application
*
* @param app
* @return
*/
public List<ServiceInstance> extractServiceInstances(App app) {
log.debug("Discovered service [{}]", app.getId());
if (app.getTasks().isEmpty()) {
return Collections.emptyList();
}
return app.getTasks()
.parallelStream()
.filter(task -> null == task.getHealthCheckResults() ||
task.getHealthCheckResults()
.stream()
.allMatch(HealthCheckResults::getAlive)
)
.map(task -> new DefaultServiceInstance(
ServiceIdConverter.convertToServiceId(task.getAppId()),
task.getHost(),
task.getPorts().stream().findFirst().orElse(0),
false
)).map(serviceInstance -> {
if (app.getLabels() != null && !app.getLabels().isEmpty())
serviceInstance.getMetadata().putAll(app.getLabels());
return serviceInstance;
})
.collect(Collectors.toList());
}
示例15: getInstances
import org.springframework.cloud.client.DefaultServiceInstance; //导入依赖的package包/类
@Override
public List<ServiceInstance> getInstances(final String serviceId) {
List<ServiceInstance> instances = receptorService.getActualLRPsByProcessGuid(
serviceId, new Converter<ActualLRPResponse, ServiceInstance>() {
@Override
public ServiceInstance convert(ActualLRPResponse response) {
return new DefaultServiceInstance(serviceId, response
.getAddress(), response.getPorts()[0].getHostPort(),
false);
}
});
return instances;
}