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
da04c537
Commit
da04c537
authored
Sep 24, 2015
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use new commons code to provide hostInfo.
Set the default sid from common properties.
parent
8f9acab3
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
31 additions
and
61 deletions
+31
-61
EurekaClientAutoConfiguration.java
...k/cloud/netflix/eureka/EurekaClientAutoConfiguration.java
+9
-2
EurekaInstanceConfigBean.java
...mework/cloud/netflix/eureka/EurekaInstanceConfigBean.java
+12
-53
EurekaInstanceConfigBeanTests.java
...k/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java
+2
-2
InstanceInfoFactoryTests.java
...mework/cloud/netflix/eureka/InstanceInfoFactoryTests.java
+8
-4
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java
View file @
da04c537
...
...
@@ -16,12 +16,16 @@
package
org
.
springframework
.
cloud
.
netflix
.
eureka
;
import
static
org
.
springframework
.
cloud
.
util
.
IdUtils
.
getDefaultInstanceId
;
import
java.lang.annotation.Documented
;
import
java.lang.annotation.ElementType
;
import
java.lang.annotation.Retention
;
import
java.lang.annotation.RetentionPolicy
;
import
java.lang.annotation.Target
;
import
lombok.SneakyThrows
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.autoconfigure.AutoConfigureAfter
;
...
...
@@ -44,6 +48,7 @@ import org.springframework.context.ApplicationContext;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Conditional
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
com.netflix.appinfo.ApplicationInfoManager
;
import
com.netflix.appinfo.EurekaInstanceConfig
;
...
...
@@ -51,8 +56,6 @@ import com.netflix.appinfo.InstanceInfo;
import
com.netflix.discovery.EurekaClient
;
import
com.netflix.discovery.EurekaClientConfig
;
import
lombok.SneakyThrows
;
/**
* @author Dave Syer
*/
...
...
@@ -68,6 +71,9 @@ public class EurekaClientAutoConfiguration {
@Value
(
"${server.port:${SERVER_PORT:${PORT:8080}}}"
)
int
nonSecurePort
;
@Autowired
ConfigurableEnvironment
env
;
@Bean
@ConditionalOnMissingBean
(
value
=
EurekaClientConfig
.
class
,
search
=
SearchStrategy
.
CURRENT
)
public
EurekaClientConfigBean
eurekaClientConfigBean
()
{
...
...
@@ -79,6 +85,7 @@ public class EurekaClientAutoConfiguration {
public
EurekaInstanceConfigBean
eurekaInstanceConfigBean
()
{
EurekaInstanceConfigBean
instance
=
new
EurekaInstanceConfigBean
();
instance
.
setNonSecurePort
(
this
.
nonSecurePort
);
instance
.
setSid
(
getDefaultInstanceId
(
env
));
return
instance
;
}
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBean.java
View file @
da04c537
...
...
@@ -16,29 +16,26 @@
package
org
.
springframework
.
cloud
.
netflix
.
eureka
;
import
java.io.IOException
;
import
java.net.Inet4Address
;
import
java.net.InetAddress
;
import
java.net.NetworkInterface
;
import
java.util.Enumeration
;
import
static
org
.
springframework
.
cloud
.
util
.
InetUtils
.
getFirstNonLoopbackHostInfo
;
import
java.util.HashMap
;
import
java.util.Map
;
import
com.netflix.appinfo.MyDataCenterInfo
;
import
lombok.AccessLevel
;
import
lombok.Data
;
import
lombok.Getter
;
import
lombok.Setter
;
import
lombok.SneakyThrows
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.boot.context.properties.ConfigurationProperties
;
import
org.springframework.cloud.util.InetUtils.HostInfo
;
import
com.netflix.appinfo.DataCenterInfo
;
import
com.netflix.appinfo.EurekaInstanceConfig
;
import
com.netflix.appinfo.InstanceInfo.InstanceStatus
;
import
com.netflix.appinfo.MyDataCenterInfo
;
/**
* @author Dave Syer
...
...
@@ -52,7 +49,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
@Getter
(
AccessLevel
.
PRIVATE
)
@Setter
(
AccessLevel
.
PRIVATE
)
private
HostInfo
hostInfo
=
init
HostInfo
();
private
HostInfo
hostInfo
=
getFirstNonLoopback
HostInfo
();
@Value
(
"${spring.application.name:unknown}"
)
private
String
appname
=
"unknown"
;
...
...
@@ -86,7 +83,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
private
DataCenterInfo
dataCenterInfo
=
new
MyDataCenterInfo
(
DataCenterInfo
.
Name
.
MyOwn
);
private
String
ipAddress
=
this
.
hostInfo
.
ipAddress
;
private
String
ipAddress
=
this
.
hostInfo
.
getIpAddress
()
;
private
String
statusPageUrlPath
=
"/info"
;
...
...
@@ -104,7 +101,7 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
private
String
namespace
=
"eureka"
;
private
String
hostname
=
this
.
hostInfo
.
hostname
;
private
String
hostname
=
this
.
hostInfo
.
getHostname
()
;
private
boolean
preferIpAddress
=
false
;
...
...
@@ -127,40 +124,6 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
return
this
.
securePortEnabled
;
}
private
HostInfo
initHostInfo
()
{
this
.
hostInfo
=
this
.
hostInfo
==
null
?
new
HostInfo
()
:
this
.
hostInfo
;
InetAddress
address
=
getFirstNonLoopbackAddress
();
this
.
hostInfo
.
ipAddress
=
address
.
getHostAddress
();
this
.
hostInfo
.
hostname
=
address
.
getHostName
();
return
this
.
hostInfo
;
}
//TODO: move this method to s-c-commons
@SneakyThrows
static
InetAddress
getFirstNonLoopbackAddress
()
{
try
{
for
(
Enumeration
<
NetworkInterface
>
enumNic
=
NetworkInterface
.
getNetworkInterfaces
();
enumNic
.
hasMoreElements
();
)
{
NetworkInterface
ifc
=
enumNic
.
nextElement
();
if
(
ifc
.
isUp
())
{
for
(
Enumeration
<
InetAddress
>
enumAddr
=
ifc
.
getInetAddresses
();
enumAddr
.
hasMoreElements
();
)
{
InetAddress
address
=
enumAddr
.
nextElement
();
if
(
address
instanceof
Inet4Address
&&
!
address
.
isLoopbackAddress
())
{
return
address
;
}
}
}
}
}
catch
(
IOException
ex
)
{
logger
.
error
(
"Cannot get host info"
,
ex
);
}
return
InetAddress
.
getLocalHost
();
}
public
void
setHostname
(
String
hostname
)
{
this
.
hostname
=
hostname
;
this
.
hostInfo
.
override
=
true
;
...
...
@@ -169,18 +132,14 @@ public class EurekaInstanceConfigBean implements EurekaInstanceConfig {
@Override
public
String
getHostName
(
boolean
refresh
)
{
if
(
refresh
)
{
this
.
hostInfo
=
initHostInfo
();
this
.
ipAddress
=
this
.
hostInfo
.
ipAddress
;
boolean
originalOverride
=
this
.
hostInfo
.
override
;
this
.
hostInfo
=
getFirstNonLoopbackHostInfo
();
this
.
hostInfo
.
setOverride
(
originalOverride
);
this
.
ipAddress
=
this
.
hostInfo
.
getIpAddress
();
if
(!
this
.
hostInfo
.
override
)
{
this
.
hostname
=
this
.
hostInfo
.
hostname
;
this
.
hostname
=
this
.
hostInfo
.
getHostname
()
;
}
}
return
this
.
preferIpAddress
?
this
.
ipAddress
:
this
.
hostname
;
}
private
final
class
HostInfo
{
public
boolean
override
;
private
String
ipAddress
;
private
String
hostname
;
}
}
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/EurekaInstanceConfigBeanTests.java
View file @
da04c537
...
...
@@ -19,6 +19,7 @@ package org.springframework.cloud.netflix.eureka;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
springframework
.
boot
.
test
.
EnvironmentTestUtils
.
addEnvironment
;
import
static
org
.
springframework
.
cloud
.
util
.
InetUtils
.
getFirstNonLoopbackHostInfo
;
import
org.junit.After
;
import
org.junit.Before
;
...
...
@@ -42,8 +43,7 @@ public class EurekaInstanceConfigBeanTests {
@Before
public
void
init
()
{
this
.
hostName
=
EurekaInstanceConfigBean
.
getFirstNonLoopbackAddress
()
.
getHostName
();
this
.
hostName
=
getFirstNonLoopbackHostInfo
().
getHostname
();
}
@After
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/eureka/InstanceInfoFactoryTests.java
View file @
da04c537
...
...
@@ -3,6 +3,7 @@ package org.springframework.cloud.netflix.eureka;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
springframework
.
boot
.
test
.
EnvironmentTestUtils
.
addEnvironment
;
import
static
org
.
springframework
.
cloud
.
util
.
InetUtils
.
getFirstNonLoopbackHostInfo
;
import
org.junit.Test
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
...
...
@@ -17,19 +18,22 @@ public class InstanceInfoFactoryTests {
@Test
public
void
instanceIdIsHostNameByDefault
()
{
assertEquals
(
EurekaInstanceConfigBean
.
getFirstNonLoopbackAddress
().
getHostName
(),
setupInstance
().
getId
());
InstanceInfo
instanceInfo
=
setupInstance
();
assertEquals
(
getFirstNonLoopbackHostInfo
().
getHostname
(),
instanceInfo
.
getId
());
}
@Test
public
void
instanceIdIsIpWhenIpPreferred
()
throws
Exception
{
assertTrue
(
setupInstance
(
"eureka.instance.preferIpAddress:true"
).
getId
().
matches
(
InstanceInfo
instanceInfo
=
setupInstance
(
"eureka.instance.preferIpAddress:true"
);
assertTrue
(
instanceInfo
.
getId
().
matches
(
"(\\d+\\.){3}\\d+"
));
}
@Test
public
void
instanceIdIsSidWhenSet
()
{
assertEquals
(
"special"
,
setupInstance
(
"eureka.instance.sid:special"
).
getId
());
InstanceInfo
instanceInfo
=
setupInstance
(
"eureka.instance.sid:special"
);
assertEquals
(
"special"
,
instanceInfo
.
getId
());
}
private
InstanceInfo
setupInstance
(
String
...
pairs
)
{
...
...
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