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
29ad943f
Unverified
Commit
29ad943f
authored
Jan 23, 2018
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use scheme in load balancing if provided.
parent
d4e7f25e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
45 additions
and
30 deletions
+45
-30
RibbonLoadBalancerClient.java
...mework/cloud/netflix/ribbon/RibbonLoadBalancerClient.java
+6
-1
RibbonLoadBalancerClientTests.java
...k/cloud/netflix/ribbon/RibbonLoadBalancerClientTests.java
+39
-29
No files found.
spring-cloud-netflix-ribbon/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonLoadBalancerClient.java
View file @
29ad943f
...
...
@@ -50,7 +50,7 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
String
serviceId
=
instance
.
getServiceId
();
RibbonLoadBalancerContext
context
=
this
.
clientFactory
.
getLoadBalancerContext
(
serviceId
);
Server
server
=
new
Server
(
instance
.
getHost
(),
instance
.
getPort
());
Server
server
=
new
Server
(
instance
.
get
Scheme
(),
instance
.
get
Host
(),
instance
.
getPort
());
IClientConfig
clientConfig
=
clientFactory
.
getClientConfig
(
serviceId
);
ServerIntrospector
serverIntrospector
=
serverIntrospector
(
serviceId
);
URI
uri
=
RibbonUtils
.
updateToSecureConnectionIfNeeded
(
original
,
clientConfig
,
...
...
@@ -195,6 +195,11 @@ public class RibbonLoadBalancerClient implements LoadBalancerClient {
}
@Override
public
String
getScheme
()
{
return
this
.
server
.
getScheme
();
}
@Override
public
String
toString
()
{
final
StringBuffer
sb
=
new
StringBuffer
(
"RibbonServer{"
);
sb
.
append
(
"serviceId='"
).
append
(
serviceId
).
append
(
'\''
);
...
...
spring-cloud-netflix-ribbon/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonLoadBalancerClientTests.java
View file @
29ad943f
...
...
@@ -21,6 +21,7 @@ import java.net.URI;
import
java.net.URL
;
import
java.util.Collections
;
import
java.util.Map
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.mockito.Mock
;
...
...
@@ -28,7 +29,9 @@ 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
org.springframework.web.util.DefaultUriTemplateHandler
;
import
com.netflix.client.config.CommonClientConfigKey
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.loadbalancer.BaseLoadBalancer
;
...
...
@@ -36,12 +39,10 @@ import com.netflix.loadbalancer.LoadBalancerStats;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.ServerStats
;
import
static
org
.
hamcrest
.
Matchers
.
instanceOf
;
import
static
org
.
hamcrest
.
Matchers
.
is
;
import
static
org
.
assertj
.
core
.
api
.
Assertions
.
assertThat
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertNull
;
import
static
org
.
junit
.
Assert
.
assertThat
;
import
static
org
.
junit
.
Assert
.
fail
;
import
static
org
.
mockito
.
BDDMockito
.
given
;
import
static
org
.
mockito
.
Matchers
.
anyDouble
;
...
...
@@ -99,8 +100,9 @@ public class RibbonLoadBalancerClientTests {
ServiceInstance
serviceInstance
=
client
.
choose
(
server
.
getServiceId
());
URI
uri
=
client
.
reconstructURI
(
serviceInstance
,
new
URL
(
scheme
+
"://"
+
server
.
getServiceId
()).
toURI
());
assertEquals
(
server
.
getHost
(),
uri
.
getHost
());
assertEquals
(
server
.
getPort
(),
uri
.
getPort
());
assertThat
(
uri
).
hasScheme
(
scheme
)
.
hasHost
(
serviceInstance
.
getHost
())
.
hasPort
(
serviceInstance
.
getPort
());
}
@Test
...
...
@@ -122,10 +124,27 @@ public class RibbonLoadBalancerClientTests {
RibbonLoadBalancerClient
client
=
getRibbonLoadBalancerClient
(
server
);
ServiceInstance
serviceInstance
=
client
.
choose
(
server
.
getServiceId
());
URI
expanded
=
new
DefaultUri
TemplateHandler
()
URI
expanded
=
new
DefaultUri
BuilderFactory
()
.
expand
(
scheme
+
"://"
+
server
.
getServiceId
()
+
path
);
URI
reconstructed
=
client
.
reconstructURI
(
serviceInstance
,
expanded
);
assertEquals
(
expanded
.
getPath
(),
reconstructed
.
getPath
());
assertThat
(
reconstructed
).
hasPath
(
path
);
}
@Test
public
void
testReconstructHonorsRibbonServerScheme
()
{
RibbonServer
server
=
new
RibbonServer
(
"testService"
,
new
Server
(
"ws"
,
"myhost"
,
9080
),
false
,
Collections
.
singletonMap
(
"mykey"
,
"myvalue"
));
IClientConfig
config
=
mock
(
IClientConfig
.
class
);
when
(
config
.
get
(
CommonClientConfigKey
.
IsSecure
)).
thenReturn
(
false
);
when
(
clientFactory
.
getClientConfig
(
server
.
getServiceId
())).
thenReturn
(
config
);
RibbonLoadBalancerClient
client
=
getRibbonLoadBalancerClient
(
server
);
ServiceInstance
serviceInstance
=
client
.
choose
(
server
.
getServiceId
());
URI
uri
=
client
.
reconstructURI
(
serviceInstance
,
URI
.
create
(
"http://testService"
));
assertThat
(
uri
).
hasScheme
(
"ws"
).
hasHost
(
"myhost"
).
hasPort
(
9080
);
}
@Test
...
...
@@ -193,13 +212,10 @@ public class RibbonLoadBalancerClientTests {
RibbonLoadBalancerClient
client
=
getRibbonLoadBalancerClient
(
server
);
final
String
returnVal
=
"myval"
;
Object
actualReturn
=
client
.
execute
(
server
.
getServiceId
(),
new
LoadBalancerRequest
<
Object
>()
{
@Override
public
Object
apply
(
ServiceInstance
instance
)
throws
Exception
{
assertServiceInstance
(
server
,
instance
);
return
returnVal
;
}
});
(
LoadBalancerRequest
<
Object
>)
instance
->
{
assertServiceInstance
(
server
,
instance
);
return
returnVal
;
});
verifyServerStats
();
assertEquals
(
"retVal was wrong"
,
returnVal
,
actualReturn
);
}
...
...
@@ -210,13 +226,10 @@ public class RibbonLoadBalancerClientTests {
RibbonLoadBalancerClient
client
=
getRibbonLoadBalancerClient
(
ribbonServer
);
try
{
client
.
execute
(
ribbonServer
.
getServiceId
(),
new
LoadBalancerRequest
<
Object
>()
{
@Override
public
Object
apply
(
ServiceInstance
instance
)
throws
Exception
{
assertServiceInstance
(
ribbonServer
,
instance
);
throw
new
RuntimeException
();
}
});
instance
->
{
assertServiceInstance
(
ribbonServer
,
instance
);
throw
new
RuntimeException
();
});
fail
(
"Should have thrown exception"
);
}
catch
(
Exception
ex
)
{
...
...
@@ -231,17 +244,14 @@ public class RibbonLoadBalancerClientTests {
RibbonLoadBalancerClient
client
=
getRibbonLoadBalancerClient
(
ribbonServer
);
try
{
client
.
execute
(
ribbonServer
.
getServiceId
(),
new
LoadBalancerRequest
<
Object
>()
{
@Override
public
Object
apply
(
ServiceInstance
instance
)
throws
Exception
{
assertServiceInstance
(
ribbonServer
,
instance
);
throw
new
IOException
();
}
});
instance
->
{
assertServiceInstance
(
ribbonServer
,
instance
);
throw
new
IOException
();
});
fail
(
"Should have thrown exception"
);
}
catch
(
Exception
ex
)
{
assertThat
(
"wrong exception type"
,
ex
,
is
(
instanceOf
(
IOException
.
class
))
);
assertThat
(
ex
).
isInstanceOf
(
IOException
.
class
);
}
verifyServerStats
();
}
...
...
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