Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
S
spring-cloud-netflix
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
openSource
spring-cloud-netflix
Commits
0a8f594b
Unverified
Commit
0a8f594b
authored
Apr 18, 2018
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Uses precomputed information in RibbonServer if available.
fixes gh-2860
parent
24209635
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
79 additions
and
21 deletions
+79
-21
RibbonLoadBalancerClient.java
...mework/cloud/netflix/ribbon/RibbonLoadBalancerClient.java
+21
-8
RibbonUtils.java
...org/springframework/cloud/netflix/ribbon/RibbonUtils.java
+29
-4
RibbonLoadBalancerClientTests.java
...k/cloud/netflix/ribbon/RibbonLoadBalancerClientTests.java
+29
-9
No files found.
spring-cloud-netflix-ribbon/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonLoadBalancerClient.java
View file @
0a8f594b
...
...
@@ -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
);
}
...
...
spring-cloud-netflix-ribbon/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonUtils.java
View file @
0a8f594b
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
...
...
spring-cloud-netflix-ribbon/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonLoadBalancerClientTests.java
View file @
0a8f594b
...
...
@@ -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
My
Server
(
"myhost"
,
9080
),
false
,
Collections
.
singletonMap
(
"mykey"
,
"myvalue"
));
}
protected
RibbonServer
getSecureRibbonServer
()
{
return
new
RibbonServer
(
"testService"
,
new
Server
(
"myhost"
,
8443
),
false
,
return
new
RibbonServer
(
"testService"
,
new
My
Server
(
"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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment