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

【enhance】支持springboot war包方式启动 #975

Merged
merged 1 commit into from
Nov 25, 2022
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,6 +132,9 @@ private void check(long maxWaitTimeMs, long sleepTimeMs, Supplier<Boolean> check
// ignored
}
}
if (!checkFunc.get()) {
LOGGER.error("=======配置中心配置内容: [{}]==================", JSONObject.toJSONString(kieClient.query(null)));
}
Assert.assertTrue(checkFunc.get());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,29 @@
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;
}

@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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,21 @@

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;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.util.Collections;
import java.util.concurrent.atomic.AtomicBoolean;

/**
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";
Expand Down Expand Up @@ -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);
}
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
com.huaweicloud.sermant.router.dubbo.declarer.SpringApplicationDeclarer
Original file line number Diff line number Diff line change
Expand Up @@ -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)
};
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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;
}
}
}
Loading