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
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
155 additions
and
64 deletions
+155
-64
DomainExtractingServerList.java
...oud/netflix/ribbon/eureka/DomainExtractingServerList.java
+71
-63
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;
...
@@ -18,6 +18,8 @@ package org.springframework.cloud.netflix.ribbon.eureka;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
com.netflix.client.config.CommonClientConfigKey
;
import
com.netflix.client.config.IClientConfig
;
import
lombok.Getter
;
import
lombok.Getter
;
import
lombok.Setter
;
import
lombok.Setter
;
...
@@ -34,73 +36,79 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
...
@@ -34,73 +36,79 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
*/
*/
public
class
DomainExtractingServerList
implements
ServerList
<
Server
>
{
public
class
DomainExtractingServerList
implements
ServerList
<
Server
>
{
private
ServerList
<
Server
>
list
;
private
ServerList
<
Server
>
list
;
private
IClientConfig
clientConfig
;
public
DomainExtractingServerList
(
ServerList
<
Server
>
list
)
{
this
.
list
=
list
;
public
DomainExtractingServerList
(
ServerList
<
Server
>
list
,
IClientConfig
clientConfig
)
{
}
this
.
list
=
list
;
this
.
clientConfig
=
clientConfig
;
@Override
}
public
List
<
Server
>
getInitialListOfServers
()
{
List
<
Server
>
servers
=
setZones
(
list
.
getInitialListOfServers
());
@Override
return
servers
;
public
List
<
Server
>
getInitialListOfServers
()
{
}
List
<
Server
>
servers
=
setZones
(
list
.
getInitialListOfServers
());
return
servers
;
@Override
}
public
List
<
Server
>
getUpdatedListOfServers
()
{
List
<
Server
>
servers
=
setZones
(
list
.
getUpdatedListOfServers
());
@Override
return
servers
;
public
List
<
Server
>
getUpdatedListOfServers
()
{
}
List
<
Server
>
servers
=
setZones
(
list
.
getUpdatedListOfServers
());
return
servers
;
private
List
<
Server
>
setZones
(
List
<
Server
>
servers
)
{
}
List
<
Server
>
result
=
new
ArrayList
<
Server
>();
for
(
Server
server
:
servers
)
{
private
List
<
Server
>
setZones
(
List
<
Server
>
servers
)
{
if
(
server
instanceof
DiscoveryEnabledServer
)
{
List
<
Server
>
result
=
new
ArrayList
<>();
result
.
add
(
new
DomainExtractingServer
((
DiscoveryEnabledServer
)
server
));
boolean
isSecure
=
clientConfig
.
getPropertyAsBoolean
(
CommonClientConfigKey
.
IsSecure
,
Boolean
.
TRUE
);
}
boolean
shouldUseIpAddr
=
clientConfig
.
getPropertyAsBoolean
(
CommonClientConfigKey
.
UseIPAddrForServer
,
Boolean
.
FALSE
);
else
{
for
(
Server
server
:
servers
)
{
result
.
add
(
server
);
if
(
server
instanceof
DiscoveryEnabledServer
)
{
}
result
.
add
(
new
DomainExtractingServer
((
DiscoveryEnabledServer
)
server
,
}
isSecure
,
shouldUseIpAddr
));
return
result
;
}
}
else
{
result
.
add
(
server
);
}
}
return
result
;
}
}
}
class
DomainExtractingServer
extends
DiscoveryEnabledServer
{
class
DomainExtractingServer
extends
DiscoveryEnabledServer
{
@Getter
@Getter
@Setter
@Setter
private
String
id
;
private
String
id
;
public
DomainExtractingServer
(
DiscoveryEnabledServer
serve
r
)
{
public
DomainExtractingServer
(
DiscoveryEnabledServer
server
,
boolean
useSecurePort
,
boolean
useIpAdd
r
)
{
super
(
server
.
getInstanceInfo
(),
true
,
false
);
super
(
server
.
getInstanceInfo
(),
useSecurePort
,
useIpAddr
);
setZone
(
extractApproximateZone
(
server
));
setZone
(
extractApproximateZone
(
server
));
setId
(
extractId
(
server
));
setId
(
extractId
(
server
));
}
}
private
String
extractId
(
Server
server
)
{
private
String
extractId
(
Server
server
)
{
if
(
server
instanceof
DiscoveryEnabledServer
)
{
if
(
server
instanceof
DiscoveryEnabledServer
)
{
DiscoveryEnabledServer
enabled
=
(
DiscoveryEnabledServer
)
server
;
DiscoveryEnabledServer
enabled
=
(
DiscoveryEnabledServer
)
server
;
InstanceInfo
instance
=
enabled
.
getInstanceInfo
();
InstanceInfo
instance
=
enabled
.
getInstanceInfo
();
if
(
instance
.
getMetadata
().
containsKey
(
"instanceId"
))
{
if
(
instance
.
getMetadata
().
containsKey
(
"instanceId"
))
{
return
instance
.
getMetadata
().
get
(
"instanceId"
);
return
instance
.
getMetadata
().
get
(
"instanceId"
);
}
}
}
}
return
serv
er
.
getId
();
return
sup
er
.
getId
();
}
}
private
String
extractApproximateZone
(
Server
server
)
{
private
String
extractApproximateZone
(
Server
server
)
{
String
host
=
server
.
getHost
();
String
host
=
server
.
getHost
();
if
(!
host
.
contains
(
"."
))
{
if
(!
host
.
contains
(
"."
))
{
return
host
;
return
host
;
}
}
String
[]
split
=
StringUtils
.
split
(
host
,
"."
);
String
[]
split
=
StringUtils
.
split
(
host
,
"."
);
StringBuilder
builder
=
new
StringBuilder
(
split
[
1
]);
StringBuilder
builder
=
new
StringBuilder
(
split
[
1
]);
for
(
int
i
=
2
;
i
<
split
.
length
;
i
++)
{
for
(
int
i
=
2
;
i
<
split
.
length
;
i
++)
{
builder
.
append
(
"."
).
append
(
split
[
i
]);
builder
.
append
(
"."
).
append
(
split
[
i
]);
}
}
return
builder
.
toString
();
return
builder
.
toString
();
}
}
}
}
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
...
@@ -81,7 +81,7 @@ public class EurekaRibbonClientPreprocessor implements RibbonClientPreprocessor
// the server zone is populated. TODO: find a way to back off if AWS
// the server zone is populated. TODO: find a way to back off if AWS
// metadata *is* available.
// metadata *is* available.
// @see com.netflix.appinfo.AmazonInfo.Builder
// @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