spring-cloud / spring-cloud-netflix

Integration with Netflix OSS components
http://cloud.spring.io/spring-cloud-netflix/
Apache License 2.0
4.87k stars 2.44k forks source link

IRule get wrong service IP address #3400

Closed staticLin closed 5 years ago

staticLin commented 5 years ago

English is not my first language, I appreciate for your time to read this and if there anything I can do , please don’t hesitate contact me.I've got a problem in configuring ribbon rule,when i was used @configuration classes to configure my custom IRule bean,like @bean -> return new RandomRule,it will share common IRule instance in RibbonClientConfiguration when auto configure a ILoadBalancer to inject a IRule instance,it cause a problem when i use the Feign or LoadBalancerClient to initiate a service call third time that will get wrong service IP,get the 404 wrong code.This problem is because of ILoadBalancer instance in IRule,when the first time AService feign call come,will set A ILoadBalancer instance to IRule,second time BService feign call will set B ILoadBalancer instance to IRule,however the IRule is singleton,so next service call will get wrong service IP through the wrong ILoadBalancer in IRule.So i want to change the way that auto configure ILoadBalancer,make sure to inject different IRule instance each time.For now,change the custom IRule scope to prototype is also the way to fix it.

SpringCloud version:Greenwich.RELEASE

staticLin commented 5 years ago

@Configuration public class RibbonConfiguration {

@Bean
public IRule ribbonRule(IRule rule) {
    return new RandomRule();
}

}

@Autowired
private LoadBalancerClient loadBalancerClient;

@RequestMapping("/testRibbon")
public boolean testRibbon(){
    //second time call method testRibbon,the ServiceInstance will choose wrong service ip address
    ServiceInstance securityInstance = loadBalancerClient.choose("security-service");
    ServiceInstance roleInstance = loadBalancerClient.choose("role-service");
    return true;
}
ryanjbaxter commented 5 years ago

I am not sure I completely understand the problem.
Can you provide a complete, minimal, verifiable sample that reproduces the problem? It should be available as a GitHub (or similar) project or attached to this issue as a zip file. Hopefully that will make the problem easier to understand.

staticLin commented 5 years ago

I am not sure I completely understand the problem. Can you provide a complete, minimal, verifiable sample that reproduces the problem? It should be available as a GitHub (or similar) project or attached to this issue as a zip file. Hopefully that will make the problem easier to understand.

sure, the repo url is https://github.com/staticLin/sample.git

spencergibb commented 5 years ago

As I said, this is misconfigured.

Please look at https://cloud.spring.io/spring-cloud-static/Greenwich.RELEASE/single/spring-cloud.html#_customizing_the_default_for_all_ribbon_clients

You need to reference RibbonConfiguration in @RibbonClients

@RibbonClients(defaultConfiguration = RibbonConfiguration.class)

And either remove @Configuration from it or exclude it from component scanning.

staticLin commented 5 years ago

As I said, this is misconfigured.

Please look at https://cloud.spring.io/spring-cloud-static/Greenwich.RELEASE/single/spring-cloud.html#_customizing_the_default_for_all_ribbon_clients

You need to reference RibbonConfiguration in @RibbonClients

@RibbonClients(defaultConfiguration = RibbonConfiguration.class)

And either remove @Configuration from it or exclude it from component scanning.

i got it, thanks for your time

spencergibb commented 5 years ago

Let us know if it works

staticLin commented 5 years ago

Let us know if it works

it works well with @RibbonClients configuration. The key is the location that configure IRule Bean in the ApplicationContext.