Uses precomputed information in RibbonServer if available.

fixes gh-2860
parent 24209635
......@@ -20,15 +20,19 @@ import java.io.IOException;
import java.net.URI;
import java.util.Collections;
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.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;
import static org.springframework.cloud.netflix.ribbon.RibbonUtils.updateToSecureConnectionIfNeeded;
/**
* @author Spencer Gibb
......@@ -50,11 +54,20 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
String serviceId = instance.getServiceId();
RibbonLoadBalancerContext context = this.clientFactory
.getLoadBalancerContext(serviceId);
Server server = new Server(instance.getScheme(), instance.getHost(), instance.getPort());
IClientConfig clientConfig = clientFactory.getClientConfig(serviceId);
ServerIntrospector serverIntrospector = serverIntrospector(serviceId);
URI uri = RibbonUtils.updateToSecureConnectionIfNeeded(original, clientConfig,
serverIntrospector, server);
URI uri;
Server server;
if (instance instanceof RibbonServer) {
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);
}
......
package org.springframework.cloud.netflix.ribbon;
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.IClientConfig;
import com.netflix.config.ConfigurationManager;
......@@ -11,8 +11,9 @@ import com.netflix.config.DynamicPropertyFactory;
import com.netflix.config.DynamicStringProperty;
import com.netflix.loadbalancer.Server;
import java.util.HashMap;
import java.util.Map;
import org.springframework.cloud.client.ServiceInstance;
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.EnableZoneAffinity;
......@@ -101,6 +102,30 @@ public class RibbonUtils {
* This assumes the uri is already encoded to avoid double encoding.
*
* @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 serverIntrospector
* @param server
......
......@@ -22,22 +22,22 @@ import java.net.URL;
import java.util.Collections;
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.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerRequest;
import org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.RibbonServer;
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.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
......@@ -69,13 +69,23 @@ public class RibbonLoadBalancerClientTests {
@Mock
private ServerStats serverStats;
private Server server = null;
@Before
public void init() {
server = null;
MockitoAnnotations.initMocks(this);
given(this.clientFactory.getLoadBalancerContext(anyString())).willReturn(
new RibbonLoadBalancerContext(this.loadBalancer));
given(this.clientFactory.getInstance(anyString(), eq(ServerIntrospector.class)))
.willReturn(new DefaultServerIntrospector() {
@Override
public boolean isSecure(Server server) {
RibbonLoadBalancerClientTests.this.server = server;
return super.isSecure(server);
}
@Override
public Map<String, String> getMetadata(Server server) {
return Collections.singletonMap("mykey", "myvalue");
......@@ -102,6 +112,8 @@ public class RibbonLoadBalancerClientTests {
assertThat(uri).hasScheme(scheme)
.hasHost(serviceInstance.getHost())
.hasPort(serviceInstance.getPort());
assertThat(this.server).isNotNull()
.isInstanceOf(MyServer.class);
}
@Test
......@@ -256,15 +268,23 @@ public class RibbonLoadBalancerClientTests {
}
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"));
}
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"));
}
protected static class MyServer extends Server {
public MyServer(String host, int port) {
super(host, port);
}
}
protected void verifyServerStats() {
verify(this.serverStats).incrementActiveRequestsCount();
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