Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
thythm
ribbon
提交
17277267
R
ribbon
项目概览
thythm
/
ribbon
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
ribbon
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
17277267
编写于
1月 09, 2014
作者:
A
Allen Wang
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'master' of
https://github.com/Netflix/ribbon
into cp
上级
7e6eafa7
d88c9a74
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
463 addition
and
8 deletion
+463
-8
CHANGES.md
CHANGES.md
+15
-0
build.gradle
build.gradle
+4
-2
gradle.properties
gradle.properties
+1
-1
ribbon-core/src/main/java/com/netflix/client/ClientFactory.java
...-core/src/main/java/com/netflix/client/ClientFactory.java
+12
-4
ribbon-core/src/main/java/com/netflix/client/LoadBalancerContext.java
...src/main/java/com/netflix/client/LoadBalancerContext.java
+2
-0
ribbon-core/src/main/java/com/netflix/client/config/CommonClientConfigKey.java
...java/com/netflix/client/config/CommonClientConfigKey.java
+1
-0
ribbon-eureka/src/main/java/com/netflix/niws/loadbalancer/DiscoveryEnabledNIWSServerList.java
...lix/niws/loadbalancer/DiscoveryEnabledNIWSServerList.java
+46
-1
ribbon-eureka/src/test/java/com/netflix/niws/loadbalancer/DiscoveryEnabledLoadBalancerSupportsPortOverrideTest.java
...DiscoveryEnabledLoadBalancerSupportsPortOverrideTest.java
+212
-0
ribbon-httpclient/src/test/java/com/netflix/niws/client/http/ForceClientPortTest.java
...ava/com/netflix/niws/client/http/ForceClientPortTest.java
+170
-0
未找到文件。
CHANGES.md
浏览文件 @
17277267
### Version 0.3.4
https://github.com/Netflix/ribbon/pull/62
*
Fix Issue #61: FollowRedirects impossible to turn off.
### Version 0.3.3
https://github.com/Netflix/ribbon/pull/60
*
add hook for custom ssl factory and AcceptAllSocketFactory implementation
### Version 0.3.2
[
pull request 57
](
https://github.com/Netflix/ribbon/pull/57
)
*
Make sure NamedConnectionPool instances register with Servo with unique names.
### Version 0.3.1
[
pull request 56
](
https://github.com/Netflix/ribbon/pull/56
)
...
...
build.gradle
浏览文件 @
17277267
...
...
@@ -31,7 +31,8 @@ subprojects {
compile
'com.netflix.archaius:archaius-core:0.5.12'
compile
'com.netflix.netflix-commons:netflix-commons-util:0.1.1'
compile
'commons-collections:commons-collections:3.2.1'
testCompile
'junit:junit:4.10'
testCompile
'org.powermock:powermock-easymock-release-full:1.4.10'
testCompile
'org.easymock:easymock:3.1'
testCompile
'org.slf4j:slf4j-log4j12:1.7.2'
}
...
...
@@ -56,13 +57,14 @@ project(':ribbon-httpclient') {
testCompile
'commons-io:commons-io:2.0.1'
testCompile
'com.sun.jersey:jersey-server:1.11'
testCompile
'com.google.mockwebserver:mockwebserver:20130505'
testCompile
project
(
':ribbon-eureka'
)
}
}
project
(
':ribbon-eureka'
)
{
dependencies
{
compile
project
(
':ribbon-core'
)
compile
'com.netflix.eureka:eureka-client:1.1.107'
compile
'com.netflix.eureka:eureka-client:1.1.107'
}
}
...
...
gradle.properties
浏览文件 @
17277267
version
=
0.3.
4
version
=
0.3.
6
ribbon-core/src/main/java/com/netflix/client/ClientFactory.java
浏览文件 @
17277267
...
...
@@ -92,14 +92,22 @@ public class ClientFactory {
* @throws RuntimeException if an error occurs in creating the client.
*/
public
static
synchronized
IClient
getNamedClient
(
String
name
)
{
return
getNamedClient
(
name
,
DefaultClientConfigImpl
.
class
);
}
/**
* Return the named client from map if already created. Otherwise creates the client using the configuration returned by {@link #createNamedClient(String, Class)}.
*
* @throws RuntimeException if an error occurs in creating the client.
*/
public
static
synchronized
IClient
getNamedClient
(
String
name
,
Class
<?
extends
IClientConfig
>
configClass
)
{
if
(
simpleClientMap
.
get
(
name
)
!=
null
)
{
return
simpleClientMap
.
get
(
name
);
return
simpleClientMap
.
get
(
name
);
}
IClientConfig
niwsClientConfig
=
getNamedConfig
(
name
);
try
{
return
registerClientFromProperties
(
name
,
niwsClientConfig
);
return
createNamedClient
(
name
,
configClass
);
}
catch
(
ClientException
e
)
{
throw
new
RuntimeException
(
"Unable to create client"
,
e
);
throw
new
RuntimeException
(
"Unable to create client"
,
e
);
}
}
...
...
ribbon-core/src/main/java/com/netflix/client/LoadBalancerContext.java
浏览文件 @
17277267
...
...
@@ -95,6 +95,7 @@ public abstract class LoadBalancerContext<T extends ClientRequest, S extends IRe
okToRetryOnAllOperations
=
clientConfig
.
getPropertyAsBoolean
(
CommonClientConfigKey
.
OkToRetryOnAllOperations
,
okToRetryOnAllOperations
);
tracer
=
getExecuteTracer
();
Monitors
.
registerObject
(
"Client_"
+
clientName
,
this
);
}
...
...
@@ -421,6 +422,7 @@ public abstract class LoadBalancerContext<T extends ClientRequest, S extends IRe
}
host
=
svc
.
getHost
();
port
=
svc
.
getPort
();
if
(
host
==
null
){
throw
new
ClientException
(
ClientException
.
ErrorType
.
GENERAL
,
"Invalid Server for :"
+
svc
);
...
...
ribbon-core/src/main/java/com/netflix/client/config/CommonClientConfigKey.java
浏览文件 @
17277267
...
...
@@ -24,6 +24,7 @@ public enum CommonClientConfigKey implements IClientConfigKey {
Version
(
"Version"
),
Port
(
"Port"
),
SecurePort
(
"SecurePort"
),
ForceClientPortConfiguration
(
"ForceClientPortConfiguration"
),
// use client defined port regardless of server advert
VipAddress
(
"VipAddress"
),
DeploymentContextBasedVipAddresses
(
"DeploymentContextBasedVipAddresses"
),
MaxAutoRetries
(
"MaxAutoRetries"
),
...
...
ribbon-eureka/src/main/java/com/netflix/niws/loadbalancer/DiscoveryEnabledNIWSServerList.java
浏览文件 @
17277267
...
...
@@ -23,12 +23,15 @@ import java.util.List;
import
com.netflix.appinfo.InstanceInfo
;
import
com.netflix.appinfo.InstanceInfo.InstanceStatus
;
import
com.netflix.client.config.CommonClientConfigKey
;
import
com.netflix.client.config.DefaultClientConfigImpl
;
import
com.netflix.client.config.IClientConfig
;
import
com.netflix.config.ConfigurationManager
;
import
com.netflix.discovery.DiscoveryClient
;
import
com.netflix.discovery.DiscoveryManager
;
import
com.netflix.loadbalancer.AbstractServerList
;
import
com.netflix.loadbalancer.DynamicServerListLoadBalancer
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
/**
* The server list class that fetches the server information from Eureka client. ServerList is used by
...
...
@@ -39,6 +42,8 @@ import com.netflix.loadbalancer.DynamicServerListLoadBalancer;
*/
public
class
DiscoveryEnabledNIWSServerList
extends
AbstractServerList
<
DiscoveryEnabledServer
>{
private
static
final
Logger
logger
=
LoggerFactory
.
getLogger
(
DiscoveryEnabledNIWSServerList
.
class
);
String
clientName
;
String
vipAddresses
;
boolean
isSecure
=
false
;
...
...
@@ -47,15 +52,46 @@ public class DiscoveryEnabledNIWSServerList extends AbstractServerList<Discovery
String
datacenter
;
String
targetRegion
;
int
overridePort
=
DefaultClientConfigImpl
.
DEFAULT_PORT
;
boolean
shouldUseOverridePort
=
false
;
@Override
public
void
initWithNiwsConfig
(
IClientConfig
clientConfig
)
{
this
.
clientName
=
clientConfig
.
getClientName
();
clientName
=
clientConfig
.
getClientName
();
vipAddresses
=
clientConfig
.
resolveDeploymentContextbasedVipAddresses
();
isSecure
=
Boolean
.
parseBoolean
(
""
+
clientConfig
.
getProperty
(
CommonClientConfigKey
.
IsSecure
,
"false"
));
prioritizeVipAddressBasedServers
=
Boolean
.
parseBoolean
(
""
+
clientConfig
.
getProperty
(
CommonClientConfigKey
.
PrioritizeVipAddressBasedServers
,
prioritizeVipAddressBasedServers
));
datacenter
=
ConfigurationManager
.
getDeploymentContext
().
getDeploymentDatacenter
();
targetRegion
=
(
String
)
clientConfig
.
getProperty
(
CommonClientConfigKey
.
TargetRegion
);
// override client configuration and use client-defined port
if
(
clientConfig
.
getPropertyAsBoolean
(
CommonClientConfigKey
.
ForceClientPortConfiguration
,
false
)){
if
(
isSecure
){
if
(
clientConfig
.
containsProperty
(
CommonClientConfigKey
.
SecurePort
)){
overridePort
=
clientConfig
.
getPropertyAsInteger
(
CommonClientConfigKey
.
SecurePort
,
DefaultClientConfigImpl
.
DEFAULT_PORT
);
shouldUseOverridePort
=
true
;
}
else
{
logger
.
warn
(
clientName
+
" set to force client port but no secure port is set, so ignoring"
);
}
}
else
{
if
(
clientConfig
.
containsProperty
(
CommonClientConfigKey
.
Port
)){
overridePort
=
clientConfig
.
getPropertyAsInteger
(
CommonClientConfigKey
.
Port
,
DefaultClientConfigImpl
.
DEFAULT_PORT
);
shouldUseOverridePort
=
true
;
}
else
{
logger
.
warn
(
clientName
+
" set to force client port but no port is set, so ignoring"
);
}
}
}
}
...
...
@@ -85,6 +121,15 @@ public class DiscoveryEnabledNIWSServerList extends AbstractServerList<Discovery
List
<
InstanceInfo
>
listOfinstanceInfo
=
discoveryClient
.
getInstancesByVipAddress
(
vipAddress
,
isSecure
,
targetRegion
);
for
(
InstanceInfo
ii
:
listOfinstanceInfo
)
{
if
(
ii
.
getStatus
().
equals
(
InstanceStatus
.
UP
))
{
if
(
shouldUseOverridePort
){
if
(
logger
.
isDebugEnabled
()){
logger
.
debug
(
"Overriding port on client name: "
+
clientName
+
" to "
+
overridePort
);
}
ii
=
new
InstanceInfo
.
Builder
(
ii
).
setPort
(
overridePort
).
build
();
}
DiscoveryEnabledServer
des
=
new
DiscoveryEnabledServer
(
ii
,
isSecure
);
des
.
setZone
(
DiscoveryClient
.
getZone
(
ii
));
serverList
.
add
(
des
);
...
...
ribbon-eureka/src/test/java/com/netflix/niws/loadbalancer/DiscoveryEnabledLoadBalancerSupportsPortOverrideTest.java
0 → 100644
浏览文件 @
17277267
/*
*
* Copyright 2014 Netflix, Inc.
*
* 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
com.netflix.niws.loadbalancer
;
import
com.netflix.appinfo.InstanceInfo
;
import
com.netflix.client.config.DefaultClientConfigImpl
;
import
com.netflix.config.ConfigurationManager
;
import
com.netflix.discovery.DiscoveryClient
;
import
com.netflix.discovery.DiscoveryManager
;
import
org.easymock.EasyMock
;
import
org.junit.After
;
import
org.junit.Assert
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.powermock.api.easymock.PowerMock
;
import
org.powermock.core.classloader.annotations.PowerMockIgnore
;
import
org.powermock.core.classloader.annotations.PrepareForTest
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
java.util.ArrayList
;
import
java.util.List
;
import
static
org
.
easymock
.
EasyMock
.
expect
;
import
static
org
.
powermock
.
api
.
easymock
.
PowerMock
.
createMock
;
import
static
org
.
powermock
.
api
.
easymock
.
PowerMock
.
replay
;
import
static
org
.
powermock
.
api
.
easymock
.
PowerMock
.
verify
;
/**
* Verify behavior of the override flag ForceClientPortConfiguration("ForceClientPortConfiguration")
*
* WARNING: This feature should be treated as temporary workaround to support
* more than 2 ports per server until relevant load balancer paradigms
* (i.e., Eureka Server/Client) can accommodate more than 2 ports in its metadata.
*
* Currently only works with the DiscoveryEnabledNIWSServerList since this is where the current limitation is applicable
*
* See also: https://github.com/Netflix/eureka/issues/71
*
* I'll add that testing this is painful due to the DiscoveryManager.getInstance()... singletons man...
*
* Created by jzarfoss on 1/7/14.
*/
@RunWith
(
PowerMockRunner
.
class
)
@PrepareForTest
(
{
DiscoveryManager
.
class
,
DiscoveryClient
.
class
}
)
@PowerMockIgnore
(
"javax.management.*"
)
public
class
DiscoveryEnabledLoadBalancerSupportsPortOverrideTest
{
@Before
public
void
setupMock
(){
PowerMock
.
mockStatic
(
DiscoveryManager
.
class
);
PowerMock
.
mockStatic
(
DiscoveryClient
.
class
);
DiscoveryClient
mockedDiscoveryClient
=
createMock
(
DiscoveryClient
.
class
);
DiscoveryManager
mockedDiscoveryManager
=
createMock
(
DiscoveryManager
.
class
);
expect
(
DiscoveryClient
.
getZone
((
InstanceInfo
)
EasyMock
.
anyObject
())).
andReturn
(
"dummyZone"
).
anyTimes
();
expect
(
DiscoveryManager
.
getInstance
()).
andReturn
(
mockedDiscoveryManager
).
anyTimes
();
expect
(
mockedDiscoveryManager
.
getDiscoveryClient
()).
andReturn
(
mockedDiscoveryClient
).
anyTimes
();
expect
(
mockedDiscoveryClient
.
getInstancesByVipAddress
(
"dummy"
,
false
,
"region"
)).
andReturn
(
getDummyInstanceInfo
(
"dummy"
,
"http://www.host.com"
,
7000
)).
anyTimes
();
expect
(
mockedDiscoveryClient
.
getInstancesByVipAddress
(
"secureDummy"
,
true
,
"region"
)).
andReturn
(
getDummyInstanceInfo
(
"secureDummy"
,
"http://www.host.com"
,
7001
)).
anyTimes
();
replay
(
DiscoveryManager
.
class
);
replay
(
DiscoveryClient
.
class
);
replay
(
mockedDiscoveryManager
);
replay
(
mockedDiscoveryClient
);
}
@After
public
void
afterMock
(){
verify
(
DiscoveryManager
.
class
);
verify
(
DiscoveryClient
.
class
);
}
@Test
public
void
testDefaultHonorsVipPortDefinition
()
throws
Exception
{
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testDefaultHonorsVipPortDefinition.ribbon.DeploymentContextBasedVipAddresses"
,
"dummy"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testDefaultHonorsVipPortDefinition.ribbon.IsSecure"
,
"false"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testDefaultHonorsVipPortDefinition.ribbon.Port"
,
"6999"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testDefaultHonorsVipPortDefinition.ribbon.TargetRegion"
,
"region"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testDefaultHonorsVipPortDefinition.ribbon.NIWSServerListClassName"
,
DiscoveryEnabledNIWSServerList
.
class
.
getName
());
DiscoveryEnabledNIWSServerList
deList
=
new
DiscoveryEnabledNIWSServerList
();
DefaultClientConfigImpl
clientConfig
=
DefaultClientConfigImpl
.
class
.
newInstance
();
clientConfig
.
loadProperties
(
"DiscoveryEnabled.testDefaultHonorsVipPortDefinition"
);
deList
.
initWithNiwsConfig
(
clientConfig
);
List
<
DiscoveryEnabledServer
>
serverList
=
deList
.
getInitialListOfServers
();
Assert
.
assertEquals
(
1
,
serverList
.
size
());
Assert
.
assertEquals
(
7000
,
serverList
.
get
(
0
).
getPort
());
}
@Test
public
void
testDefaultHonorsVipSecurePortDefinition
()
throws
Exception
{
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testDefaultHonorsVipSecurePortDefinition.ribbon.DeploymentContextBasedVipAddresses"
,
"secureDummy"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testDefaultHonorsVipSecurePortDefinition.ribbon.IsSecure"
,
"true"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testDefaultHonorsVipSecurePortDefinition.ribbon.SecurePort"
,
"6999"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testDefaultHonorsVipSecurePortDefinition.ribbon.TargetRegion"
,
"region"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testDefaultHonorsVipSecurePortDefinition.ribbon.NIWSServerListClassName"
,
DiscoveryEnabledNIWSServerList
.
class
.
getName
());
DiscoveryEnabledNIWSServerList
deList
=
new
DiscoveryEnabledNIWSServerList
();
DefaultClientConfigImpl
clientConfig
=
DefaultClientConfigImpl
.
class
.
newInstance
();
clientConfig
.
loadProperties
(
"DiscoveryEnabled.testDefaultHonorsVipSecurePortDefinition"
);
deList
.
initWithNiwsConfig
(
clientConfig
);
List
<
DiscoveryEnabledServer
>
serverList
=
deList
.
getInitialListOfServers
();
Assert
.
assertEquals
(
1
,
serverList
.
size
());
Assert
.
assertEquals
(
7001
,
serverList
.
get
(
0
).
getPort
());
}
@Test
public
void
testVipPortCanBeOverriden
()
throws
Exception
{
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testVipPortCanBeOverriden.ribbon.DeploymentContextBasedVipAddresses"
,
"dummy"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testVipPortCanBeOverriden.ribbon.IsSecure"
,
"false"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testVipPortCanBeOverriden.ribbon.Port"
,
"6999"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testVipPortCanBeOverriden.ribbon.TargetRegion"
,
"region"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testVipPortCanBeOverriden.ribbon.NIWSServerListClassName"
,
DiscoveryEnabledNIWSServerList
.
class
.
getName
());
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testVipPortCanBeOverriden.ribbon.ForceClientPortConfiguration"
,
"true"
);
DiscoveryEnabledNIWSServerList
deList
=
new
DiscoveryEnabledNIWSServerList
();
DefaultClientConfigImpl
clientConfig
=
DefaultClientConfigImpl
.
class
.
newInstance
();
clientConfig
.
loadProperties
(
"DiscoveryEnabled.testVipPortCanBeOverriden"
);
deList
.
initWithNiwsConfig
(
clientConfig
);
List
<
DiscoveryEnabledServer
>
serverList
=
deList
.
getInitialListOfServers
();
Assert
.
assertEquals
(
1
,
serverList
.
size
());
Assert
.
assertEquals
(
6999
,
serverList
.
get
(
0
).
getPort
());
}
@Test
public
void
testSecureVipPortCanBeOverriden
()
throws
Exception
{
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testSecureVipPortCanBeOverriden.ribbon.DeploymentContextBasedVipAddresses"
,
"secureDummy"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testSecureVipPortCanBeOverriden.ribbon.IsSecure"
,
"true"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testSecureVipPortCanBeOverriden.ribbon.SecurePort"
,
"6998"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testSecureVipPortCanBeOverriden.ribbon.TargetRegion"
,
"region"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testSecureVipPortCanBeOverriden.ribbon.NIWSServerListClassName"
,
DiscoveryEnabledNIWSServerList
.
class
.
getName
());
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"DiscoveryEnabled.testSecureVipPortCanBeOverriden.ribbon.ForceClientPortConfiguration"
,
"true"
);
DiscoveryEnabledNIWSServerList
deList
=
new
DiscoveryEnabledNIWSServerList
();
DefaultClientConfigImpl
clientConfig
=
DefaultClientConfigImpl
.
class
.
newInstance
();
clientConfig
.
loadProperties
(
"DiscoveryEnabled.testSecureVipPortCanBeOverriden"
);
deList
.
initWithNiwsConfig
(
clientConfig
);
List
<
DiscoveryEnabledServer
>
serverList
=
deList
.
getInitialListOfServers
();
Assert
.
assertEquals
(
1
,
serverList
.
size
());
Assert
.
assertEquals
(
6998
,
serverList
.
get
(
0
).
getPort
());
}
protected
static
List
<
InstanceInfo
>
getDummyInstanceInfo
(
String
appName
,
String
host
,
int
port
){
List
<
InstanceInfo
>
list
=
new
ArrayList
<
InstanceInfo
>();
InstanceInfo
info
=
InstanceInfo
.
Builder
.
newBuilder
().
setAppName
(
appName
)
.
setHostName
(
host
)
.
setPort
(
port
)
.
build
();
list
.
add
(
info
);
return
list
;
}
}
ribbon-httpclient/src/test/java/com/netflix/niws/client/http/ForceClientPortTest.java
0 → 100644
浏览文件 @
17277267
/*
*
* Copyright 2014 Netflix, Inc.
*
* 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
com.netflix.niws.client.http
;
import
com.netflix.appinfo.AmazonInfo
;
import
com.netflix.appinfo.InstanceInfo
;
import
com.netflix.client.ClientFactory
;
import
com.netflix.client.http.HttpRequest
;
import
com.netflix.client.http.HttpResponse
;
import
com.netflix.config.ConfigurationManager
;
import
com.netflix.discovery.DiscoveryClient
;
import
com.netflix.discovery.DiscoveryManager
;
import
com.netflix.loadbalancer.DynamicServerListLoadBalancer
;
import
com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList
;
import
org.easymock.EasyMock
;
import
org.junit.After
;
import
org.junit.Before
;
import
org.junit.Test
;
import
org.junit.runner.RunWith
;
import
org.powermock.api.easymock.PowerMock
;
import
org.powermock.core.classloader.annotations.PowerMockIgnore
;
import
org.powermock.core.classloader.annotations.PrepareForTest
;
import
org.powermock.modules.junit4.PowerMockRunner
;
import
java.net.URI
;
import
java.util.ArrayList
;
import
java.util.List
;
import
static
org
.
easymock
.
EasyMock
.
expect
;
import
static
org
.
junit
.
Assert
.
assertEquals
;
import
static
org
.
powermock
.
api
.
easymock
.
PowerMock
.
createMock
;
import
static
org
.
powermock
.
api
.
easymock
.
PowerMock
.
replay
;
import
static
org
.
powermock
.
api
.
easymock
.
PowerMock
.
verify
;
/**
*
* This is essentially a 'live' test variant of {@link com.netflix.niws.loadbalancer.DiscoveryEnabledLoadBalancerSupportsPortOverrideTest}
* with all of the plumbing working together.
*
* Created by jzarfoss on 1/7/14.
*/
@RunWith
(
PowerMockRunner
.
class
)
@PrepareForTest
(
{
DiscoveryManager
.
class
,
DiscoveryClient
.
class
}
)
@PowerMockIgnore
({
"javax.management.*"
,
"org.apache.http.conn.ssl.*"
})
public
class
ForceClientPortTest
{
@Before
public
void
setupMock
(){
PowerMock
.
mockStatic
(
DiscoveryManager
.
class
);
PowerMock
.
mockStatic
(
DiscoveryClient
.
class
);
DiscoveryClient
mockedDiscoveryClient
=
createMock
(
DiscoveryClient
.
class
);
DiscoveryManager
mockedDiscoveryManager
=
createMock
(
DiscoveryManager
.
class
);
expect
(
DiscoveryClient
.
getZone
((
InstanceInfo
)
EasyMock
.
anyObject
())).
andReturn
(
"dummyZone"
).
anyTimes
();
expect
(
DiscoveryManager
.
getInstance
()).
andReturn
(
mockedDiscoveryManager
).
anyTimes
();
expect
(
mockedDiscoveryManager
.
getDiscoveryClient
()).
andReturn
(
mockedDiscoveryClient
).
anyTimes
();
// I'm only testing non-secure ports here, since the ssl libraries are full of static libraries and they were
// giving me some headaches with all of the mocks
expect
(
mockedDiscoveryClient
.
getInstancesByVipAddress
(
"google"
,
false
,
"region"
)).
andReturn
(
getDummyGoogleInstanceInfo
(
"google"
,
80
)).
anyTimes
();
expect
(
mockedDiscoveryClient
.
getInstancesByVipAddress
(
"badGoogle"
,
false
,
"region"
)).
andReturn
(
getDummyGoogleInstanceInfo
(
"badGoogle"
,
81
)).
anyTimes
();
replay
(
DiscoveryManager
.
class
);
replay
(
DiscoveryClient
.
class
);
replay
(
mockedDiscoveryManager
);
replay
(
mockedDiscoveryClient
);
}
@After
public
void
afterMock
(){
verify
(
DiscoveryManager
.
class
);
verify
(
DiscoveryClient
.
class
);
}
@Test
(
expected
=
com
.
netflix
.
client
.
ClientException
.
class
)
public
void
negativeTest
()
throws
Exception
{
// establish that actually using port 81 doesn't work
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.negativeTest.ribbon.DeploymentContextBasedVipAddresses"
,
"badGoogle"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.negativeTest.ribbon.TargetRegion"
,
"region"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.negativeTest.ribbon.NFLoadBalancerClassName"
,
DynamicServerListLoadBalancer
.
class
.
getName
());
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.negativeTest.ribbon.NIWSServerListClassName"
,
DiscoveryEnabledNIWSServerList
.
class
.
getName
());
RestClient
client
=
(
RestClient
)
ClientFactory
.
getNamedClient
(
"ForceClientPortTest.negativeTest"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
().
uri
(
new
URI
(
"/"
)).
build
();
client
.
executeWithLoadBalancer
(
request
);
}
@Test
public
void
testDefaultHonorsServerPortDefinition
()
throws
Exception
{
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.testDefaultHonorsServerPortDefinition.ribbon.DeploymentContextBasedVipAddresses"
,
"google"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.testDefaultHonorsServerPortDefinition.ribbon.TargetRegion"
,
"region"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.testDefaultHonorsServerPortDefinition.ribbon.IsSecure"
,
"false"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.testDefaultHonorsServerPortDefinition.ribbon.Port"
,
"81"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.testDefaultHonorsServerPortDefinition.ribbon.NIWSServerListClassName"
,
DiscoveryEnabledNIWSServerList
.
class
.
getName
());
RestClient
client
=
(
RestClient
)
ClientFactory
.
getNamedClient
(
"ForceClientPortTest.testDefaultHonorsServerPortDefinition"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
().
uri
(
new
URI
(
"/"
)).
build
();
HttpResponse
response
=
client
.
executeWithLoadBalancer
(
request
);
assertEquals
(
200
,
response
.
getStatus
());
}
@Test
public
void
testOverrideForcesClientPort
()
throws
Exception
{
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.testOverrideForcesClientPort.ribbon.DeploymentContextBasedVipAddresses"
,
"badGoogle"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.testOverrideForcesClientPort.ribbon.TargetRegion"
,
"region"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.testOverrideForcesClientPort.ribbon.IsSecure"
,
"false"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.testOverrideForcesClientPort.ribbon.Port"
,
"80"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.testOverrideForcesClientPort.ribbon.ForceClientPortConfiguration"
,
"true"
);
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"ForceClientPortTest.testOverrideForcesClientPort.ribbon.NIWSServerListClassName"
,
DiscoveryEnabledNIWSServerList
.
class
.
getName
());
RestClient
client
=
(
RestClient
)
ClientFactory
.
getNamedClient
(
"ForceClientPortTest.testOverrideForcesClientPort"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
().
uri
(
new
URI
(
"/"
)).
build
();
HttpResponse
response
=
client
.
executeWithLoadBalancer
(
request
);
assertEquals
(
200
,
response
.
getStatus
());
}
protected
static
List
<
InstanceInfo
>
getDummyGoogleInstanceInfo
(
String
appName
,
int
port
){
List
<
InstanceInfo
>
list
=
new
ArrayList
<
InstanceInfo
>();
InstanceInfo
info
=
InstanceInfo
.
Builder
.
newBuilder
().
setAppName
(
appName
)
.
setHostName
(
"www.google.com"
)
.
setPort
(
port
)
.
setDataCenterInfo
(
AmazonInfo
.
Builder
.
newBuilder
().
build
())
.
build
();
list
.
add
(
info
);
return
list
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录