diff --git a/CHANGELOG.md b/CHANGELOG.md index f5e94ea57..378bd31ee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,3 +26,5 @@ - [feat:support zuul circuit breaker fallback response.](https://github.com/Tencent/spring-cloud-tencent/pull/1522) - [feat:support auth.](https://github.com/Tencent/spring-cloud-tencent/pull/1541) - [feat:support smooth upgrade from tsf. ](https://github.com/Tencent/spring-cloud-tencent/pull/1542) +- [feat:support polaris event.](https://github.com/Tencent/spring-cloud-tencent/pull/1543) + diff --git a/pom.xml b/pom.xml index 7ebc2986a..6905732c9 100644 --- a/pom.xml +++ b/pom.xml @@ -163,7 +163,6 @@ pom import - diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java index 6d128baf5..7630a60f9 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisAutoServiceRegistration.java @@ -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; @@ -85,6 +92,25 @@ protected void registerManagement() { @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; } diff --git a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java index 9a0f99936..f8b93b9b1 100644 --- a/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java +++ b/spring-cloud-starter-tencent-polaris-discovery/src/main/java/com/tencent/cloud/polaris/registry/PolarisRegistration.java @@ -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 customizers; + private String serviceId; private boolean registerEnabled = false; private Map metadata; private Map> extendedMetadata; @@ -164,15 +163,15 @@ public void customize() { } } - 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 boolean isRegisterEnabled() { return registerEnabled; } + public SDKContext getPolarisContext() { + return polarisContext; + } + @Override public String toString() { return "PolarisRegistration{" + diff --git a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java index e689feaca..9e7612bc1 100644 --- a/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java +++ b/spring-cloud-tencent-commons/src/main/java/com/tencent/cloud/common/constant/OrderConstant.java @@ -246,5 +246,10 @@ public static final class Modifier { * 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; } } diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 751023f9e..10ee2068e 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -73,7 +73,7 @@ 2.0.2.0-Hoxton.SR12-SNAPSHOT - 2.0.0.1 + 2.0.1.0-RC1 32.1.3-jre 1.2.13 1.7.0 @@ -146,7 +146,6 @@ ${revision} - com.tencent.cloud spring-cloud-starter-tencent-polaris-ratelimit diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java index c9f3e991d..a77abde5d 100644 --- a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/config/PolarisContextAutoConfiguration.java @@ -29,6 +29,8 @@ 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 PolarisAdminConfigModifier polarisAdminConfigModifier(PolarisAdminPropert 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() { diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterConfigModifier.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterConfigModifier.java new file mode 100644 index 000000000..175055afc --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterConfigModifier.java @@ -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; + } +} diff --git a/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterProperties.java b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterProperties.java new file mode 100644 index 000000000..4854693d8 --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/main/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterProperties.java @@ -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 + + '}'; + } +} diff --git a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json index f3244d1fd..afc371b87 100644 --- a/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-tencent-polaris-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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": [] diff --git a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterConfigModifierTest.java b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterConfigModifierTest.java new file mode 100644 index 000000000..091bdf6a8 --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterConfigModifierTest.java @@ -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); + }); + } +} diff --git a/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterPropertiesTest.java b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterPropertiesTest.java new file mode 100644 index 000000000..112db0722 --- /dev/null +++ b/spring-cloud-tencent-polaris-context/src/test/java/com/tencent/cloud/polaris/context/event/PushGatewayEventReporterPropertiesTest.java @@ -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); + }); + } +}