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
3dfb8b5d
Commit
3dfb8b5d
authored
Oct 22, 2015
by
Spencer Gibb
Committed by
Dave Syer
Nov 11, 2015
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix support for registering random ports with eureka.
fixes gh-570 ... again
parent
31e82a75
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
31 additions
and
75 deletions
+31
-75
DiscoveryClientConfiguration.java
...rk/cloud/netflix/eureka/DiscoveryClientConfiguration.java
+1
-3
EurekaClientAutoConfiguration.java
...k/cloud/netflix/eureka/EurekaClientAutoConfiguration.java
+1
-1
EurekaDiscoveryClientConfiguration.java
...ud/netflix/eureka/EurekaDiscoveryClientConfiguration.java
+13
-2
InstanceInfoFactory.java
...ngframework/cloud/netflix/eureka/InstanceInfoFactory.java
+2
-2
MutableInstanceInfo.java
...ngframework/cloud/netflix/eureka/MutableInstanceInfo.java
+0
-44
DiscoveryClientConfigServiceAutoConfigurationTests.java
...g/DiscoveryClientConfigServiceAutoConfigurationTests.java
+14
-23
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/DiscoveryClientConfiguration.java
View file @
3dfb8b5d
...
...
@@ -71,8 +71,7 @@ public class DiscoveryClientConfiguration {
@ConditionalOnMissingBean
(
value
=
EurekaClient
.
class
,
search
=
SearchStrategy
.
CURRENT
)
@SneakyThrows
public
EurekaClient
eurekaClient
(
ApplicationInfoManager
applicationInfoManager
,
EurekaClientConfig
config
,
EurekaInstanceConfig
instance
)
{
applicationInfoManager
.
initComponent
(
instance
);
EurekaClientConfig
config
)
{
return
new
CloudEurekaClient
(
applicationInfoManager
,
config
,
optionalArgs
,
this
.
context
);
}
}
...
...
@@ -93,7 +92,6 @@ public class DiscoveryClientConfiguration {
@org
.
springframework
.
cloud
.
context
.
config
.
annotation
.
RefreshScope
public
EurekaClient
eurekaClient
(
ApplicationInfoManager
applicationInfoManager
,
EurekaClientConfig
config
,
EurekaInstanceConfig
instance
)
{
applicationInfoManager
.
initComponent
(
instance
);
return
new
CloudEurekaClient
(
applicationInfoManager
,
config
,
optionalArgs
,
this
.
context
);
}
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java
View file @
3dfb8b5d
...
...
@@ -90,7 +90,7 @@ public class EurekaClientAutoConfiguration {
@Bean
@ConditionalOnMissingBean
(
InstanceInfo
.
class
)
public
Mutable
InstanceInfo
instanceInfo
(
EurekaInstanceConfig
config
)
{
public
InstanceInfo
instanceInfo
(
EurekaInstanceConfig
config
)
{
return
new
InstanceInfoFactory
().
create
(
config
);
}
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/EurekaDiscoveryClientConfiguration.java
View file @
3dfb8b5d
...
...
@@ -16,9 +16,11 @@
package
org
.
springframework
.
cloud
.
netflix
.
eureka
;
import
java.lang.reflect.Field
;
import
java.util.concurrent.atomic.AtomicBoolean
;
import
java.util.concurrent.atomic.AtomicInteger
;
import
lombok.SneakyThrows
;
import
lombok.extern.apachecommons.CommonsLog
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -46,9 +48,11 @@ import org.springframework.core.Ordered;
import
com.netflix.appinfo.ApplicationInfoManager
;
import
com.netflix.appinfo.EurekaInstanceConfig
;
import
com.netflix.appinfo.HealthCheckHandler
;
import
com.netflix.appinfo.InstanceInfo
;
import
com.netflix.appinfo.InstanceInfo.InstanceStatus
;
import
com.netflix.discovery.EurekaClient
;
import
com.netflix.discovery.EurekaClientConfig
;
import
org.springframework.util.ReflectionUtils
;
/**
* @author Dave Syer
...
...
@@ -86,14 +90,14 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
private
EurekaClient
eurekaClient
;
@Autowired
private
Mutable
InstanceInfo
instanceInfo
;
private
InstanceInfo
instanceInfo
;
@Override
public
void
start
()
{
// only set the port if the nonSecurePort is 0 and this.port != 0
if
(
this
.
port
.
get
()
!=
0
&&
this
.
instanceConfig
.
getNonSecurePort
()
==
0
)
{
this
.
instanceConfig
.
setNonSecurePort
(
this
.
port
.
get
());
instanceInfo
.
setPort
(
this
.
port
.
get
()
);
setInstanceInfoPort
(
);
}
// only initialize if nonSecurePort is greater than 0 and it isn't already running
...
...
@@ -120,6 +124,13 @@ public class EurekaDiscoveryClientConfiguration implements SmartLifecycle, Order
}
}
@SneakyThrows
private
void
setInstanceInfoPort
()
{
Field
port
=
ReflectionUtils
.
findField
(
InstanceInfo
.
class
,
"port"
);
ReflectionUtils
.
makeAccessible
(
port
);
port
.
setInt
(
this
.
instanceInfo
,
this
.
port
.
get
());
}
@Override
public
void
stop
()
{
if
(
this
.
applicationInfoManager
.
getInfo
()
!=
null
)
{
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/InstanceInfoFactory.java
View file @
3dfb8b5d
...
...
@@ -31,7 +31,7 @@ import com.netflix.appinfo.LeaseInfo;
@CommonsLog
public
class
InstanceInfoFactory
{
public
Mutable
InstanceInfo
create
(
EurekaInstanceConfig
config
)
{
public
InstanceInfo
create
(
EurekaInstanceConfig
config
)
{
LeaseInfo
.
Builder
leaseInfoBuilder
=
LeaseInfo
.
Builder
.
newBuilder
()
.
setRenewalIntervalInSecs
(
config
.
getLeaseRenewalIntervalInSeconds
())
.
setDurationInSecs
(
config
.
getLeaseExpirationDurationInSeconds
());
...
...
@@ -87,6 +87,6 @@ public class InstanceInfoFactory {
InstanceInfo
instanceInfo
=
builder
.
build
();
instanceInfo
.
setLeaseInfo
(
leaseInfoBuilder
.
build
());
return
new
MutableInstanceInfo
(
instanceInfo
)
;
return
instanceInfo
;
}
}
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/eureka/MutableInstanceInfo.java
deleted
100644 → 0
View file @
31e82a75
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org
.
springframework
.
cloud
.
netflix
.
eureka
;
import
com.netflix.appinfo.InstanceInfo
;
/**
* @author Spencer Gibb
*/
class
MutableInstanceInfo
extends
InstanceInfo
{
private
Integer
port
;
public
MutableInstanceInfo
(
InstanceInfo
ii
)
{
super
(
ii
);
}
@Override
public
int
getPort
()
{
if
(
this
.
port
!=
null
)
{
return
this
.
port
;
}
return
super
.
getPort
();
}
public
void
setPort
(
int
port
)
{
this
.
port
=
port
;
}
}
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/config/DiscoveryClientConfigServiceAutoConfigurationTests.java
View file @
3dfb8b5d
...
...
@@ -20,20 +20,17 @@ import static org.junit.Assert.assertEquals;
import
static
org
.
mockito
.
BDDMockito
.
given
;
import
static
org
.
mockito
.
Mockito
.
times
;
import
javax.annotation.PostConstruct
;
import
org.junit.After
;
import
org.junit.Test
;
import
org.mockito.Mockito
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.test.EnvironmentTestUtils
;
import
org.springframework.cloud.config.client.ConfigClientProperties
;
import
org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration
;
import
org.springframework.cloud.netflix.eureka.EurekaDiscoveryClientConfiguration
;
import
org.springframework.context.annotation.AnnotationConfigApplicationContext
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.core.env.ConfigurableEnvironment
;
import
com.netflix.appinfo.ApplicationInfoManager
;
import
com.netflix.appinfo.InstanceInfo
;
...
...
@@ -46,11 +43,6 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
private
AnnotationConfigApplicationContext
context
;
private
EurekaClient
client
=
Mockito
.
mock
(
EurekaClient
.
class
);
private
InstanceInfo
info
=
InstanceInfo
.
Builder
.
newBuilder
().
setAppName
(
"app"
)
.
setHostName
(
"foo"
).
setHomePageUrl
(
"/"
,
null
).
build
();
@After
public
void
close
()
{
if
(
this
.
context
!=
null
)
{
...
...
@@ -63,14 +55,16 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
@Test
public
void
onWhenRequested
()
throws
Exception
{
given
(
this
.
client
.
getNextServerFromEureka
(
"CONFIGSERVER"
,
false
))
.
willReturn
(
this
.
info
);
setup
(
"spring.cloud.config.discovery.enabled=true"
);
setup
(
"spring.cloud.config.discovery.enabled=true"
,
"eureka.instance.metadataMap.foo:bar"
,
"eureka.instance.nonSecurePort:7001"
,
"eureka.instance.hostname:foo"
);
assertEquals
(
1
,
this
.
context
.
getBeanNamesForType
(
DiscoveryClientConfigServiceAutoConfiguration
.
class
).
length
);
Mockito
.
verify
(
this
.
client
,
times
(
2
)).
getNextServerFromEureka
(
"CONFIGSERVER"
,
EurekaClient
eurekaClient
=
this
.
context
.
getParent
().
getBean
(
EurekaClient
.
class
);
Mockito
.
verify
(
eurekaClient
,
times
(
2
)).
getNextServerFromEureka
(
"CONFIGSERVER"
,
false
);
Mockito
.
verify
(
this
.
c
lient
,
times
(
1
)).
shutdown
();
Mockito
.
verify
(
eurekaC
lient
,
times
(
1
)).
shutdown
();
ConfigClientProperties
locator
=
this
.
context
.
getBean
(
ConfigClientProperties
.
class
);
assertEquals
(
"http://foo:7001/"
,
locator
.
getRawUri
());
...
...
@@ -82,8 +76,6 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
private
void
setup
(
String
...
env
)
{
AnnotationConfigApplicationContext
parent
=
new
AnnotationConfigApplicationContext
();
EnvironmentTestUtils
.
addEnvironment
(
parent
,
env
);
parent
.
getDefaultListableBeanFactory
().
registerSingleton
(
"eurekaClient"
,
this
.
client
);
parent
.
register
(
PropertyPlaceholderAutoConfiguration
.
class
,
DiscoveryClientConfigServiceBootstrapConfiguration
.
class
,
EnvironmentKnobbler
.
class
,
ConfigClientProperties
.
class
);
...
...
@@ -100,13 +92,12 @@ public class DiscoveryClientConfigServiceAutoConfigurationTests {
@Configuration
protected
static
class
EnvironmentKnobbler
{
@Autowired
private
ConfigurableEnvironment
environment
;
@PostConstruct
public
void
init
()
{
EnvironmentTestUtils
.
addEnvironment
(
this
.
environment
,
"eureka.instance.metadataMap.foo:bar"
);
@Bean
public
EurekaClient
eurekaClient
(
InstanceInfo
info
)
{
EurekaClient
client
=
Mockito
.
mock
(
EurekaClient
.
class
);
given
(
client
.
getNextServerFromEureka
(
"CONFIGSERVER"
,
false
))
.
willReturn
(
info
);
return
client
;
}
}
...
...
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