Use updated Registration rather than getLocalServiceInstance()

parent 6c65266f
......@@ -33,6 +33,7 @@ import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.cloud.client.discovery.event.HeartbeatMonitor;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.commons.httpclient.ApacheHttpClientConnectionManagerFactory;
import org.springframework.cloud.commons.httpclient.ApacheHttpClientFactory;
import org.springframework.cloud.commons.httpclient.HttpClientConfiguration;
......@@ -72,6 +73,9 @@ public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {
@Autowired(required = false)
private List<RibbonRequestCustomizer> requestCustomizers = Collections.emptyList();
@Autowired(required = false)
private Registration registration;
@Autowired
private DiscoveryClient discovery;
......@@ -88,7 +92,7 @@ public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {
@ConditionalOnMissingBean(DiscoveryClientRouteLocator.class)
public DiscoveryClientRouteLocator discoveryRouteLocator() {
return new DiscoveryClientRouteLocator(this.server.getServletPrefix(),
this.discovery, this.zuulProperties, this.serviceRouteMapper);
this.discovery, this.zuulProperties, this.serviceRouteMapper, this.registration);
}
// pre filters
......
......@@ -53,22 +53,20 @@ public class DiscoveryClientRouteLocator extends SimpleRouteLocator
private ServiceRouteMapper serviceRouteMapper;
@Deprecated
public DiscoveryClientRouteLocator(String servletPath, DiscoveryClient discovery,
ZuulProperties properties) {
ZuulProperties properties) {
this(servletPath, discovery, properties, (ServiceInstance)null);
}
public DiscoveryClientRouteLocator(String servletPath, DiscoveryClient discovery,
ZuulProperties properties, ServiceInstance localServiceInstance) {
super(servletPath, properties);
if (properties.isIgnoreLocalService()) {
ServiceInstance instance = null;
try {
instance = discovery.getLocalServiceInstance();
} catch (Exception e) {
log.warn("Error locating local service instance", e);
}
if (instance != null) {
String localServiceId = instance.getServiceId();
if (!properties.getIgnoredServices().contains(localServiceId)) {
properties.getIgnoredServices().add(localServiceId);
}
if (properties.isIgnoreLocalService() && localServiceInstance != null) {
String localServiceId = localServiceInstance.getServiceId();
if (!properties.getIgnoredServices().contains(localServiceId)) {
properties.getIgnoredServices().add(localServiceId);
}
}
this.serviceRouteMapper = new SimpleServiceRouteMapper();
......@@ -76,9 +74,16 @@ public class DiscoveryClientRouteLocator extends SimpleRouteLocator
this.properties = properties;
}
@Deprecated
public DiscoveryClientRouteLocator(String servletPath, DiscoveryClient discovery,
ZuulProperties properties, ServiceRouteMapper serviceRouteMapper) {
this(servletPath, discovery, properties);
this(servletPath, discovery, properties, (ServiceInstance)null);
this.serviceRouteMapper = serviceRouteMapper;
}
public DiscoveryClientRouteLocator(String servletPath, DiscoveryClient discovery,
ZuulProperties properties, ServiceRouteMapper serviceRouteMapper, ServiceInstance localServiceInstance) {
this(servletPath, discovery, properties, localServiceInstance);
this.serviceRouteMapper = serviceRouteMapper;
}
......
......@@ -16,25 +16,26 @@
package org.springframework.cloud.netflix.zuul.filters.discovery;
import java.net.URI;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import com.netflix.zuul.context.RequestContext;
import java.util.Map;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.netflix.zuul.filters.Route;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.cloud.netflix.zuul.util.RequestUtils;
import org.springframework.core.env.ConfigurableEnvironment;
import com.netflix.zuul.context.RequestContext;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
......@@ -603,11 +604,40 @@ public class DiscoveryClientRouteLocatorTests {
public void testIgnoredLocalServiceByDefault() {
given(this.discovery.getServices())
.willReturn(Collections.singletonList(MYSERVICE));
given(this.discovery.getLocalServiceInstance()).willReturn(
new DefaultServiceInstance(MYSERVICE, "localhost", 80, false));
Registration registration = new Registration() {
@Override
public String getServiceId() {
return MYSERVICE;
}
@Override
public String getHost() {
return "localhost";
}
@Override
public int getPort() {
return 80;
}
@Override
public boolean isSecure() {
return false;
}
@Override
public URI getUri() {
return null;
}
@Override
public Map<String, String> getMetadata() {
return null;
}
};
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/",
this.discovery, this.properties);
this.discovery, this.properties, registration);
LinkedHashMap<String, ZuulRoute> routes = routeLocator.locateRoutes();
ZuulRoute actual = routes.get("/**");
......@@ -637,10 +667,9 @@ public class DiscoveryClientRouteLocatorTests {
@Test
public void testLocalServiceExceptionIgnored() {
given(this.discovery.getServices()).willReturn(Collections.<String>emptyList());
given(this.discovery.getLocalServiceInstance()).willThrow(new RuntimeException());
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/",
this.discovery, this.properties);
this.discovery, this.properties, (Registration)null);
// if no exception is thrown in constructor, this is a success
routeLocator.locateRoutes();
......
......@@ -19,12 +19,15 @@ package org.springframework.cloud.netflix.eureka.serviceregistry;
import java.io.Closeable;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.netflix.eureka.CloudEurekaClient;
import org.springframework.cloud.netflix.eureka.CloudEurekaInstanceConfig;
......@@ -118,6 +121,34 @@ public class EurekaRegistration implements Registration, Closeable {
return this.instanceConfig.getAppname();
}
@Override
public String getHost() {
return this.instanceConfig.getHostName(false);
}
@Override
public int getPort() {
if (this.instanceConfig.getSecurePortEnabled()) {
return this.instanceConfig.getSecurePort();
}
return this.instanceConfig.getNonSecurePort();
}
@Override
public boolean isSecure() {
return this.instanceConfig.getSecurePortEnabled();
}
@Override
public URI getUri() {
return DefaultServiceInstance.getUri(this);
}
@Override
public Map<String, String> getMetadata() {
return this.instanceConfig.getMetadataMap();
}
public CloudEurekaClient getEurekaClient() {
if (this.cloudEurekaClient.get() == null) {
try {
......
......@@ -83,7 +83,7 @@ public class EurekaSampleApplication implements ApplicationContextAware, Closeab
@RequestMapping("/")
public String home() {
return "Hello world "+discoveryClient.getLocalServiceInstance().getUri();
return "Hello world "+ registration.getUri();
}
@Override
......
......@@ -56,7 +56,7 @@ import static org.springframework.restdocs.restassured.operation.preprocess.Rest
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"spring.jmx.enabled=true", "management.security.enabled=false" })
"spring.jmx.enabled=false", "management.security.enabled=false" })
@DirtiesContext
public abstract class AbstractDocumentationTests {
......
......@@ -85,6 +85,11 @@
<artifactId>spring-cloud-contract-verifier</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
......
......@@ -18,14 +18,12 @@ package org.springframework.cloud.netflix.hystrix.stream;
import javax.annotation.PostConstruct;
import com.netflix.hystrix.HystrixCircuitBreaker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.actuator.HasFeatures;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.cloud.stream.config.BindingProperties;
......@@ -35,6 +33,8 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.MessageChannel;
import org.springframework.scheduling.annotation.EnableScheduling;
import com.netflix.hystrix.HystrixCircuitBreaker;
/**
* Autoconfiguration for a Spring Cloud Hystrix on Spring Cloud Stream. Enabled by default
* if spring-cloud-stream is on the classpath, and can be switched off with
......@@ -64,6 +64,9 @@ public class HystrixStreamAutoConfiguration {
@Output(HystrixStreamClient.OUTPUT)
private MessageChannel outboundChannel;
@Autowired(required = false)
private Registration registration;
@Bean
public HasFeatures hystrixStreamQueueFeature() {
return HasFeatures.namedFeature("Hystrix Stream (Queue)",
......@@ -94,8 +97,8 @@ public class HystrixStreamAutoConfiguration {
}
@Bean
public HystrixStreamTask hystrixStreamTask(DiscoveryClient discoveryClient) {
return new HystrixStreamTask(this.outboundChannel, discoveryClient,
public HystrixStreamTask hystrixStreamTask() {
return new HystrixStreamTask(this.outboundChannel, this.registration,
this.properties);
}
......
......@@ -37,7 +37,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.messaging.MessageChannel;
......@@ -57,7 +57,7 @@ public class HystrixStreamTask implements ApplicationContextAware {
private MessageChannel outboundChannel;
private DiscoveryClient discoveryClient;
private Registration registration;
private HystrixStreamProperties properties;
......@@ -69,9 +69,9 @@ public class HystrixStreamTask implements ApplicationContextAware {
private final JsonFactory jsonFactory = new JsonFactory();
public HystrixStreamTask(MessageChannel outboundChannel,
DiscoveryClient discoveryClient, HystrixStreamProperties properties) {
Registration registration, HystrixStreamProperties properties) {
this.outboundChannel = outboundChannel;
this.discoveryClient = discoveryClient;
this.registration = registration;
this.properties = properties;
this.jsonMetrics = new LinkedBlockingQueue<>(properties.getSize());
}
......@@ -121,8 +121,6 @@ public class HystrixStreamTask implements ApplicationContextAware {
log.trace("gathering metrics size: " + instances.size());
}
ServiceInstance localService = this.discoveryClient.getLocalServiceInstance();
for (HystrixCommandMetrics commandMetrics : instances) {
HystrixCommandKey key = commandMetrics.getCommandKey();
HystrixCircuitBreaker circuitBreaker = HystrixCircuitBreaker.Factory
......@@ -133,13 +131,13 @@ public class HystrixStreamTask implements ApplicationContextAware {
json.writeStartObject();
addServiceData(json, localService);
addServiceData(json, registration);
json.writeObjectFieldStart("data");
json.writeStringField("type", "HystrixCommand");
String name = key.name();
if (this.properties.isPrefixMetricName()) {
name = localService.getServiceId() + "." + name;
if (this.properties.isPrefixMetricName() && registration != null) {
name = registration.getServiceId() + "." + name;
}
json.writeStringField("name", name);
......@@ -322,7 +320,7 @@ public class HystrixStreamTask implements ApplicationContextAware {
JsonGenerator json = this.jsonFactory.createGenerator(jsonString);
json.writeStartObject();
addServiceData(json, localService);
addServiceData(json, this.registration);
json.writeObjectFieldStart("data");
json.writeStringField("type", "HystrixThreadPool");
......
package org.springframework.cloud.netflix.hystrix.stream;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandMetrics;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesCommandDefault;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.context.ApplicationContext;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandMetrics;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesCommandDefault;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verifyZeroInteractions;
......@@ -34,7 +33,7 @@ public class HystrixStreamTaskTests {
@Mock DiscoveryClient discoveryClient;
@Mock ApplicationContext context;
@Spy HystrixStreamProperties properties;
@Mock ServiceInstance serviceInstance;
@Mock Registration registration;
@InjectMocks HystrixStreamTask hystrixStreamTask;
@Test
......@@ -59,7 +58,6 @@ public class HystrixStreamTaskTests {
HystrixCommandMetrics.getInstance(hystrixCommandKey,
HystrixCommandGroupKey.Factory.asKey("commandGroupKey"),
new HystrixPropertiesCommandDefault(hystrixCommandKey, HystrixCommandProperties.defaultSetter()));
given(this.discoveryClient.getLocalServiceInstance()).willReturn(this.serviceInstance);
this.hystrixStreamTask.setApplicationContext(this.context);
this.hystrixStreamTask.gatherMetrics();
......
......@@ -16,38 +16,37 @@
package org.springframework.cloud.netflix.hystrix.stream;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.stream.test.binder.MessageCollector;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Spencer Gibb
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = HystrixStreamTests.Application.class, webEnvironment = WebEnvironment.RANDOM_PORT, value = {
"server.port=0", "spring.jmx.enabled=true", "spring.application.name=mytestapp" })
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"debug=true", "spring.jmx.enabled=true", "spring.application.name=mytestapp" })
@DirtiesContext
public class HystrixStreamTests {
......@@ -57,8 +56,8 @@ public class HystrixStreamTests {
@Autowired
private Application application;
@Autowired
private DiscoveryClient discoveryClient;
@Autowired(required = false)
private Registration registration;
@Autowired
private ObjectMapper mapper;
......@@ -73,7 +72,7 @@ public class HystrixStreamTests {
@EnableAutoConfiguration
@EnableCircuitBreaker
@RestController
@Configuration
@SpringBootConfiguration
public static class Application {
@HystrixCommand
......@@ -88,9 +87,8 @@ public class HystrixStreamTests {
this.application.hello();
// It is important that local service instance resolves for metrics
// origin details to be populated
ServiceInstance localServiceInstance = discoveryClient.getLocalServiceInstance();
assertThat(localServiceInstance).isNotNull();
assertThat(localServiceInstance.getServiceId()).isEqualTo("mytestapp");
assertThat(this.registration).isNotNull();
assertThat(this.registration.getServiceId()).isEqualTo("mytestapp");
this.task.gatherMetrics();
Message<?> message = this.collector.forChannel(output).take();
assertThat(message.getPayload()).isInstanceOf(String.class);
......
......@@ -42,7 +42,7 @@ import org.springframework.web.bind.annotation.RestController;
* @author Marius Bogoevici
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class)
@SpringBootTest(classes = TestApplication.class, properties = "spring.application.name=application")
@AutoConfigureMessageVerifier
public abstract class StreamSourceTestBase {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment