Spring cloud plug in principle


The article was first published inCSDN personal blog

1、 Plug in overview

Plug in positioning

The spring cloud plug-in is a forward proxy plug-in for spring cloud. All spring cloud requests are processed by the plug-in for load balancing.

Effective time

When the request header’s rpctype = springcloud and the plug-in is enabled, it will be called by the downstream plug-in according to the request parameter matching rules.

2、 Plug in processing flow

1) Review the general request processing flow in the abstract # execute class:

public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChain chain) {
    //Get plug-in data
    String pluginName = named();
    final PluginData pluginData = BaseDataCache.getInstance().obtainPluginData(pluginName);
    //Plug in validation
    if (pluginData != null && pluginData.getEnabled()) {
        //Get selector data
        final Collection<SelectorData> selectors = BaseDataCache.getInstance().obtainSelectorData(pluginName);
        //Match selector
        final SelectorData selectorData = matchSelector(exchange, selectors);
        //Get rule data
        final List<RuleData> rules = BaseDataCache.getInstance().obtainRuleData(selectorData.getId());
        //Matching rules
        RuleData rule;
        if (selectorData.getType() == SelectorTypeEnum.FULL_FLOW.getCode()) {
            //get last
            rule = rules.get(rules.size() - 1);
        } else {
            rule = matchRule(exchange, rules);
        //Perform custom processing
        return doExecute(exchange, chain, selectorData, rule);
    //Continue plug-in chain processing
    return chain.execute(exchange);

Abstractsoulplugin determines whether the plug-in exists and is enabled

  • If the judgment is passed, the plug-in processing starts

    1. Match selector
    2. Matching rules
    3. Perform custom processing
  • Otherwise, the plug-in chain processing continues.

2) Let’s take a look at the spring cloud plug-in’s custom processing flow (springcloudplug # doexecute)

protected Mono<Void> doExecute(final ServerWebExchange exchange, final SoulPluginChain chain, final SelectorData selector, final RuleData rule) {
    //Get rule processing object
    final SpringCloudRuleHandle ruleHandle = GsonUtils.getInstance().fromJson(rule.getHandle(), SpringCloudRuleHandle.class);
    //Get selector handle object
    final SpringCloudSelectorHandle selectorHandle = GsonUtils.getInstance().fromJson(selector.getHandle(), SpringCloudSelectorHandle.class);
    //Load balancer selection service instance
    final ServiceInstance serviceInstance = loadBalancer.choose(selectorHandle.getServiceId());
    //Rebuild URI
    final URI uri = loadBalancer.reconstructURI(serviceInstance, URI.create(soulContext.getRealUrl()));
    //Generate real URL
    String realURL = buildRealURL(uri.toASCIIString(), soulContext.getHttpMethod(), exchange.getRequest().getURI().getQuery());
    //Set the real URL and timeout
    exchange.getAttributes().put(Constants.HTTP_URL, realURL);
    exchange.getAttributes().put(Constants.HTTP_TIME_OUT, ruleHandle.getTimeout());
    //Continue plug-in chain processing
    return chain.execute(exchange);

Spring cloudplugin first obtains the selector processing object, then uses the load balancer to select the service instance according to the service ID of the processing object and reconstruct the URI. According to the URI, the real URL is generated. Finally, the final URL and timeout time are set and handed over to the downstream of the plug-in chain for processing.

be careful:

The spring cloud plug-in itself is only responsible for selecting the server instances to be distributed according to the selectors, rules and injected load balancers, and does not directly request the back-end services.

3、 Load balancer

In the process of spring cloud plug-in processing, the plug-in itself does not assume the responsibility of probing and load balancing like divide plug-in, but is handled by a load balancer.

The load balancer needs to be implemented org.springframework.cloud . client.loadbalancer.LoadBalancerClient The official use is org.springframework.cloud . netflix.ribbon.RibbonLoadBalancerClient .

3.1 what is a ribbon?

Ribbon is an open source client load balancer released by Netflix. It is an important part of spring cloud Netflix. It connects Netflix’s middle tier services together.
The ribbon client component provides a series of perfect configuration items, such as connection timeout, Retry, etc. Simply put, all the services after load balancer are listed in the configuration file. Ribbon will automatically connect these services based on certain rules (such as simple polling, random connection, etc.), and it is easy to implement a custom load balancing algorithm.

3.2 what can ribbon do?

Ribbon is a load balancing access service on the client side. Its main functions are as follows:

  • Service discovery, discovering a list of dependent services
  • Service selection rules, how to select an effective service among multiple services
  • Service monitoring, detection of invalid services, efficient elimination of invalid services

3.3 responsibilities of ribbon in plug-ins

By integrating ribbon, spring cloud plug-in can easily implement the service discovery and load balancing strategy of spring cloud service.

In the springcloud plug-in, ribbon mainly undertakes the following responsibilities:

  • Service discovery: automatically discover dependent service list
  • Service monitoring: automatically eliminate invalid services and maintain effective service list
  • Service selection: select an effective service according to certain rules (load balancing)

4、 Summary

The spring cloud plug-in realizes the load balancing of spring cloud service through the load balancer. After selecting a real URL of an effective service, it is handed over to the downstream of the plug-in chain for processing.

Load balancer is an important part of spring cloud plug-in. The load balancer of plug-in is implemented by ribbon by default.