[tag:`Working on upgrading gateway project to spring cloud - 2.7.12 and Spring Cloud 2021.0.7 but due to this netflix -spring libraries are upgrading and EurekaRibbonClient Configuration class is removed . Now Getting Errors : 2024-02-16 14:50:33 [http-nio-8888-exec-8] [] [] ERROR c.s.g.z.f.p.DefaultZuulFallbackProvider - Root cause of internal server error in Zuul Fallback provider - com.netflix.client.ClientException: Load balancer does not have available server for client: backuprestore
My own ribbonconfig class
package com.netflix.loadbalancer;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method;
import com.netflix.appinfo.EurekaInstanceConfig; import com.netflix.discovery.EurekaClientConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.loadbalancer.config.LoadBalancerZoneConfig; import org.springframework.cloud.netflix.eureka.loadbalancer.EurekaLoadBalancerProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.StringUtils;
import com.netflix.client.ClientFactory; import com.netflix.client.config.CommonClientConfigKey; import com.netflix.client.config.IClientConfig;
/**
This is the default Ribbon configuration for all Ribbon clients. Routing rules and Load balancer
to be used by Ribbon can be configured.
*/ @Configuration @EnableConfigurationProperties public class GatewayRibbonConfig extends EurekaRibbonClientConfiguration {
private static final Logger logger = LoggerFactory.getLogger(GatewayRibbonConfig.class);
@Value("${zuul.ribbonRuleClass:#{null}}")
private String ribbonRuleClassName;
@Value("${zuul.ribbonLoadBalancerClass:#{null}}")
private String ribbonLoadBalancerClassName;
/**
* Ribbon rule to be used for load balancing and filtering servers
* @param config
* @return
* @throws ClassNotFoundException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@ConditionalOnMissingBean
@Bean
public IRule ribbonRule(IClientConfig config){
IRule rule = null;
String ruleClassName = ribbonRuleClassName;
if (StringUtils.isEmpty(ruleClassName)) {
ruleClassName = config.get(CommonClientConfigKey.NFLoadBalancerRuleClassName);
}
if (StringUtils.isEmpty(ruleClassName)) {
throw new IllegalArgumentException("NFLoadBalancerRuleClassName is not specified in the IClientConfig");
}
Class clazz = null;
try {
clazz = Class.forName(ruleClassName);
if (clazz.getConstructor(IClientConfig.class) != null) {
try {
return (IRule) clazz.getConstructor(IClientConfig.class).newInstance(config);
}catch (InstantiationException e) {
logger.warn("Error getting/invoking IClientConfig constructor of {}", ruleClassName, e);
} catch (IllegalAccessException e) {
logger.warn("Error getting/invoking IClientConfig constructor of {}", ruleClassName, e);
}
}
} catch (NoSuchMethodException ignored) {
// OK for a class to not take an IClientConfig, I
} catch (SecurityException | IllegalArgumentException | InvocationTargetException | ClassNotFoundException e) {
logger.warn("Error getting/invoking IClientConfig constructor of {}", ruleClassName, e);
}
try {
if (clazz.getConstructor() != null) {
rule = (IRule) clazz.getConstructor().newInstance();
try {
Method method = clazz.getMethod("initWithNiwsConfig", IClientConfig.class);
method.invoke(rule, config);
} catch (NoSuchMethodException igonore) {
// can be ignored
} catch( SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
logger.warn("Error invoking initWithNiwsConfig method of {}", ruleClassName, e);
throw new RuntimeException("Error while initilaizing rule " + ruleClassName,e) ;
}
}
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
| IllegalArgumentException | InvocationTargetException e) {
logger.error("Error getting/invoking no-arg constructor of {}", ruleClassName, e);
throw new RuntimeException("Error while initilaizing rule " + ruleClassName,e) ;
}
return rule;
}
/*@SuppressWarnings("deprecation")
@Bean
@ConditionalOnMissingBean
public ServerList<?> ribbonServerList(IClientConfig config) {
return new com.netflix.niws.loadbalancer.DiscoveryEnabledNIWSServerList(config);
}*/
/**
* Load balancer to be used by ribbon
* @param config
* @param serverList
* @param serverListFilter
* @param rule
* @param ping
* @param serverListUpdater
* @return
* @throws ClassNotFoundException
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
@Bean
@ConditionalOnMissingBean
public ILoadBalancer loadBalancer(IClientConfig config,
ServerList<Server> serverList,
ServerListFilter<Server> serverListFilter,
IRule rule, IPing ping,
ServerListUpdater serverListUpdater) throws ClassNotFoundException {
String loadBalancerClassName = ribbonLoadBalancerClassName;
if (StringUtils.isEmpty(loadBalancerClassName)) {
loadBalancerClassName = config.get(CommonClientConfigKey.NFLoadBalancerClassName);
}
if (StringUtils.isEmpty(loadBalancerClassName)) {
throw new IllegalArgumentException("NFLoadBalancerClassName is not specified in the IClientConfig");
}
Class clazz = Class.forName(loadBalancerClassName);
try {
if (clazz.getConstructor(IClientConfig.class,IRule.class,IPing.class,ServerList.class,ServerListFilter.class,ServerListUpdater.class) != null) {
try {
return (ILoadBalancer) clazz.getConstructor(IClientConfig.class,IRule.class,IPing.class,ServerList.class,ServerListFilter.class,ServerListUpdater.class).newInstance(config, rule, ping, serverList,
serverListFilter, serverListUpdater);
} catch (InstantiationException e) {
logger.warn("Error getting/invoking IClientConfig,IRule, IPing, ServerList,ServerListFilter,ServerListUpdater constructor of {}", loadBalancerClassName, e);
} catch (IllegalAccessException e) {
logger.warn("Error getting/invoking IClientConfig,IRule, IPing, ServerList,ServerListFilter,ServerListUpdater constructor of {}", loadBalancerClassName, e);
}
}
} catch (NoSuchMethodException ignored) {
// OK for a class to not take an IClientConfig, I
} catch (SecurityException | IllegalArgumentException | InvocationTargetException e) {
logger.warn("Error getting/invoking IClientConfig,IRule, IPing, ServerList,ServerListFilter,ServerListUpdater constructor of {}", loadBalancerClassName, e);
}
ILoadBalancer lb;
try {
lb = (ILoadBalancer) ClientFactory.instantiateInstanceWithClientConfig(loadBalancerClassName, config);
} catch (Exception e) {
throw new RuntimeException(e);
}
return lb;
//return new DynamicServerListLoadBalancer<>(config, rule, ping, serverList,
// serverListFilter, serverListUpdater);
}
}
snapshot of dependencies
buildscript { ext { springBootVersion = '2.7.12' springfoxSwaggerVersion = '2.9.2' jacocoVersion = '0.8.9' swaggerVersion= '2.4.12' }
dependencyManagement { imports { mavenBom 'org.springframework.cloud:spring-cloud-dependencies:2021.0.7' } }
implementation ("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client:2.2.10.RELEASE")
implementation ("org.springframework.cloud:spring-cloud-starter-netflix-zuul:2.2.10.RELEASE")
implementation ("org.springframework.cloud:spring-cloud-starter-netflix-hystrix:2.2.10.RELEASE")
implementation group: 'org.springframework.cloud', name: 'spring-cloud-starter-netflix-ribbon', version: '2`.2.10.RELEASE'`
Trying to upgrade java and gradle