|
|
@ -24,6 +24,7 @@ import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import com.tencent.cloud.common.util.JacksonUtils;
|
|
|
|
import com.tencent.cloud.common.util.JacksonUtils;
|
|
|
|
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
|
|
|
|
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
|
|
|
|
|
|
|
|
import com.tencent.cloud.polaris.contract.config.PolarisContractProperties;
|
|
|
|
import com.tencent.polaris.api.core.ProviderAPI;
|
|
|
|
import com.tencent.polaris.api.core.ProviderAPI;
|
|
|
|
import com.tencent.polaris.api.plugin.server.InterfaceDescriptor;
|
|
|
|
import com.tencent.polaris.api.plugin.server.InterfaceDescriptor;
|
|
|
|
import com.tencent.polaris.api.plugin.server.ReportServiceContractRequest;
|
|
|
|
import com.tencent.polaris.api.plugin.server.ReportServiceContractRequest;
|
|
|
@ -53,52 +54,54 @@ public class PolarisContractReporter implements ApplicationListener<ApplicationR
|
|
|
|
private final Logger LOG = LoggerFactory.getLogger(PolarisContractReporter.class);
|
|
|
|
private final Logger LOG = LoggerFactory.getLogger(PolarisContractReporter.class);
|
|
|
|
private final ServiceModelToSwagger2Mapper swagger2Mapper;
|
|
|
|
private final ServiceModelToSwagger2Mapper swagger2Mapper;
|
|
|
|
private final DocumentationCache documentationCache;
|
|
|
|
private final DocumentationCache documentationCache;
|
|
|
|
private final String groupName;
|
|
|
|
private final PolarisContractProperties polarisContractProperties;
|
|
|
|
|
|
|
|
|
|
|
|
private final ProviderAPI providerAPI;
|
|
|
|
private final ProviderAPI providerAPI;
|
|
|
|
|
|
|
|
|
|
|
|
private final PolarisDiscoveryProperties polarisDiscoveryProperties;
|
|
|
|
private final PolarisDiscoveryProperties polarisDiscoveryProperties;
|
|
|
|
|
|
|
|
|
|
|
|
public PolarisContractReporter(DocumentationCache documentationCache, ServiceModelToSwagger2Mapper swagger2Mapper,
|
|
|
|
public PolarisContractReporter(DocumentationCache documentationCache, ServiceModelToSwagger2Mapper swagger2Mapper,
|
|
|
|
String groupName, ProviderAPI providerAPI, PolarisDiscoveryProperties polarisDiscoveryProperties) {
|
|
|
|
PolarisContractProperties polarisContractProperties, ProviderAPI providerAPI, PolarisDiscoveryProperties polarisDiscoveryProperties) {
|
|
|
|
this.swagger2Mapper = swagger2Mapper;
|
|
|
|
this.swagger2Mapper = swagger2Mapper;
|
|
|
|
this.documentationCache = documentationCache;
|
|
|
|
this.documentationCache = documentationCache;
|
|
|
|
this.groupName = groupName;
|
|
|
|
this.polarisContractProperties = polarisContractProperties;
|
|
|
|
this.providerAPI = providerAPI;
|
|
|
|
this.providerAPI = providerAPI;
|
|
|
|
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
|
|
|
|
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Override
|
|
|
|
public void onApplicationEvent(@NonNull ApplicationReadyEvent applicationReadyEvent) {
|
|
|
|
public void onApplicationEvent(@NonNull ApplicationReadyEvent applicationReadyEvent) {
|
|
|
|
try {
|
|
|
|
if (polarisContractProperties.isReportEnabled()) {
|
|
|
|
Documentation documentation = documentationCache.documentationByGroup(groupName);
|
|
|
|
try {
|
|
|
|
Swagger swagger = swagger2Mapper.mapDocumentation(documentation);
|
|
|
|
Documentation documentation = documentationCache.documentationByGroup(polarisContractProperties.getGroup());
|
|
|
|
if (swagger != null) {
|
|
|
|
Swagger swagger = swagger2Mapper.mapDocumentation(documentation);
|
|
|
|
ReportServiceContractRequest request = new ReportServiceContractRequest();
|
|
|
|
if (swagger != null) {
|
|
|
|
request.setName(polarisDiscoveryProperties.getService());
|
|
|
|
ReportServiceContractRequest request = new ReportServiceContractRequest();
|
|
|
|
request.setNamespace(polarisDiscoveryProperties.getNamespace());
|
|
|
|
request.setName(polarisDiscoveryProperties.getService());
|
|
|
|
request.setService(polarisDiscoveryProperties.getService());
|
|
|
|
request.setNamespace(polarisDiscoveryProperties.getNamespace());
|
|
|
|
request.setProtocol("http");
|
|
|
|
request.setService(polarisDiscoveryProperties.getService());
|
|
|
|
request.setVersion(polarisDiscoveryProperties.getVersion());
|
|
|
|
request.setProtocol("http");
|
|
|
|
List<InterfaceDescriptor> interfaceDescriptorList = getInterfaceDescriptorFromSwagger(swagger);
|
|
|
|
request.setVersion(polarisDiscoveryProperties.getVersion());
|
|
|
|
request.setInterfaceDescriptors(interfaceDescriptorList);
|
|
|
|
List<InterfaceDescriptor> interfaceDescriptorList = getInterfaceDescriptorFromSwagger(swagger);
|
|
|
|
ReportServiceContractResponse response = providerAPI.reportServiceContract(request);
|
|
|
|
request.setInterfaceDescriptors(interfaceDescriptorList);
|
|
|
|
LOG.info("Service contract [Namespace: {}. Name: {}. Service: {}. Protocol:{}. Version: {}. API counter: {}] is reported.",
|
|
|
|
ReportServiceContractResponse response = providerAPI.reportServiceContract(request);
|
|
|
|
request.getNamespace(), request.getName(), request.getService(), request.getProtocol(),
|
|
|
|
LOG.info("Service contract [Namespace: {}. Name: {}. Service: {}. Protocol:{}. Version: {}. API counter: {}] is reported.",
|
|
|
|
request.getVersion(), request.getInterfaceDescriptors().size());
|
|
|
|
request.getNamespace(), request.getName(), request.getService(), request.getProtocol(),
|
|
|
|
if (LOG.isDebugEnabled()) {
|
|
|
|
request.getVersion(), request.getInterfaceDescriptors().size());
|
|
|
|
String jsonValue = JacksonUtils.serialize2Json(swagger);
|
|
|
|
if (LOG.isDebugEnabled()) {
|
|
|
|
LOG.debug("OpenApi json data: {}", jsonValue);
|
|
|
|
String jsonValue = JacksonUtils.serialize2Json(swagger);
|
|
|
|
|
|
|
|
LOG.debug("OpenApi json data: {}", jsonValue);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
LOG.warn("Swagger or json is null, documentationCache keys:{}, group:{}", documentationCache.all()
|
|
|
|
|
|
|
|
.keySet(), polarisContractProperties.getGroup());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
catch (Throwable t) {
|
|
|
|
LOG.warn("Swagger or json is null, documentationCache keys:{}, group:{}", documentationCache.all()
|
|
|
|
LOG.error("Report contract failed.", t);
|
|
|
|
.keySet(), groupName);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Throwable t) {
|
|
|
|
|
|
|
|
LOG.error("Report contract failed.", t);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private List<InterfaceDescriptor> getInterfaceDescriptorFromSwagger(Swagger swagger) {
|
|
|
|
private List<InterfaceDescriptor> getInterfaceDescriptorFromSwagger(Swagger swagger) {
|
|
|
|