Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
thythm
ribbon
提交
39b4a419
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 搜索 >>
未验证
提交
39b4a419
编写于
5月 09, 2019
作者:
E
elandau
提交者:
GitHub
5月 09, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #410 from elandau/feature/value_of_properties
config: custom types with valueOf
上级
1cbcf285
7274de5e
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
83 addition
and
10 deletion
+83
-10
ribbon-archaius/src/main/java/com/netflix/client/config/DefaultClientConfigImpl.java
...va/com/netflix/client/config/DefaultClientConfigImpl.java
+18
-7
ribbon-core/src/main/java/com/netflix/client/config/AbstractReloadableClientConfig.java
...netflix/client/config/AbstractReloadableClientConfig.java
+28
-3
ribbon-core/src/test/java/com/netflix/client/config/ClientConfigTest.java
...test/java/com/netflix/client/config/ClientConfigTest.java
+37
-0
未找到文件。
ribbon-archaius/src/main/java/com/netflix/client/config/DefaultClientConfigImpl.java
浏览文件 @
39b4a419
...
...
@@ -21,6 +21,8 @@ import com.netflix.client.VipAddressResolver;
import
com.netflix.config.ConfigurationManager
;
import
org.apache.commons.configuration.event.ConfigurationEvent
;
import
org.apache.commons.configuration.event.ConfigurationListener
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.util.Arrays
;
import
java.util.Optional
;
...
...
@@ -73,6 +75,7 @@ myclient.foo.ReadTimeout=1000
*
*/
public
class
DefaultClientConfigImpl
extends
AbstractReloadableClientConfig
{
private
static
final
Logger
LOG
=
LoggerFactory
.
getLogger
(
DefaultClientConfigImpl
.
class
);
@Deprecated
public
static
final
Boolean
DEFAULT_PRIORITIZE_VIP_ADDRESS_BASED_SERVERS
=
Boolean
.
TRUE
;
...
...
@@ -616,11 +619,9 @@ public class DefaultClientConfigImpl extends AbstractReloadableClientConfig {
@Override
protected
<
T
>
Optional
<
T
>
loadProperty
(
String
key
,
Class
<
T
>
type
)
{
if
(
String
.
class
.
equals
(
type
))
{
return
Optional
.
ofNullable
(
ConfigurationManager
.
getConfigInstance
().
getStringArray
(
key
))
.
filter
(
ar
->
ar
.
length
>
0
)
.
map
(
ar
->
(
T
)
Arrays
.
stream
(
ar
).
collect
(
Collectors
.
joining
(
","
)));
}
else
if
(
Integer
.
class
.
equals
(
type
))
{
LOG
.
debug
(
"Loading property {}"
,
key
);
if
(
Integer
.
class
.
equals
(
type
))
{
return
Optional
.
ofNullable
((
T
)
ConfigurationManager
.
getConfigInstance
().
getInteger
(
key
,
null
));
}
else
if
(
Boolean
.
class
.
equals
(
type
))
{
return
Optional
.
ofNullable
((
T
)
ConfigurationManager
.
getConfigInstance
().
getBoolean
(
key
,
null
));
...
...
@@ -632,9 +633,19 @@ public class DefaultClientConfigImpl extends AbstractReloadableClientConfig {
return
Optional
.
ofNullable
((
T
)
ConfigurationManager
.
getConfigInstance
().
getDouble
(
key
,
null
));
}
else
if
(
TimeUnit
.
class
.
equals
(
type
))
{
return
Optional
.
ofNullable
((
T
)
TimeUnit
.
valueOf
(
ConfigurationManager
.
getConfigInstance
().
getString
(
key
,
null
)));
}
else
{
return
Optional
.
ofNullable
(
ConfigurationManager
.
getConfigInstance
().
getStringArray
(
key
))
.
filter
(
ar
->
ar
.
length
>
0
)
.
map
(
ar
->
Arrays
.
stream
(
ar
).
collect
(
Collectors
.
joining
(
","
)))
.
map
(
value
->
{
if
(
type
.
equals
(
String
.
class
))
{
return
(
T
)
value
;
}
else
{
return
resolveWithValueOf
(
type
,
value
)
.
orElseThrow
(()
->
new
IllegalArgumentException
(
"Unable to convert value to desired type "
+
type
));
}
});
}
throw
new
IllegalArgumentException
(
"Unable to convert value to desired type "
+
type
);
}
public
DefaultClientConfigImpl
withProperty
(
IClientConfigKey
key
,
Object
value
)
{
...
...
ribbon-core/src/main/java/com/netflix/client/config/AbstractReloadableClientConfig.java
浏览文件 @
39b4a419
...
...
@@ -6,6 +6,7 @@ import org.apache.commons.lang.StringUtils;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
java.lang.reflect.Method
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -250,15 +251,38 @@ public abstract class AbstractReloadableClientConfig implements IClientConfig {
/**
* Returns the internal property to the desiredn type
*/
private
static
Map
<
Class
<?>,
Optional
<
Method
>>
valueOfMethods
=
new
ConcurrentHashMap
<>();
public
static
<
T
>
Optional
<
T
>
resolveWithValueOf
(
Class
<
T
>
type
,
String
value
)
{
return
valueOfMethods
.
computeIfAbsent
(
type
,
ignore
->
{
try
{
return
Optional
.
of
(
type
.
getDeclaredMethod
(
"valueOf"
,
String
.
class
));
}
catch
(
NoSuchMethodException
e
)
{
return
Optional
.
empty
();
}
catch
(
Exception
e
)
{
LOG
.
warn
(
"Unable to determine if type "
+
type
+
" has a valueOf() static method"
,
e
);
return
Optional
.
empty
();
}
}).
map
(
method
->
{
try
{
return
(
T
)
method
.
invoke
(
null
,
value
);
}
catch
(
Exception
e
)
{
throw
new
RuntimeException
(
e
);
}
});
}
protected
<
T
>
Optional
<
T
>
resolveDefaultProperty
(
IClientConfigKey
<
T
>
key
)
{
return
Optional
.
ofNullable
(
defaultProperties
.
get
(
key
.
key
()))
.
map
(
value
->
{
final
Class
type
=
key
.
type
();
final
Class
<
T
>
type
=
key
.
type
();
// Unfortunately there's some legacy code setting string values for typed keys. Here are do our best to parse
// and store the typed value
if
(!
value
.
getClass
().
equals
(
type
))
{
try
{
if
(
value
.
getClass
().
equals
(
String
.
class
))
{
if
(
type
.
equals
(
String
.
class
))
{
return
(
T
)
value
.
toString
();
}
else
if
(
value
.
getClass
().
equals
(
String
.
class
))
{
final
String
strValue
=
(
String
)
value
;
if
(
Integer
.
class
.
equals
(
type
))
{
return
(
T
)
Integer
.
valueOf
(
strValue
);
...
...
@@ -273,7 +297,8 @@ public abstract class AbstractReloadableClientConfig implements IClientConfig {
}
else
if
(
TimeUnit
.
class
.
equals
(
type
))
{
return
(
T
)
TimeUnit
.
valueOf
(
strValue
);
}
else
{
throw
new
IllegalArgumentException
(
"Unsupported value type `"
+
type
+
"'"
);
return
resolveWithValueOf
(
type
,
strValue
)
.
orElseThrow
(()
->
new
IllegalArgumentException
(
"Unsupported value type `"
+
type
+
"'"
));
}
}
else
{
throw
new
IllegalArgumentException
(
"Incompatible value type `"
+
value
.
getClass
()
+
"` while expecting '"
+
type
+
"`"
);
...
...
ribbon-core/src/test/java/com/netflix/client/config/ClientConfigTest.java
浏览文件 @
39b4a419
...
...
@@ -154,5 +154,42 @@ public class ClientConfigTest {
Assert
.
assertEquals
(
200
,
prop
.
get
().
intValue
());
}
static
class
CustomValueOf
{
private
final
String
value
;
public
static
CustomValueOf
valueOf
(
String
value
)
{
return
new
CustomValueOf
(
value
);
}
public
CustomValueOf
(
String
value
)
{
this
.
value
=
value
;
}
public
String
getValue
()
{
return
value
;
}
}
public
static
IClientConfigKey
<
CustomValueOf
>
CUSTOM_KEY
=
new
CommonClientConfigKey
<
CustomValueOf
>(
"CustomValueOf"
,
new
CustomValueOf
(
"default"
))
{};
@Test
public
void
testValueOfWithDefault
()
{
DefaultClientConfigImpl
clientConfig
=
new
DefaultClientConfigImpl
();
CustomValueOf
prop
=
clientConfig
.
getOrDefault
(
CUSTOM_KEY
);
Assert
.
assertEquals
(
"default"
,
prop
.
getValue
());
}
@Test
public
void
testValueOf
()
{
ConfigurationManager
.
getConfigInstance
().
setProperty
(
"testValueOf.ribbon.CustomValueOf"
,
"value"
);
DefaultClientConfigImpl
clientConfig
=
new
DefaultClientConfigImpl
();
clientConfig
.
setClientName
(
"testValueOf"
);
Property
<
CustomValueOf
>
prop
=
clientConfig
.
getDynamicProperty
(
CUSTOM_KEY
);
Assert
.
assertEquals
(
"value"
,
prop
.
get
().
getValue
());
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录