@ -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 ;
@ -55,7 +56,7 @@ public class PolarisContractReporter implements ApplicationListener<ApplicationR
private final org . springdoc . webmvc . api . MultipleOpenApiResource multipleOpenApiWebMvcResource ;
private final org . springdoc . webmvc . api . MultipleOpenApiResource multipleOpenApiWebMvcResource ;
private final org . springdoc . webflux . api . MultipleOpenApiResource multipleOpenApiWebFluxResource ;
private final org . springdoc . webflux . api . MultipleOpenApiResource multipleOpenApiWebFluxResource ;
private final String groupName ;
private final PolarisContractProperties polarisContractProperties ;
private final ProviderAPI providerAPI ;
private final ProviderAPI providerAPI ;
@ -63,54 +64,56 @@ public class PolarisContractReporter implements ApplicationListener<ApplicationR
public PolarisContractReporter ( org . springdoc . webmvc . api . MultipleOpenApiResource multipleOpenApiWebMvcResource ,
public PolarisContractReporter ( org . springdoc . webmvc . api . MultipleOpenApiResource multipleOpenApiWebMvcResource ,
org . springdoc . webflux . api . MultipleOpenApiResource multipleOpenApiWebFluxResource ,
org . springdoc . webflux . api . MultipleOpenApiResource multipleOpenApiWebFluxResource ,
String groupName , ProviderAPI providerAPI ,
PolarisContractProperties polarisContractProperties , ProviderAPI providerAPI ,
PolarisDiscoveryProperties polarisDiscoveryProperties ) {
PolarisDiscoveryProperties polarisDiscoveryProperties ) {
this . multipleOpenApiWebMvcResource = multipleOpenApiWebMvcResource ;
this . multipleOpenApiWebMvcResource = multipleOpenApiWebMvcResource ;
this . multipleOpenApiWebFluxResource = multipleOpenApiWebFluxResource ;
this . multipleOpenApiWebFluxResource = multipleOpenApiWebFluxResource ;
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 ( ) ) {
AbstractOpenApiResource openApiResource = null ;
try {
if ( multipleOpenApiWebMvcResource ! = null ) {
AbstractOpenApiResource openApiResource = null ;
openApiResource = OpenApiWebMvcUtil . getOpenApiResourceOrThrow ( multipleOpenApiWebMvcResource , groupName ) ;
if ( multipleOpenApiWebMvcResource ! = null ) {
}
openApiResource = OpenApiWebMvcUtil . getOpenApiResourceOrThrow ( multipleOpenApiWebMvcResource , polarisContractProperties . getGroup ( ) ) ;
else if ( multipleOpenApiWebFluxResource ! = null ) {
}
openApiResource = OpenApiWebFluxUtil . getOpenApiResourceOrThrow ( multipleOpenApiWebFluxResource , groupName ) ;
else if ( multipleOpenApiWebFluxResource ! = null ) {
}
openApiResource = OpenApiWebFluxUtil . getOpenApiResourceOrThrow ( multipleOpenApiWebFluxResource , polarisContractProperties . getGroup ( ) ) ;
OpenAPI openAPI = null ;
}
if ( openApiResource ! = null ) {
OpenAPI openAPI = null ;
openAPI = AbstractOpenApiResourceUtil . getOpenApi ( openApiResource ) ;
if ( openApiResource ! = null ) {
}
openAPI = AbstractOpenApiResourceUtil . getOpenApi ( openApiResource ) ;
if ( openAPI ! = null ) {
}
ReportServiceContractRequest request = new ReportServiceContractRequest ( ) ;
if ( openAPI ! = 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 ( openAPI ) ;
request . setVersion ( polarisDiscoveryProperties . getVersion ( ) ) ;
request . setInterfaceDescriptors ( interfaceDescriptorList ) ;
List < InterfaceDescriptor > interfaceDescriptorList = getInterfaceDescriptorFromSwagger ( openAPI ) ;
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 ( openAPI ) ;
if ( LOG . isDebugEnabled ( ) ) {
LOG . debug ( "OpenApi json data: {}" , jsonValue ) ;
String jsonValue = JacksonUtils . serialize2Json ( openAPI ) ;
LOG . debug ( "OpenApi json data: {}" , jsonValue ) ;
}
}
else {
LOG . warn ( "OpenAPI or json is null, group:{}" , polarisContractProperties . getGroup ( ) ) ;
}
}
}
}
else {
catch ( Throwable t ) {
LOG . warn ( "OpenAPI or json is null, group:{}" , groupName ) ;
LOG . error( "Report contract failed." , t ) ;
}
}
}
}
catch ( Throwable t ) {
LOG . error ( "Report contract failed." , t ) ;
}
}
}
private List < InterfaceDescriptor > getInterfaceDescriptorFromSwagger ( OpenAPI openAPI ) {
private List < InterfaceDescriptor > getInterfaceDescriptorFromSwagger ( OpenAPI openAPI ) {