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
39eb9815
Commit
39eb9815
authored
Jan 29, 2015
by
Spencer Gibb
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'loadbalancer-builder'
parents
91e3bee5
af0b24d1
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
245 additions
and
134 deletions
+245
-134
RibbonClientConfiguration.java
...ework/cloud/netflix/ribbon/RibbonClientConfiguration.java
+34
-7
DomainExtractingServerList.java
...oud/netflix/ribbon/eureka/DomainExtractingServerList.java
+12
-17
EurekaRibbonClientConfiguration.java
...etflix/ribbon/eureka/EurekaRibbonClientConfiguration.java
+25
-50
RibbonClientPreprocessorIntegrationTests.java
...flix/ribbon/RibbonClientPreprocessorIntegrationTests.java
+21
-29
RibbonClientPreprocessorOverridesIntegrationTests.java
...on/RibbonClientPreprocessorOverridesIntegrationTests.java
+120
-0
RibbonClientsPreprocessorIntegrationTests.java
...lix/ribbon/RibbonClientsPreprocessorIntegrationTests.java
+7
-17
DomainExtractingServerListTests.java
...etflix/ribbon/eureka/DomainExtractingServerListTests.java
+6
-6
EurekaRibbonClientConfigurationTests.java
...x/ribbon/eureka/EurekaRibbonClientConfigurationTests.java
+3
-5
EurekaRibbonClientPreprocessorIntegrationTests.java
...ureka/EurekaRibbonClientPreprocessorIntegrationTests.java
+17
-3
No files found.
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/RibbonClientConfiguration.java
View file @
39eb9815
...
...
@@ -24,10 +24,7 @@ import org.springframework.context.annotation.Configuration;
import
com.netflix.client.config.DefaultClientConfigImpl
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.loadbalancer.ILoadBalancer
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.ServerListFilter
;
import
com.netflix.loadbalancer.ZoneAwareLoadBalancer
;
import
com.netflix.loadbalancer.*
;
import
com.netflix.niws.client.http.RestClient
;
import
com.netflix.servo.monitor.Monitors
;
...
...
@@ -55,6 +52,29 @@ public class RibbonClientConfiguration {
@Bean
@ConditionalOnMissingBean
public
IRule
ribbonRule
(
IClientConfig
config
)
{
ZoneAvoidanceRule
rule
=
new
ZoneAvoidanceRule
();
rule
.
initWithNiwsConfig
(
config
);
return
rule
;
}
@Bean
@ConditionalOnMissingBean
public
IPing
ribbonPing
(
IClientConfig
config
)
{
// TODO: use PingUrl
return
new
NoOpPing
();
}
@Bean
@ConditionalOnMissingBean
public
ServerList
<
Server
>
ribbonServerList
(
IClientConfig
config
)
{
ConfigurationBasedServerList
serverList
=
new
ConfigurationBasedServerList
();
serverList
.
initWithNiwsConfig
(
config
);
return
serverList
;
}
@Bean
@ConditionalOnMissingBean
public
RestClient
ribbonRestClient
(
IClientConfig
config
,
ILoadBalancer
loadBalancer
)
{
RestClient
client
=
new
RestClient
(
config
);
client
.
setLoadBalancer
(
loadBalancer
);
...
...
@@ -65,9 +85,16 @@ public class RibbonClientConfiguration {
@Bean
@ConditionalOnMissingBean
public
ILoadBalancer
ribbonLoadBalancer
(
IClientConfig
config
,
ServerListFilter
<
Server
>
filter
)
{
ZoneAwareLoadBalancer
<
Server
>
balancer
=
new
ZoneAwareLoadBalancer
<>(
config
);
balancer
.
setFilter
(
filter
);
ServerList
<
Server
>
serverList
,
ServerListFilter
<
Server
>
serverListFilter
,
IRule
rule
,
IPing
ping
)
{
ZoneAwareLoadBalancer
<
Server
>
balancer
=
LoadBalancerBuilder
.
newBuilder
()
.
withClientConfig
(
config
)
.
withRule
(
rule
)
.
withPing
(
ping
)
.
withServerListFilter
(
serverListFilter
)
.
withDynamicServerList
(
serverList
)
.
buildDynamicServerListLoadBalancer
();
return
balancer
;
}
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/eureka/DomainExtractingServerList.java
View file @
39eb9815
...
...
@@ -34,15 +34,15 @@ import com.netflix.niws.loadbalancer.DiscoveryEnabledServer;
/**
* @author Dave Syer
*/
public
class
DomainExtractingServerList
implements
ServerList
<
Server
>
{
public
class
DomainExtractingServerList
implements
ServerList
<
DiscoveryEnabled
Server
>
{
private
ServerList
<
Server
>
list
;
private
ServerList
<
DiscoveryEnabled
Server
>
list
;
private
IClientConfig
clientConfig
;
private
boolean
approximateZoneFromHostname
;
public
DomainExtractingServerList
(
ServerList
<
Server
>
list
,
public
DomainExtractingServerList
(
ServerList
<
DiscoveryEnabled
Server
>
list
,
IClientConfig
clientConfig
,
boolean
approximateZoneFromHostname
)
{
this
.
list
=
list
;
this
.
clientConfig
=
clientConfig
;
...
...
@@ -50,31 +50,26 @@ public class DomainExtractingServerList implements ServerList<Server> {
}
@Override
public
List
<
Server
>
getInitialListOfServers
()
{
List
<
Server
>
servers
=
setZones
(
this
.
list
.
getInitialListOfServers
());
public
List
<
DiscoveryEnabled
Server
>
getInitialListOfServers
()
{
List
<
DiscoveryEnabled
Server
>
servers
=
setZones
(
this
.
list
.
getInitialListOfServers
());
return
servers
;
}
@Override
public
List
<
Server
>
getUpdatedListOfServers
()
{
List
<
Server
>
servers
=
setZones
(
this
.
list
.
getUpdatedListOfServers
());
public
List
<
DiscoveryEnabled
Server
>
getUpdatedListOfServers
()
{
List
<
DiscoveryEnabled
Server
>
servers
=
setZones
(
this
.
list
.
getUpdatedListOfServers
());
return
servers
;
}
private
List
<
Server
>
setZones
(
List
<
Server
>
servers
)
{
List
<
Server
>
result
=
new
ArrayList
<>();
private
List
<
DiscoveryEnabledServer
>
setZones
(
List
<
DiscoveryEnabled
Server
>
servers
)
{
List
<
DiscoveryEnabled
Server
>
result
=
new
ArrayList
<>();
boolean
isSecure
=
this
.
clientConfig
.
getPropertyAsBoolean
(
CommonClientConfigKey
.
IsSecure
,
Boolean
.
TRUE
);
boolean
shouldUseIpAddr
=
this
.
clientConfig
.
getPropertyAsBoolean
(
CommonClientConfigKey
.
UseIPAddrForServer
,
Boolean
.
FALSE
);
for
(
Server
server
:
servers
)
{
if
(
server
instanceof
DiscoveryEnabledServer
)
{
result
.
add
(
new
DomainExtractingServer
((
DiscoveryEnabledServer
)
server
,
isSecure
,
shouldUseIpAddr
,
this
.
approximateZoneFromHostname
));
}
else
{
result
.
add
(
server
);
}
for
(
DiscoveryEnabledServer
server
:
servers
)
{
result
.
add
(
new
DomainExtractingServer
(
server
,
isSecure
,
shouldUseIpAddr
,
this
.
approximateZoneFromHostname
));
}
return
result
;
}
...
...
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientConfiguration.java
View file @
39eb9815
...
...
@@ -16,31 +16,27 @@
package
org
.
springframework
.
cloud
.
netflix
.
ribbon
.
eureka
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
DeploymentContextBasedVipAddresses
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
EnableZoneAffinity
;
import
javax.annotation.PostConstruct
;
import
org.springframework.beans.BeansException
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.b
eans.factory.config.BeanPostProcessor
;
import
org.springframework.c
loud.netflix.ribbon.ZonePreferenceServerListFilter
;
import
org.springframework.b
oot.autoconfigure.condition.ConditionalOnMissingBean
;
import
org.springframework.c
ontext.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.config.ConfigurationManager
;
import
com.netflix.config.DeploymentContext.ContextKey
;
import
com.netflix.config.DynamicPropertyFactory
;
import
com.netflix.config.DynamicStringProperty
;
import
com.netflix.discovery.EurekaClientConfig
;
import
com.netflix.loadbalancer.DynamicServerListLoadBalancer
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.IPing
;
import
com.netflix.loadbalancer.ServerList
;
import
com.netflix.loadbalancer.ZoneAvoidanceRule
;
import
com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
DeploymentContextBasedVipAddresses
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
EnableZoneAffinity
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
NFLoadBalancerRuleClassName
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
NIWSServerListClassName
;
import
static
com
.
netflix
.
client
.
config
.
CommonClientConfigKey
.
NIWSServerListFilterClassName
;
import
com.netflix.niws.loadbalancer.NIWSDiscoveryPing
;
/**
* Preprocessor that configures defaults for eureka-discovered ribbon clients. Such as:
...
...
@@ -51,7 +47,7 @@ import static com.netflix.client.config.CommonClientConfigKey.NIWSServerListFilt
* @author Dave Syer
*/
@Configuration
public
class
EurekaRibbonClientConfiguration
implements
BeanPostProcessor
{
public
class
EurekaRibbonClientConfiguration
{
@Value
(
"${ribbon.eureka.approximateZoneFromHostname:false}"
)
private
boolean
approximateZoneFromHostname
=
false
;
...
...
@@ -75,6 +71,22 @@ public class EurekaRibbonClientConfiguration implements BeanPostProcessor {
this
.
serviceId
=
serviceId
;
}
@Bean
@ConditionalOnMissingBean
public
IPing
ribbonPing
(
IClientConfig
config
)
{
NIWSDiscoveryPing
ping
=
new
NIWSDiscoveryPing
();
ping
.
initWithNiwsConfig
(
config
);
return
ping
;
}
@Bean
@ConditionalOnMissingBean
public
ServerList
<?>
ribbonServerList
(
IClientConfig
config
)
{
DiscoveryEnabledNIWSServerList
discoveryServerList
=
new
DiscoveryEnabledNIWSServerList
(
config
);
DomainExtractingServerList
serverList
=
new
DomainExtractingServerList
(
discoveryServerList
,
config
,
this
.
approximateZoneFromHostname
);
return
serverList
;
}
@PostConstruct
public
void
preprocess
()
{
if
(
this
.
clientConfig
!=
null
...
...
@@ -89,47 +101,10 @@ public class EurekaRibbonClientConfiguration implements BeanPostProcessor {
zone
);
}
}
// TODO: should this look more like hibernate spring boot props?
setProp
(
this
.
serviceId
,
NIWSServerListClassName
.
key
(),
DiscoveryEnabledNIWSServerList
.
class
.
getName
());
// FIXME: what should this be?
setProp
(
this
.
serviceId
,
DeploymentContextBasedVipAddresses
.
key
(),
this
.
serviceId
);
setProp
(
this
.
serviceId
,
NFLoadBalancerRuleClassName
.
key
(),
ZoneAvoidanceRule
.
class
.
getName
());
setProp
(
this
.
serviceId
,
NIWSServerListFilterClassName
.
key
(),
ZonePreferenceServerListFilter
.
class
.
getName
());
setProp
(
this
.
serviceId
,
EnableZoneAffinity
.
key
(),
"true"
);
}
@Override
public
Object
postProcessBeforeInitialization
(
Object
bean
,
String
beanName
)
throws
BeansException
{
return
bean
;
}
@Override
public
Object
postProcessAfterInitialization
(
Object
bean
,
String
beanName
)
throws
BeansException
{
if
(
bean
instanceof
DynamicServerListLoadBalancer
)
{
wrapServerList
((
DynamicServerListLoadBalancer
<?>)
bean
);
}
return
bean
;
}
private
void
wrapServerList
(
DynamicServerListLoadBalancer
<?>
balancer
)
{
@SuppressWarnings
(
"unchecked"
)
DynamicServerListLoadBalancer
<
Server
>
dynamic
=
(
DynamicServerListLoadBalancer
<
Server
>)
balancer
;
ServerList
<
Server
>
list
=
dynamic
.
getServerListImpl
();
if
(!(
list
instanceof
DomainExtractingServerList
))
{
// This is optional: you can use the native Eureka AWS features as long as
// the server zone is populated. TODO: verify that we back off if AWS
// metadata *is* available.
// @see com.netflix.appinfo.AmazonInfo.Builder
dynamic
.
setServerListImpl
(
new
DomainExtractingServerList
(
list
,
dynamic
.
getClientConfig
(),
this
.
approximateZoneFromHostname
));
}
}
protected
void
setProp
(
String
serviceId
,
String
suffix
,
String
value
)
{
// how to set the namespace properly?
String
key
=
getKey
(
serviceId
,
suffix
);
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonClientPreprocessorIntegrationTests.java
View file @
39eb9815
...
...
@@ -16,8 +16,6 @@
package
org
.
springframework
.
cloud
.
netflix
.
ribbon
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -25,15 +23,12 @@ import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfigurati
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration
;
import
org.springframework.cloud.netflix.ribbon.RibbonClientPreprocessorIntegrationTests.PlainConfiguration
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
import
com.netflix.loadbalancer.AvailabilityFilteringRule
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.ZoneAwareLoadBalancer
;
import
com.netflix.loadbalancer.*
;
/**
* @author Dave Syer
...
...
@@ -47,38 +42,35 @@ public class RibbonClientPreprocessorIntegrationTests {
private
SpringClientFactory
factory
;
@Test
public
void
ruleDefaultsToAvailability
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
ZoneAwareLoadBalancer
<
Server
>
loadBalancer
=
(
ZoneAwareLoadBalancer
<
Server
>)
this
.
factory
.
getLoadBalancer
(
"foo"
);
AvailabilityFilteringRule
.
class
.
cast
(
loadBalancer
.
getRule
());
public
void
ruleDefaultsToZoneAvoidance
()
throws
Exception
{
ZoneAvoidanceRule
.
class
.
cast
(
getLoadBalancer
().
getRule
());
}
@Test
public
void
serverListFilterDefaultsToZonePreference
()
throws
Exception
{
ZonePreferenceServerListFilter
.
class
.
cast
(
getLoadBalancer
().
getFilter
());
}
@Test
public
void
pingDefaultsToNoOp
()
throws
Exception
{
NoOpPing
.
class
.
cast
(
getLoadBalancer
().
getPing
());
}
@Test
public
void
serverList
FilterOverride
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
ZoneAwareLoadBalancer
<
Server
>
loadBalancer
=
(
ZoneAwareLoadBalancer
<
Server
>)
this
.
factory
.
getLoadBalancer
(
"foo"
);
assertEquals
(
"myTestZone"
,
ZonePreferenceServerListFilter
.
class
.
cast
(
loadBalancer
.
getFilter
())
.
getZone
()
);
public
void
serverList
DefaultsToConfigurationBased
()
throws
Exception
{
ConfigurationBasedServerList
.
class
.
cast
(
getLoadBalancer
().
getServerListImpl
());
}
@SuppressWarnings
(
"unchecked"
)
private
ZoneAwareLoadBalancer
<
Server
>
getLoadBalancer
()
{
return
(
ZoneAwareLoadBalancer
<
Server
>)
this
.
factory
.
getLoadBalancer
(
"foo"
);
}
@Configuration
@RibbonClient
(
name
=
"foo"
,
configuration
=
FooConfiguration
.
class
)
@RibbonClient
(
name
=
"foo"
)
@Import
({
PropertyPlaceholderAutoConfiguration
.
class
,
ArchaiusAutoConfiguration
.
class
,
RibbonAutoConfiguration
.
class
})
protected
static
class
PlainConfiguration
{
}
@Configuration
protected
static
class
FooConfiguration
{
@Bean
public
ZonePreferenceServerListFilter
serverListFilter
()
{
ZonePreferenceServerListFilter
filter
=
new
ZonePreferenceServerListFilter
();
filter
.
setZone
(
"myTestZone"
);
return
filter
;
}
}
}
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonClientPreprocessorOverridesIntegrationTests.java
0 → 100644
View file @
39eb9815
/*
* Copyright 2013-2014 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
.
ribbon
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.loadbalancer.*
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration
;
import
org.springframework.boot.test.SpringApplicationConfiguration
;
import
org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration
;
import
org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
import
org.springframework.test.annotation.DirtiesContext
;
import
org.springframework.test.context.junit4.SpringJUnit4ClassRunner
;
/**
* @author Dave Syer
*/
@RunWith
(
SpringJUnit4ClassRunner
.
class
)
@SpringApplicationConfiguration
(
classes
=
RibbonClientPreprocessorOverridesIntegrationTests
.
TestConfiguration
.
class
)
@DirtiesContext
public
class
RibbonClientPreprocessorOverridesIntegrationTests
{
@Autowired
private
SpringClientFactory
factory
;
@Test
public
void
ruleOverridesToRandom
()
throws
Exception
{
RandomRule
.
class
.
cast
(
getLoadBalancer
().
getRule
());
}
@Test
public
void
pingOverridesToDummy
()
throws
Exception
{
DummyPing
.
class
.
cast
(
getLoadBalancer
().
getPing
());
}
@Test
public
void
serverListOverridesToMy
()
throws
Exception
{
MyServiceList
.
class
.
cast
(
getLoadBalancer
().
getServerListImpl
());
}
@SuppressWarnings
(
"unchecked"
)
private
ZoneAwareLoadBalancer
<
Server
>
getLoadBalancer
()
{
return
(
ZoneAwareLoadBalancer
<
Server
>)
this
.
factory
.
getLoadBalancer
(
"foo"
);
}
@Test
public
void
serverListFilterOverride
()
throws
Exception
{
ServerListFilter
<
Server
>
filter
=
getLoadBalancer
().
getFilter
();
assertEquals
(
"MyTestZone"
,
ZonePreferenceServerListFilter
.
class
.
cast
(
filter
)
.
getZone
());
}
@Configuration
@RibbonClient
(
name
=
"foo"
,
configuration
=
FooConfiguration
.
class
)
@Import
({
PropertyPlaceholderAutoConfiguration
.
class
,
ArchaiusAutoConfiguration
.
class
,
EurekaClientAutoConfiguration
.
class
,
RibbonAutoConfiguration
.
class
})
protected
static
class
TestConfiguration
{
}
@Configuration
public
static
class
FooConfiguration
{
public
FooConfiguration
()
{
System
.
out
.
println
(
"here"
);
}
@Bean
public
IRule
ribbonRule
()
{
return
new
RandomRule
();
}
@Bean
public
IPing
ribbonPing
()
{
return
new
DummyPing
();
}
@Bean
public
ServerList
<
Server
>
ribbonServerList
(
IClientConfig
config
)
{
return
new
MyServiceList
(
config
);
}
@Bean
public
ZonePreferenceServerListFilter
serverListFilter
()
{
ZonePreferenceServerListFilter
filter
=
new
ZonePreferenceServerListFilter
();
filter
.
setZone
(
"MyTestZone"
);
return
filter
;
}
}
public
static
class
MyServiceList
extends
ConfigurationBasedServerList
{
public
MyServiceList
(
IClientConfig
config
)
{
super
.
initWithNiwsConfig
(
config
);
}
}
}
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/RibbonClientsPreprocessorIntegrationTests.java
View file @
39eb9815
...
...
@@ -24,8 +24,6 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
import
org.springframework.cloud.netflix.archaius.ArchaiusAutoConfiguration
;
import
org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration
;
import
org.springframework.cloud.netflix.ribbon.RibbonClientsPreprocessorIntegrationTests.TestConfiguration
;
import
org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList
;
import
org.springframework.cloud.netflix.ribbon.eureka.RibbonEurekaAutoConfiguration
;
import
org.springframework.context.annotation.Bean
;
import
org.springframework.context.annotation.Configuration
;
import
org.springframework.context.annotation.Import
;
...
...
@@ -50,28 +48,20 @@ public class RibbonClientsPreprocessorIntegrationTests {
private
SpringClientFactory
factory
;
@Test
public
void
serverListIsWrapped
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
ZoneAwareLoadBalancer
<
Server
>
loadBalancer
=
(
ZoneAwareLoadBalancer
<
Server
>)
this
.
factory
.
getLoadBalancer
(
"foo"
);
DomainExtractingServerList
.
class
.
cast
(
loadBalancer
.
getServerListImpl
());
public
void
ruleDefaultsToZoneAvoidance
()
throws
Exception
{
ZoneAvoidanceRule
.
class
.
cast
(
getLoadBalancer
().
getRule
());
}
@Test
public
void
ruleDefaultsToZoneAvoidance
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
ZoneAwareLoadBalancer
<
Server
>
loadBalancer
=
(
ZoneAwareLoadBalancer
<
Server
>)
this
.
factory
@SuppressWarnings
(
"unchecked"
)
private
ZoneAwareLoadBalancer
<
Server
>
getLoadBalancer
()
{
return
(
ZoneAwareLoadBalancer
<
Server
>)
this
.
factory
.
getLoadBalancer
(
"foo"
);
ZoneAvoidanceRule
.
class
.
cast
(
loadBalancer
.
getRule
());
}
@Test
public
void
serverListFilterOverride
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
ZoneAwareLoadBalancer
<
Server
>
loadBalancer
=
(
ZoneAwareLoadBalancer
<
Server
>)
this
.
factory
.
getLoadBalancer
(
"foo"
);
assertEquals
(
"myTestZone"
,
ZonePreferenceServerListFilter
.
class
.
cast
(
loadBalancer
.
getFilter
())
ZonePreferenceServerListFilter
.
class
.
cast
(
getLoadBalancer
()
.
getFilter
())
.
getZone
());
}
...
...
@@ -79,7 +69,7 @@ public class RibbonClientsPreprocessorIntegrationTests {
@RibbonClients
(
@RibbonClient
(
name
=
"foo"
,
configuration
=
FooConfiguration
.
class
))
@Import
({
PropertyPlaceholderAutoConfiguration
.
class
,
ArchaiusAutoConfiguration
.
class
,
EurekaClientAutoConfiguration
.
class
,
RibbonAutoConfiguration
.
class
,
RibbonEurekaAutoConfiguration
.
class
})
RibbonAutoConfiguration
.
class
})
protected
static
class
TestConfiguration
{
}
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/eureka/DomainExtractingServerListTests.java
View file @
39eb9815
...
...
@@ -54,7 +54,7 @@ public class DomainExtractingServerListTests {
public
void
testDomainExtractingServer
()
{
DomainExtractingServerList
serverList
=
getDomainExtractingServerList
(
new
DefaultClientConfigImpl
(),
true
);
List
<
Server
>
servers
=
serverList
.
getInitialListOfServers
();
List
<
DiscoveryEnabled
Server
>
servers
=
serverList
.
getInitialListOfServers
();
assertNotNull
(
"servers was null"
,
servers
);
assertEquals
(
"servers was not size 1"
,
1
,
servers
.
size
());
DomainExtractingServer
des
=
assertDomainExtractingServer
(
servers
,
ZONE
);
...
...
@@ -65,14 +65,14 @@ public class DomainExtractingServerListTests {
public
void
testDomainExtractingServerDontApproximateZone
()
{
DomainExtractingServerList
serverList
=
getDomainExtractingServerList
(
new
DefaultClientConfigImpl
(),
false
);
List
<
Server
>
servers
=
serverList
.
getInitialListOfServers
();
List
<
DiscoveryEnabled
Server
>
servers
=
serverList
.
getInitialListOfServers
();
assertNotNull
(
"servers was null"
,
servers
);
assertEquals
(
"servers was not size 1"
,
1
,
servers
.
size
());
DomainExtractingServer
des
=
assertDomainExtractingServer
(
servers
,
null
);
assertEquals
(
"hostPort was wrong"
,
HOST_NAME
+
":"
+
PORT
,
des
.
getHostPort
());
}
protected
DomainExtractingServer
assertDomainExtractingServer
(
List
<
Server
>
servers
,
protected
DomainExtractingServer
assertDomainExtractingServer
(
List
<
DiscoveryEnabled
Server
>
servers
,
String
zone
)
{
Server
actualServer
=
servers
.
get
(
0
);
assertTrue
(
"server was not a DomainExtractingServer"
,
...
...
@@ -89,7 +89,7 @@ public class DomainExtractingServerListTests {
config
.
setProperty
(
CommonClientConfigKey
.
UseIPAddrForServer
,
true
);
DomainExtractingServerList
serverList
=
getDomainExtractingServerList
(
config
,
true
);
List
<
Server
>
servers
=
serverList
.
getInitialListOfServers
();
List
<
DiscoveryEnabled
Server
>
servers
=
serverList
.
getInitialListOfServers
();
assertNotNull
(
"servers was null"
,
servers
);
assertEquals
(
"servers was not size 1"
,
1
,
servers
.
size
());
DomainExtractingServer
des
=
assertDomainExtractingServer
(
servers
,
ZONE
);
...
...
@@ -100,7 +100,7 @@ public class DomainExtractingServerListTests {
DefaultClientConfigImpl
config
,
boolean
approximateZoneFromHostname
)
{
DiscoveryEnabledServer
server
=
mock
(
DiscoveryEnabledServer
.
class
);
@SuppressWarnings
(
"unchecked"
)
ServerList
<
Server
>
originalServerList
=
mock
(
ServerList
.
class
);
ServerList
<
DiscoveryEnabled
Server
>
originalServerList
=
mock
(
ServerList
.
class
);
InstanceInfo
instanceInfo
=
mock
(
InstanceInfo
.
class
);
given
(
server
.
getInstanceInfo
()).
willReturn
(
instanceInfo
);
given
(
server
.
getHost
()).
willReturn
(
HOST_NAME
);
...
...
@@ -111,7 +111,7 @@ public class DomainExtractingServerListTests {
given
(
instanceInfo
.
getIPAddr
()).
willReturn
(
IP_ADDR
);
given
(
instanceInfo
.
getPort
()).
willReturn
(
PORT
);
given
(
originalServerList
.
getInitialListOfServers
()).
willReturn
(
Arrays
.
<
Server
>
asList
(
server
));
Arrays
.
asList
(
server
));
return
new
DomainExtractingServerList
(
originalServerList
,
config
,
approximateZoneFromHostname
);
}
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientConfigurationTests.java
View file @
39eb9815
...
...
@@ -26,12 +26,10 @@ import com.netflix.config.ConfigurationManager;
import
com.netflix.config.DeploymentContext.ContextKey
;
import
com.netflix.config.DynamicStringProperty
;
import
com.netflix.loadbalancer.ILoadBalancer
;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.ZoneAwareLoadBalancer
;
import
com.netflix.niws.loadbalancer.DiscoveryEnabledServer
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
junit
.
Assert
.
assertNotNull
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
static
org
.
junit
.
Assert
.*;
import
static
org
.
springframework
.
cloud
.
netflix
.
ribbon
.
eureka
.
EurekaRibbonClientConfiguration
.
VALUE_NOT_SET
;
/**
...
...
@@ -56,7 +54,7 @@ public class EurekaRibbonClientConfigurationTests {
ILoadBalancer
balancer
=
clientFactory
.
getLoadBalancer
(
"service"
);
assertNotNull
(
balancer
);
@SuppressWarnings
(
"unchecked"
)
ZoneAwareLoadBalancer
<
Server
>
aware
=
(
ZoneAwareLoadBalancer
<
Server
>)
balancer
;
ZoneAwareLoadBalancer
<
DiscoveryEnabledServer
>
aware
=
(
ZoneAwareLoadBalancer
<
DiscoveryEnabled
Server
>)
balancer
;
assertTrue
(
aware
.
getServerListImpl
()
instanceof
DomainExtractingServerList
);
assertEquals
(
"foo"
,
ConfigurationManager
.
getDeploymentContext
().
getValue
(
ContextKey
.
zone
));
...
...
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/ribbon/eureka/EurekaRibbonClientPreprocessorIntegrationTests.java
View file @
39eb9815
...
...
@@ -35,6 +35,7 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import
com.netflix.loadbalancer.Server
;
import
com.netflix.loadbalancer.ZoneAvoidanceRule
;
import
com.netflix.loadbalancer.ZoneAwareLoadBalancer
;
import
com.netflix.niws.loadbalancer.NIWSDiscoveryPing
;
/**
* @author Dave Syer
...
...
@@ -48,11 +49,24 @@ public class EurekaRibbonClientPreprocessorIntegrationTests {
private
SpringClientFactory
factory
;
@Test
public
void
serverListDefaultsToDomainExtracting
()
throws
Exception
{
DomainExtractingServerList
.
class
.
cast
(
getLoadBalancer
().
getServerListImpl
());
}
@Test
public
void
ruleDefaultsToZoneAvoidance
()
throws
Exception
{
@SuppressWarnings
(
"unchecked"
)
ZoneAwareLoadBalancer
<
Server
>
loadBalancer
=
(
ZoneAwareLoadBalancer
<
Server
>)
this
.
factory
ZoneAvoidanceRule
.
class
.
cast
(
getLoadBalancer
().
getRule
());
}
@Test
public
void
pingDefaultsToDiscoveryPing
()
throws
Exception
{
NIWSDiscoveryPing
.
class
.
cast
(
getLoadBalancer
().
getPing
());
}
@SuppressWarnings
(
"unchecked"
)
private
ZoneAwareLoadBalancer
<
Server
>
getLoadBalancer
()
{
return
(
ZoneAwareLoadBalancer
<
Server
>)
this
.
factory
.
getLoadBalancer
(
"foo"
);
ZoneAvoidanceRule
.
class
.
cast
(
loadBalancer
.
getRule
());
}
@Configuration
...
...
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