Java源码示例:com.ecwid.consul.v1.catalog.model.CatalogService
示例1
@BeforeClass
public static void beforeClass() {
assumeTrue(CanConnect.to(new InetSocketAddress(CONSUL_HOST, CONSUL_PORT)));
ConsulClient client = new ConsulClient();
Response<List<CatalogService>> response = client.getCatalogService("vault",
QueryParams.DEFAULT);
if (response.getValue().isEmpty()) {
NewService service = new NewService();
service.setAddress("localhost");
service.setPort(8200);
service.setId("vault");
service.setName("vault");
client.agentServiceRegister(service);
}
}
示例2
/**
* Update grpc port of consul tags after Conusl Registered
* */
@EventListener
public void listenInstanceRegisteredEvent(InstanceRegisteredEvent instanceRegisteredEvent){
if(alphaServerPort == 0){
if(instanceRegisteredEvent.getConfig() instanceof ConsulDiscoveryProperties){
ConsulDiscoveryProperties properties = (ConsulDiscoveryProperties)instanceRegisteredEvent.getConfig();
this.consuleInstanceId = formatConsulInstanceId(properties.getInstanceId());
Response<List<CatalogService>> services = consulClient.getCatalogService(serviceName,null);
if(services.getValue() != null){
services.getValue().stream().filter(service ->
service.getServiceId().equalsIgnoreCase(this.consuleInstanceId)).forEach(service -> {
NewService newservice = new NewService();
newservice.setName(service.getServiceName());
newservice.setId(service.getServiceId());
newservice.setAddress(service.getAddress());
newservice.setPort(service.getServicePort());
List<String> tags = service.getServiceTags();
tags.remove("alpha-server-port=0");
tags.add("alpha-server-port="+actualAlphaServerPort);
newservice.setTags(tags);
consulClient.agentServiceRegister(newservice);
});
}
}
}
}
示例3
@ReadOperation
public ConsulData invoke() {
ConsulData data = new ConsulData();
// data.setKeyValues(kvClient.getKeyValueRecurse());
Response<Map<String, Service>> agentServices = this.consul.getAgentServices();
data.setAgentServices(agentServices.getValue());
Response<Map<String, List<String>>> catalogServices = this.consul
.getCatalogServices(CatalogServicesRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT).build());
for (String serviceId : catalogServices.getValue().keySet()) {
Response<List<CatalogService>> response = this.consul
.getCatalogService(serviceId, CatalogServiceRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT).build());
data.getCatalogServices().put(serviceId, response.getValue());
}
Response<List<Node>> catalogNodes = this.consul
.getCatalogNodes(CatalogNodesRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT).build());
data.setCatalogNodes(catalogNodes.getValue());
return data;
}
示例4
@Test
public void testServiceRegistry() throws Exception {
ConfigurableApplicationContext context = new SpringApplicationBuilder(TestConfiguration.class)
.web(WebApplicationType.NONE)
.run(
"--debug=false",
"--spring.main.banner-mode=OFF",
"--spring.application.name=" + UUID.randomUUID().toString(),
"--ribbon.enabled=false",
"--ribbon.eureka.enabled=false",
"--management.endpoint.enabled=false",
"--spring.cloud.consul.enabled=true",
"--spring.cloud.consul.host=" + container.getContainerIpAddress(),
"--spring.cloud.consul.port=" + container.getMappedPort(8500),
"--spring.cloud.consul.config.enabled=false",
"--spring.cloud.consul.discovery.enabled=true",
"--spring.cloud.service-registry.auto-registration.enabled=false",
"--camel.cloud.service-registry.service-host=localhost",
"--spring.main.allow-bean-definition-overriding=true"
);
// TODO: Remove --spring.main.allow-bean-definition-overriding=true when new version of spring-cloud
// is released that supports Spring Boot 2.1 more properly
try {
final ConsulClient client = context.getBean(ConsulClient.class);
final ServiceRegistry registry = context.getBean(ServiceRegistry.class);
registry.register(
DefaultServiceDefinition.builder()
.withHost(SERVICE_HOST)
.withPort(SERVICE_PORT)
.withName(SERVICE_NAME)
.withId(SERVICE_ID)
.build()
);
List<CatalogService> services = client.getCatalogService(SERVICE_NAME, QueryParams.DEFAULT).getValue();
assertThat(services).hasSize(1);
assertThat(services).first().hasFieldOrPropertyWithValue("serviceId", SERVICE_ID);
assertThat(services).first().hasFieldOrPropertyWithValue("serviceName", SERVICE_NAME);
assertThat(services).first().hasFieldOrPropertyWithValue("serviceAddress", SERVICE_HOST);
assertThat(services).first().hasFieldOrPropertyWithValue("servicePort", SERVICE_PORT);
} finally {
context.close();
}
}
示例5
public Map<String, List<CatalogService>> getCatalogServices() {
return this.catalogServices;
}
示例6
public void setCatalogServices(
Map<String, List<CatalogService>> catalogServices) {
this.catalogServices = catalogServices;
}