ServiceMetadata是一个很重要的概念,其存储着Provider和Consumer相关的信息
1.ServiceMetadataBeforeInit接口
在ServiceMetadata的init方法开始时调用,意图很明显,即在init之前再对ServiceMetadata做处理
public interface ServiceMetadataBeforeInit {
void beforeInit(ServiceMetadata var1);
}
有4个实现,分别如下
- EdasInvokeTypeServiceMetadataBeforeInit
根据invokeType添加Registry
public void beforeInit(ServiceMetadata serviceMetadata) {
String invokeType = (String)serviceMetadata.getAttachment("service.invoke.type");
if (StringUtils.isEmpty(invokeType)) {
invokeType = System.getProperty("service.invoke.type", "HSF");
}
if (invokeType.equalsIgnoreCase("DUBBO")) {
List<Registry> registries = new ArrayList();
DubboRegistry dubboRegistry = new DubboRegistry();
registries.add(dubboRegistry);
serviceMetadata.setRegistries(registries);
} else if (invokeType.equalsIgnoreCase("HSF,DUBBO") || invokeType.equalsIgnoreCase("DUBBO,HSF")) {
serviceMetadata.putAttachment("addExtraDubboRegistry", true);
}
}
- DefaultServiceMetadataBeforeInit
添加默认的Registry和Protocol
public void beforeInit(ServiceMetadata serviceMetadata) {
List<Registry> registries = serviceMetadata.getRegistries();
if (registries == null) {
registries = new ArrayList();
}
if (((List)registries).isEmpty()) {
Registry registry = (Registry)HSFServiceContainer.getInstance(Registry.class, this.config.getString("hsf.registry.default"));
if (registry != null) {
((List)registries).add(registry);
}
}
serviceMetadata.setRegistries((List)registries);
List<Protocol> protocols = serviceMetadata.getProtocols();
if (protocols == null) {
protocols = new ArrayList();
}
if (((List)protocols).isEmpty()) {
((List)protocols).addAll(HSFServiceContainer.getInstances(Protocol.class, new String[0]));
}
serviceMetadata.setProtocols((List)protocols);
}
- EdasServiceMetadataBeforeInit
对参数做检查补充
- ExtraDubboRegistryServiceMetadataBeforeInit
根据EdasInvokeTypeServiceMetadataBeforeInit添加的参数补充添加DubboRegistry
public void beforeInit(ServiceMetadata serviceMetadata) {
Boolean flag = (Boolean)serviceMetadata.getAttachment("addExtraDubboRegistry");
if (flag != null && flag) {
boolean exist = false;
Iterator i$ = serviceMetadata.getRegistries().iterator();
while(i$.hasNext()) {
Registry registry = (Registry)i$.next();
if (registry instanceof DubboRegistry) {
exist = true;
break;
}
}
if (!exist) {
serviceMetadata.getRegistries().add(new DubboRegistry());
}
}
}
网友评论