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

Eagerly create RibbonClients #1334

Closed
JakobFels opened this issue Sep 12, 2016 · 8 comments
Closed

Eagerly create RibbonClients #1334

JakobFels opened this issue Sep 12, 2016 · 8 comments

Comments

@JakobFels
Copy link

From what I've understood, each Ribbon Client created by Spring basically gets it's own ApplicationContext.
This context is created when the client is first used, which makes the very first request rather slow.
Is it possible to eagerly create these clients/contexts?

@spencergibb
Copy link
Member

@KamikaZeeFu not currently. In fact, we've had problems when feign clients (similar to ribbon clients) get eagerly created. Besides the first request is slow, are you experiencing problems?

@jansyk13
Copy link

@spencergibb Problem is not the lazy initialization, but the fact that the creation of client is not synchronized. SpringClientFactory#instantiateWithConfig

@spencergibb
Copy link
Member

And what problems does that cause @jansyk13?

@JakobFels
Copy link
Author

@spencergibb well the main problem is that it creates unpredictable delays.
I have a setup with a Zuul reverse proxy that calls a couple of backend services.
When i deploy a new version of a backend, the first request will be delayed, which causes random request to run into the zuul configured timeout. My default response time is around 300-400ms, these initial context init spikes push it to above 1s.

Of course I could just increase my timeouts, but that is rather unsatisfying.

@spencergibb
Copy link
Member

It would likely be feasible to eagerly create the zuul ribbon clients since we know all of the routes ahead of time.

@spencergibb spencergibb changed the title Is it possible to eagerly create RibbonClients? Eagerly create RibbonClients Oct 5, 2016
@jebeaudet
Copy link
Contributor

In the meantime, you could use something like this to create the clients when the proxy boots, we use that to refresh them if we ever change the read/connect timeout values in archaius :

@Component
public class RibbonEarlyInstantiator implements ApplicationListener<ApplicationStartedEvent>{

    @Autowired
    private ZuulProperties zuulProperties;
    @Autowired
    private SpringClientFactory springClientFactory;

    @Override
    public void onApplicationEvent(ApplicationStartedEvent event)
    {
        Map<String, ZuulRoute> routes = zuulProperties.getRoutes();
        routes.values()
              .stream()
              .filter(route -> route.getServiceId() != null)
              .map(route -> route.getServiceId())
              .distinct()
              .forEach(serviceId -> {
                  logger.info("Instantiating the context for the client '{}'", serviceId);
                  springClientFactory.getClient(serviceId, RibbonLoadBalancingHttpClient.class); //Or RestClient.class if you still use it
              });
    }
}

@bijukunjummen
Copy link
Contributor

Upstream issue: spring-cloud/spring-cloud-commons#184

bijukunjummen added a commit to bijukunjummen/spring-cloud-netflix that referenced this issue Mar 14, 2017
ryanjbaxter added a commit that referenced this issue Mar 15, 2017
GH-1334: Support for eagerly initializing zuul Ribbon contexts
@spencergibb spencergibb added this to the 1.3.0.RC1 milestone Mar 16, 2017
@spencergibb
Copy link
Member

Closed via 5bf87c4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants