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
162eb1a2
Commit
162eb1a2
authored
Nov 21, 2014
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow ipAddress to be used based on ribbon.UseIPAddrForServer.
fixes gh-67
parent
b6426e1b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
98 additions
and
7 deletions
+98
-7
DomainExtractingServerList.java
...oud/netflix/ribbon/eureka/DomainExtractingServerList.java
+14
-6
EurekaRibbonClientPreprocessor.java
...netflix/ribbon/eureka/EurekaRibbonClientPreprocessor.java
+1
-1
DomainExtractingServerListTests.java
...etflix/ribbon/eureka/DomainExtractingServerListTests.java
+83
-0
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/eureka/DomainExtractingServerList.java
View file @
162eb1a2
...
...
@@ -18,6 +18,8 @@ package org.springframework.cloud.netflix.ribbon.eureka;
import
java.util.ArrayList
;
import
java.util.List
;
import
com.netflix.client.config.CommonClientConfigKey
;
import
com.netflix.client.config.IClientConfig
;
import
lombok.Getter
;
import
lombok.Setter
;
...
...
@@ -35,9 +37,11 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
public
class
DomainExtractingServerList
implements
ServerList
<
Server
>
{
private
ServerList
<
Server
>
list
;
private
IClientConfig
clientConfig
;
public
DomainExtractingServerList
(
ServerList
<
Server
>
list
)
{
public
DomainExtractingServerList
(
ServerList
<
Server
>
list
,
IClientConfig
clientConfig
)
{
this
.
list
=
list
;
this
.
clientConfig
=
clientConfig
;
}
@Override
...
...
@@ -53,10 +57,13 @@ public class DomainExtractingServerList implements ServerList<Server> {
}
private
List
<
Server
>
setZones
(
List
<
Server
>
servers
)
{
List
<
Server
>
result
=
new
ArrayList
<
Server
>();
List
<
Server
>
result
=
new
ArrayList
<>();
boolean
isSecure
=
clientConfig
.
getPropertyAsBoolean
(
CommonClientConfigKey
.
IsSecure
,
Boolean
.
TRUE
);
boolean
shouldUseIpAddr
=
clientConfig
.
getPropertyAsBoolean
(
CommonClientConfigKey
.
UseIPAddrForServer
,
Boolean
.
FALSE
);
for
(
Server
server
:
servers
)
{
if
(
server
instanceof
DiscoveryEnabledServer
)
{
result
.
add
(
new
DomainExtractingServer
((
DiscoveryEnabledServer
)
server
));
result
.
add
(
new
DomainExtractingServer
((
DiscoveryEnabledServer
)
server
,
isSecure
,
shouldUseIpAddr
));
}
else
{
result
.
add
(
server
);
...
...
@@ -73,8 +80,8 @@ class DomainExtractingServer extends DiscoveryEnabledServer {
@Setter
private
String
id
;
public
DomainExtractingServer
(
DiscoveryEnabledServer
serve
r
)
{
super
(
server
.
getInstanceInfo
(),
true
,
false
);
public
DomainExtractingServer
(
DiscoveryEnabledServer
server
,
boolean
useSecurePort
,
boolean
useIpAdd
r
)
{
super
(
server
.
getInstanceInfo
(),
useSecurePort
,
useIpAddr
);
setZone
(
extractApproximateZone
(
server
));
setId
(
extractId
(
server
));
}
...
...
@@ -87,7 +94,7 @@ class DomainExtractingServer extends DiscoveryEnabledServer {
return
instance
.
getMetadata
().
get
(
"instanceId"
);
}
}
return
serv
er
.
getId
();
return
sup
er
.
getId
();
}
private
String
extractApproximateZone
(
Server
server
)
{
...
...
@@ -104,3 +111,4 @@ class DomainExtractingServer extends DiscoveryEnabledServer {
}
}
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientPreprocessor.java
View file @
162eb1a2
...
...
@@ -81,7 +81,7 @@ public class EurekaRibbonClientPreprocessor implements RibbonClientPreprocessor
// the server zone is populated. TODO: find a way to back off if AWS
// metadata *is* available.
// @see com.netflix.appinfo.AmazonInfo.Builder
dynamic
.
setServerListImpl
(
new
DomainExtractingServerList
(
list
));
dynamic
.
setServerListImpl
(
new
DomainExtractingServerList
(
list
,
dynamic
.
getClientConfig
()
));
}
}
}
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/eureka/DomainExtractingServerListTests.java
0 → 100644
View file @
162eb1a2
package
org
.
springframework
.
cloud
.
netflix
.
ribbon
.
eureka
;
import
com.google.common.collect.ImmutableMap
;
import
com.netflix.appinfo.InstanceInfo
;
import
com.netflix.client.config.CommonClientConfigKey
;
import
com.netflix.client.config.DefaultClientConfigImpl
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.ServerList
;
import
com.netflix.niws.loadbalancer.DiscoveryEnabledServer
;
import
org.junit.Test
;
import
java.util.Arrays
;
import
java.util.List
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
mockito
.
Mockito
.*;
/**
* @author Spencer Gibb
*/
public
class
DomainExtractingServerListTests
{
static
final
String
IP_ADDR
=
"10.0.0.2"
;
static
final
int
PORT
=
8080
;
static
final
String
ZONE
=
"myzone.mydomain.com"
;
static
final
String
HOST_NAME
=
"myHostName."
+
ZONE
;
static
final
String
INSTANCE_ID
=
"myInstanceId"
;
@Test
public
void
testDomainExtractingServer
()
{
DomainExtractingServerList
serverList
=
getDomainExtractingServerList
(
new
DefaultClientConfigImpl
());
List
<
Server
>
servers
=
serverList
.
getInitialListOfServers
();
assertNotNull
(
"servers was null"
,
servers
);
assertEquals
(
"servers was not size 1"
,
1
,
servers
.
size
());
DomainExtractingServer
des
=
assertDomainExtractingServer
(
servers
);
assertEquals
(
"hostPort was wrong"
,
HOST_NAME
+
":"
+
PORT
,
des
.
getHostPort
());
}
protected
DomainExtractingServer
assertDomainExtractingServer
(
List
<
Server
>
servers
)
{
Server
actualServer
=
servers
.
get
(
0
);
assertTrue
(
"server was not a DomainExtractingServer"
,
actualServer
instanceof
DomainExtractingServer
);
DomainExtractingServer
des
=
DomainExtractingServer
.
class
.
cast
(
actualServer
);
assertEquals
(
"zone was wrong"
,
ZONE
,
des
.
getZone
());
assertEquals
(
"instanceId was wrong"
,
INSTANCE_ID
,
des
.
getId
());
return
des
;
}
@Test
public
void
testDomainExtractingServerUseIpAddress
()
{
DefaultClientConfigImpl
config
=
new
DefaultClientConfigImpl
();
config
.
setProperty
(
CommonClientConfigKey
.
UseIPAddrForServer
,
true
);
DomainExtractingServerList
serverList
=
getDomainExtractingServerList
(
config
);
List
<
Server
>
servers
=
serverList
.
getInitialListOfServers
();
assertNotNull
(
"servers was null"
,
servers
);
assertEquals
(
"servers was not size 1"
,
1
,
servers
.
size
());
DomainExtractingServer
des
=
assertDomainExtractingServer
(
servers
);
assertEquals
(
"hostPort was wrong"
,
IP_ADDR
+
":"
+
PORT
,
des
.
getHostPort
());
}
protected
DomainExtractingServerList
getDomainExtractingServerList
(
DefaultClientConfigImpl
config
)
{
DiscoveryEnabledServer
server
=
mock
(
DiscoveryEnabledServer
.
class
);
ServerList
originalServerList
=
mock
(
ServerList
.
class
);
InstanceInfo
instanceInfo
=
mock
(
InstanceInfo
.
class
);
when
(
server
.
getInstanceInfo
()).
thenReturn
(
instanceInfo
);
when
(
server
.
getHost
()).
thenReturn
(
HOST_NAME
);
when
(
instanceInfo
.
getMetadata
()).
thenReturn
(
ImmutableMap
.<
String
,
String
>
builder
().
put
(
"instanceId"
,
INSTANCE_ID
).
build
());
when
(
instanceInfo
.
getHostName
()).
thenReturn
(
HOST_NAME
);
when
(
instanceInfo
.
getIPAddr
()).
thenReturn
(
IP_ADDR
);
when
(
instanceInfo
.
getPort
()).
thenReturn
(
PORT
);
when
(
originalServerList
.
getInitialListOfServers
()).
thenReturn
(
Arrays
.
asList
(
server
));
return
new
DomainExtractingServerList
(
originalServerList
,
config
);
}
}
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