Uses precomputed information in RibbonServer if available.

fixes gh-2860
parent 24209635
...@@ -20,15 +20,19 @@ import java.io.IOException; ...@@ -20,15 +20,19 @@ import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest; import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer; import static org.springframework.cloud.netflix.ribbon.RibbonUtils.updateToSecureConnectionIfNeeded;
import com.netflix.loadbalancer.Server;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
...@@ -50,11 +54,20 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient { ...@@ -50,11 +54,20 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
String serviceId = instance.getServiceId(); String serviceId = instance.getServiceId();
RibbonLoadBalancerContext context = this.clientFactory RibbonLoadBalancerContext context = this.clientFactory
.getLoadBalancerContext(serviceId); .getLoadBalancerContext(serviceId);
Server server = new Server(instance.getScheme(), instance.getHost(), instance.getPort());
IClientConfig clientConfig = clientFactory.getClientConfig(serviceId); URI uri;
ServerIntrospector serverIntrospector = serverIntrospector(serviceId); Server server;
URI uri = RibbonUtils.updateToSecureConnectionIfNeeded(original, clientConfig, if (instance instanceof RibbonServer) {
serverIntrospector, server); RibbonServer ribbonServer = (RibbonServer) instance;
server = ribbonServer.getServer();
uri = updateToSecureConnectionIfNeeded(original, ribbonServer);
} else {
server = new Server(instance.getScheme(), instance.getHost(), instance.getPort());
IClientConfig clientConfig = clientFactory.getClientConfig(serviceId);
ServerIntrospector serverIntrospector = serverIntrospector(serviceId);
uri = updateToSecureConnectionIfNeeded(original, clientConfig,
serverIntrospector, server);
}
return context.reconstructURIWithServer(server, uri); return context.reconstructURIWithServer(server, uri);
} }
......
package org.springframework.cloud.netflix.ribbon; package org.springframework.cloud.netflix.ribbon;
import java.net.URI; import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
import com.netflix.client.config.CommonClientConfigKey; import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.IClientConfig; import com.netflix.client.config.IClientConfig;
import com.netflix.config.ConfigurationManager; import com.netflix.config.ConfigurationManager;
...@@ -11,8 +11,9 @@ import com.netflix.config.DynamicPropertyFactory; ...@@ -11,8 +11,9 @@ import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty; import com.netflix.config.DynamicStringProperty;
import com.netflix.loadbalancer.Server; import com.netflix.loadbalancer.Server;
import java.util.HashMap; import org.springframework.cloud.client.ServiceInstance;
import java.util.Map; import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponentsBuilder;
import static com.netflix.client.config.CommonClientConfigKey.DeploymentContextBasedVipAddresses; import static com.netflix.client.config.CommonClientConfigKey.DeploymentContextBasedVipAddresses;
import static com.netflix.client.config.CommonClientConfigKey.EnableZoneAffinity; import static com.netflix.client.config.CommonClientConfigKey.EnableZoneAffinity;
...@@ -101,6 +102,30 @@ public class RibbonUtils { ...@@ -101,6 +102,30 @@ public class RibbonUtils {
* This assumes the uri is already encoded to avoid double encoding. * This assumes the uri is already encoded to avoid double encoding.
* *
* @param uri * @param uri
* @param ribbonServer
* @return
*/
static URI updateToSecureConnectionIfNeeded(URI uri, ServiceInstance ribbonServer) {
String scheme = uri.getScheme();
if (StringUtils.isEmpty(scheme)) {
scheme = "http";
}
if (!StringUtils.isEmpty(uri.toString())
&& unsecureSchemeMapping.containsKey(scheme)
&& ribbonServer.isSecure()) {
return upgradeConnection(uri, unsecureSchemeMapping.get(scheme));
}
return uri;
}
/**
* Replace the scheme to the secure variant if needed. If the {@link #unsecureSchemeMapping} map contains the uri
* scheme and {@link #isSecure(IClientConfig, ServerIntrospector, Server)} is true, update the scheme.
* This assumes the uri is already encoded to avoid double encoding.
*
* @param uri
* @param config * @param config
* @param serverIntrospector * @param serverIntrospector
* @param server * @param server
......
...@@ -22,22 +22,22 @@ import java.net.URL; ...@@ -22,22 +22,22 @@ import java.net.URL;
import java.util.Collections; import java.util.Collections;
import java.util.Map; import java.util.Map;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.LoadBalancerStats;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerStats;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest; import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer; import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer;
import org.springframework.web.util.DefaultUriBuilderFactory; import org.springframework.web.util.DefaultUriBuilderFactory;
import com.netflix.client.config.CommonClientConfigKey;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.LoadBalancerStats;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerStats;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
...@@ -69,13 +69,23 @@ public class RibbonLoadBalancerClientTests { ...@@ -69,13 +69,23 @@ public class RibbonLoadBalancerClientTests {
@Mock @Mock
private ServerStats serverStats; private ServerStats serverStats;
private Server server = null;
@Before @Before
public void init() { public void init() {
server = null;
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
given(this.clientFactory.getLoadBalancerContext(anyString())).willReturn( given(this.clientFactory.getLoadBalancerContext(anyString())).willReturn(
new RibbonLoadBalancerContext(this.loadBalancer)); new RibbonLoadBalancerContext(this.loadBalancer));
given(this.clientFactory.getInstance(anyString(), eq(ServerIntrospector.class))) given(this.clientFactory.getInstance(anyString(), eq(ServerIntrospector.class)))
.willReturn(new DefaultServerIntrospector() { .willReturn(new DefaultServerIntrospector() {
@Override
public boolean isSecure(Server server) {
RibbonLoadBalancerClientTests.this.server = server;
return super.isSecure(server);
}
@Override @Override
public Map<String, String> getMetadata(Server server) { public Map<String, String> getMetadata(Server server) {
return Collections.singletonMap("mykey", "myvalue"); return Collections.singletonMap("mykey", "myvalue");
...@@ -102,6 +112,8 @@ public class RibbonLoadBalancerClientTests { ...@@ -102,6 +112,8 @@ public class RibbonLoadBalancerClientTests {
assertThat(uri).hasScheme(scheme) assertThat(uri).hasScheme(scheme)
.hasHost(serviceInstance.getHost()) .hasHost(serviceInstance.getHost())
.hasPort(serviceInstance.getPort()); .hasPort(serviceInstance.getPort());
assertThat(this.server).isNotNull()
.isInstanceOf(MyServer.class);
} }
@Test @Test
...@@ -256,15 +268,23 @@ public class RibbonLoadBalancerClientTests { ...@@ -256,15 +268,23 @@ public class RibbonLoadBalancerClientTests {
} }
protected RibbonServer getRibbonServer() { protected RibbonServer getRibbonServer() {
return new RibbonServer("testService", new Server("myhost", 9080), false, return new RibbonServer("testService", new MyServer("myhost", 9080), false,
Collections.singletonMap("mykey", "myvalue")); Collections.singletonMap("mykey", "myvalue"));
} }
protected RibbonServer getSecureRibbonServer() { protected RibbonServer getSecureRibbonServer() {
return new RibbonServer("testService", new Server("myhost", 8443), false, return new RibbonServer("testService", new MyServer("myhost", 8443), false,
Collections.singletonMap("mykey", "myvalue")); Collections.singletonMap("mykey", "myvalue"));
} }
protected static class MyServer extends Server {
public MyServer(String host, int port) {
super(host, port);
}
}
protected void verifyServerStats() { protected void verifyServerStats() {
verify(this.serverStats).incrementActiveRequestsCount(); verify(this.serverStats).incrementActiveRequestsCount();
verify(this.serverStats).decrementActiveRequestsCount(); verify(this.serverStats).decrementActiveRequestsCount();
......
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