Remove deprecated DiscoveryClient.getLocalServiceInstance()
This commit is contained in:
@@ -20,7 +20,6 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.cloud.client.DefaultServiceInstance;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
@@ -29,9 +28,6 @@ import org.springframework.util.StringUtils;
|
||||
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.Member;
|
||||
import com.ecwid.consul.v1.agent.model.Self;
|
||||
import com.ecwid.consul.v1.agent.model.Service;
|
||||
import com.ecwid.consul.v1.health.model.HealthService;
|
||||
|
||||
import static org.springframework.cloud.consul.discovery.ConsulServerUtils.findHost;
|
||||
@@ -46,6 +42,7 @@ import lombok.extern.apachecommons.CommonsLog;
|
||||
@CommonsLog
|
||||
public class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
@Deprecated
|
||||
interface LocalResolver {
|
||||
String getInstanceId();
|
||||
Integer getPort();
|
||||
@@ -53,19 +50,16 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
|
||||
private final ConsulClient client;
|
||||
private final ConsulDiscoveryProperties properties;
|
||||
private final LocalResolver localResolver;
|
||||
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
public ConsulDiscoveryClient(ConsulClient client, ConsulDiscoveryProperties properties,
|
||||
LocalResolver localResolver) {
|
||||
public ConsulDiscoveryClient(ConsulClient client, ConsulDiscoveryProperties properties) {
|
||||
this.client = client;
|
||||
this.properties = properties;
|
||||
this.localResolver = localResolver;
|
||||
}
|
||||
|
||||
public void setServerProperties(ServerProperties serverProperties) {
|
||||
this.serverProperties = serverProperties;
|
||||
@Deprecated
|
||||
public ConsulDiscoveryClient(ConsulClient client, ConsulDiscoveryProperties properties,
|
||||
LocalResolver localResolver) {
|
||||
this(client, properties);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -73,66 +67,6 @@ public class ConsulDiscoveryClient implements DiscoveryClient {
|
||||
return "Spring Cloud Consul Discovery Client";
|
||||
}
|
||||
|
||||
@Override
|
||||
public ServiceInstance getLocalServiceInstance() {
|
||||
Response<Map<String, Service>> agentServices = client.getAgentServices();
|
||||
Service service = agentServices.getValue().get(localResolver.getInstanceId());
|
||||
String instanceId;
|
||||
Integer port;
|
||||
Map<String, String> metadata;
|
||||
String host = "localhost";
|
||||
|
||||
// if we have a response from consul, that is the ultimate source of truth
|
||||
if (service != null) {
|
||||
instanceId = service.getId();
|
||||
port = service.getPort();
|
||||
host = service.getAddress();
|
||||
metadata = getMetadata(service.getTags());
|
||||
} else {
|
||||
// possibly called before registration, use configuration or best guess
|
||||
log.warn("getLocalServiceInstance(): Unable to locate service in consul agent: "
|
||||
+ localResolver.getInstanceId());
|
||||
|
||||
instanceId = localResolver.getInstanceId();
|
||||
port = localResolver.getPort();
|
||||
if (port != null && port == 0 && serverProperties != null
|
||||
&& serverProperties.getPort() != null) {
|
||||
port = serverProperties.getPort();
|
||||
}
|
||||
metadata = getMetadata(this.properties.getTags());
|
||||
|
||||
if (StringUtils.hasText(this.properties.getHostname())) {
|
||||
host = this.properties.getHostname();
|
||||
} else if (this.properties.isPreferAgentAddress()){
|
||||
// try and use the agent host
|
||||
String agentHost = getAgentHost();
|
||||
if (agentHost != null) {
|
||||
host = agentHost;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (port == null) {
|
||||
log.warn("getLocalServiceInstance(): Unable to determine port.");
|
||||
port = 0;
|
||||
}
|
||||
|
||||
return new DefaultServiceInstance(instanceId, host, port, false, metadata);
|
||||
}
|
||||
|
||||
private String getAgentHost() {
|
||||
Response<Self> agentSelf = client.getAgentSelf();
|
||||
Member member = agentSelf.getValue().getMember();
|
||||
if (member != null) {
|
||||
if (properties.isPreferIpAddress()) {
|
||||
return member.getAddress();
|
||||
} else if (StringUtils.hasText(member.getName())) {
|
||||
return member.getName();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<ServiceInstance> getInstances(final String serviceId) {
|
||||
return getInstances(serviceId, QueryParams.DEFAULT);
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
package org.springframework.cloud.consul.discovery;
|
||||
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
@@ -24,8 +23,6 @@ import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.commons.util.InetUtils;
|
||||
import org.springframework.cloud.consul.ConditionalOnConsulEnabled;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@@ -43,9 +40,6 @@ public class ConsulDiscoveryClientConfiguration {
|
||||
@Autowired
|
||||
private ConsulClient consulClient;
|
||||
|
||||
@Autowired(required = false)
|
||||
private ServerProperties serverProperties;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty("spring.cloud.consul.discovery.heartbeat.enabled")
|
||||
@@ -68,46 +62,8 @@ public class ConsulDiscoveryClientConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public ConsulDiscoveryClient consulDiscoveryClient(ConsulDiscoveryProperties discoveryProperties, final ApplicationContext context) {
|
||||
ConsulDiscoveryClient discoveryClient = new ConsulDiscoveryClient(consulClient,
|
||||
discoveryProperties, new RegistrationLocalResolver(context));
|
||||
discoveryClient.setServerProperties(serverProperties); //null ok
|
||||
return discoveryClient;
|
||||
}
|
||||
|
||||
//FIXME: remove?
|
||||
class RegistrationLocalResolver implements ConsulDiscoveryClient.LocalResolver {
|
||||
private ApplicationContext context;
|
||||
|
||||
public RegistrationLocalResolver(ApplicationContext context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getInstanceId() {
|
||||
ConsulRegistration registration = getBean(ConsulRegistration.class);
|
||||
if (registration != null) {
|
||||
return registration.getInstanceId();
|
||||
}
|
||||
throw new IllegalStateException("Must have one of ConsulRegistration or ConsulLifecycle");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer getPort() {
|
||||
ConsulRegistration registration = getBean(ConsulRegistration.class);
|
||||
if (registration != null) {
|
||||
return registration.getService().getPort();
|
||||
}
|
||||
throw new IllegalStateException("Must have one of ConsulRegistration or ConsulLifecycle");
|
||||
}
|
||||
|
||||
<T> T getBean(Class<T> type) {
|
||||
try {
|
||||
return context.getBean(type);
|
||||
} catch (NoSuchBeanDefinitionException e) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
public ConsulDiscoveryClient consulDiscoveryClient(ConsulDiscoveryProperties discoveryProperties) {
|
||||
return new ConsulDiscoveryClient(consulClient, discoveryProperties);
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -88,16 +88,6 @@ public class ConsulDiscoveryClientCustomizedTests {
|
||||
assertEquals("metadata key foo2 was wrong", "bar2=baz2", foo2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLocalInstance() {
|
||||
ServiceInstance instance = discoveryClient.getLocalServiceInstance();
|
||||
assertNotNull("instance was null", instance);
|
||||
assertNotIpAddress(instance);
|
||||
assertEquals("instance id was wrong", "testConsulDiscovery2Id",
|
||||
instance.getServiceId());
|
||||
assertInstance(instance);
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@EnableDiscoveryClient
|
||||
|
||||
@@ -1,159 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.consul.discovery;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import com.ecwid.consul.v1.agent.model.NewService;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.ecwid.consul.transport.RawResponse;
|
||||
import com.ecwid.consul.v1.ConsulClient;
|
||||
import com.ecwid.consul.v1.Response;
|
||||
import com.ecwid.consul.v1.agent.model.Member;
|
||||
import com.ecwid.consul.v1.agent.model.Self;
|
||||
import com.ecwid.consul.v1.agent.model.Service;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.entry;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.MOCK;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = MOCK,
|
||||
classes = ConsulDiscoveryClientLocalServiceInstanceTests.LocalServiceTestConfig.class,
|
||||
properties = {"spring.cloud.consul.discovery.catalogServicesWatch.enabled=false"})
|
||||
public class ConsulDiscoveryClientLocalServiceInstanceTests {
|
||||
|
||||
private static final String SERVICE_ID = "service1:8080";
|
||||
private static final String ADDRESS = "service1addr";
|
||||
private static final String SERVICE = "service1";
|
||||
private static final int PORT = 8080;
|
||||
private static final String KEY = "foo";
|
||||
private static final String VALUE = "bar";
|
||||
private static final String TAG = KEY+"="+VALUE;
|
||||
|
||||
@MockBean
|
||||
private ConsulClient consul;
|
||||
|
||||
@MockBean
|
||||
private ConsulRegistration lifecycle;
|
||||
|
||||
@MockBean
|
||||
private ConsulDiscoveryProperties properties;
|
||||
|
||||
@Autowired
|
||||
private ConsulDiscoveryClient discoveryClient;
|
||||
public static final RawResponse RAW_RESPONSE = new RawResponse(200, null, null, 1L, false, null);
|
||||
|
||||
@Test
|
||||
public void localServiceInstanceFromConsul() {
|
||||
Service service = new Service();
|
||||
service.setAddress(ADDRESS);
|
||||
service.setService(SERVICE);
|
||||
service.setPort(PORT);
|
||||
service.setId(SERVICE_ID);
|
||||
service.setTags(Arrays.asList(TAG));
|
||||
|
||||
given(this.lifecycle.getInstanceId()).willReturn(SERVICE_ID);
|
||||
|
||||
given(this.consul.getAgentServices()).willReturn(new Response<>(Collections.singletonMap(SERVICE_ID, service), RAW_RESPONSE));
|
||||
|
||||
ServiceInstance serviceInstance = this.discoveryClient.getLocalServiceInstance();
|
||||
|
||||
assertServiceInstance(serviceInstance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void localServiceInstanceFromConfig() {
|
||||
mockFromConfig(PORT, ADDRESS);
|
||||
|
||||
ServiceInstance serviceInstance = this.discoveryClient.getLocalServiceInstance();
|
||||
|
||||
assertServiceInstance(serviceInstance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void localServiceInstanceFromConfigPortFromServerProperties() {
|
||||
mockFromConfig(0, ADDRESS);
|
||||
|
||||
ServerProperties serverProperties = new ServerProperties();
|
||||
serverProperties.setPort(PORT);
|
||||
this.discoveryClient.setServerProperties(serverProperties);
|
||||
|
||||
ServiceInstance serviceInstance = this.discoveryClient.getLocalServiceInstance();
|
||||
|
||||
this.discoveryClient.setServerProperties(null);
|
||||
|
||||
assertServiceInstance(serviceInstance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void localServiceInstanceFromConfigHostFromAgent() {
|
||||
mockFromConfig(PORT, null);
|
||||
given(this.properties.isPreferAgentAddress()).willReturn(true);
|
||||
Self self = new Self();
|
||||
Member member = new Member();
|
||||
member.setName(ADDRESS);
|
||||
self.setMember(member);
|
||||
given(this.consul.getAgentSelf()).willReturn(new Response<>(self, RAW_RESPONSE));
|
||||
|
||||
ServiceInstance serviceInstance = this.discoveryClient.getLocalServiceInstance();
|
||||
|
||||
assertServiceInstance(serviceInstance);
|
||||
}
|
||||
|
||||
private void assertServiceInstance(ServiceInstance serviceInstance) {
|
||||
assertThat(serviceInstance.getHost()).isEqualTo(ADDRESS);
|
||||
assertThat(serviceInstance.getPort()).isEqualTo(PORT);
|
||||
assertThat(serviceInstance.getServiceId()).isEqualTo(SERVICE_ID);
|
||||
assertThat(serviceInstance.getMetadata()).isNotEmpty().hasSize(1).contains(entry(KEY, VALUE));
|
||||
}
|
||||
|
||||
private void mockFromConfig(int port, String address) {
|
||||
given(this.lifecycle.getInstanceId()).willReturn(SERVICE_ID);
|
||||
NewService service = new NewService();
|
||||
service.setPort(port);
|
||||
given(this.lifecycle.getService()).willReturn(service);
|
||||
given(this.properties.getTags()).willReturn(Arrays.asList(TAG));
|
||||
given(this.properties.getHostname()).willReturn(address);
|
||||
given(this.properties.getLifecycle()).willReturn(new ConsulDiscoveryProperties.Lifecycle());
|
||||
|
||||
given(this.consul.getAgentServices()).willReturn(new Response<>(Collections.<String, Service>emptyMap(), RAW_RESPONSE));
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ImportAutoConfiguration({ ConsulDiscoveryClientConfiguration.class })
|
||||
protected static class LocalServiceTestConfig {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -89,28 +89,6 @@ public class ConsulDiscoveryClientTests {
|
||||
Character.isDigit(instance.getHost().charAt(0)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLocalInstance() {
|
||||
ServiceInstance instance = discoveryClient.getLocalServiceInstance();
|
||||
assertNotNull("instance was null", instance);
|
||||
assertIpAddress(instance);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getLocalInstanceNotRegistered() {
|
||||
ConsulClient mockConsulClient = mock(ConsulClient.class);
|
||||
ConsulDiscoveryProperties properties = new ConsulDiscoveryProperties(new InetUtils(new InetUtilsProperties()));
|
||||
ConsulDiscoveryClient.LocalResolver localResolver = mock(ConsulDiscoveryClient.LocalResolver.class);
|
||||
ConsulDiscoveryClient discoveryClient = new ConsulDiscoveryClient(mockConsulClient, properties, localResolver);
|
||||
|
||||
Response<Map<String, Service>> response = new Response<>(Collections.<String, Service>emptyMap(), null, null, null);
|
||||
|
||||
when(mockConsulClient.getAgentServices()).thenReturn(response);
|
||||
when(localResolver.getPort()).thenReturn(null);
|
||||
|
||||
ServiceInstance serviceInstance = discoveryClient.getLocalServiceInstance();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@EnableAutoConfiguration
|
||||
@EnableDiscoveryClient
|
||||
|
||||
@@ -18,8 +18,6 @@ package org.springframework.cloud.consul.sample;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
@@ -27,9 +25,9 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
|
||||
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.cloud.netflix.feign.EnableFeignClients;
|
||||
import org.springframework.cloud.netflix.feign.FeignClient;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -41,6 +39,8 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@@ -67,12 +67,15 @@ public class SampleConsulApplication /*implements ApplicationListener<SimpleRemo
|
||||
@Autowired
|
||||
private RestTemplate restTemplate;
|
||||
|
||||
@Autowired
|
||||
private Registration registration;
|
||||
|
||||
@Value("${spring.application.name:testConsulApp}")
|
||||
private String appName;
|
||||
|
||||
@RequestMapping("/me")
|
||||
public ServiceInstance me() {
|
||||
return discoveryClient.getLocalServiceInstance();
|
||||
return this.registration;
|
||||
}
|
||||
|
||||
@RequestMapping("/")
|
||||
|
||||
Reference in New Issue
Block a user