??metaService這個(gè)服務(wù)是用來提供“元服務(wù)”的,我們可以看到源碼中只有一個(gè)DiscoveryService和一個(gè)ServiceController,DiscoveryService封裝了三個(gè)方法,通過EurekaClient分別獲取
Admin Service服務(wù)列表(IP+Port)
Config Service服務(wù)列表(IP+Port)
meta Service服務(wù)列表(IP+Port)
我們來看一下其中一個(gè)方法:
public List<InstanceInfo> getMetaServiceInstances() {
Application application = eurekaClient.getApplication(ServiceNameConsts.APOLLO_METASERVICE);
if (application == null) {
Tracer.logEvent("Apollo.EurekaDiscovery.NotFound", ServiceNameConsts.APOLLO_METASERVICE);
}
return application != null ? application.getInstances() : Collections.emptyList();
}
返回的是應(yīng)用實(shí)例信息。而ServiceController則是進(jìn)一步封裝,提供3個(gè)接口獲取服務(wù)信息。接口信息如下:
http://localhost:8080/services/meta
返回值:[]
http://localhost:8080/services/admin
返回值:
[
{
"appName": "APOLLO-ADMINSERVICE",
"instanceId": "HQ-PAB41602.sdb.local:apollo-adminservice:8090",
"homepageUrl": "http://172.16.3.147:8090/"
}
]
這個(gè)接口是獲取adminService服務(wù)的接口
http://localhost:8080/services/config
返回值:
[
{
"appName": "APOLLO-CONFIGSERVICE",
"instanceId": "HQ-PAB41602.sdb.local:apollo-configservice:8080",
"homepageUrl": "http://172.16.3.147:8080/"
}
]
同樣的我們看一下其中一個(gè)方法:
@RequestMapping("/meta")
public List<ServiceDTO> getMetaService() {
List<InstanceInfo> instances = discoveryService.getMetaServiceInstances();
List<ServiceDTO> result = instances.stream().map(new Function<InstanceInfo, ServiceDTO>() {
@Override
public ServiceDTO apply(InstanceInfo instance) {
ServiceDTO service = new ServiceDTO();
service.setAppName(instance.getAppName());
service.setInstanceId(instance.getInstanceId());
service.setHomepageUrl(instance.getHomePageUrl());
return service;
}
}).collect(Collectors.toList());
return result;
}
僅僅是提供了一個(gè)簡(jiǎn)單的封裝而已。
??Meta Server從Eureka獲取metaService,Config Service和Admin Service的服務(wù)信息,相當(dāng)于是一個(gè)Eureka Client。
??增設(shè)一個(gè)Meta Server的角色主要是為了封裝服務(wù)發(fā)現(xiàn)的細(xì)節(jié),對(duì)Portal和Client而言,永遠(yuǎn)通過一個(gè)Http接口獲取Admin Service和Config Service的服務(wù)信息,而不需要關(guān)心背后實(shí)際的服務(wù)注冊(cè)和發(fā)現(xiàn)組件。
??Meta Server只是一個(gè)邏輯角色,在部署時(shí)和Config Service是在一個(gè)JVM進(jìn)程中的,所以IP、端口和Config Service一致。
??由于和Config Service部署在一個(gè)JVM中,所以相應(yīng)的metaService也是都是多實(shí)例、無狀態(tài)部署,保證了服務(wù)的高可用性。