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
4aaa7d05
Commit
4aaa7d05
authored
Oct 11, 2016
by
Bartłomiej Słota
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
InstanceRegistry class refactor + renew method test #1376
parent
0985c8f0
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
76 additions
and
49 deletions
+76
-49
InstanceRegistry.java
...amework/cloud/netflix/eureka/server/InstanceRegistry.java
+23
-34
InstanceRegistryTest.java
...ork/cloud/netflix/eureka/server/InstanceRegistryTest.java
+53
-15
No files found.
spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistry.java
View file @
4aaa7d05
...
...
@@ -36,6 +36,7 @@ import com.netflix.eureka.registry.PeerAwareInstanceRegistryImpl;
import
com.netflix.eureka.resources.ServerCodecs
;
import
lombok.extern.apachecommons.CommonsLog
;
import
org.springframework.context.ApplicationEvent
;
/**
* @author Spencer Gibb
...
...
@@ -79,33 +80,27 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl
@Override
public
void
register
(
InstanceInfo
info
,
int
leaseDuration
,
boolean
isReplication
)
{
logRegistration
(
info
,
isReplication
,
leaseDuration
);
publishEurekaInstanceRegisteredEvent
(
info
,
leaseDuration
,
isReplication
);
handleRegistration
(
info
,
leaseDuration
,
isReplication
);
super
.
register
(
info
,
leaseDuration
,
isReplication
);
}
@Override
public
void
register
(
final
InstanceInfo
info
,
final
boolean
isReplication
)
{
final
int
instanceLeaseDuration
=
resolveInstanceLeaseDuration
(
info
);
logRegistration
(
info
,
isReplication
,
instanceLeaseDuration
);
publishEurekaInstanceRegisteredEvent
(
info
,
instanceLeaseDuration
,
isReplication
);
handleRegistration
(
info
,
resolveInstanceLeaseDuration
(
info
),
isReplication
);
super
.
register
(
info
,
isReplication
);
}
@Override
public
boolean
cancel
(
String
appName
,
String
serverId
,
boolean
isReplication
)
{
logCancelation
(
appName
,
serverId
,
isReplication
);
publishEurekaInstanceCanceledEvent
(
appName
,
serverId
,
isReplication
);
handleCancelation
(
appName
,
serverId
,
isReplication
);
return
super
.
cancel
(
appName
,
serverId
,
isReplication
);
}
@Override
public
boolean
renew
(
final
String
appName
,
final
String
serverId
,
boolean
isReplication
)
{
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
"renew "
+
appName
+
" serverId "
+
serverId
+
", isReplication {}"
log
(
"renew "
+
appName
+
" serverId "
+
serverId
+
", isReplication {}"
+
isReplication
);
}
List
<
Application
>
applications
=
getSortedApplications
();
for
(
Application
input
:
applications
)
{
if
(
input
.
getName
().
equals
(
appName
))
{
...
...
@@ -116,8 +111,8 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl
break
;
}
}
this
.
ctxt
.
publishEvent
(
new
EurekaInstanceRenewedEvent
(
this
,
appName
,
serverId
,
instance
,
isReplication
));
publishEvent
(
new
EurekaInstanceRenewedEvent
(
this
,
appName
,
serverId
,
instance
,
isReplication
));
break
;
}
}
...
...
@@ -126,38 +121,32 @@ public class InstanceRegistry extends PeerAwareInstanceRegistryImpl
@Override
protected
boolean
internalCancel
(
String
appName
,
String
id
,
boolean
isReplication
)
{
logCancelation
(
appName
,
id
,
isReplication
);
publishEurekaInstanceCanceledEvent
(
appName
,
id
,
isReplication
);
handleCancelation
(
appName
,
id
,
isReplication
);
return
super
.
internalCancel
(
appName
,
id
,
isReplication
);
}
private
void
logRegistration
(
InstanceInfo
info
,
boolean
isReplication
,
int
instanceLeaseDuration
)
{
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
"register "
+
info
.
getAppName
()
+
", vip "
+
info
.
getVIPAddress
()
+
", leaseDuration "
+
instanceLeaseDuration
+
", isReplication "
+
isReplication
);
}
private
void
handleCancelation
(
String
appName
,
String
id
,
boolean
isReplication
)
{
log
(
"cancel "
+
appName
+
", serverId "
+
id
+
", isReplication "
+
isReplication
);
publishEvent
(
new
EurekaInstanceCanceledEvent
(
this
,
appName
,
id
,
isReplication
));
}
private
void
logCancelation
(
String
appName
,
String
serverId
,
boolean
isReplication
)
{
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
"cancel "
+
appName
+
" serverId "
+
serverId
+
", isReplication "
private
void
handleRegistration
(
InstanceInfo
info
,
int
leaseDuration
,
boolean
isReplication
)
{
log
(
"register "
+
info
.
getAppName
()
+
", vip "
+
info
.
getVIPAddress
()
+
", leaseDuration "
+
leaseDuration
+
", isReplication "
+
isReplication
);
}
publishEvent
(
new
EurekaInstanceRegisteredEvent
(
this
,
info
,
leaseDuration
,
isReplication
));
}
private
void
publishEurekaInstanceRegisteredEvent
(
InstanceInfo
info
,
int
leaseDuration
,
boolean
isReplication
)
{
// TODO: what to publish from info (whole object?)
this
.
ctxt
.
publishEvent
(
new
EurekaInstanceRegisteredEvent
(
this
,
info
,
leaseDuration
,
isReplication
));
private
void
log
(
String
message
)
{
if
(
log
.
isDebugEnabled
())
{
log
.
debug
(
message
);
}
}
private
void
publishEurekaInstanceCanceledEvent
(
String
appName
,
String
serverId
,
boolean
isReplication
)
{
this
.
ctxt
.
publishEvent
(
new
EurekaInstanceCanceledEvent
(
this
,
appName
,
serverId
,
isReplication
));
private
void
publishEvent
(
ApplicationEvent
applicationEvent
)
{
this
.
ctxt
.
publishEvent
(
applicationEvent
);
}
private
int
resolveInstanceLeaseDuration
(
final
InstanceInfo
info
)
{
...
...
spring-cloud-netflix-eureka-server/src/test/java/org/springframework/cloud/netflix/eureka/server/InstanceRegistryTest.java
View file @
4aaa7d05
...
...
@@ -3,10 +3,13 @@ package org.springframework.cloud.netflix.eureka.server;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
mockito
.
Matchers
.
isA
;
import
static
org
.
mockito
.
Mockito
.
doAnswer
;
import
static
org
.
mockito
.
Mockito
.
doReturn
;
import
java.util.ArrayList
;
import
java.util.LinkedList
;
import
java.util.List
;
import
com.netflix.discovery.shared.Application
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
...
...
@@ -17,9 +20,10 @@ import org.springframework.boot.builder.SpringApplicationBuilder;
import
org.springframework.boot.test.context.SpringBootTest
;
import
org.springframework.boot.test.mock.mockito.MockBean
;
import
org.springframework.boot.test.mock.mockito.SpyBean
;
import
org.springframework.cloud.netflix.eureka.server.InstanceRegistryTest.Application
;
import
org.springframework.cloud.netflix.eureka.server.InstanceRegistryTest.
Test
Application
;
import
org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceCanceledEvent
;
import
org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRegisteredEvent
;
import
org.springframework.cloud.netflix.eureka.server.event.EurekaInstanceRenewedEvent
;
import
org.springframework.context.ApplicationEvent
;
import
org.springframework.context.ApplicationListener
;
import
org.springframework.context.annotation.Configuration
;
...
...
@@ -33,13 +37,15 @@ import com.netflix.eureka.registry.PeerAwareInstanceRegistry;
* @author Bartlomiej Slota
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringBootTest
(
classes
=
Application
.
class
,
@SpringBootTest
(
classes
=
Test
Application
.
class
,
webEnvironment
=
SpringBootTest
.
WebEnvironment
.
RANDOM_PORT
,
value
=
{
"spring.application.name=eureka"
,
"logging.level.org.springframework."
+
"cloud.netflix.eureka.server.InstanceRegistry=DEBUG"
})
public
class
InstanceRegistryTest
{
private
final
List
<
ApplicationEvent
>
applicationEvents
=
new
LinkedList
<>();
private
static
final
String
APP_NAME
=
"MY-APP-NAME"
;
private
static
final
String
HOST_NAME
=
"my-host-name"
;
@SpyBean
(
PeerAwareInstanceRegistry
.
class
)
private
InstanceRegistry
instanceRegistry
;
...
...
@@ -52,6 +58,9 @@ public class InstanceRegistryTest {
private
ApplicationListener
<
EurekaInstanceCanceledEvent
>
instanceCanceledEventListenerMock
;
@MockBean
private
ApplicationListener
<
EurekaInstanceRenewedEvent
>
instanceRenewedEventListener
;
@Before
public
void
setup
()
{
applicationEvents
.
clear
();
...
...
@@ -60,21 +69,23 @@ public class InstanceRegistryTest {
.
onApplicationEvent
(
isA
(
EurekaInstanceRegisteredEvent
.
class
));
doAnswer
(
applicationListenerAnswer
).
when
(
instanceCanceledEventListenerMock
)
.
onApplicationEvent
(
isA
(
EurekaInstanceCanceledEvent
.
class
));
doAnswer
(
applicationListenerAnswer
).
when
(
instanceRenewedEventListener
)
.
onApplicationEvent
(
isA
(
EurekaInstanceRenewedEvent
.
class
));
}
@Test
public
void
testRegister
()
throws
Exception
{
// creating instance info
LeaseInfo
leaseInfo
=
getLeaseInfo
();
InstanceInfo
instanceInfo
=
getInstanceInfo
(
leaseInfo
);
final
LeaseInfo
leaseInfo
=
getLeaseInfo
();
final
InstanceInfo
instanceInfo
=
getInstanceInfo
(
leaseInfo
);
// calling tested method
instanceRegistry
.
register
(
instanceInfo
,
false
);
// event of proper type is registered
assertEquals
(
1
,
applicationEvents
.
size
());
assertTrue
(
applicationEvents
.
get
(
0
)
instanceof
EurekaInstanceRegisteredEvent
);
// event details are correct
EurekaInstanceRegisteredEvent
registeredEvent
=
final
EurekaInstanceRegisteredEvent
registeredEvent
=
(
EurekaInstanceRegisteredEvent
)
(
applicationEvents
.
get
(
0
));
assertEquals
(
instanceInfo
,
registeredEvent
.
getInstanceInfo
());
assertEquals
(
leaseInfo
.
getDurationInSecs
(),
registeredEvent
.
getLeaseDuration
());
...
...
@@ -85,11 +96,11 @@ public class InstanceRegistryTest {
@Test
public
void
testDefaultLeaseDurationRegisterEvent
()
throws
Exception
{
// creating instance info
InstanceInfo
instanceInfo
=
getInstanceInfo
(
null
);
final
InstanceInfo
instanceInfo
=
getInstanceInfo
(
null
);
// calling tested method
instanceRegistry
.
register
(
instanceInfo
,
false
);
// instance info duration is set to default
EurekaInstanceRegisteredEvent
registeredEvent
=
final
EurekaInstanceRegisteredEvent
registeredEvent
=
(
EurekaInstanceRegisteredEvent
)
(
applicationEvents
.
get
(
0
));
assertEquals
(
LeaseInfo
.
DEFAULT_LEASE_DURATION
,
registeredEvent
.
getLeaseDuration
());
...
...
@@ -98,25 +109,52 @@ public class InstanceRegistryTest {
@Test
public
void
testInternalCancel
()
throws
Exception
{
// calling tested method
instanceRegistry
.
internalCancel
(
"my-app"
,
"appId"
,
false
);
instanceRegistry
.
internalCancel
(
APP_NAME
,
HOST_NAME
,
false
);
// event of proper type is registered
assertEquals
(
1
,
applicationEvents
.
size
());
assertTrue
(
applicationEvents
.
get
(
0
)
instanceof
EurekaInstanceCanceledEvent
);
// event details are correct
EurekaInstanceCanceledEvent
registeredEvent
=
final
EurekaInstanceCanceledEvent
registeredEvent
=
(
EurekaInstanceCanceledEvent
)
(
applicationEvents
.
get
(
0
));
assertEquals
(
"my-app"
,
registeredEvent
.
getAppName
());
assertEquals
(
"appId"
,
registeredEvent
.
getServerId
());
assertEquals
(
APP_NAME
,
registeredEvent
.
getAppName
());
assertEquals
(
HOST_NAME
,
registeredEvent
.
getServerId
());
assertEquals
(
instanceRegistry
,
registeredEvent
.
getSource
());
assertFalse
(
registeredEvent
.
isReplication
());
}
@Test
public
void
testRenew
()
throws
Exception
{
// creating application list
final
LeaseInfo
leaseInfo
=
getLeaseInfo
();
final
InstanceInfo
instanceInfo
=
getInstanceInfo
(
leaseInfo
);
final
List
<
InstanceInfo
>
instances
=
new
ArrayList
<>();
instances
.
add
(
instanceInfo
);
final
Application
application
=
new
Application
(
APP_NAME
,
instances
);
final
List
<
Application
>
applications
=
new
ArrayList
<>();
applications
.
add
(
application
);
// stubbing applications list
doReturn
(
applications
).
when
(
instanceRegistry
).
getSortedApplications
();
// calling tested method
instanceRegistry
.
renew
(
APP_NAME
,
HOST_NAME
,
false
);
// event of proper type is registered
assertEquals
(
1
,
applicationEvents
.
size
());
assertTrue
(
applicationEvents
.
get
(
0
)
instanceof
EurekaInstanceRenewedEvent
);
// event details are correct
final
EurekaInstanceRenewedEvent
registeredEvent
=
(
EurekaInstanceRenewedEvent
)
(
applicationEvents
.
get
(
0
));
assertEquals
(
APP_NAME
,
registeredEvent
.
getAppName
());
assertEquals
(
HOST_NAME
,
registeredEvent
.
getServerId
());
assertEquals
(
instanceRegistry
,
registeredEvent
.
getSource
());
assertEquals
(
instanceInfo
,
registeredEvent
.
getInstanceInfo
());
assertFalse
(
registeredEvent
.
isReplication
());
}
@Configuration
@EnableAutoConfiguration
@EnableEurekaServer
protected
static
class
Application
{
protected
static
class
Test
Application
{
public
static
void
main
(
String
[]
args
)
{
new
SpringApplicationBuilder
(
Application
.
class
).
run
(
args
);
new
SpringApplicationBuilder
(
Test
Application
.
class
).
run
(
args
);
}
}
...
...
@@ -129,8 +167,8 @@ public class InstanceRegistryTest {
private
InstanceInfo
getInstanceInfo
(
LeaseInfo
leaseInfo
)
{
InstanceInfo
.
Builder
builder
=
InstanceInfo
.
Builder
.
newBuilder
();
builder
.
setAppName
(
"my-app-name"
);
builder
.
setHostName
(
"my-host-name"
);
builder
.
setAppName
(
APP_NAME
);
builder
.
setHostName
(
HOST_NAME
);
builder
.
setPort
(
8008
);
builder
.
setLeaseInfo
(
leaseInfo
);
return
builder
.
build
();
...
...
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