feat:add swagger report switch. (#1147)

pull/1157/head
Haotian Zhang 1 year ago committed by GitHub
parent 2dac3957d0
commit 79e4a04ecb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -8,3 +8,4 @@
- [feat:support service contract reporting.](https://github.com/Tencent/spring-cloud-tencent/pull/1135) - [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: 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 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)

@ -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,25 +54,26 @@ 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) {
if (polarisContractProperties.isReportEnabled()) {
try { try {
Documentation documentation = documentationCache.documentationByGroup(groupName); Documentation documentation = documentationCache.documentationByGroup(polarisContractProperties.getGroup());
Swagger swagger = swagger2Mapper.mapDocumentation(documentation); Swagger swagger = swagger2Mapper.mapDocumentation(documentation);
if (swagger != null) { if (swagger != null) {
ReportServiceContractRequest request = new ReportServiceContractRequest(); ReportServiceContractRequest request = new ReportServiceContractRequest();
@ -93,13 +95,14 @@ public class PolarisContractReporter implements ApplicationListener<ApplicationR
} }
else { else {
LOG.warn("Swagger or json is null, documentationCache keys:{}, group:{}", documentationCache.all() LOG.warn("Swagger or json is null, documentationCache keys:{}, group:{}", documentationCache.all()
.keySet(), groupName); .keySet(), polarisContractProperties.getGroup());
} }
} }
catch (Throwable t) { catch (Throwable t) {
LOG.error("Report contract failed.", t); LOG.error("Report contract failed.", t);
} }
} }
}
private List<InterfaceDescriptor> getInterfaceDescriptorFromSwagger(Swagger swagger) { private List<InterfaceDescriptor> getInterfaceDescriptorFromSwagger(Swagger swagger) {
List<InterfaceDescriptor> interfaceDescriptorList = new ArrayList<>(); List<InterfaceDescriptor> interfaceDescriptorList = new ArrayList<>();

@ -47,4 +47,8 @@ public interface ContractProperties {
boolean isExposure(); boolean isExposure();
void setExposure(boolean exposure); void setExposure(boolean exposure);
boolean isReportEnabled();
void setReportEnabled(boolean reportEnabled);
} }

@ -21,6 +21,7 @@ import java.util.Objects;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.context.properties.ConfigurationProperties;
/** /**
@ -53,6 +54,9 @@ public class PolarisContractProperties implements ContractProperties {
private boolean exposure = true; private boolean exposure = true;
@Value("${spring.cloud.polaris.contract.report.enabled:true}")
private boolean reportEnabled = true;
public PolarisContractProperties(@Nullable ExtendedContractProperties extendContractProperties) { public PolarisContractProperties(@Nullable ExtendedContractProperties extendContractProperties) {
this.extendContractProperties = extendContractProperties; this.extendContractProperties = extendContractProperties;
} }
@ -134,4 +138,14 @@ public class PolarisContractProperties implements ContractProperties {
public void setExposure(boolean exposure) { public void setExposure(boolean exposure) {
this.exposure = exposure; this.exposure = exposure;
} }
@Override
public boolean isReportEnabled() {
return reportEnabled;
}
@Override
public void setReportEnabled(boolean reportEnabled) {
this.reportEnabled = reportEnabled;
}
} }

@ -121,7 +121,7 @@ public class PolarisSwaggerAutoConfiguration {
public PolarisContractReporter polarisContractReporter(DocumentationCache documentationCache, public PolarisContractReporter polarisContractReporter(DocumentationCache documentationCache,
ServiceModelToSwagger2Mapper swagger2Mapper, PolarisContractProperties polarisContractProperties, ServiceModelToSwagger2Mapper swagger2Mapper, PolarisContractProperties polarisContractProperties,
PolarisSDKContextManager polarisSDKContextManager, PolarisDiscoveryProperties polarisDiscoveryProperties) { PolarisSDKContextManager polarisSDKContextManager, PolarisDiscoveryProperties polarisDiscoveryProperties) {
return new PolarisContractReporter(documentationCache, swagger2Mapper, polarisContractProperties.getGroup(), return new PolarisContractReporter(documentationCache, swagger2Mapper, polarisContractProperties,
polarisSDKContextManager.getProviderAPI(), polarisDiscoveryProperties); polarisSDKContextManager.getProviderAPI(), polarisDiscoveryProperties);
} }

@ -35,6 +35,12 @@
"type": "java.lang.Boolean", "type": "java.lang.Boolean",
"defaultValue": "true", "defaultValue": "true",
"description": "Enable polaris contract exposure or not." "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."
} }
] ]
} }

@ -17,6 +17,8 @@ spring:
health-check-url: /discovery/service/caller/healthCheck health-check-url: /discovery/service/caller/healthCheck
contract: contract:
exposure: true exposure: true
report:
enabled: true
stat: stat:
enabled: true enabled: true
port: 28081 port: 28081

Loading…
Cancel
Save