This article was first published on CSDN personal blog
I. Overview of plug-ins
Plug-in positioning
The springcloud plug-in is a springcloud forward proxy plug-in that loads all springcloud requests.
Effective time
When the request header’s rpcType = SpringCloud and the plug-in is enabled, it matches the rules according to the request parameters and is ultimately handed over to the downstream plug-in for responsive proxy invocation.
Second, plug-in processing process
AbstractSoulPlugin # execute
public Mono<Void> execute(final ServerWebExchange exchange, final SoulPluginChain chain) {
// Get plug-in data
String pluginName = named();
final PluginData pluginData = BaseDataCache.getInstance().obtainPluginData(pluginName);
// Validate the plugin
if(pluginData ! =null && pluginData.getEnabled()) {
// Get selector data
finalCollection<SelectorData> selectors = BaseDataCache.getInstance().obtainSelectorData(pluginName); .// Match selector
finalSelectorData selectorData = matchSelector(exchange, selectors); .// Get rule data
finalList<RuleData> rules = BaseDataCache.getInstance().obtainRuleData(selectorData.getId()); .// Match 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);
}
Copy the code
AbstractSoulPlugin Determines whether a plug-in exists and is enabled:
- If yes, the plug-in processing starts
- Match selector
- Match rule
- Perform custom processing
- Otherwise, continue with plug-in chain processing.
SpringCloudPlugin # doExecute: SpringCloudPlugin # doExecute
protected Mono<Void> doExecute(final ServerWebExchange exchange, final SoulPluginChain chain, final SelectorData selector, final RuleData rule) {...// Get the rule processing object
final SpringCloudRuleHandle ruleHandle = GsonUtils.getInstance().fromJson(rule.getHandle(), SpringCloudRuleHandle.class);
// Get the selector handler object
finalSpringCloudSelectorHandle selectorHandle = GsonUtils.getInstance().fromJson(selector.getHandle(), SpringCloudSelectorHandle.class); .// Load balancer selects the service instance
finalServiceInstance serviceInstance = loadBalancer.choose(selectorHandle.getServiceId()); ./ / to rebuild a URI
final URI uri = loadBalancer.reconstructURI(serviceInstance, URI.create(soulContext.getRealUrl()));
// Generate a 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);
}
Copy the code
The SpringCloudPlugin 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 rebuild the URI, which generates the real URL, and finally sets the final URL and timeout time to be processed by the downstream of the plug-in chain.
Note:
The SpringCloud plug-in itself is only responsible for selecting server instances to be distributed based on selectors, rules, and injected load balancers, and does not make requests directly to back-end services.
Load balancer
The SpringCloud plug-in does not take on the probing and load balancing duties of the Divide plug-in. Instead, it is handled by a load balancer.
The load balancer to implement org. Springframework. Cloud. Client. Loadbalancer. LoadBalancerClient, Official use org.springframework.cloud.net flix. Ribbon. RibbonLoadBalancerClient.
3.1 What is the Ribbon?
Ribbon is an open source client load balancer released by Netflix. It is an important part of SpringCloud-Netflix, connecting the middle tier services of Netflix together. The Ribbon client component provides a comprehensive set of configuration options, such as connection timeout and retry. Simply put, if the Ribbon lists all the services behind Load Balancer in the configuration file, the Ribbon will automatically connect these services based on certain rules (e.g. simple polling, random linking, etc.). You can easily implement custom Load balancing algorithms.
3.2 What does the Ribbon do?
The Ribbon implements load balancing on the client side. The Ribbon provides the following functions:
- Service discovery, discovering a list of dependent services
- Service selection rules, how to select a valid service among multiple services
- Service monitoring, detection of invalid services, efficient elimination of invalid services
3.3 Ribbon’s Responsibilities in Plug-ins
By integrating with the ribbon, springcloud plug-ins can easily implement service discovery and load balancing strategies for springcloud services.
In springcloud, the ribbon takes on the following responsibilities:
- Service discovery: Automatically discovers the list of dependent services
- Service monitoring: automatically eliminate invalid services and maintain valid service list
- Service selection: Selecting a valid service according to some rule (load balancing)
Four, summary
The SpringCloud plug-in realizes the load balancing of SpringCloud service through the load balancer. After selecting the real URL of a valid service, it is delivered to the downstream of the plug-in chain for processing.
Load balancer is an important part of the springcloud plug-in. The load balancer is implemented by default using the ribbon.