diff --git a/CHANGELOG.md b/CHANGELOG.md index 370c49f0d..b4665eae3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,3 +8,4 @@ - [feat:support service contract reporting.](https://github.com/Tencent/spring-cloud-tencent/pull/1135) - [feat: support log path configuration parameters.](https://github.com/Tencent/spring-cloud-tencent/pull/1143) - [feat:add swagger exposure filters.](https://github.com/Tencent/spring-cloud-tencent/pull/1144) +- [feat:add swagger report switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1147) diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java index 2321c0c90..1470136e8 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/PolarisContractReporter.java @@ -24,6 +24,7 @@ import java.util.Map; import com.tencent.cloud.common.util.JacksonUtils; 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.plugin.server.InterfaceDescriptor; import com.tencent.polaris.api.plugin.server.ReportServiceContractRequest; @@ -53,52 +54,54 @@ public class PolarisContractReporter implements ApplicationListener interfaceDescriptorList = getInterfaceDescriptorFromSwagger(swagger); - request.setInterfaceDescriptors(interfaceDescriptorList); - ReportServiceContractResponse response = providerAPI.reportServiceContract(request); - LOG.info("Service contract [Namespace: {}. Name: {}. Service: {}. Protocol:{}. Version: {}. API counter: {}] is reported.", - request.getNamespace(), request.getName(), request.getService(), request.getProtocol(), - request.getVersion(), request.getInterfaceDescriptors().size()); - if (LOG.isDebugEnabled()) { - String jsonValue = JacksonUtils.serialize2Json(swagger); - LOG.debug("OpenApi json data: {}", jsonValue); + if (polarisContractProperties.isReportEnabled()) { + try { + Documentation documentation = documentationCache.documentationByGroup(polarisContractProperties.getGroup()); + Swagger swagger = swagger2Mapper.mapDocumentation(documentation); + if (swagger != null) { + ReportServiceContractRequest request = new ReportServiceContractRequest(); + request.setName(polarisDiscoveryProperties.getService()); + request.setNamespace(polarisDiscoveryProperties.getNamespace()); + request.setService(polarisDiscoveryProperties.getService()); + request.setProtocol("http"); + request.setVersion(polarisDiscoveryProperties.getVersion()); + List interfaceDescriptorList = getInterfaceDescriptorFromSwagger(swagger); + request.setInterfaceDescriptors(interfaceDescriptorList); + ReportServiceContractResponse response = providerAPI.reportServiceContract(request); + LOG.info("Service contract [Namespace: {}. Name: {}. Service: {}. Protocol:{}. Version: {}. API counter: {}] is reported.", + request.getNamespace(), request.getName(), request.getService(), request.getProtocol(), + request.getVersion(), request.getInterfaceDescriptors().size()); + if (LOG.isDebugEnabled()) { + 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 { - LOG.warn("Swagger or json is null, documentationCache keys:{}, group:{}", documentationCache.all() - .keySet(), groupName); + catch (Throwable t) { + LOG.error("Report contract failed.", t); } } - catch (Throwable t) { - LOG.error("Report contract failed.", t); - } } private List getInterfaceDescriptorFromSwagger(Swagger swagger) { diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/ContractProperties.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/ContractProperties.java index 3e915f5d4..7a34c8360 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/ContractProperties.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/ContractProperties.java @@ -47,4 +47,8 @@ public interface ContractProperties { boolean isExposure(); void setExposure(boolean exposure); + + boolean isReportEnabled(); + + void setReportEnabled(boolean reportEnabled); } diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractProperties.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractProperties.java index 68681ae36..2b43226a5 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractProperties.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisContractProperties.java @@ -21,6 +21,7 @@ import java.util.Objects; import javax.annotation.Nullable; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; /** @@ -53,6 +54,9 @@ public class PolarisContractProperties implements ContractProperties { private boolean exposure = true; + @Value("${spring.cloud.polaris.contract.report.enabled:true}") + private boolean reportEnabled = true; + public PolarisContractProperties(@Nullable ExtendedContractProperties extendContractProperties) { this.extendContractProperties = extendContractProperties; } @@ -134,4 +138,14 @@ public class PolarisContractProperties implements ContractProperties { public void setExposure(boolean exposure) { this.exposure = exposure; } + + @Override + public boolean isReportEnabled() { + return reportEnabled; + } + + @Override + public void setReportEnabled(boolean reportEnabled) { + this.reportEnabled = reportEnabled; + } } diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisSwaggerAutoConfiguration.java b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisSwaggerAutoConfiguration.java index 03f69b24f..e0ace300c 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisSwaggerAutoConfiguration.java +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/java/com/tencent/cloud/polaris/contract/config/PolarisSwaggerAutoConfiguration.java @@ -121,7 +121,7 @@ public class PolarisSwaggerAutoConfiguration { public PolarisContractReporter polarisContractReporter(DocumentationCache documentationCache, ServiceModelToSwagger2Mapper swagger2Mapper, PolarisContractProperties polarisContractProperties, PolarisSDKContextManager polarisSDKContextManager, PolarisDiscoveryProperties polarisDiscoveryProperties) { - return new PolarisContractReporter(documentationCache, swagger2Mapper, polarisContractProperties.getGroup(), + return new PolarisContractReporter(documentationCache, swagger2Mapper, polarisContractProperties, polarisSDKContextManager.getProviderAPI(), polarisDiscoveryProperties); } diff --git a/spring-cloud-starter-tencent-polaris-contract/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-starter-tencent-polaris-contract/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 4e5e4b72d..0ef878076 100644 --- a/spring-cloud-starter-tencent-polaris-contract/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-starter-tencent-polaris-contract/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -35,6 +35,12 @@ "type": "java.lang.Boolean", "defaultValue": "true", "description": "Enable polaris contract exposure or not." + }, + { + "name": "spring.cloud.polaris.contract.report.enabled", + "type": "java.lang.Boolean", + "defaultValue": "true", + "description": "Enable polaris contract report or not." } ] } diff --git a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml index 62e92a007..19efa9a8f 100644 --- a/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml +++ b/spring-cloud-tencent-examples/polaris-discovery-example/discovery-caller-service/src/main/resources/bootstrap.yml @@ -17,6 +17,8 @@ spring: health-check-url: /discovery/service/caller/healthCheck contract: exposure: true + report: + enabled: true stat: enabled: true port: 28081