feat: refactor Feign eager load, add LoadBalancer warm-up, and fix gateway trailing slash compatibility (#1800)
Signed-off-by: Haotian Zhang <928016560@qq.com>pull/1804/head
parent
0f2d7073a4
commit
fc8a4dbcd1
@ -0,0 +1,164 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 Tencent. 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.eager.instrument.feign;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Proxy;
|
||||||
|
import java.net.URI;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.eager.instrument.loadbalancer.LoadBalancerEagerLoadProperties;
|
||||||
|
import com.tencent.cloud.polaris.eager.instrument.loadbalancer.LoadBalancerWarmUpUtils;
|
||||||
|
import com.tencent.polaris.api.utils.StringUtils;
|
||||||
|
import feign.Target;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.aop.framework.AopProxy;
|
||||||
|
import org.springframework.aop.framework.JdkDynamicAopProxyUtils;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
|
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Feign eager load context initializer.
|
||||||
|
* Implements ApplicationListener<ApplicationReadyEvent> to warm up FeignClient services
|
||||||
|
* after the application is ready.
|
||||||
|
*
|
||||||
|
* @author Yuwei Fu
|
||||||
|
*/
|
||||||
|
public class FeignEagerLoadContextInitializer implements ApplicationListener<ApplicationReadyEvent> {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(FeignEagerLoadContextInitializer.class);
|
||||||
|
|
||||||
|
private final ApplicationContext applicationContext;
|
||||||
|
|
||||||
|
private final LoadBalancerClientFactory loadBalancerClientFactory;
|
||||||
|
|
||||||
|
private final LoadBalancerEagerLoadProperties loadBalancerEagerLoadProperties;
|
||||||
|
|
||||||
|
public FeignEagerLoadContextInitializer(ApplicationContext applicationContext,
|
||||||
|
LoadBalancerClientFactory loadBalancerClientFactory,
|
||||||
|
LoadBalancerEagerLoadProperties loadBalancerEagerLoadProperties) {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
this.loadBalancerClientFactory = loadBalancerClientFactory;
|
||||||
|
this.loadBalancerEagerLoadProperties = loadBalancerEagerLoadProperties;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Target.HardCodedTarget<?> getHardCodedTarget(Object proxy) {
|
||||||
|
try {
|
||||||
|
int count = 0;
|
||||||
|
Object invocationHandler = proxy;
|
||||||
|
// Avoid infinite loop
|
||||||
|
while (count++ < 100) {
|
||||||
|
invocationHandler = Proxy.getInvocationHandler(invocationHandler);
|
||||||
|
if (invocationHandler instanceof AopProxy) {
|
||||||
|
invocationHandler = JdkDynamicAopProxyUtils.getTarget(invocationHandler);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (Field field : invocationHandler.getClass().getDeclaredFields()) {
|
||||||
|
field.setAccessible(true);
|
||||||
|
Object fieldValue = field.get(invocationHandler);
|
||||||
|
if (fieldValue instanceof Target.HardCodedTarget) {
|
||||||
|
return (Target.HardCodedTarget<?>) fieldValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
if (LOG.isDebugEnabled()) {
|
||||||
|
LOG.debug("proxy:{}, getTarget failed.", proxy, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
|
||||||
|
LOG.info("feign eager-load start");
|
||||||
|
|
||||||
|
// Get services that are already warmed by PolarisLoadBalancerEagerContextInitializer
|
||||||
|
Set<String> skipServices = getLoadBalancerEagerLoadServices();
|
||||||
|
|
||||||
|
// Set to track already warmed services
|
||||||
|
Set<String> warmedServices = new HashSet<>();
|
||||||
|
|
||||||
|
// Warm up FeignClient services
|
||||||
|
for (Object bean : applicationContext.getBeansWithAnnotation(FeignClient.class).values()) {
|
||||||
|
try {
|
||||||
|
if (Proxy.isProxyClass(bean.getClass())) {
|
||||||
|
Target.HardCodedTarget<?> hardCodedTarget = getHardCodedTarget(bean);
|
||||||
|
if (hardCodedTarget != null) {
|
||||||
|
FeignClient feignClient = hardCodedTarget.type().getAnnotation(FeignClient.class);
|
||||||
|
// if feignClient contains url, it doesn't need to eager load.
|
||||||
|
if (StringUtils.isEmpty(feignClient.url())) {
|
||||||
|
// support variables and default values.
|
||||||
|
String url = hardCodedTarget.name();
|
||||||
|
// refer to FeignClientFactoryBean, convert to URL, then take the host as the service name.
|
||||||
|
if (!url.startsWith("http://") && !url.startsWith("https://")) {
|
||||||
|
url = "http://" + url;
|
||||||
|
}
|
||||||
|
String serviceName = URI.create(url).getHost();
|
||||||
|
|
||||||
|
// Skip if already warmed by PolarisLoadBalancerEagerContextInitializer
|
||||||
|
if (skipServices.contains(serviceName)) {
|
||||||
|
LOG.debug("[{}] skip eager-load, already configured in LoadBalancerEagerLoadProperties.clients", serviceName);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Skip if already warmed in this round
|
||||||
|
if (warmedServices.contains(serviceName)) {
|
||||||
|
LOG.debug("[{}] already warmed, skip.", serviceName);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG.info("[{}] eager-load start, feign name: {}", serviceName, hardCodedTarget.name());
|
||||||
|
LoadBalancerWarmUpUtils.warmUp(loadBalancerClientFactory, serviceName);
|
||||||
|
|
||||||
|
warmedServices.add(serviceName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
LOG.debug("[{}] eager-load failed.", bean, e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LOG.info("feign eager-load end");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get services configured in LoadBalancerEagerLoadProperties.
|
||||||
|
* These services are warmed by PolarisLoadBalancerEagerContextInitializer.
|
||||||
|
* @return set of service names to skip
|
||||||
|
*/
|
||||||
|
private Set<String> getLoadBalancerEagerLoadServices() {
|
||||||
|
Set<String> services = new HashSet<>();
|
||||||
|
if (loadBalancerEagerLoadProperties != null
|
||||||
|
&& loadBalancerEagerLoadProperties.isEnabled()
|
||||||
|
&& loadBalancerEagerLoadProperties.getClients() != null) {
|
||||||
|
services.addAll(loadBalancerEagerLoadProperties.getClients());
|
||||||
|
}
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,128 +0,0 @@
|
|||||||
/*
|
|
||||||
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
|
||||||
*
|
|
||||||
* Copyright (C) 2021 Tencent. 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.eager.instrument.feign;
|
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
|
||||||
import java.lang.reflect.Proxy;
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
import com.tencent.cloud.polaris.discovery.PolarisDiscoveryClient;
|
|
||||||
import com.tencent.cloud.polaris.discovery.reactive.PolarisReactiveDiscoveryClient;
|
|
||||||
import com.tencent.polaris.api.utils.StringUtils;
|
|
||||||
import feign.Target;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
import org.springframework.cloud.openfeign.FeignClient;
|
|
||||||
import org.springframework.context.ApplicationContext;
|
|
||||||
import org.springframework.context.SmartLifecycle;
|
|
||||||
|
|
||||||
public class FeignEagerLoadSmartLifecycle implements SmartLifecycle {
|
|
||||||
|
|
||||||
private static final Logger LOG = LoggerFactory.getLogger(FeignEagerLoadSmartLifecycle.class);
|
|
||||||
|
|
||||||
private final ApplicationContext applicationContext;
|
|
||||||
|
|
||||||
private final PolarisDiscoveryClient polarisDiscoveryClient;
|
|
||||||
|
|
||||||
private final PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient;
|
|
||||||
|
|
||||||
public FeignEagerLoadSmartLifecycle(ApplicationContext applicationContext, PolarisDiscoveryClient polarisDiscoveryClient,
|
|
||||||
PolarisReactiveDiscoveryClient polarisReactiveDiscoveryClient) {
|
|
||||||
this.applicationContext = applicationContext;
|
|
||||||
this.polarisDiscoveryClient = polarisDiscoveryClient;
|
|
||||||
this.polarisReactiveDiscoveryClient = polarisReactiveDiscoveryClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void start() {
|
|
||||||
LOG.info("feign eager-load start");
|
|
||||||
for (Object bean : applicationContext.getBeansWithAnnotation(FeignClient.class).values()) {
|
|
||||||
try {
|
|
||||||
if (Proxy.isProxyClass(bean.getClass())) {
|
|
||||||
Target.HardCodedTarget<?> hardCodedTarget = getHardCodedTarget(bean);
|
|
||||||
if (hardCodedTarget != null) {
|
|
||||||
FeignClient feignClient = hardCodedTarget.type().getAnnotation(FeignClient.class);
|
|
||||||
// if feignClient contains url, it doesn't need to eager load.
|
|
||||||
if (StringUtils.isEmpty(feignClient.url())) {
|
|
||||||
// support variables and default values.
|
|
||||||
String url = hardCodedTarget.name();
|
|
||||||
// refer to FeignClientFactoryBean, convert to URL, then take the host as the service name.
|
|
||||||
if (!url.startsWith("http://") && !url.startsWith("https://")) {
|
|
||||||
url = "http://" + url;
|
|
||||||
}
|
|
||||||
String serviceName = URI.create(url).getHost();
|
|
||||||
|
|
||||||
LOG.info("[{}] eager-load start", serviceName);
|
|
||||||
if (polarisDiscoveryClient != null) {
|
|
||||||
polarisDiscoveryClient.getInstances(serviceName);
|
|
||||||
}
|
|
||||||
else if (polarisReactiveDiscoveryClient != null) {
|
|
||||||
polarisReactiveDiscoveryClient.getInstances(serviceName).subscribe();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
LOG.warn("[{}] no discovery client found.", serviceName);
|
|
||||||
}
|
|
||||||
LOG.info("[{}] eager-load end", serviceName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
LOG.debug("[{}] eager-load failed.", bean, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
LOG.info("feign eager-load end");
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Target.HardCodedTarget<?> getHardCodedTarget(Object proxy) {
|
|
||||||
try {
|
|
||||||
Object invocationHandler = Proxy.getInvocationHandler(proxy);
|
|
||||||
|
|
||||||
for (Field field : invocationHandler.getClass().getDeclaredFields()) {
|
|
||||||
field.setAccessible(true);
|
|
||||||
Object fieldValue = field.get(invocationHandler);
|
|
||||||
if (fieldValue instanceof Target.HardCodedTarget) {
|
|
||||||
return (Target.HardCodedTarget<?>) fieldValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception e) {
|
|
||||||
if (LOG.isDebugEnabled()) {
|
|
||||||
LOG.debug("proxy:{}, getTarget failed.", proxy, e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void stop() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isRunning() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getPhase() {
|
|
||||||
return 10;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -0,0 +1,47 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 Tencent. 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.eager.instrument.loadbalancer;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
@ConfigurationProperties("spring.cloud.loadbalancer.eager-load")
|
||||||
|
public class LoadBalancerEagerLoadProperties {
|
||||||
|
|
||||||
|
private List<String> clients;
|
||||||
|
|
||||||
|
private boolean enabled = true;
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getClients() {
|
||||||
|
return clients;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setClients(List<String> clients) {
|
||||||
|
this.clients = clients;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 Tencent. 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.eager.instrument.loadbalancer;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
|
||||||
|
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Utility class for load balancer warm-up operations.
|
||||||
|
* Provides common warm-up logic for eager loading services.
|
||||||
|
*
|
||||||
|
* @author Yuwei Fu
|
||||||
|
*/
|
||||||
|
public final class LoadBalancerWarmUpUtils {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(LoadBalancerWarmUpUtils.class);
|
||||||
|
|
||||||
|
private LoadBalancerWarmUpUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Warm up a service by triggering load balancer initialization.
|
||||||
|
* @param factory the LoadBalancerClientFactory
|
||||||
|
* @param serviceName the service name to warm up
|
||||||
|
* @return true if warm-up succeeded, false otherwise
|
||||||
|
*/
|
||||||
|
public static boolean warmUp(LoadBalancerClientFactory factory, String serviceName) {
|
||||||
|
try {
|
||||||
|
ReactiveLoadBalancer<ServiceInstance> loadBalancer = factory.getInstance(serviceName);
|
||||||
|
if (loadBalancer != null) {
|
||||||
|
loadBalancer.choose();
|
||||||
|
LOG.info("[{}] eager-load end", serviceName);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
LOG.warn("[{}] no loadBalancer found.", serviceName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
LOG.debug("[{}] eager-load failed.", serviceName, e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,66 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 Tencent. 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.eager.instrument.loadbalancer;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import com.tencent.polaris.api.utils.CollectionUtils;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
|
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||||
|
import org.springframework.context.ApplicationListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Yuwei Fu
|
||||||
|
*/
|
||||||
|
public class PolarisLoadBalancerEagerContextInitializer implements ApplicationListener<ApplicationReadyEvent> {
|
||||||
|
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(PolarisLoadBalancerEagerContextInitializer.class);
|
||||||
|
|
||||||
|
private final LoadBalancerClientFactory factory;
|
||||||
|
|
||||||
|
private final List<String> serviceNames;
|
||||||
|
|
||||||
|
public PolarisLoadBalancerEagerContextInitializer(LoadBalancerClientFactory factory, List<String> serviceNames) {
|
||||||
|
this.factory = factory;
|
||||||
|
this.serviceNames = serviceNames;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
|
||||||
|
|
||||||
|
LOG.info("spring cloud eager-load start");
|
||||||
|
try {
|
||||||
|
if (!CollectionUtils.isEmpty(serviceNames)) {
|
||||||
|
for (String serviceName : serviceNames) {
|
||||||
|
LoadBalancerWarmUpUtils.warmUp(factory, serviceName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LOG.info("spring cloud eager-load end");
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
if (LOG.isDebugEnabled()) {
|
||||||
|
LOG.debug("spring cloud eager-load failed.", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,50 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 Tencent. 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 org.springframework.aop.framework;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
public final class JdkDynamicAopProxyUtils {
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(JdkDynamicAopProxyUtils.class);
|
||||||
|
|
||||||
|
private JdkDynamicAopProxyUtils() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Object getTarget(Object invocationHandler) {
|
||||||
|
if (invocationHandler instanceof JdkDynamicAopProxy) {
|
||||||
|
try {
|
||||||
|
JdkDynamicAopProxy jdkDynamicAopProxy = (JdkDynamicAopProxy) invocationHandler;
|
||||||
|
Field advisedField = JdkDynamicAopProxy.class.getDeclaredField("advised");
|
||||||
|
advisedField.setAccessible(true);
|
||||||
|
AdvisedSupport advisedSupport = (AdvisedSupport) advisedField.get(jdkDynamicAopProxy);
|
||||||
|
|
||||||
|
if (advisedSupport != null && advisedSupport.getTargetSource() != null) {
|
||||||
|
return advisedSupport.getTargetSource().getTarget();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
LOGGER.error("Unexpected error occurred while getting target from JdkDynamicAopProxy", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,249 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 Tencent. 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.eager.instrument.feign;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.eager.instrument.loadbalancer.LoadBalancerEagerLoadProperties;
|
||||||
|
import com.tencent.cloud.polaris.registry.PolarisAutoServiceRegistration;
|
||||||
|
import org.aspectj.lang.ProceedingJoinPoint;
|
||||||
|
import org.aspectj.lang.annotation.Around;
|
||||||
|
import org.aspectj.lang.annotation.Aspect;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
|
||||||
|
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||||
|
import org.springframework.cloud.openfeign.EnableFeignClients;
|
||||||
|
import org.springframework.cloud.openfeign.FeignClient;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.never;
|
||||||
|
import static org.mockito.Mockito.reset;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link FeignEagerLoadContextInitializer}.
|
||||||
|
*
|
||||||
|
* @author Test Author
|
||||||
|
*/
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest(webEnvironment = DEFINED_PORT,
|
||||||
|
classes = FeignEagerLoadContextInitializerTest.TestApplication.class,
|
||||||
|
properties = {
|
||||||
|
"server.port=48085",
|
||||||
|
"spring.config.location = classpath:application-test.yml",
|
||||||
|
"spring.main.web-application-type = servlet",
|
||||||
|
"spring.cloud.gateway.enabled = false",
|
||||||
|
"spring.cloud.polaris.discovery.eager-load.enabled = true",
|
||||||
|
"spring.cloud.polaris.discovery.eager-load.feign.enabled = true",
|
||||||
|
"spring.cloud.loadbalancer.eager-load.enabled = true",
|
||||||
|
"spring.cloud.loadbalancer.eager-load.clients = test-service-1,test-service-2,test-feign-service-skip"
|
||||||
|
})
|
||||||
|
public class FeignEagerLoadContextInitializerTest {
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private LoadBalancerClientFactory loadBalancerClientFactory;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private FeignEagerLoadContextInitializer feignEagerLoadContextInitializer;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private LoadBalancerEagerLoadProperties loadBalancerEagerLoadProperties;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private ConfigurableApplicationContext applicationContext;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
reset(loadBalancerClientFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLoadBalancerEagerLoadPropertiesLoaded() {
|
||||||
|
// Verify LoadBalancerEagerLoadProperties is loaded correctly
|
||||||
|
assertThat(loadBalancerEagerLoadProperties.getClients())
|
||||||
|
.isNotNull()
|
||||||
|
.containsExactlyInAnyOrder("test-service-1", "test-service-2", "test-feign-service-skip");
|
||||||
|
assertThat(loadBalancerEagerLoadProperties.isEnabled()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFeignClient() {
|
||||||
|
// Prepare mock
|
||||||
|
ReactiveLoadBalancer<ServiceInstance> mockLoadBalancer = mock(ReactiveLoadBalancer.class);
|
||||||
|
when(loadBalancerClientFactory.getInstance(anyString())).thenReturn(mockLoadBalancer);
|
||||||
|
|
||||||
|
// Execute warm-up by triggering ApplicationReadyEvent
|
||||||
|
feignEagerLoadContextInitializer.onApplicationEvent(mock(ApplicationReadyEvent.class));
|
||||||
|
|
||||||
|
// Verify services in LoadBalancerEagerLoadProperties.clients are NOT warmed (skipped)
|
||||||
|
// because they are handled by PolarisLoadBalancerEagerContextInitializer
|
||||||
|
verify(loadBalancerClientFactory, never()).getInstance("test-service-1");
|
||||||
|
verify(loadBalancerClientFactory, never()).getInstance("test-service-2");
|
||||||
|
|
||||||
|
// Verify FeignClient services are warmed (without url)
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-feign-service");
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-feign-service-http");
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-feign-service-https");
|
||||||
|
|
||||||
|
// Verify FeignClient with url is NOT warmed
|
||||||
|
verify(loadBalancerClientFactory, never()).getInstance("localhost:48085");
|
||||||
|
|
||||||
|
// Verify choose method is called
|
||||||
|
verify(mockLoadBalancer, times(3)).choose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSkipServicesInLoadBalancerEagerLoadProperties() {
|
||||||
|
// Prepare mock
|
||||||
|
ReactiveLoadBalancer<ServiceInstance> mockLoadBalancer = mock(ReactiveLoadBalancer.class);
|
||||||
|
when(loadBalancerClientFactory.getInstance(anyString())).thenReturn(mockLoadBalancer);
|
||||||
|
|
||||||
|
// Execute warm-up by triggering ApplicationReadyEvent
|
||||||
|
feignEagerLoadContextInitializer.onApplicationEvent(mock(ApplicationReadyEvent.class));
|
||||||
|
|
||||||
|
// Verify services configured in LoadBalancerEagerLoadProperties.clients are skipped
|
||||||
|
// test-feign-service-skip is configured in LoadBalancerEagerLoadProperties.clients
|
||||||
|
verify(loadBalancerClientFactory, never()).getInstance("test-feign-service-skip");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFeignClientWithHttpPrefix() {
|
||||||
|
// Prepare mock
|
||||||
|
ReactiveLoadBalancer<ServiceInstance> mockLoadBalancer = mock(ReactiveLoadBalancer.class);
|
||||||
|
when(loadBalancerClientFactory.getInstance(anyString())).thenReturn(mockLoadBalancer);
|
||||||
|
|
||||||
|
// Execute warm-up by triggering ApplicationReadyEvent
|
||||||
|
feignEagerLoadContextInitializer.onApplicationEvent(mock(ApplicationReadyEvent.class));
|
||||||
|
|
||||||
|
// Verify service name is correctly extracted from http:// prefix
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-feign-service-http");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFeignClientWithHttpsPrefix() {
|
||||||
|
// Prepare mock
|
||||||
|
ReactiveLoadBalancer<ServiceInstance> mockLoadBalancer = mock(ReactiveLoadBalancer.class);
|
||||||
|
when(loadBalancerClientFactory.getInstance(anyString())).thenReturn(mockLoadBalancer);
|
||||||
|
|
||||||
|
// Execute warm-up by triggering ApplicationReadyEvent
|
||||||
|
feignEagerLoadContextInitializer.onApplicationEvent(mock(ApplicationReadyEvent.class));
|
||||||
|
|
||||||
|
// Verify service name is correctly extracted from https:// prefix
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-feign-service-https");
|
||||||
|
}
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableFeignClients
|
||||||
|
@RestController
|
||||||
|
protected static class TestApplication {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public TestBeanPostProcessor testBeanPostProcessor() {
|
||||||
|
return new TestBeanPostProcessor();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public FeignAspect feignAspect() {
|
||||||
|
return new FeignAspect();
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping("/test")
|
||||||
|
public String test() {
|
||||||
|
return "test";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Normal FeignClient (without url)
|
||||||
|
@FeignClient(name = "test-feign-service")
|
||||||
|
public interface TestFeignClient {
|
||||||
|
@RequestMapping("/test")
|
||||||
|
String test();
|
||||||
|
}
|
||||||
|
|
||||||
|
// FeignClient with http:// prefix
|
||||||
|
@FeignClient(name = "http://test-feign-service-http")
|
||||||
|
public interface TestFeignClientWithHttp {
|
||||||
|
@RequestMapping("/test")
|
||||||
|
String test();
|
||||||
|
}
|
||||||
|
|
||||||
|
// FeignClient with https:// prefix
|
||||||
|
@FeignClient(name = "https://test-feign-service-https")
|
||||||
|
public interface TestFeignClientWithHttps {
|
||||||
|
@RequestMapping("/test")
|
||||||
|
String test();
|
||||||
|
}
|
||||||
|
|
||||||
|
// FeignClient with url (should skip warm-up)
|
||||||
|
@FeignClient(name = "test-feign-with-url", url = "http://localhost:48085")
|
||||||
|
public interface TestFeignClientWithUrl {
|
||||||
|
@RequestMapping("/test")
|
||||||
|
String test();
|
||||||
|
}
|
||||||
|
|
||||||
|
// FeignClient that is also in LoadBalancerEagerLoadProperties.clients
|
||||||
|
// This service should be skipped in FeignEagerLoadContextInitializer
|
||||||
|
@FeignClient(name = "test-feign-service-skip")
|
||||||
|
public interface TestFeignClientSkip {
|
||||||
|
@RequestMapping("/test")
|
||||||
|
String test();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static class TestBeanPostProcessor implements BeanPostProcessor {
|
||||||
|
@Override
|
||||||
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||||
|
if (bean instanceof PolarisAutoServiceRegistration) {
|
||||||
|
return org.mockito.Mockito.mock(PolarisAutoServiceRegistration.class);
|
||||||
|
}
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Aspect
|
||||||
|
static class FeignAspect {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(FeignAspect.class);
|
||||||
|
|
||||||
|
@Around("@within(org.springframework.cloud.openfeign.FeignClient)")
|
||||||
|
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||||
|
LOG.info("FeignAspect around");
|
||||||
|
return joinPoint.proceed();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,164 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 Tencent. 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.eager.instrument.loadbalancer;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.registry.PolarisAutoServiceRegistration;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
|
||||||
|
import org.springframework.beans.BeansException;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
||||||
|
import org.springframework.cloud.client.ServiceInstance;
|
||||||
|
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
|
||||||
|
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.reset;
|
||||||
|
import static org.mockito.Mockito.times;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.DEFINED_PORT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link PolarisLoadBalancerEagerContextInitializer}.
|
||||||
|
*
|
||||||
|
* @author Test Author
|
||||||
|
*/
|
||||||
|
@ExtendWith(SpringExtension.class)
|
||||||
|
@SpringBootTest(webEnvironment = DEFINED_PORT,
|
||||||
|
classes = PolarisLoadBalancerEagerContextInitializerTest.TestApplication.class,
|
||||||
|
properties = {
|
||||||
|
"server.port=48086",
|
||||||
|
"spring.config.location = classpath:application-test.yml",
|
||||||
|
"spring.main.web-application-type = servlet",
|
||||||
|
"spring.cloud.gateway.enabled = false",
|
||||||
|
"spring.cloud.polaris.discovery.eager-load.enabled = true",
|
||||||
|
"spring.cloud.loadbalancer.eager-load.enabled = true",
|
||||||
|
"spring.cloud.loadbalancer.eager-load.clients = test-service-1,test-service-2,test-service-3"
|
||||||
|
})
|
||||||
|
public class PolarisLoadBalancerEagerContextInitializerTest {
|
||||||
|
|
||||||
|
@MockBean
|
||||||
|
private LoadBalancerClientFactory loadBalancerClientFactory;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private PolarisLoadBalancerEagerContextInitializer polarisLoadBalancerEagerContextInitializer;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private LoadBalancerEagerLoadProperties loadBalancerEagerLoadProperties;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void setUp() {
|
||||||
|
reset(loadBalancerClientFactory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLoadBalancerEagerLoadPropertiesLoaded() {
|
||||||
|
// Verify LoadBalancerEagerLoadProperties is loaded correctly
|
||||||
|
assertThat(loadBalancerEagerLoadProperties.getClients())
|
||||||
|
.isNotNull()
|
||||||
|
.containsExactlyInAnyOrder("test-service-1", "test-service-2", "test-service-3");
|
||||||
|
assertThat(loadBalancerEagerLoadProperties.isEnabled()).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWarmUpServices() {
|
||||||
|
// Prepare mock
|
||||||
|
ReactiveLoadBalancer<ServiceInstance> mockLoadBalancer = mock(ReactiveLoadBalancer.class);
|
||||||
|
when(loadBalancerClientFactory.getInstance(anyString())).thenReturn(mockLoadBalancer);
|
||||||
|
|
||||||
|
// Execute warm-up by triggering ApplicationReadyEvent
|
||||||
|
polarisLoadBalancerEagerContextInitializer.onApplicationEvent(mock(ApplicationReadyEvent.class));
|
||||||
|
|
||||||
|
// Verify all services in LoadBalancerEagerLoadProperties.clients are warmed
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-service-1");
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-service-2");
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-service-3");
|
||||||
|
|
||||||
|
// Verify choose method is called for each service
|
||||||
|
verify(mockLoadBalancer, times(3)).choose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWarmUpWithNullLoadBalancer() {
|
||||||
|
// Prepare mock - return null for some services
|
||||||
|
when(loadBalancerClientFactory.getInstance("test-service-1")).thenReturn(null);
|
||||||
|
ReactiveLoadBalancer<ServiceInstance> mockLoadBalancer = mock(ReactiveLoadBalancer.class);
|
||||||
|
when(loadBalancerClientFactory.getInstance("test-service-2")).thenReturn(mockLoadBalancer);
|
||||||
|
when(loadBalancerClientFactory.getInstance("test-service-3")).thenReturn(mockLoadBalancer);
|
||||||
|
|
||||||
|
// Execute warm-up by triggering ApplicationReadyEvent
|
||||||
|
polarisLoadBalancerEagerContextInitializer.onApplicationEvent(mock(ApplicationReadyEvent.class));
|
||||||
|
|
||||||
|
// Verify all services are attempted
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-service-1");
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-service-2");
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-service-3");
|
||||||
|
|
||||||
|
// Verify choose method is only called for non-null load balancers
|
||||||
|
verify(mockLoadBalancer, times(2)).choose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testWarmUpWithException() {
|
||||||
|
// Prepare mock - throw exception for some services
|
||||||
|
when(loadBalancerClientFactory.getInstance("test-service-1"))
|
||||||
|
.thenThrow(new RuntimeException("Test exception"));
|
||||||
|
ReactiveLoadBalancer<ServiceInstance> mockLoadBalancer = mock(ReactiveLoadBalancer.class);
|
||||||
|
when(loadBalancerClientFactory.getInstance("test-service-2")).thenReturn(mockLoadBalancer);
|
||||||
|
when(loadBalancerClientFactory.getInstance("test-service-3")).thenReturn(mockLoadBalancer);
|
||||||
|
|
||||||
|
// Execute warm-up by triggering ApplicationReadyEvent
|
||||||
|
polarisLoadBalancerEagerContextInitializer.onApplicationEvent(mock(ApplicationReadyEvent.class));
|
||||||
|
|
||||||
|
// Verify all services are attempted (exception should not stop the loop)
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-service-1");
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-service-2");
|
||||||
|
verify(loadBalancerClientFactory, times(1)).getInstance("test-service-3");
|
||||||
|
}
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
protected static class TestApplication {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public TestBeanPostProcessor testBeanPostProcessor() {
|
||||||
|
return new TestBeanPostProcessor();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
static class TestBeanPostProcessor implements BeanPostProcessor {
|
||||||
|
@Override
|
||||||
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||||
|
if (bean instanceof PolarisAutoServiceRegistration) {
|
||||||
|
return org.mockito.Mockito.mock(PolarisAutoServiceRegistration.class);
|
||||||
|
}
|
||||||
|
return bean;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
spring-cloud-tencent-plugin-starters/spring-cloud-tencent-unit-plugin/src/main/java/com/tencent/cloud/plugin/unit/discovery/UnitFeignEagerLoadSmartLifecycle.java → spring-cloud-tencent-plugin-starters/spring-cloud-tencent-unit-plugin/src/main/java/com/tencent/cloud/plugin/unit/discovery/UnitFeignEagerLoadContextInitializer.java
16
spring-cloud-tencent-plugin-starters/spring-cloud-tencent-unit-plugin/src/main/java/com/tencent/cloud/plugin/unit/discovery/UnitFeignEagerLoadSmartLifecycle.java → spring-cloud-tencent-plugin-starters/spring-cloud-tencent-unit-plugin/src/main/java/com/tencent/cloud/plugin/unit/discovery/UnitFeignEagerLoadContextInitializer.java
@ -0,0 +1,42 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 Tencent. 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.unit.discovery;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.eager.instrument.loadbalancer.PolarisLoadBalancerEagerContextInitializer;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unit LoadBalancer eager load context initializer.
|
||||||
|
* Ignores loadbalancer eager load in unit mode.
|
||||||
|
*/
|
||||||
|
public class UnitLoadBalancerEagerContextInitializer extends PolarisLoadBalancerEagerContextInitializer {
|
||||||
|
|
||||||
|
private static final Logger LOG = LoggerFactory.getLogger(UnitLoadBalancerEagerContextInitializer.class);
|
||||||
|
|
||||||
|
public UnitLoadBalancerEagerContextInitializer() {
|
||||||
|
super(null, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
|
||||||
|
LOG.info("ignore loadbalancer eager load in unit mode");
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in new issue