feat: support lossless config from console & support warmup (#1435)
parent
ee94dc5121
commit
22b2300d5f
@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 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.plugin.lossless.config;
|
||||
|
||||
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
|
||||
import com.tencent.polaris.api.config.consumer.WeightAdjustConfig;
|
||||
import com.tencent.polaris.plugin.lossless.warmup.WarmupWeightAdjuster;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Test for {@link WarmupConfigModifier}.
|
||||
*
|
||||
* @author Shedfree Wu
|
||||
*/
|
||||
public class WarmupConfigModifierTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(TestApplication.class))
|
||||
.withPropertyValues("spring.cloud.nacos.discovery.enabled=false")
|
||||
.withPropertyValues("spring.cloud.polaris.enabled=true")
|
||||
.withPropertyValues("spring.cloud.polaris.warmup.enabled=true")
|
||||
.withPropertyValues("spring.application.name=test")
|
||||
.withPropertyValues("spring.cloud.gateway.enabled=false");
|
||||
private final ApplicationContextRunner disabledContextRunner = new ApplicationContextRunner()
|
||||
.withConfiguration(AutoConfigurations.of(TestApplication.class))
|
||||
.withPropertyValues("spring.cloud.nacos.discovery.enabled=false")
|
||||
.withPropertyValues("spring.cloud.polaris.enabled=true")
|
||||
.withPropertyValues("spring.cloud.polaris.warmup.enabled=false")
|
||||
.withPropertyValues("spring.application.name=test")
|
||||
.withPropertyValues("spring.cloud.gateway.enabled=false");
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
PolarisSDKContextManager.innerDestroy();
|
||||
}
|
||||
|
||||
@Test
|
||||
void testModify() {
|
||||
contextRunner.run(context -> {
|
||||
PolarisSDKContextManager polarisSDKContextManager = context.getBean(PolarisSDKContextManager.class);
|
||||
WeightAdjustConfig weightAdjustConfig = polarisSDKContextManager.getSDKContext().
|
||||
getConfig().getConsumer().getWeightAdjust();
|
||||
assertThat(weightAdjustConfig.isEnable()).isTrue();
|
||||
assertThat(weightAdjustConfig.getChain().contains(WarmupWeightAdjuster.WARMUP_WEIGHT_ADJUSTER_NAME)).isTrue();
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
void testDisabled() {
|
||||
disabledContextRunner.run(context -> {
|
||||
PolarisSDKContextManager polarisSDKContextManager = context.getBean(PolarisSDKContextManager.class);
|
||||
WeightAdjustConfig weightAdjustConfig = polarisSDKContextManager.getSDKContext().
|
||||
getConfig().getConsumer().getWeightAdjust();
|
||||
assertThat(weightAdjustConfig.isEnable()).isTrue();
|
||||
assertThat(weightAdjustConfig.getChain().contains(WarmupWeightAdjuster.WARMUP_WEIGHT_ADJUSTER_NAME)).isFalse();
|
||||
});
|
||||
}
|
||||
|
||||
@SpringBootApplication
|
||||
protected static class TestApplication {
|
||||
|
||||
}
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 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.plugin.lossless;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.tencent.cloud.common.util.OkHttpUtil;
|
||||
import com.tencent.cloud.plugin.lossless.config.LosslessProperties;
|
||||
import com.tencent.cloud.rpc.enhancement.transformer.RegistrationTransformer;
|
||||
import com.tencent.polaris.api.plugin.lossless.InstanceProperties;
|
||||
import com.tencent.polaris.api.plugin.lossless.LosslessActionProvider;
|
||||
import com.tencent.polaris.api.pojo.BaseInstance;
|
||||
import com.tencent.polaris.api.utils.StringUtils;
|
||||
|
||||
import org.springframework.cloud.client.serviceregistry.Registration;
|
||||
import org.springframework.cloud.client.serviceregistry.ServiceRegistry;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
|
||||
/**
|
||||
* LosslessActionProvider for Spring Cloud.
|
||||
*
|
||||
* @author Shedfree Wu
|
||||
*/
|
||||
public class SpringCloudLosslessActionProvider implements LosslessActionProvider {
|
||||
private ServiceRegistry<Registration> serviceRegistry;
|
||||
|
||||
private LosslessProperties losslessProperties;
|
||||
|
||||
private Runnable originalRegisterAction;
|
||||
|
||||
private Registration registration;
|
||||
|
||||
public SpringCloudLosslessActionProvider(ServiceRegistry<Registration> serviceRegistry, Registration registration,
|
||||
LosslessProperties losslessProperties, Runnable originalRegisterAction) {
|
||||
this.serviceRegistry = serviceRegistry;
|
||||
this.registration = registration;
|
||||
this.losslessProperties = losslessProperties;
|
||||
this.originalRegisterAction = originalRegisterAction;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "spring-cloud";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doRegister(InstanceProperties instanceProperties) {
|
||||
// use lambda to do original register
|
||||
originalRegisterAction.run();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doDeregister() {
|
||||
serviceRegistry.deregister(registration);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check whether health check is enable.
|
||||
* @return true: register after passing doHealthCheck, false: register after delayRegisterInterval.
|
||||
*/
|
||||
@Override
|
||||
public boolean isEnableHealthCheck() {
|
||||
return StringUtils.isNotBlank(losslessProperties.getHealthCheckPath());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean doHealthCheck() {
|
||||
Map<String, String> headers = new HashMap<>(1);
|
||||
headers.put(HttpHeaders.USER_AGENT, "polaris");
|
||||
|
||||
return OkHttpUtil.checkUrl("localhost", registration.getPort(),
|
||||
losslessProperties.getHealthCheckPath(), headers);
|
||||
}
|
||||
|
||||
public static BaseInstance getBaseInstance(Registration registration, RegistrationTransformer registrationTransformer) {
|
||||
return registrationTransformer.transform(registration);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 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.plugin.lossless.config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import com.tencent.cloud.common.constant.OrderConstant.Modifier;
|
||||
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
|
||||
import com.tencent.polaris.factory.config.ConfigurationImpl;
|
||||
import com.tencent.polaris.factory.config.consumer.WeightAdjustConfigImpl;
|
||||
import com.tencent.polaris.plugin.lossless.warmup.WarmupWeightAdjuster;
|
||||
|
||||
/**
|
||||
* Config modifier for warmup.
|
||||
*
|
||||
* @author Shedfree Wu
|
||||
*/
|
||||
public class WarmupConfigModifier implements PolarisConfigModifier {
|
||||
|
||||
private final WarmupProperties warmupProperties;
|
||||
|
||||
public WarmupConfigModifier(WarmupProperties warmupProperties) {
|
||||
this.warmupProperties = warmupProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void modify(ConfigurationImpl configuration) {
|
||||
WeightAdjustConfigImpl weightAdjustConfig = (WeightAdjustConfigImpl) configuration.getConsumer().getWeightAdjust();
|
||||
if (warmupProperties.isEnabled()) {
|
||||
Set<String> chainSet = new TreeSet<>(
|
||||
Optional.ofNullable(weightAdjustConfig.getChain()).orElse(Collections.emptyList()));
|
||||
chainSet.add(WarmupWeightAdjuster.WARMUP_WEIGHT_ADJUSTER_NAME);
|
||||
weightAdjustConfig.setChain(new ArrayList<>(chainSet));
|
||||
}
|
||||
else {
|
||||
Set<String> chainSet = new TreeSet<>(
|
||||
Optional.ofNullable(weightAdjustConfig.getChain()).orElse(Collections.emptyList()));
|
||||
chainSet.remove(WarmupWeightAdjuster.WARMUP_WEIGHT_ADJUSTER_NAME);
|
||||
weightAdjustConfig.setChain(new ArrayList<>(chainSet));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return Modifier.LOSSLESS_ORDER;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making Spring Cloud Tencent available.
|
||||
*
|
||||
* Copyright (C) 2019 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.plugin.lossless.config;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
@ConfigurationProperties("spring.cloud.polaris.warmup")
|
||||
public class WarmupProperties {
|
||||
|
||||
private boolean enabled = false;
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue