diff --git a/sermant-integration-tests/spring-test/spring-intergration-test/src/test/java/com/huaweicloud/intergration/config/NacosConfigTest.java b/sermant-integration-tests/spring-test/spring-intergration-test/src/test/java/com/huaweicloud/intergration/config/NacosConfigTest.java index 4f17443676..3b4cabbbe9 100644 --- a/sermant-integration-tests/spring-test/spring-intergration-test/src/test/java/com/huaweicloud/intergration/config/NacosConfigTest.java +++ b/sermant-integration-tests/spring-test/spring-intergration-test/src/test/java/com/huaweicloud/intergration/config/NacosConfigTest.java @@ -17,13 +17,15 @@ package com.huaweicloud.intergration.config; -import com.alibaba.nacos.api.config.ConfigType; -import com.alibaba.nacos.api.exception.NacosException; -import com.alibaba.nacos.client.config.NacosConfigService; import com.huaweicloud.intergration.common.utils.RequestUtils; import com.huaweicloud.intergration.config.rule.NacosTestRule; import com.huaweicloud.intergration.config.supprt.KieClient; +import com.alibaba.fastjson.JSONObject; +import com.alibaba.nacos.api.config.ConfigType; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.client.config.NacosConfigService; + import org.junit.Assert; import org.junit.Before; import org.junit.Rule; @@ -130,6 +132,9 @@ private void check(long maxWaitTimeMs, long sleepTimeMs, Supplier check // ignored } } + if (!checkFunc.get()) { + LOGGER.error("=======配置中心配置内容: [{}]==================", JSONObject.toJSONString(kieClient.query(null))); + } Assert.assertTrue(checkFunc.get()); } diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringBootDeclarer.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringApplicationDeclarer.java similarity index 73% rename from sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringBootDeclarer.java rename to sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringApplicationDeclarer.java index 5edf6c611d..cc5d1e8037 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringBootDeclarer.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringApplicationDeclarer.java @@ -23,28 +23,28 @@ import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; /** - * 增强SpringBootApplication类的main方法 + * 增强SpringApplication类的run方法 * * @author provenceee * @since 2022-01-24 */ -public class SpringBootDeclarer extends AbstractPluginDeclarer { - private static final String[] ENHANCE_CLASS = {"org.springframework.boot.autoconfigure.SpringBootApplication"}; +public class SpringApplicationDeclarer extends AbstractPluginDeclarer { + private static final String ENHANCE_CLASS = "org.springframework.boot.SpringApplication"; - private static final String INTERCEPT_CLASS = SpringBootInterceptor.class.getCanonicalName(); + private static final String INTERCEPT_CLASS = SpringApplicationInterceptor.class.getCanonicalName(); - private static final String METHOD_NAME = "main"; + private static final String METHOD_NAME = "run"; @Override public ClassMatcher getClassMatcher() { - return ClassMatcher.isAnnotatedWith(ENHANCE_CLASS); + return ClassMatcher.nameEquals(ENHANCE_CLASS); } @Override public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { return new InterceptDeclarer[]{ - InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME).and(MethodMatcher.isStaticMethod()), + InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME).and(MethodMatcher.isMemberMethod()), INTERCEPT_CLASS) }; } -} +} \ No newline at end of file diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringBootInterceptor.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringApplicationInterceptor.java similarity index 70% rename from sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringBootInterceptor.java rename to sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringApplicationInterceptor.java index 252e346c4c..0f7ef1eb4a 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringBootInterceptor.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/java/com/huawei/flowcontrol/config/SpringApplicationInterceptor.java @@ -23,13 +23,17 @@ import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; import com.huaweicloud.sermant.core.service.ServiceManager; +import java.util.concurrent.atomic.AtomicBoolean; + /** * 结束阶段开始初始化流控配置监听 * * @author zhouss * @since 2022-01-28 */ -public class SpringBootInterceptor extends AbstractInterceptor { +public class SpringApplicationInterceptor extends AbstractInterceptor { + private static final AtomicBoolean INIT = new AtomicBoolean(); + @Override public ExecuteContext before(ExecuteContext context) throws Exception { return context; @@ -37,8 +41,11 @@ public ExecuteContext before(ExecuteContext context) throws Exception { @Override public ExecuteContext after(ExecuteContext context) { - final FlowControlInitServiceImpl service = ServiceManager.getService(FlowControlInitServiceImpl.class); - service.doStart(); + Object logStartupInfo = context.getMemberFieldValue("logStartupInfo"); + if ((logStartupInfo instanceof Boolean) && (Boolean) logStartupInfo && INIT.compareAndSet(false, true)) { + final FlowControlInitServiceImpl service = ServiceManager.getService(FlowControlInitServiceImpl.class); + service.doStart(); + } return context; } -} +} \ No newline at end of file diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer index 0cfdf58a79..794262452e 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -22,7 +22,7 @@ com.huawei.flowcontrol.config.DubboServiceNameDeclarer com.huawei.flowcontrol.config.ApacheDubboConfigDeclarer com.huawei.flowcontrol.config.AlibabaDubboConfigDeclarer com.huawei.flowcontrol.config.SpringFactoriesDeclarer -com.huawei.flowcontrol.config.SpringBootDeclarer +com.huawei.flowcontrol.config.SpringApplicationDeclarer #com.huawei.flowcontrol.retry.AlibabaDubboInvokerDeclarer #com.huawei.flowcontrol.retry.ApacheDubboInvokerDeclarer com.huawei.flowcontrol.retry.ExtentionLoaderDeclarer @@ -30,4 +30,4 @@ com.huawei.flowcontrol.retry.FeignRequestDeclarer com.huawei.flowcontrol.retry.HttpRequestDeclarer com.huawei.flowcontrol.retry.SpringRibbonLbDeclarer com.huawei.flowcontrol.retry.SpringLbDeclarer -com.huawei.flowcontrol.ClusterDeclarer +com.huawei.flowcontrol.ClusterDeclarer \ No newline at end of file diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/test/java/com/huawei/flowcontrol/config/SpringBootInterceptorTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/test/java/com/huawei/flowcontrol/config/SpringApplicationInterceptorTest.java similarity index 77% rename from sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/test/java/com/huawei/flowcontrol/config/SpringBootInterceptorTest.java rename to sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/test/java/com/huawei/flowcontrol/config/SpringApplicationInterceptorTest.java index 0f9cd4f4ed..41591c8df2 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/test/java/com/huawei/flowcontrol/config/SpringBootInterceptorTest.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-plugin/src/test/java/com/huawei/flowcontrol/config/SpringApplicationInterceptorTest.java @@ -17,16 +17,13 @@ package com.huawei.flowcontrol.config; -import static org.junit.Assert.*; - -import com.huawei.flowcontrol.TestHelper; import com.huawei.flowcontrol.common.config.FlowControlConfig; import com.huawei.flowcontrol.common.init.FlowControlInitServiceImpl; +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; import com.huaweicloud.sermant.core.plugin.config.PluginConfigManager; import com.huaweicloud.sermant.core.service.ServiceManager; -import org.checkerframework.checker.units.qual.A; import org.junit.After; import org.junit.Assert; import org.junit.Before; @@ -34,6 +31,7 @@ import org.mockito.MockedStatic; import org.mockito.Mockito; +import java.util.Collections; import java.util.concurrent.atomic.AtomicBoolean; /** @@ -42,7 +40,7 @@ * @author zhouss * @since 2022-08-30 */ -public class SpringBootInterceptorTest { +public class SpringApplicationInterceptorTest { private final FlowControlConfig flowControlConfig = new FlowControlConfig(); private final AtomicBoolean executed = new AtomicBoolean(); @@ -78,9 +76,22 @@ public void doStart() { */ @Test public void testStart() throws Exception { - final SpringBootInterceptor springBootInterceptor = new SpringBootInterceptor(); - springBootInterceptor.after(TestHelper.buildDefaultContext()); + final SpringApplicationInterceptor springApplicationInterceptor = new SpringApplicationInterceptor(); + springApplicationInterceptor.after(buildContext()); Assert.assertTrue(executed.get()); } -} + private ExecuteContext buildContext() throws NoSuchMethodException { + return ExecuteContext.forMemberMethod( + new TestObject(), + String.class.getMethod("trim"), + new Object[0], + Collections.emptyMap(), + Collections.emptyMap() + ); + } + + private static class TestObject { + private final boolean logStartupInfo = true; + } +} \ No newline at end of file diff --git a/sermant-plugins/sermant-flowcontrol/flowcontrol-service/src/test/java/com/huawei/fowcontrol/res4j/monitor/MonitorTest.java b/sermant-plugins/sermant-flowcontrol/flowcontrol-service/src/test/java/com/huawei/fowcontrol/res4j/monitor/MonitorTest.java index 700b5534bd..18118b97aa 100644 --- a/sermant-plugins/sermant-flowcontrol/flowcontrol-service/src/test/java/com/huawei/fowcontrol/res4j/monitor/MonitorTest.java +++ b/sermant-plugins/sermant-flowcontrol/flowcontrol-service/src/test/java/com/huawei/fowcontrol/res4j/monitor/MonitorTest.java @@ -28,7 +28,6 @@ import org.junit.Assert; import org.junit.Test; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -42,8 +41,6 @@ * @since 2022-09-15 */ public class MonitorTest { - private static final String FILED_NAME = "monitors"; - private static final long DEFAULT_VALUE = 1000L; private static final String NAME = "default"; @@ -71,11 +68,13 @@ public void testCollect() { MetricEntity metricEntity = new MetricEntity(); metricEntity.getFuseRequest().getAndAdd(DEFAULT_VALUE); monitors.put(NAME, metricEntity); - ReflectUtils.setFieldValue(service, FILED_NAME, monitors); + ServiceCollectorService.MONITORS.clear(); + ServiceCollectorService.MONITORS.putAll(monitors); Assert.assertNotNull(metricFamilySamplesList); + metricFamilySamplesList = service.collect(); metricFamilySamplesList.forEach(metricFamilySamples -> metricFamilySamples.samples.forEach(sample -> { if (StringUtils.equal(sample.name, MetricType.FUSED_REQUEST.getName())) { - Assert.assertEquals(sample.value, DEFAULT_VALUE); + Assert.assertEquals(sample.value, DEFAULT_VALUE, 0.0); } })); } diff --git a/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/declarer/SpringBootDeclarer.java b/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/declarer/SpringApplicationDeclarer.java similarity index 68% rename from sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/declarer/SpringBootDeclarer.java rename to sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/declarer/SpringApplicationDeclarer.java index d1867247e4..7eb67d69bc 100644 --- a/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/declarer/SpringBootDeclarer.java +++ b/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/declarer/SpringApplicationDeclarer.java @@ -16,37 +16,31 @@ package com.huaweicloud.sermant.router.dubbo.declarer; -import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; /** - * 增强SpringBootApplication类的main方法 + * 增强SpringApplication类的run方法 * * @author provenceee * @since 2022-01-24 */ -public class SpringBootDeclarer extends AbstractDeclarer { - private static final String[] ENHANCE_CLASS = {"org.springframework.boot.autoconfigure.SpringBootApplication"}; +public class SpringApplicationDeclarer extends AbstractDeclarer { + private static final String[] ENHANCE_CLASS = {"org.springframework.boot.SpringApplication"}; private static final String INTERCEPT_CLASS - = "com.huaweicloud.sermant.router.dubbo.interceptor.SpringBootInterceptor"; + = "com.huaweicloud.sermant.router.dubbo.interceptor.SpringApplicationInterceptor"; - private static final String METHOD_NAME = "main"; + private static final String METHOD_NAME = "run"; /** * 构造方法 */ - public SpringBootDeclarer() { + public SpringApplicationDeclarer() { super(ENHANCE_CLASS, INTERCEPT_CLASS, METHOD_NAME); } - @Override - public ClassMatcher getClassMatcher() { - return ClassMatcher.isAnnotatedWith(ENHANCE_CLASS); - } - @Override public MethodMatcher getMethodMatcher() { - return super.getMethodMatcher().and(MethodMatcher.isStaticMethod()); + return super.getMethodMatcher().and(MethodMatcher.isMemberMethod()); } -} +} \ No newline at end of file diff --git a/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/interceptor/SpringBootInterceptor.java b/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/interceptor/SpringApplicationInterceptor.java similarity index 72% rename from sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/interceptor/SpringBootInterceptor.java rename to sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/interceptor/SpringApplicationInterceptor.java index 718a829b11..92065f808a 100644 --- a/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/interceptor/SpringBootInterceptor.java +++ b/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/java/com/huaweicloud/sermant/router/dubbo/interceptor/SpringApplicationInterceptor.java @@ -23,19 +23,23 @@ import com.huaweicloud.sermant.router.dubbo.cache.DubboCache; import com.huaweicloud.sermant.router.dubbo.service.DubboConfigService; +import java.util.concurrent.atomic.AtomicBoolean; + /** - * 增强SpringBootApplication类的main方法 + * 增强SpringApplication类的run方法 * * @author provenceee * @since 2022-01-24 */ -public class SpringBootInterceptor extends AbstractInterceptor { +public class SpringApplicationInterceptor extends AbstractInterceptor { + private static final AtomicBoolean INIT = new AtomicBoolean(); + private final DubboConfigService configService; /** * 构造方法 */ - public SpringBootInterceptor() { + public SpringApplicationInterceptor() { configService = ServiceManager.getService(DubboConfigService.class); } @@ -46,7 +50,10 @@ public ExecuteContext before(ExecuteContext context) { @Override public ExecuteContext after(ExecuteContext context) { - configService.init(RouterConstant.DUBBO_CACHE_NAME, DubboCache.INSTANCE.getAppName()); + Object logStartupInfo = context.getMemberFieldValue("logStartupInfo"); + if ((logStartupInfo instanceof Boolean) && (Boolean) logStartupInfo && INIT.compareAndSet(false, true)) { + configService.init(RouterConstant.DUBBO_CACHE_NAME, DubboCache.INSTANCE.getAppName()); + } return context; } -} +} \ No newline at end of file diff --git a/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer index 025f36f413..198d75563d 100644 --- a/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer +++ b/sermant-plugins/sermant-router/dubbo-router-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -6,4 +6,4 @@ com.huaweicloud.sermant.router.dubbo.declarer.AbstractDirectoryDeclarer com.huaweicloud.sermant.router.dubbo.declarer.ApplicationConfigDeclarer com.huaweicloud.sermant.router.dubbo.declarer.ClusterUtilsDeclarer com.huaweicloud.sermant.router.dubbo.declarer.ContextFilterDeclarer -com.huaweicloud.sermant.router.dubbo.declarer.SpringBootDeclarer \ No newline at end of file +com.huaweicloud.sermant.router.dubbo.declarer.SpringApplicationDeclarer \ No newline at end of file diff --git a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/declarer/SpringBootDeclarer.java b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/declarer/SpringApplicationDeclarer.java similarity index 67% rename from sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/declarer/SpringBootDeclarer.java rename to sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/declarer/SpringApplicationDeclarer.java index 2884758c26..972da473aa 100644 --- a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/declarer/SpringBootDeclarer.java +++ b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/declarer/SpringApplicationDeclarer.java @@ -17,37 +17,33 @@ package com.huawei.dubbo.registry.declarer; import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; -import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; +import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; /** - * 增强SpringBootApplication类的main方法 + * 增强SpringApplication类的run方法 * * @author provenceee * @since 2022-01-24 */ -public class SpringBootDeclarer extends AbstractDeclarer { - private static final String[] ENHANCE_CLASS = {"org.springframework.boot.autoconfigure.SpringBootApplication"}; +public class SpringApplicationDeclarer extends AbstractDeclarer { + private static final String[] ENHANCE_CLASS = {"org.springframework.boot.SpringApplication"}; - private static final String INTERCEPT_CLASS = "com.huawei.dubbo.registry.interceptor.SpringBootInterceptor"; + private static final String INTERCEPT_CLASS = "com.huawei.dubbo.registry.interceptor.SpringApplicationInterceptor"; - private static final String METHOD_NAME = "main"; + private static final String METHOD_NAME = "run"; /** * 构造方法 */ - public SpringBootDeclarer() { + public SpringApplicationDeclarer() { super(ENHANCE_CLASS); } - @Override - public ClassMatcher getClassMatcher() { - return ClassMatcher.isAnnotatedWith(ENHANCE_CLASS); - } - @Override public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { return new InterceptDeclarer[]{ - InterceptDeclarer.build(getStaticMethod(METHOD_NAME), INTERCEPT_CLASS) + InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME).and(MethodMatcher.isMemberMethod()), + INTERCEPT_CLASS) }; } -} +} \ No newline at end of file diff --git a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/interceptor/SpringBootInterceptor.java b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/interceptor/SpringApplicationInterceptor.java similarity index 72% rename from sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/interceptor/SpringBootInterceptor.java rename to sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/interceptor/SpringApplicationInterceptor.java index 9b3b22b0ce..2868c614de 100644 --- a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/interceptor/SpringBootInterceptor.java +++ b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/java/com/huawei/dubbo/registry/interceptor/SpringApplicationInterceptor.java @@ -22,19 +22,23 @@ import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; import com.huaweicloud.sermant.core.service.ServiceManager; +import java.util.concurrent.atomic.AtomicBoolean; + /** - * 增强SpringBootApplication类的main方法 + * 增强SpringApplication类的run方法 * * @author provenceee * @since 2022-01-24 */ -public class SpringBootInterceptor extends AbstractInterceptor { +public class SpringApplicationInterceptor extends AbstractInterceptor { + private static final AtomicBoolean INIT = new AtomicBoolean(); + private final RegistryService registryService; /** * 构造方法 */ - public SpringBootInterceptor() { + public SpringApplicationInterceptor() { registryService = ServiceManager.getService(RegistryService.class); } @@ -45,7 +49,10 @@ public ExecuteContext before(ExecuteContext context) { @Override public ExecuteContext after(ExecuteContext context) { - registryService.startRegistration(); + Object logStartupInfo = context.getMemberFieldValue("logStartupInfo"); + if ((logStartupInfo instanceof Boolean) && (Boolean) logStartupInfo && INIT.compareAndSet(false, true)) { + registryService.startRegistration(); + } return context; } -} +} \ No newline at end of file diff --git a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer index 6c37f03310..ae4135e125 100644 --- a/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer +++ b/sermant-plugins/sermant-service-registry/dubbo-registry-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -6,4 +6,4 @@ com.huawei.dubbo.registry.declarer.MigrationRuleDeclarer com.huawei.dubbo.registry.declarer.MigrationRuleHandlerDeclarer com.huawei.dubbo.registry.declarer.RegistryConfigDeclarer com.huawei.dubbo.registry.declarer.RegistryProtocolDeclarer -com.huawei.dubbo.registry.declarer.SpringBootDeclarer \ No newline at end of file +com.huawei.dubbo.registry.declarer.SpringApplicationDeclarer \ No newline at end of file diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/declarers/SpringBootDeclarer.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/declarers/SpringApplicationDeclarer.java similarity index 70% rename from sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/declarers/SpringBootDeclarer.java rename to sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/declarers/SpringApplicationDeclarer.java index 5386984c71..7340d030da 100644 --- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/declarers/SpringBootDeclarer.java +++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/declarers/SpringApplicationDeclarer.java @@ -16,35 +16,35 @@ package com.huawei.discovery.declarers; -import com.huawei.discovery.interceptors.SpringBootInterceptor; +import com.huawei.discovery.interceptors.SpringApplicationInterceptor; import com.huaweicloud.sermant.core.plugin.agent.declarer.InterceptDeclarer; import com.huaweicloud.sermant.core.plugin.agent.matcher.ClassMatcher; import com.huaweicloud.sermant.core.plugin.agent.matcher.MethodMatcher; /** - * 增强SpringBootApplication类的main方法 + * 增强SpringApplication类的run方法 * * @author chengyouling * @since 2022-10-10 */ -public class SpringBootDeclarer extends BaseDeclarer { - private static final String[] ENHANCE_CLASS = {"org.springframework.boot.autoconfigure.SpringBootApplication"}; +public class SpringApplicationDeclarer extends BaseDeclarer { + private static final String ENHANCE_CLASS = "org.springframework.boot.SpringApplication"; - private static final String INTERCEPT_CLASS = SpringBootInterceptor.class.getCanonicalName(); + private static final String INTERCEPT_CLASS = SpringApplicationInterceptor.class.getCanonicalName(); - private static final String METHOD_NAME = "main"; + private static final String METHOD_NAME = "run"; @Override public ClassMatcher getClassMatcher() { - return ClassMatcher.isAnnotatedWith(ENHANCE_CLASS); + return ClassMatcher.nameEquals(ENHANCE_CLASS); } @Override public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { return new InterceptDeclarer[]{ - InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME).and(MethodMatcher.isStaticMethod()), + InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME).and(MethodMatcher.isMemberMethod()), INTERCEPT_CLASS) }; } -} +} \ No newline at end of file diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/SpringBootInterceptor.java b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/SpringApplicationInterceptor.java similarity index 73% rename from sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/SpringBootInterceptor.java rename to sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/SpringApplicationInterceptor.java index 9f75f24ab4..3f2313844d 100644 --- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/SpringBootInterceptor.java +++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/java/com/huawei/discovery/interceptors/SpringApplicationInterceptor.java @@ -24,13 +24,16 @@ import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; import com.huaweicloud.sermant.core.service.ServiceManager; +import java.util.concurrent.atomic.AtomicBoolean; + /** * 结束阶段开始注册微服务 * * @author chengyouling * @since 2022-10-10 */ -public class SpringBootInterceptor extends AbstractInterceptor { +public class SpringApplicationInterceptor extends AbstractInterceptor { + private static final AtomicBoolean INIT = new AtomicBoolean(); private final RegistryService registryService; @@ -39,7 +42,7 @@ public class SpringBootInterceptor extends AbstractInterceptor { /** * 构造方法 */ - public SpringBootInterceptor() { + public SpringApplicationInterceptor() { registryService = ServiceManager.getService(RegistryService.class); configCenterService = ServiceManager.getService(ConfigCenterService.class); } @@ -51,8 +54,11 @@ public ExecuteContext before(ExecuteContext context) throws Exception { @Override public ExecuteContext after(ExecuteContext context) { - registryService.registry(RegisterContext.INSTANCE.getServiceInstance()); - configCenterService.init(RegisterContext.INSTANCE.getServiceInstance().getServiceName()); + Object logStartupInfo = context.getMemberFieldValue("logStartupInfo"); + if ((logStartupInfo instanceof Boolean) && (Boolean) logStartupInfo && INIT.compareAndSet(false, true)) { + registryService.registry(RegisterContext.INSTANCE.getServiceInstance()); + configCenterService.init(RegisterContext.INSTANCE.getServiceInstance().getServiceName()); + } return context; } -} +} \ No newline at end of file diff --git a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer index 498d5d8252..234b60cd76 100644 --- a/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer +++ b/sermant-plugins/sermant-springboot-registry/springboot-registry-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -22,7 +22,7 @@ com.huawei.discovery.declarers.OkHttpClientDeclarer com.huawei.discovery.declarers.FeignInvokeDeclarer com.huawei.discovery.declarers.OkHttp3ClientDeclarer com.huawei.discovery.declarers.SpringEnvironmentInfoDeclarer -com.huawei.discovery.declarers.SpringBootDeclarer +com.huawei.discovery.declarers.SpringApplicationDeclarer com.huawei.discovery.declarers.httpconnection.HttpUrlConnectionConnectDeclarer com.huawei.discovery.declarers.httpconnection.HttpUrlConnectionResponseStreamDeclarer com.huawei.discovery.declarers.SpringContextDeclarer