Replaces deprecations.

Mostly from ewcid consul-api.
This commit is contained in:
Spencer Gibb
2020-04-09 01:23:51 -04:00
parent ce0ac8b2be
commit cbd5e8f22b
13 changed files with 103 additions and 86 deletions

View File

@@ -24,6 +24,7 @@ import javax.annotation.PostConstruct;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.event.EventListRequest;
import com.ecwid.consul.v1.event.model.Event;
import com.ecwid.consul.v1.event.model.EventParams;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -77,7 +78,8 @@ public class EventService {
}
public Response<List<Event>> getEventsResponse() {
return this.consul.eventList(QueryParams.DEFAULT);
return this.consul.eventList(EventListRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT).build());
}
public List<Event> getEvents() {
@@ -102,8 +104,8 @@ public class EventService {
if (this.properties != null) {
eventTimeout = this.properties.getEventTimeout();
}
Response<List<Event>> watch = this.consul
.eventList(new QueryParams(eventTimeout, index));
Response<List<Event>> watch = this.consul.eventList(EventListRequest.newBuilder()
.setQueryParams(new QueryParams(eventTimeout, index)).build());
return filterEvents(readEvents(watch), lastIndex);
}

View File

@@ -32,10 +32,10 @@ import org.springframework.cloud.endpoint.event.RefreshEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.util.StringUtils;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;

View File

@@ -24,6 +24,9 @@ import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.Service;
import com.ecwid.consul.v1.catalog.CatalogNodesRequest;
import com.ecwid.consul.v1.catalog.CatalogServiceRequest;
import com.ecwid.consul.v1.catalog.CatalogServicesRequest;
import com.ecwid.consul.v1.catalog.model.CatalogService;
import com.ecwid.consul.v1.catalog.model.Node;
@@ -51,16 +54,19 @@ public class ConsulEndpoint {
data.setAgentServices(agentServices.getValue());
Response<Map<String, List<String>>> catalogServices = this.consul
.getCatalogServices(QueryParams.DEFAULT);
.getCatalogServices(CatalogServicesRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT).build());
for (String serviceId : catalogServices.getValue().keySet()) {
Response<List<CatalogService>> response = this.consul
.getCatalogService(serviceId, QueryParams.DEFAULT);
.getCatalogService(serviceId, CatalogServiceRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT).build());
data.getCatalogServices().put(serviceId, response.getValue());
}
Response<List<Node>> catalogNodes = this.consul
.getCatalogNodes(QueryParams.DEFAULT);
.getCatalogNodes(CatalogNodesRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT).build());
data.setCatalogNodes(catalogNodes.getValue());
return data;

View File

@@ -22,6 +22,7 @@ import java.util.Map;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.catalog.CatalogServicesRequest;
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health;
@@ -41,7 +42,8 @@ public class ConsulHealthIndicator extends AbstractHealthIndicator {
protected void doHealthCheck(Health.Builder builder) throws Exception {
final Response<String> leaderStatus = this.consul.getStatusLeader();
final Response<Map<String, List<String>>> services = this.consul
.getCatalogServices(QueryParams.DEFAULT);
.getCatalogServices(CatalogServicesRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT).build());
builder.up().withDetail("leader", leaderStatus.getValue()).withDetail("services",
services.getValue());
}

View File

@@ -26,6 +26,7 @@ import java.util.concurrent.atomic.AtomicReference;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.catalog.CatalogServicesRequest;
import io.micrometer.core.annotation.Timed;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -126,10 +127,12 @@ public class ConsulCatalogWatch
index = this.catalogServicesIndex.get().longValue();
}
Response<Map<String, List<String>>> response = this.consul.getCatalogServices(
new QueryParams(this.properties.getCatalogServicesWatchTimeout(),
index),
this.properties.getAclToken());
CatalogServicesRequest request = CatalogServicesRequest.newBuilder()
.setQueryParams(new QueryParams(
this.properties.getCatalogServicesWatchTimeout(), index))
.setToken(this.properties.getAclToken()).build();
Response<Map<String, List<String>>> response = this.consul
.getCatalogServices(request);
Long consulIndex = response.getConsulIndex();
if (consulIndex != null) {
this.catalogServicesIndex.set(BigInteger.valueOf(consulIndex));

View File

@@ -23,6 +23,8 @@ import java.util.Map;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.catalog.CatalogServicesRequest;
import com.ecwid.consul.v1.health.HealthServicesRequest;
import com.ecwid.consul.v1.health.model.HealthService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -30,7 +32,6 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.util.StringUtils;
import static org.springframework.cloud.consul.discovery.ConsulServerUtils.findHost;
import static org.springframework.cloud.consul.discovery.ConsulServerUtils.getMetadata;
@@ -77,18 +78,13 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
private void addInstancesToList(List<ServiceInstance> instances, String serviceId,
QueryParams queryParams) {
String aclToken = this.properties.getAclToken();
Response<List<HealthService>> services;
if (StringUtils.hasText(aclToken)) {
services = this.client.getHealthServices(serviceId,
this.properties.getDefaultQueryTag(),
this.properties.isQueryPassing(), queryParams, aclToken);
}
else {
services = this.client.getHealthServices(serviceId,
this.properties.getDefaultQueryTag(),
this.properties.isQueryPassing(), queryParams);
}
HealthServicesRequest request = HealthServicesRequest.newBuilder()
.setTag(this.properties.getDefaultQueryTag())
.setPassing(this.properties.isQueryPassing()).setQueryParams(queryParams)
.setToken(this.properties.getAclToken()).build();
Response<List<HealthService>> services = this.client.getHealthServices(serviceId,
request);
for (HealthService service : services.getValue()) {
String host = findHost(service);
@@ -106,7 +102,8 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
List<ServiceInstance> instances = new ArrayList<>();
Response<Map<String, List<String>>> services = this.client
.getCatalogServices(QueryParams.DEFAULT);
.getCatalogServices(CatalogServicesRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT).build());
for (String serviceId : services.getValue().keySet()) {
addInstancesToList(instances, serviceId, QueryParams.DEFAULT);
}
@@ -117,15 +114,11 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
public List<String> getServices() {
String aclToken = this.properties.getAclToken();
if (StringUtils.hasText(aclToken)) {
return new ArrayList<>(
this.client.getCatalogServices(QueryParams.DEFAULT, aclToken)
.getValue().keySet());
}
else {
return new ArrayList<>(this.client.getCatalogServices(QueryParams.DEFAULT)
.getValue().keySet());
}
CatalogServicesRequest request = CatalogServicesRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT)
.setToken(this.properties.getAclToken()).build();
return new ArrayList<>(
this.client.getCatalogServices(request).getValue().keySet());
}
@Override

View File

@@ -23,6 +23,7 @@ import java.util.List;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.health.HealthServicesRequest;
import com.ecwid.consul.v1.health.model.HealthService;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractServerList;
@@ -76,10 +77,12 @@ public class ConsulServerList extends AbstractServerList<ConsulServer> {
if (this.client == null) {
return Collections.emptyList();
}
String tag = getTag(); // null is ok
Response<List<HealthService>> response = this.client.getHealthServices(
this.serviceId, tag, this.properties.isQueryPassing(),
createQueryParamsForClientRequest(), this.properties.getAclToken());
HealthServicesRequest request = HealthServicesRequest.newBuilder()
.setTag(getTag()).setPassing(this.properties.isQueryPassing())
.setQueryParams(createQueryParamsForClientRequest())
.setToken(this.properties.getAclToken()).build();
Response<List<HealthService>> response = this.client
.getHealthServices(this.serviceId, request);
if (response.getValue() == null || response.getValue().isEmpty()) {
return Collections.emptyList();
}

View File

@@ -24,6 +24,8 @@ import java.util.Map;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.catalog.CatalogServicesRequest;
import com.ecwid.consul.v1.health.HealthServicesRequest;
import com.ecwid.consul.v1.health.model.HealthService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -34,7 +36,6 @@ import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.ReactiveDiscoveryClient;
import org.springframework.cloud.consul.discovery.ConsulDiscoveryProperties;
import org.springframework.util.StringUtils;
import static org.springframework.cloud.consul.discovery.ConsulServerUtils.findHost;
import static org.springframework.cloud.consul.discovery.ConsulServerUtils.getMetadata;
@@ -79,15 +80,14 @@ public class ConsulReactiveDiscoveryClient implements ReactiveDiscoveryClient {
}
private List<HealthService> getHealthServices(String serviceId) {
Response<List<HealthService>> services = StringUtils
.hasText(properties.getAclToken())
? client.getHealthServices(serviceId,
properties.getDefaultQueryTag(),
properties.isQueryPassing(), QueryParams.DEFAULT,
properties.getAclToken())
: client.getHealthServices(serviceId,
properties.getDefaultQueryTag(),
properties.isQueryPassing(), QueryParams.DEFAULT);
HealthServicesRequest request = HealthServicesRequest.newBuilder()
.setTag(this.properties.getDefaultQueryTag())
.setPassing(this.properties.isQueryPassing())
.setQueryParams(QueryParams.DEFAULT)
.setToken(this.properties.getAclToken()).build();
Response<List<HealthService>> services = client.getHealthServices(serviceId,
request);
return services == null ? Collections.emptyList() : services.getValue();
}
@@ -106,11 +106,11 @@ public class ConsulReactiveDiscoveryClient implements ReactiveDiscoveryClient {
@Override
public Flux<String> getServices() {
return Flux.defer(() -> {
Response<Map<String, List<String>>> services = StringUtils
.hasText(properties.getAclToken())
? client.getCatalogServices(QueryParams.DEFAULT,
properties.getAclToken())
: client.getCatalogServices(QueryParams.DEFAULT);
CatalogServicesRequest request = CatalogServicesRequest.newBuilder()
.setToken(properties.getAclToken())
.setQueryParams(QueryParams.DEFAULT).build();
Response<Map<String, List<String>>> services = client
.getCatalogServices(request);
return services == null ? Flux.empty()
: Flux.fromIterable(services.getValue().keySet());
}).onErrorResume(exception -> {

View File

@@ -23,6 +23,7 @@ import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.NewService;
import com.ecwid.consul.v1.health.HealthChecksForServiceRequest;
import com.ecwid.consul.v1.health.model.Check;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@@ -119,7 +120,8 @@ public class ConsulServiceRegistry implements ServiceRegistry<ConsulRegistration
public Object getStatus(ConsulRegistration registration) {
String serviceId = registration.getServiceId();
Response<List<Check>> response = this.client.getHealthChecksForService(serviceId,
QueryParams.DEFAULT);
HealthChecksForServiceRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT).build());
List<Check> checks = response.getValue();
for (Check check : checks) {

View File

@@ -21,6 +21,7 @@ import java.util.List;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.health.HealthChecksForServiceRequest;
import com.ecwid.consul.v1.health.model.Check;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -75,7 +76,8 @@ public class TtlSchedulerRemoveTests {
private Check getCheckForService(String serviceId) {
Response<List<Check>> checkResponse = this.consul
.getHealthChecksForService(serviceId, QueryParams.DEFAULT);
.getHealthChecksForService(serviceId, HealthChecksForServiceRequest
.newBuilder().setQueryParams(QueryParams.DEFAULT).build());
if (checkResponse.getValue().size() > 0) {
return checkResponse.getValue().get(0);
}

View File

@@ -21,6 +21,7 @@ import java.util.List;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.health.HealthChecksForServiceRequest;
import com.ecwid.consul.v1.health.model.Check;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -72,7 +73,8 @@ public class TtlSchedulerTests {
private Check getCheckForService(String serviceId) {
Response<List<Check>> checkResponse = this.consul
.getHealthChecksForService(serviceId, QueryParams.DEFAULT);
.getHealthChecksForService(serviceId, HealthChecksForServiceRequest
.newBuilder().setQueryParams(QueryParams.DEFAULT).build());
if (checkResponse.getValue().size() > 0) {
return checkResponse.getValue().get(0);
}

View File

@@ -20,8 +20,9 @@ import java.util.List;
import java.util.Map;
import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.catalog.CatalogServicesRequest;
import com.ecwid.consul.v1.health.HealthServicesRequest;
import com.ecwid.consul.v1.health.model.HealthService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -38,6 +39,8 @@ import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Collections.singletonMap;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -69,70 +72,68 @@ class ConsulReactiveDiscoveryClientTests {
@Test
public void shouldReturnEmptyFluxOfServicesWhenConsulFails() {
Flux<String> services = client.getServices();
when(consulClient.getCatalogServices(QueryParams.DEFAULT))
when(consulClient.getCatalogServices(any(CatalogServicesRequest.class)))
.thenThrow(new RuntimeException("Possible runtime exception"));
StepVerifier.create(services).expectNextCount(0).expectComplete().verify();
verify(consulClient).getCatalogServices(QueryParams.DEFAULT);
verify(consulClient).getCatalogServices(any(CatalogServicesRequest.class));
}
@Test
public void shouldReturnFluxOfServices() {
Flux<String> services = client.getServices();
when(consulClient.getCatalogServices(QueryParams.DEFAULT))
when(consulClient.getCatalogServices(any(CatalogServicesRequest.class)))
.thenReturn(consulServicesResponse());
StepVerifier.create(services).expectNext("my-service").expectComplete().verify();
verify(properties).getAclToken();
verify(consulClient).getCatalogServices(QueryParams.DEFAULT);
verify(consulClient).getCatalogServices(any(CatalogServicesRequest.class));
}
@Test
public void shouldReturnFluxOfServicesWithAclToken() {
when(properties.getAclToken()).thenReturn("aclToken");
when(consulClient.getCatalogServices(QueryParams.DEFAULT, "aclToken"))
when(consulClient.getCatalogServices(any(CatalogServicesRequest.class)))
.thenReturn(consulServicesResponse());
Flux<String> services = client.getServices();
StepVerifier.create(services).expectNext("my-service").expectComplete().verify();
verify(properties, times(2)).getAclToken();
verify(consulClient).getCatalogServices(QueryParams.DEFAULT, "aclToken");
verify(properties, times(1)).getAclToken();
verify(consulClient).getCatalogServices(any(CatalogServicesRequest.class));
}
@Test
public void shouldReturnEmptyFluxForNonExistingService() {
configureCommonProperties();
when(consulClient.getHealthServices("nonexistent-service", "queryTag", false,
QueryParams.DEFAULT)).thenReturn(emptyConsulInstancesResponse());
when(consulClient.getHealthServices(eq("nonexistent-service"),
any(HealthServicesRequest.class)))
.thenReturn(emptyConsulInstancesResponse());
Flux<ServiceInstance> instances = client.getInstances("nonexistent-service");
StepVerifier.create(instances).expectNextCount(0).expectComplete().verify();
verify(properties).getAclToken();
verify(consulClient).getHealthServices("nonexistent-service", "queryTag", false,
QueryParams.DEFAULT);
verify(consulClient).getHealthServices(eq("nonexistent-service"), any());
}
@Test
public void shouldReturnEmptyFluxWhenConsulFails() {
configureCommonProperties();
when(consulClient.getHealthServices("existing-service", "queryTag", false,
QueryParams.DEFAULT))
when(consulClient.getHealthServices(eq("existing-service"),
any(HealthServicesRequest.class)))
.thenThrow(new RuntimeException("Possible runtime exception"));
Flux<ServiceInstance> instances = client.getInstances("existing-service");
StepVerifier.create(instances).expectNextCount(0).expectComplete().verify();
verify(consulClient).getHealthServices("existing-service", "queryTag", false,
QueryParams.DEFAULT);
verify(consulClient).getHealthServices(eq("existing-service"), any());
}
@Test
public void shouldReturnFluxOfServiceInstances() {
configureCommonProperties();
Response<List<HealthService>> response = consulInstancesResponse();
when(consulClient.getHealthServices("existing-service", "queryTag", false,
QueryParams.DEFAULT)).thenReturn(response);
when(consulClient.getHealthServices(eq("existing-service"),
any(HealthServicesRequest.class))).thenReturn(response);
Flux<ServiceInstance> instances = client.getInstances("existing-service");
StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
verify(properties).getAclToken();
verify(properties).getDefaultQueryTag();
verify(properties).isQueryPassing();
verify(consulClient).getHealthServices("existing-service", "queryTag", false,
QueryParams.DEFAULT);
verify(consulClient).getHealthServices(eq("existing-service"), any());
}
@Test
@@ -140,15 +141,14 @@ class ConsulReactiveDiscoveryClientTests {
configureCommonProperties();
when(properties.getAclToken()).thenReturn("aclToken");
Response<List<HealthService>> response = consulInstancesResponse();
when(consulClient.getHealthServices("existing-service", "queryTag", false,
QueryParams.DEFAULT, "aclToken")).thenReturn(response);
when(consulClient.getHealthServices(eq("existing-service"),
any(HealthServicesRequest.class))).thenReturn(response);
Flux<ServiceInstance> instances = client.getInstances("existing-service");
StepVerifier.create(instances).expectNextCount(1).expectComplete().verify();
verify(properties, times(2)).getAclToken();
verify(properties, times(1)).getAclToken();
verify(properties).getDefaultQueryTag();
verify(properties).isQueryPassing();
verify(consulClient).getHealthServices("existing-service", "queryTag", false,
QueryParams.DEFAULT, "aclToken");
verify(consulClient).getHealthServices(eq("existing-service"), any());
}
private Response<Map<String, List<String>>> consulServicesResponse() {

View File

@@ -23,6 +23,7 @@ import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.Service;
import com.ecwid.consul.v1.health.HealthChecksForServiceRequest;
import com.ecwid.consul.v1.health.model.Check;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -82,8 +83,9 @@ public class ConsulAutoServiceRegistrationCustomizedPropsTests {
assertThat("myhost").as("service address was wrong")
.isEqualTo(service.getAddress());
Response<List<Check>> checkResponse = this.consul
.getHealthChecksForService("myTestService-B", QueryParams.DEFAULT);
Response<List<Check>> checkResponse = this.consul.getHealthChecksForService(
"myTestService-B", HealthChecksForServiceRequest.newBuilder()
.setQueryParams(QueryParams.DEFAULT).build());
List<Check> checks = checkResponse.getValue();
assertThat(checks).as("checks was wrong size").hasSize(0);
}