Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add swagger report switch. #1148

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -10,3 +10,4 @@
- [feat: add circuit breaker actuator.](https://github.com/Tencent/spring-cloud-tencent/pull/1136)
- [feat:support service contract reporting.](https://github.com/Tencent/spring-cloud-tencent/pull/1139)
- [feat:add swagger exposure filters.](https://github.com/Tencent/spring-cloud-tencent/pull/1146)
- [feat:add swagger report switch.](https://github.com/Tencent/spring-cloud-tencent/pull/1148)
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@

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<ApplicationR
private final Logger LOG = LoggerFactory.getLogger(PolarisContractReporter.class);
private final ServiceModelToSwagger2Mapper swagger2Mapper;
private final DocumentationCache documentationCache;
private final String groupName;
private final PolarisContractProperties polarisContractProperties;

private final ProviderAPI providerAPI;

private final PolarisDiscoveryProperties polarisDiscoveryProperties;

public PolarisContractReporter(DocumentationCache documentationCache, ServiceModelToSwagger2Mapper swagger2Mapper,
String groupName, ProviderAPI providerAPI, PolarisDiscoveryProperties polarisDiscoveryProperties) {
PolarisContractProperties polarisContractProperties, ProviderAPI providerAPI, PolarisDiscoveryProperties polarisDiscoveryProperties) {
this.swagger2Mapper = swagger2Mapper;
this.documentationCache = documentationCache;
this.groupName = groupName;
this.polarisContractProperties = polarisContractProperties;
this.providerAPI = providerAPI;
this.polarisDiscoveryProperties = polarisDiscoveryProperties;
}

@Override
public void onApplicationEvent(@NonNull ApplicationReadyEvent applicationReadyEvent) {
try {
Documentation documentation = documentationCache.documentationByGroup(groupName);
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<InterfaceDescriptor> 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<InterfaceDescriptor> 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<InterfaceDescriptor> getInterfaceDescriptorFromSwagger(Swagger swagger) {
Original file line number Diff line number Diff line change
@@ -47,4 +47,8 @@ public interface ContractProperties {
boolean isExposure();

void setExposure(boolean exposure);

boolean isReportEnabled();

void setReportEnabled(boolean reportEnabled);
}
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@

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 boolean isExposure() {
public void setExposure(boolean exposure) {
this.exposure = exposure;
}

@Override
public boolean isReportEnabled() {
return reportEnabled;
}

@Override
public void setReportEnabled(boolean reportEnabled) {
this.reportEnabled = reportEnabled;
}
}
Original file line number Diff line number Diff line change
@@ -128,7 +128,7 @@ public Docket polarisDocket(PolarisContractProperties polarisContractProperties)
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);
}

Original file line number Diff line number Diff line change
@@ -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."
}
]
}
Original file line number Diff line number Diff line change
@@ -24,6 +24,8 @@ spring:
health-check-url: /discovery/service/caller/healthCheck
contract:
exposure: true
report:
enabled: true
stat:
enabled: true
port: 28081