feat:support polaris event. (#1494)

pull/1495/head
Haotian Zhang 7 months ago committed by GitHub
parent a793fd9469
commit 5da0916dfa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -2,3 +2,4 @@
---
- [feat: implement circuit breaker in enhance plugin, support listen config group, support refresh single config in refresh_context mode.](https://github.com/Tencent/spring-cloud-tencent/pull/1490)
- [feat:support polaris event.](https://github.com/Tencent/spring-cloud-tencent/pull/1494)

@ -90,7 +90,7 @@
<properties>
<!-- Project revision -->
<revision>2.0.1.0-2021.0.9-RC1</revision>
<revision>2.0.1.0-2021.0.9-SNAPSHOT</revision>
<!-- Spring Framework -->
<spring.framework.version>5.3.39</spring.framework.version>

@ -17,8 +17,15 @@
package com.tencent.cloud.polaris.registry;
import java.time.LocalDateTime;
import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.polaris.api.plugin.compose.Extensions;
import com.tencent.polaris.api.plugin.event.FlowEvent;
import com.tencent.polaris.api.plugin.event.FlowEventConstants;
import com.tencent.polaris.api.pojo.ServiceEventKey;
import com.tencent.polaris.assembly.api.AssemblyAPI;
import com.tencent.polaris.client.flow.BaseFlow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -84,6 +91,25 @@ public class PolarisAutoServiceRegistration extends AbstractAutoServiceRegistrat
@Override
protected void deregister() {
// report thread end event.
if (this.registration.getPolarisContext() != null
&& this.registration.getPolarisContext().getExtensions() != null) {
Extensions extensions = this.registration.getPolarisContext().getExtensions();
FlowEvent.Builder flowEventBuilder = new FlowEvent.Builder()
.withEventType(ServiceEventKey.EventType.INSTANCE)
.withEventName(FlowEventConstants.EventName.InstanceThreadEnd)
.withTimestamp(LocalDateTime.now())
.withClientId(extensions.getValueContext().getClientId())
.withClientIp(extensions.getValueContext().getHost())
.withNamespace(polarisDiscoveryProperties.getNamespace())
.withService(registration.getServiceId())
.withHost(registration.getHost())
.withPort(registration.getPort());
FlowEvent flowEvent = flowEventBuilder.build();
BaseFlow.reportFlowEvent(extensions, flowEvent);
}
if (!this.registration.isRegisterEnabled()) {
return;
}

@ -56,13 +56,12 @@ public class PolarisRegistration implements Registration {
private final SDKContext polarisContext;
private final StaticMetadataManager staticMetadataManager;
private String serviceId;
private final String host;
private final boolean isSecure;
private final ServletWebServerApplicationContext servletWebServerApplicationContext;
private final ReactiveWebServerApplicationContext reactiveWebServerApplicationContext;
private final List<PolarisRegistrationCustomizer> customizers;
private String serviceId;
private boolean registerEnabled = false;
private Map<String, String> metadata;
private Map<String, Map<String, String>> extendedMetadata;
@ -164,15 +163,15 @@ public class PolarisRegistration implements Registration {
}
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
@Override
public String getServiceId() {
return serviceId;
}
public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}
@Override
public String getHost() {
return host;
@ -238,6 +237,10 @@ public class PolarisRegistration implements Registration {
return registerEnabled;
}
public SDKContext getPolarisContext() {
return polarisContext;
}
@Override
public String toString() {
return "PolarisRegistration{" +

@ -206,5 +206,10 @@ public class OrderConstant {
* Order of trace configuration modifier.
*/
public static Integer TRACE_ORDER = 2;
/**
* Order of push gateway event reporter modifier.
*/
public static Integer PUSH_GATEWAY_EVENT_ORDER = 2;
}
}

@ -71,10 +71,10 @@
<properties>
<!-- Project revision -->
<revision>2.0.1.0-2021.0.9-RC1</revision>
<revision>2.0.1.0-2021.0.9-SNAPSHOT</revision>
<!-- Polaris SDK version -->
<polaris.version>2.0.0.1</polaris.version>
<polaris.version>2.0.1.0-SNAPSHOT</polaris.version>
<!-- Dependencies -->
<bcpkix-jdk18on.version>1.78.1</bcpkix-jdk18on.version>

@ -23,7 +23,6 @@ import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.circuitbreaker.CircuitBreakerFactory;
import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@ -56,9 +55,6 @@ public class CircuitBreakerController {
@Autowired
private CircuitBreakerFactory circuitBreakerFactory;
@Autowired
private ReactiveCircuitBreakerFactory reactiveCircuitBreakerFactory;
@Autowired
private WebClient.Builder webClientBuilder;
@ -146,11 +142,6 @@ public class CircuitBreakerController {
.get()
.uri("/quickstart/callee/circuitBreak")
.retrieve()
.bodyToMono(String.class)
.transform(it ->
reactiveCircuitBreakerFactory
.create(MetadataContext.LOCAL_NAMESPACE + "QuickstartCalleeService#/quickstart/callee/circuitBreak#http#GET")
.run(it, throwable -> Mono.just("fallback: trigger the refuse for service callee"))
);
.bodyToMono(String.class);
}
}

@ -29,6 +29,8 @@ import com.tencent.cloud.polaris.context.admin.PolarisAdminProperties;
import com.tencent.cloud.polaris.context.config.extend.consul.ConsulProperties;
import com.tencent.cloud.polaris.context.config.extend.tsf.TsfCoreProperties;
import com.tencent.cloud.polaris.context.config.extend.tsf.TsfInstanceMetadataProvider;
import com.tencent.cloud.polaris.context.event.PushGatewayEventReporterConfigModifier;
import com.tencent.cloud.polaris.context.event.PushGatewayEventReporterProperties;
import com.tencent.cloud.polaris.context.listener.PolarisContextApplicationEventListener;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.client.api.SDKContext;
@ -81,6 +83,18 @@ public class PolarisContextAutoConfiguration {
return new PolarisAdminConfigModifier(polarisAdminProperties);
}
@Bean
@ConditionalOnMissingBean
public PushGatewayEventReporterProperties pushGatewayEventReporterProperties() {
return new PushGatewayEventReporterProperties();
}
@Bean
@ConditionalOnMissingBean
public PushGatewayEventReporterConfigModifier pushGatewayEventReporterConfigModifier(PushGatewayEventReporterProperties pushGatewayEventReporterProperties) {
return new PushGatewayEventReporterConfigModifier(pushGatewayEventReporterProperties);
}
@Bean
@ConditionalOnMissingBean
public ConsulProperties consulProperties() {

@ -0,0 +1,65 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.context.event;
import com.tencent.cloud.common.constant.OrderConstant;
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
import com.tencent.polaris.api.config.plugin.DefaultPlugins;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.factory.config.ConfigurationImpl;
import com.tencent.polaris.plugins.event.pushgateway.PushGatewayEventReporterConfig;
/**
* Modifier for push gateway event reporter.
*
* @author Haotian Zhang
*/
public class PushGatewayEventReporterConfigModifier implements PolarisConfigModifier {
private final PushGatewayEventReporterProperties properties;
public PushGatewayEventReporterConfigModifier(PushGatewayEventReporterProperties properties) {
this.properties = properties;
}
@Override
public void modify(ConfigurationImpl configuration) {
configuration.getGlobal().getEventReporter().getReporters()
.add(DefaultPlugins.PUSH_GATEWAY_EVENT_REPORTER_TYPE);
PushGatewayEventReporterConfig pushGatewayEventReporterConfig = new PushGatewayEventReporterConfig();
if (!properties.isEnabled() || StringUtils.isBlank(properties.getAddress())) {
pushGatewayEventReporterConfig.setEnable(false);
return;
}
else {
pushGatewayEventReporterConfig.setEnable(true);
}
pushGatewayEventReporterConfig.setAddress(properties.getAddress());
pushGatewayEventReporterConfig.setEventQueueSize(properties.getEventQueueSize());
pushGatewayEventReporterConfig.setMaxBatchSize(properties.getMaxBatchSize());
configuration.getGlobal().getEventReporter()
.setPluginConfig(DefaultPlugins.PUSH_GATEWAY_EVENT_REPORTER_TYPE, pushGatewayEventReporterConfig);
}
@Override
public int getOrder() {
return OrderConstant.Modifier.PUSH_GATEWAY_EVENT_ORDER;
}
}

@ -0,0 +1,91 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.context.event;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Properties for Polaris push gateway event reporter.
*
* @author Haotian Zhang
*/
@ConfigurationProperties(prefix = "spring.cloud.polaris.event.pushgateway")
public class PushGatewayEventReporterProperties {
/**
* If push gateway event enabled.
*/
private boolean enabled = false;
/**
* Address of pushgateway. For example: 1.2.3.4:9091.
*/
private String address;
/**
* Queue size for push gateway event queue. Default is 1000.
*/
private int eventQueueSize = 1000;
/**
* Max batch size for push gateway event. Default is 100.
*/
private int maxBatchSize = 100;
public boolean isEnabled() {
return enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getEventQueueSize() {
return eventQueueSize;
}
public void setEventQueueSize(int eventQueueSize) {
this.eventQueueSize = eventQueueSize;
}
public int getMaxBatchSize() {
return maxBatchSize;
}
public void setMaxBatchSize(int maxBatchSize) {
this.maxBatchSize = maxBatchSize;
}
@Override
public String toString() {
return "PushGatewayEventReporterProperties{" +
"enabled=" + enabled +
", address='" + address + '\'' +
", eventQueueSize=" + eventQueueSize +
", maxBatchSize=" + maxBatchSize +
'}';
}
}

@ -51,6 +51,29 @@
"type": "java.lang.String",
"defaultValue": "./polaris/logs",
"description": "polaris log storage path."
},
{
"name": "spring.cloud.polaris.event.pushgateway.enabled",
"type": "java.lang.Boolean",
"description": "If push gateway event reporter is enabled.",
"default": false
},
{
"name": "spring.cloud.polaris.event.pushgateway.address",
"type": "java.lang.String",
"description": "Address of pushgateway. For example: 1.2.3.4:9091."
},
{
"name": "spring.cloud.polaris.event.pushgateway.eventQueueSize",
"type": "java.lang.Integer",
"description": "Queue size for push gateway event queue. Default is 1000.",
"default": 1000
},
{
"name": "spring.cloud.polaris.event.pushgateway.maxBatchSize",
"type": "java.lang.Integer",
"description": "Max batch size for push gateway event. Default is 100.",
"default": 100
}
],
"hints": []

@ -0,0 +1,64 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.context.event;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import com.tencent.polaris.api.config.plugin.DefaultPlugins;
import com.tencent.polaris.plugins.event.pushgateway.PushGatewayEventReporterConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PushGatewayEventReporterConfigModifier}.
*
* @author Haotian Zhang
*/
public class PushGatewayEventReporterConfigModifierTest {
private final ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisContextAutoConfiguration.class))
.withPropertyValues("spring.cloud.polaris.event.pushgateway.enabled=true")
.withPropertyValues("spring.cloud.polaris.event.pushgateway.address=1.2.3.4:9091")
.withPropertyValues("spring.cloud.polaris.event.pushgateway.eventQueueSize=123")
.withPropertyValues("spring.cloud.polaris.event.pushgateway.maxBatchSize=456");
@BeforeEach
void setUp() {
PolarisSDKContextManager.innerDestroy();
}
@Test
public void testModify() {
this.applicationContextRunner.run(context -> {
PolarisSDKContextManager polarisSDKContextManager = context.getBean(PolarisSDKContextManager.class);
PushGatewayEventReporterConfig config = polarisSDKContextManager.getSDKContext().
getConfig().getGlobal().getEventReporter()
.getPluginConfig(DefaultPlugins.PUSH_GATEWAY_EVENT_REPORTER_TYPE, PushGatewayEventReporterConfig.class);
assertThat(config.isEnable()).isTrue();
assertThat(config.getAddress()).isEqualTo("1.2.3.4:9091");
assertThat(config.getEventQueueSize()).isEqualTo(123);
assertThat(config.getMaxBatchSize()).isEqualTo(456);
});
}
}

@ -0,0 +1,59 @@
/*
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
*
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/
package com.tencent.cloud.polaris.context.event;
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test for {@link PushGatewayEventReporterProperties}.
*
* @author Haotian Zhang
*/
public class PushGatewayEventReporterPropertiesTest {
private final ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
.withConfiguration(AutoConfigurations.of(PolarisContextAutoConfiguration.class))
.withPropertyValues("spring.cloud.polaris.event.pushgateway.enabled=true")
.withPropertyValues("spring.cloud.polaris.event.pushgateway.address=1.2.3.4:9091")
.withPropertyValues("spring.cloud.polaris.event.pushgateway.eventQueueSize=123")
.withPropertyValues("spring.cloud.polaris.event.pushgateway.maxBatchSize=456");
@BeforeEach
void setUp() {
PolarisSDKContextManager.innerDestroy();
}
@Test
public void testGetAndSet() {
this.applicationContextRunner.run(context -> {
PushGatewayEventReporterProperties properties = context.getBean(PushGatewayEventReporterProperties.class);
assertThat(properties.isEnabled()).isTrue();
assertThat(properties.getAddress()).isEqualTo("1.2.3.4:9091");
assertThat(properties.getEventQueueSize()).isEqualTo(123);
assertThat(properties.getMaxBatchSize()).isEqualTo(456);
});
}
}
Loading…
Cancel
Save