diff --git a/sermant-plugins/sermant-service-removal/removal-plugin/src/main/java/com/huaweicloud/sermant/declarer/SpringApplicationDeclarer.java b/sermant-plugins/sermant-service-removal/removal-plugin/src/main/java/com/huaweicloud/sermant/declarer/SpringApplicationDeclarer.java new file mode 100644 index 0000000000..dd7b2014c6 --- /dev/null +++ b/sermant-plugins/sermant-service-removal/removal-plugin/src/main/java/com/huaweicloud/sermant/declarer/SpringApplicationDeclarer.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.huaweicloud.sermant.declarer; + +import com.huaweicloud.sermant.core.plugin.agent.declarer.AbstractPluginDeclarer; +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; +import com.huaweicloud.sermant.interceptor.SpringApplicationInterceptor; + +/** + * 增强run方法 + * + * @author lilai + * @since 2023-04-10 + */ +public class SpringApplicationDeclarer extends AbstractPluginDeclarer { + private static final String ENHANCE_CLASS = "org.springframework.boot.SpringApplication"; + + private static final String INTERCEPT_CLASS = SpringApplicationInterceptor.class.getCanonicalName(); + + private static final String METHOD_NAME = "run"; + + @Override + public ClassMatcher getClassMatcher() { + return ClassMatcher.nameEquals(ENHANCE_CLASS); + } + + @Override + public InterceptDeclarer[] getInterceptDeclarers(ClassLoader classLoader) { + return new InterceptDeclarer[]{ + InterceptDeclarer.build(MethodMatcher.nameEquals(METHOD_NAME).and(MethodMatcher.isMemberMethod()), + INTERCEPT_CLASS) + }; + } +} \ No newline at end of file diff --git a/sermant-plugins/sermant-service-removal/removal-plugin/src/main/java/com/huaweicloud/sermant/interceptor/SpringApplicationInterceptor.java b/sermant-plugins/sermant-service-removal/removal-plugin/src/main/java/com/huaweicloud/sermant/interceptor/SpringApplicationInterceptor.java new file mode 100644 index 0000000000..e731a58635 --- /dev/null +++ b/sermant-plugins/sermant-service-removal/removal-plugin/src/main/java/com/huaweicloud/sermant/interceptor/SpringApplicationInterceptor.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.huaweicloud.sermant.interceptor; + +import com.huaweicloud.sermant.core.plugin.agent.entity.ExecuteContext; +import com.huaweicloud.sermant.core.plugin.agent.interceptor.AbstractInterceptor; +import com.huaweicloud.sermant.core.service.ServiceManager; +import com.huaweicloud.sermant.service.RemovalConfigService; + +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * 增强run方法 + * + * @author lilai + * @since 2023-04-10 + */ +public class SpringApplicationInterceptor extends AbstractInterceptor { + private static final AtomicBoolean INIT = new AtomicBoolean(); + + private final RemovalConfigService removalConfigService; + + /** + * 构造方法 + */ + public SpringApplicationInterceptor() { + removalConfigService = ServiceManager.getService(RemovalConfigService.class); + } + + @Override + public ExecuteContext before(ExecuteContext context) { + return context; + } + + @Override + public ExecuteContext after(ExecuteContext context) { + Object logStartupInfo = context.getMemberFieldValue("logStartupInfo"); + if ((logStartupInfo instanceof Boolean) && (Boolean) logStartupInfo && INIT.compareAndSet(false, true)) { + removalConfigService.init(); + } + return context; + } +} diff --git a/sermant-plugins/sermant-service-removal/removal-plugin/src/main/java/com/huaweicloud/sermant/service/RemovalConfigService.java b/sermant-plugins/sermant-service-removal/removal-plugin/src/main/java/com/huaweicloud/sermant/service/RemovalConfigService.java new file mode 100644 index 0000000000..dd11d2fb2d --- /dev/null +++ b/sermant-plugins/sermant-service-removal/removal-plugin/src/main/java/com/huaweicloud/sermant/service/RemovalConfigService.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.huaweicloud.sermant.service; + +import com.huaweicloud.sermant.core.plugin.service.PluginService; + +/** + * 类描述 + * + * @author lilai + * @since 2023-04-10 + */ +public interface RemovalConfigService extends PluginService { + /** + * 初始化配置监听 + */ + void init(); +} diff --git a/sermant-plugins/sermant-service-removal/removal-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer b/sermant-plugins/sermant-service-removal/removal-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer index 32962cdde1..93238d329a 100644 --- a/sermant-plugins/sermant-service-removal/removal-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer +++ b/sermant-plugins/sermant-service-removal/removal-plugin/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.agent.declarer.PluginDeclarer @@ -26,4 +26,5 @@ com.huaweicloud.sermant.declarer.SpringCloudServerListDeclarer com.huaweicloud.sermant.declarer.SpringBootCacheManagerDeclarer com.huaweicloud.sermant.declarer.SpringCloudReactiveDiscoveryDeclarer com.huaweicloud.sermant.declarer.SpringCloudDiscoveryDeclarer -com.huaweicloud.sermant.declarer.SpringCloudClientDeclarer \ No newline at end of file +com.huaweicloud.sermant.declarer.SpringCloudClientDeclarer +com.huaweicloud.sermant.declarer.SpringApplicationDeclarer \ No newline at end of file diff --git a/sermant-plugins/sermant-service-removal/removal-service/src/main/java/com/huaweicloud/sermant/service/RemovalConfigService.java b/sermant-plugins/sermant-service-removal/removal-service/src/main/java/com/huaweicloud/sermant/service/RemovalConfigService.java deleted file mode 100644 index 57e9dadd01..0000000000 --- a/sermant-plugins/sermant-service-removal/removal-service/src/main/java/com/huaweicloud/sermant/service/RemovalConfigService.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * 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.huaweicloud.sermant.service; - -import com.huaweicloud.sermant.config.RemovalDynamicConfigListener; -import com.huaweicloud.sermant.core.config.ConfigManager; -import com.huaweicloud.sermant.core.plugin.config.ServiceMeta; -import com.huaweicloud.sermant.core.plugin.service.PluginService; -import com.huaweicloud.sermant.core.service.ServiceManager; -import com.huaweicloud.sermant.core.service.dynamicconfig.DynamicConfigService; - -/** - * 配置监听服务 - * - * @author zhp - * @since 2023-04-04 - */ -public class RemovalConfigService implements PluginService { - private static final String APP_CODE = "app"; - - private static final String ENVIRONMENT_CODE = "environment"; - - private static final String CONNECTOR = "&"; - - private static final String EQUAL_SIGN = "="; - - private final ServiceMeta serviceMeta = ConfigManager.getConfig(ServiceMeta.class); - - @Override - public void start() { - String group = APP_CODE + EQUAL_SIGN + serviceMeta.getApplication() + CONNECTOR + ENVIRONMENT_CODE + EQUAL_SIGN - + serviceMeta.getEnvironment(); - DynamicConfigService dynamicConfigService = ServiceManager.getService(DynamicConfigService.class); - dynamicConfigService.addGroupListener(group, new RemovalDynamicConfigListener(), true); - } -} diff --git a/sermant-plugins/sermant-service-removal/removal-service/src/main/java/com/huaweicloud/sermant/service/RemovalConfigServiceImpl.java b/sermant-plugins/sermant-service-removal/removal-service/src/main/java/com/huaweicloud/sermant/service/RemovalConfigServiceImpl.java new file mode 100644 index 0000000000..6308a48f8e --- /dev/null +++ b/sermant-plugins/sermant-service-removal/removal-service/src/main/java/com/huaweicloud/sermant/service/RemovalConfigServiceImpl.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2023-2023 Huawei Technologies Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.huaweicloud.sermant.service; + +import com.huaweicloud.sermant.config.RemovalDynamicConfigListener; +import com.huaweicloud.sermant.core.plugin.subscribe.ConfigSubscriber; +import com.huaweicloud.sermant.core.plugin.subscribe.CseGroupConfigSubscriber; + +/** + * 配置监听服务 + * + * @author zhp + * @since 2023-04-04 + */ +public class RemovalConfigServiceImpl implements RemovalConfigService { + @Override + public void init() { + ConfigSubscriber subscriber = new CseGroupConfigSubscriber("default", new RemovalDynamicConfigListener(), + "service-removal"); + subscriber.subscribe(); + } +} diff --git a/sermant-plugins/sermant-service-removal/removal-service/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService b/sermant-plugins/sermant-service-removal/removal-service/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService index 5006099f03..74ec139e3d 100644 --- a/sermant-plugins/sermant-service-removal/removal-service/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService +++ b/sermant-plugins/sermant-service-removal/removal-service/src/main/resources/META-INF/services/com.huaweicloud.sermant.core.plugin.service.PluginService @@ -16,4 +16,4 @@ com.huaweicloud.sermant.service.RemovalEventServiceImpl com.huaweicloud.sermant.service.RequestDataCountTask -com.huaweicloud.sermant.service.RemovalConfigService \ No newline at end of file +com.huaweicloud.sermant.service.RemovalConfigServiceImpl \ No newline at end of file