Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
zhangjian1949
apollo
提交
5e0ab3cc
apollo
项目概览
zhangjian1949
/
apollo
与 Fork 源项目一致
从无法访问的项目Fork
通知
3
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
apollo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
5e0ab3cc
编写于
7月 11, 2020
作者:
J
Jason Song
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
revert to eureka client implementation to get the correct home page url
上级
52b3f6f8
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
46 addition
and
53 deletion
+46
-53
apollo-configservice/src/main/java/com/ctrip/framework/apollo/metaservice/service/DefaultDiscoveryService.java
...k/apollo/metaservice/service/DefaultDiscoveryService.java
+16
-18
apollo-configservice/src/test/java/com/ctrip/framework/apollo/metaservice/service/DefaultDiscoveryServiceTest.java
...ollo/metaservice/service/DefaultDiscoveryServiceTest.java
+30
-35
未找到文件。
apollo-configservice/src/main/java/com/ctrip/framework/apollo/metaservice/service/DefaultDiscoveryService.java
浏览文件 @
5e0ab3cc
...
...
@@ -3,47 +3,45 @@ package com.ctrip.framework.apollo.metaservice.service;
import
com.ctrip.framework.apollo.common.condition.ConditionalOnMissingProfile
;
import
com.ctrip.framework.apollo.core.dto.ServiceDTO
;
import
com.ctrip.framework.apollo.tracer.Tracer
;
import
com.netflix.appinfo.InstanceInfo
;
import
com.netflix.discovery.EurekaClient
;
import
com.netflix.discovery.shared.Application
;
import
java.util.Collections
;
import
java.util.List
;
import
java.util.function.Function
;
import
java.util.stream.Collectors
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.discovery.DiscoveryClient
;
import
org.springframework.stereotype.Service
;
import
org.springframework.util.CollectionUtils
;
/**
* Default discovery service for Eureka
*/
@Service
@ConditionalOnMissingProfile
({
"kubernetes"
})
public
class
DefaultDiscoveryService
implements
DiscoveryService
{
private
final
DiscoveryClient
discovery
Client
;
private
final
EurekaClient
eureka
Client
;
public
DefaultDiscoveryService
(
final
DiscoveryClient
discovery
Client
)
{
this
.
discoveryClient
=
discovery
Client
;
public
DefaultDiscoveryService
(
final
EurekaClient
eureka
Client
)
{
this
.
eurekaClient
=
eureka
Client
;
}
@Override
public
List
<
ServiceDTO
>
getServiceInstances
(
String
serviceId
)
{
List
<
ServiceInstance
>
instances
=
discoveryClient
.
getInstances
(
serviceId
);
if
(
CollectionUtils
.
isEmpty
(
instances
))
{
Application
application
=
eurekaClient
.
getApplication
(
serviceId
);
if
(
application
==
null
||
CollectionUtils
.
isEmpty
(
application
.
getInstances
()
))
{
Tracer
.
logEvent
(
"Apollo.Discovery.NotFound"
,
serviceId
);
return
Collections
.
emptyList
();
}
return
instances
.
stream
().
map
(
instanceInfoToServiceDTOFunc
)
return
application
.
getInstances
()
.
stream
().
map
(
instanceInfoToServiceDTOFunc
)
.
collect
(
Collectors
.
toList
());
}
private
static
Function
<
ServiceInstance
,
ServiceDTO
>
instanceInfoToServiceDTOFunc
=
instance
->
{
private
static
final
Function
<
InstanceInfo
,
ServiceDTO
>
instanceInfoToServiceDTOFunc
=
instance
->
{
ServiceDTO
service
=
new
ServiceDTO
();
service
.
setAppName
(
instance
.
getServiceId
());
service
.
setInstanceId
(
String
.
format
(
"%s:%s:%s"
,
instance
.
getHost
(),
instance
.
getServiceId
(),
instance
.
getPort
()));
String
uri
=
instance
.
getUri
().
toString
();
if
(!
uri
.
endsWith
(
"/"
))
{
uri
+=
"/"
;
}
service
.
setHomepageUrl
(
uri
);
service
.
setAppName
(
instance
.
getAppName
());
service
.
setInstanceId
(
instance
.
getInstanceId
());
service
.
setHomepageUrl
(
instance
.
getHomePageUrl
());
return
service
;
};
}
apollo-configservice/src/test/java/com/ctrip/framework/apollo/metaservice/service/DefaultDiscoveryServiceTest.java
浏览文件 @
5e0ab3cc
...
...
@@ -6,7 +6,9 @@ import static org.mockito.Mockito.when;
import
com.ctrip.framework.apollo.core.dto.ServiceDTO
;
import
com.google.common.collect.Lists
;
import
java.net.URI
;
import
com.netflix.appinfo.InstanceInfo
;
import
com.netflix.discovery.EurekaClient
;
import
com.netflix.discovery.shared.Application
;
import
java.net.URISyntaxException
;
import
java.util.ArrayList
;
import
java.util.List
;
...
...
@@ -15,14 +17,15 @@ import org.junit.Test;
import
org.junit.runner.RunWith
;
import
org.mockito.Mock
;
import
org.mockito.junit.MockitoJUnitRunner
;
import
org.springframework.cloud.client.ServiceInstance
;
import
org.springframework.cloud.client.discovery.DiscoveryClient
;
@RunWith
(
MockitoJUnitRunner
.
class
)
public
class
DefaultDiscoveryServiceTest
{
@Mock
private
DiscoveryClient
discoveryClient
;
private
EurekaClient
eurekaClient
;
@Mock
private
Application
someApplication
;
private
DefaultDiscoveryService
defaultDiscoveryService
;
...
...
@@ -30,68 +33,60 @@ public class DefaultDiscoveryServiceTest {
@Before
public
void
setUp
()
throws
Exception
{
defaultDiscoveryService
=
new
DefaultDiscoveryService
(
discovery
Client
);
defaultDiscoveryService
=
new
DefaultDiscoveryService
(
eureka
Client
);
someServiceId
=
"someServiceId"
;
}
@Test
public
void
testGetServiceInstancesWithNullInstances
()
{
when
(
discoveryClient
.
getInstances
(
someServiceId
)).
thenReturn
(
null
);
when
(
eurekaClient
.
getApplication
(
someServiceId
)).
thenReturn
(
null
);
assertTrue
(
defaultDiscoveryService
.
getServiceInstances
(
someServiceId
).
isEmpty
());
}
@Test
public
void
testGetServiceInstancesWithEmptyInstances
()
{
when
(
discoveryClient
.
getInstances
(
someServiceId
)).
thenReturn
(
new
ArrayList
<>());
when
(
eurekaClient
.
getApplication
(
someServiceId
)).
thenReturn
(
someApplication
);
when
(
someApplication
.
getInstances
()).
thenReturn
(
new
ArrayList
<>());
assertTrue
(
defaultDiscoveryService
.
getServiceInstances
(
someServiceId
).
isEmpty
());
}
@Test
public
void
testGetServiceInstances
()
throws
URISyntaxException
{
String
someHost
=
"1.2.3.4"
;
int
somePort
=
8080
;
String
someUri
=
String
.
format
(
"http://%s:%s/some-path/"
,
someHost
,
somePort
);
ServiceInstance
someServiceInstance
=
mockServiceInstance
(
someServiceId
,
someHost
,
somePort
,
String
someUri
=
"http://1.2.3.4:8080/some-path/"
;
String
someInstanceId
=
"someInstanceId"
;
InstanceInfo
someServiceInstance
=
mockServiceInstance
(
someServiceId
,
someInstanceId
,
someUri
);
String
anotherHost
=
"2.3.4.5"
;
int
anotherPort
=
9090
;
String
anotherUri
=
String
.
format
(
"http://%s:%s/some-path-with-no-slash"
,
anotherHost
,
anotherPort
);
ServiceInstance
anotherServiceInstance
=
mockServiceInstance
(
someServiceId
,
anotherHost
,
anotherPort
,
String
anotherUri
=
"http://2.3.4.5:9090/anotherPath"
;
String
anotherInstanceId
=
"anotherInstanceId"
;
InstanceInfo
anotherServiceInstance
=
mockServiceInstance
(
someServiceId
,
anotherInstanceId
,
anotherUri
);
when
(
discoveryClient
.
getInstances
(
someServiceId
))
when
(
eurekaClient
.
getApplication
(
someServiceId
)).
thenReturn
(
someApplication
);
when
(
someApplication
.
getInstances
())
.
thenReturn
(
Lists
.
newArrayList
(
someServiceInstance
,
anotherServiceInstance
));
List
<
ServiceDTO
>
serviceDTOList
=
defaultDiscoveryService
.
getServiceInstances
(
someServiceId
);
assertEquals
(
2
,
serviceDTOList
.
size
());
check
(
someServiceInstance
,
serviceDTOList
.
get
(
0
)
,
false
);
check
(
anotherServiceInstance
,
serviceDTOList
.
get
(
1
)
,
true
);
check
(
someServiceInstance
,
serviceDTOList
.
get
(
0
));
check
(
anotherServiceInstance
,
serviceDTOList
.
get
(
1
));
}
private
void
check
(
ServiceInstance
serviceInstance
,
ServiceDTO
serviceDTO
,
boolean
appendSlashToUri
)
{
assertEquals
(
serviceInstance
.
getServiceId
(),
serviceDTO
.
getAppName
());
assertEquals
(
serviceDTO
.
getInstanceId
(),
String
.
format
(
"%s:%s:%s"
,
serviceInstance
.
getHost
(),
serviceInstance
.
getServiceId
(),
serviceInstance
.
getPort
()));
if
(
appendSlashToUri
)
{
assertEquals
(
serviceInstance
.
getUri
().
toString
()
+
"/"
,
serviceDTO
.
getHomepageUrl
());
}
else
{
assertEquals
(
serviceInstance
.
getUri
().
toString
(),
serviceDTO
.
getHomepageUrl
());
}
private
void
check
(
InstanceInfo
serviceInstance
,
ServiceDTO
serviceDTO
)
{
assertEquals
(
serviceInstance
.
getAppName
(),
serviceDTO
.
getAppName
());
assertEquals
(
serviceInstance
.
getInstanceId
(),
serviceDTO
.
getInstanceId
());
assertEquals
(
serviceInstance
.
getHomePageUrl
(),
serviceDTO
.
getHomepageUrl
());
}
private
ServiceInstance
mockServiceInstance
(
String
serviceId
,
String
host
,
int
port
,
String
uri
)
throws
URISyntaxException
{
ServiceInstance
serviceInstance
=
mock
(
ServiceInstance
.
class
);
when
(
serviceInstance
.
getServiceId
()).
thenReturn
(
serviceId
);
when
(
serviceInstance
.
getHost
()).
thenReturn
(
host
);
when
(
serviceInstance
.
getPort
()).
thenReturn
(
port
);
when
(
serviceInstance
.
getUri
()).
thenReturn
(
new
URI
(
uri
));
private
InstanceInfo
mockServiceInstance
(
String
serviceId
,
String
instanceId
,
String
homePageUrl
)
{
InstanceInfo
serviceInstance
=
mock
(
InstanceInfo
.
class
);
when
(
serviceInstance
.
getAppName
()).
thenReturn
(
serviceId
);
when
(
serviceInstance
.
getInstanceId
()).
thenReturn
(
instanceId
);
when
(
serviceInstance
.
getHomePageUrl
()).
thenReturn
(
homePageUrl
);
return
serviceInstance
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录