Gateway applications need to support a variety of load balancing strategies, including random,Hashing, RoundRobin and so on. In Apache Shenyu gateway, it not only realizes such traditional algorithms, but also makes smoother traffic processing for the entry of server nodes through detailed processing such as traffic warm-up, so as to obtain better overall stability. In this article, let's walk through how Apache Shenyu is designed and implemented this part of the function.
This article based on shenyu-2.5.0 version of the source code analysis.
The implementation of LoadBalancer is in shenyu-loadbalancer module. It has based on its SPI creation mechanism. The core interface code is shown as follows. This interface well explains the concept: load balancing is to select the most appropriate node from a series of server nodes. Routing, traffic processing and load balancing is the basic function of LoadBalancerSPI.
@SPIpublic interface LoadBalancer { /** * this is select one for upstream list. * * @param upstreamList upstream list * @param ip ip * @return upstream */ Upstream select(List<Upstream> upstreamList, String ip);}
Where upstreamList represents the server nodes list available for routing. Upstream is the data structure of server node, the important elements including protocol, upstreamUrl , weight, timestamp, warmup、healthy.
public class Upstream { /** * protocol. */ private final String protocol; /** * url. */ private String url; /** * weight. */ private final int weight; /** * false close, true open. */ private boolean status; /** * startup time. */ private final long timestamp; /** * warmup. */ private final int warmup; /** * healthy. */ private boolean healthy; /** * lastHealthTimestamp. */ private long lastHealthTimestamp; /** * lastUnhealthyTimestamp. */ private long lastUnhealthyTimestamp; /** * group. */ private String group; /** * version. */ private String version;}
The class diagram of LoadBalancer moduleisshown as follows.
We can draw the outline of LoadBalancer module from the class diagram:
The abstract class AbstractLoadBalancer implements the SPI LoadBalancer interface,and supplies the template methods for selection related, such as select(), selector(),and gives the calculation of weight.
Three implementation classes which inherit AbstractLoadBalancer to realize their own logic:
RandomLoadBalancer - Weight Random
HashLoadBalancer - Consistent Hashing
RoundRobinLoadBalancer -Weight Round Robin per-packet
The factory class LoadBalancerFactory provides public static method to be called.
The implementation classes and algorithms are configurable. According to its specification, by adding profile in SHENYU_DIERECTORY directory, the data in profile should be key=value-class format, where the value-class will be load by the Apache Shenyu SPI class loader, and key value should be an name defined in LoadBalanceEnum.
This abstract class implements the LoadBalancer interface and define the abstract method doSelect() to be processed by the implementation classes. In the template method select(), It will do validation first then call the doSelect() method.
public abstract class AbstractLoadBalancer implements LoadBalancer { /** * Do select divide upstream. * * @param upstreamList the upstream list * @param ip the ip * @return the divide upstream */ protected abstract Upstream doSelect(List<Upstream> upstreamList, String ip); @Override public Upstream select(final List<Upstream> upstreamList, final String ip) { if (CollectionUtils.isEmpty(upstreamList)) { return null; } if (upstreamList.size() == 1) { return upstreamList.get(0); } return doSelect(upstreamList, ip); }}
When the timestamp of server node is not null, and the interval between current time and timestamp is within the traffic warm-up time, the formula for weight calculation is.
$$ {1-1}
ww = min(1,uptime/(warmup/weight))
$$
It can be seen from the formula that the final weight(ww) is proportional to the original-weight value. The closer the time interval is to the warmup time, the greater the final ww. That is, the longer the waiting time of the request, the higher the final weight. When there is no timestamp or other conditions, the ww is equal to the weight value of Upstream object.
The central of thinking about warm-upis to avoid bad performance when adding new server and the new JVMs starting up.
Let's see how the load balancing with Random, Hashing and RoundRobin strategy is implemented.
Each node without weight, or every node has the same weight, randomly choose one.
Server Nodes with different weight, choose one randomly by weight.
Following is the random() method of RandomLoadBalancer. When traversing server node list, if the randomly generated value is less than the weight of node, then the current node will be chosen. If after one round traversing, there is no server node match, then it will choose one randomly. The getWeight(final Upstream upstream) is defined in AbstractLoadBalancer class.
@Override public Upstream doSelect(final List<Upstream> upstreamList, final String ip) { int length = upstreamList.size(); // every upstream has the same weight? boolean sameWeight = true; // the weight of every upstream int[] weights = new int[length]; int firstUpstreamWeight = getWeight(upstreamList.get(0)); weights[0] = firstUpstreamWeight; // init the totalWeight int totalWeight = firstUpstreamWeight; int halfLengthTotalWeight = 0; for (int i = 1; i < length; i++) { int currentUpstreamWeight = getWeight(upstreamList.get(i)); if (i <= (length + 1) / 2) { halfLengthTotalWeight = totalWeight; } weights[i] = currentUpstreamWeight; totalWeight += currentUpstreamWeight; if (sameWeight && currentUpstreamWeight != firstUpstreamWeight) { // Calculate whether the weight of ownership is the same. sameWeight = false; } } if (totalWeight > 0 && !sameWeight) { return random(totalWeight, halfLengthTotalWeight, weights, upstreamList); } return random(upstreamList); } private Upstream random(final int totalWeight, final int halfLengthTotalWeight, final int[] weights, final List<Upstream> upstreamList) { // If the weights are not the same and the weights are greater than 0, then random by the total number of weights. int offset = RANDOM.nextInt(totalWeight); int index = 0; int end = weights.length; if (offset >= halfLengthTotalWeight) { index = (weights.length + 1) / 2; offset -= halfLengthTotalWeight; } else { end = (weights.length + 1) / 2; } // Determine which segment the random value falls on for (; index < end; index++) { offset -= weights[index]; if (offset < 0) { return upstreamList.get(index); } } return random(upstreamList); }
In HashLoadBalancer, it takes the advantages of consistent hashing , that maps both the input traffic and the servers to a unit circle, or name as hash ring. For the requestedip address, with its hash value to find the node closest in clockwise order as the node to be routed. Let's see how consistent hashing is implemented in HashLoadBalancer.
As to the hash algorithms, HashLoadBalancer uses MD5 hash, which has the advantage of mixing the input in an unpredictable but deterministic way. The output is a 32-bit integer. the code is shown as follows:
Importantly, how to generate the hash ring and avoid skewness? Let's thedoSelect() method inHashLoadBalancer as follows:
private static final int VIRTUAL_NODE_NUM = 5; @Override public Upstream doSelect(final List<Upstream> upstreamList, final String ip) { final ConcurrentSkipListMap<Long, Upstream> treeMap = new ConcurrentSkipListMap<>(); upstreamList.forEach(upstream -> IntStream.range(0, VIRTUAL_NODE_NUM).forEach(i -> { long addressHash = hash("SHENYU-" + upstream.getUrl() + "-HASH-" + i); treeMap.put(addressHash, upstream); })); long hash = hash(ip); SortedMap<Long, Upstream> lastRing = treeMap.tailMap(hash); if (!lastRing.isEmpty()) { return lastRing.get(lastRing.firstKey()); } return treeMap.firstEntry().getValue(); }
In this method, duplicated labels are used which are called "virtual nodes" (i.e. 5 virtual nodes point to a single "real" server). It will make the distribution in hash ring more evenly, and reduce the occurrence of data skewness.
In order to rescue the data sorted in the hash ring, and can be accessed quickly, we use ConcurrentSkipListMap of Java to store the server node lists ( with virtual nodes) and its hash value as key. This class a member of Java Collections Framework, providing expected average log(n) time cost for retrieve and access operations safely execute concurrent by multiple threads.
Furthermore, the method tailMap(K fromKey) of ConcurrentSkipListMap can return a view of portion of the map whose keys are greater or equal to the fromKey, and not need to navigate the whole map.
In the above code section, after the hash ring is generated, it uses tailMap(K fromKey) of ConcurrentSkipListMap to find the subset that the elements greater, or equal to the hash value of the requested ip, its first element is just the node to be routed. With the suitable data structure, the code looks particularly clear and concise.
Consistent hashing resolved the poor scalability of the traditional hashing by modular operation.
The original Round-robin selection is to select server nodes one by one from the candidate list. Whenever some nodes has crash ( ex, cannot be connected after 1 minute), it will be removed from the candidate list, and do not attend the next round, until the server node is recovered and it will be add to the candidate list again. In RoundRobinLoadBalancer,the weight Round Robin per-packet schema is implemented.
In order to work in concurrent system, it provides an inner static class WeigthRoundRobin to store and calculate the rolling selections of each server node. Following is the main section of this class( removed remark )
protected static class WeightedRoundRobin { private int weight; private final AtomicLong current = new AtomicLong(0); private long lastUpdate; void setWeight(final int weight) { this.weight = weight; current.set(0); } long increaseCurrent() { return current.addAndGet(weight); } void sel(final int total) { current.addAndGet(-1 * total); } void setLastUpdate(final long lastUpdate) { this.lastUpdate = lastUpdate; }}
Please focus on the these method:
setWeight(final int weight) : set the current value by weight
increaseCurrent(): Increment the current value by weight, and current set to 0.
sel(final int total): decrement the current value by total
Let's see how the weight factor being used in this round-robin selection?
First it defines a two-level ConcurrentMap type variable named as methodWeightMap , to cache the server node lists and the rolling selection data about each server node.
private final ConcurrentMap<String, ConcurrentMap<String, WeightedRoundRobin>> methodWeightMap = new ConcurrentHashMap<>(16);
In this map, the key of first level is set to upstreamUrl of first element in server node list. The type of second object is ConcurrentMap<String, WeightedRoundRobin>, the key of this inner Map is the value upstreamUrlvariable of each server node in this server list, the value object is WeightedRoundRobin, used to trace the rolling selection data about each server node. As to the implementation class for the Map object, we use ConcurrentHashMap of JUC, a hash table supporting full concurrency of retrievals and high expected concurrency for updates.
In the second level of the map, the embedded static class - WeighedRoundRobin of each node is thread-safe, implementing the weighted RoundRobin per bucket. The following is the code of the doselect() method of this class.
@Overridepublic Upstream doSelect(final List<Upstream> upstreamList, final String ip) { String key = upstreamList.get(0).getUrl(); ConcurrentMap<String, WeightedRoundRobin> map = methodWeightMap.get(key); if (Objects.isNull(map)) { methodWeightMap.putIfAbsent(key, new ConcurrentHashMap<>(16)); map = methodWeightMap.get(key); } int totalWeight = 0; long maxCurrent = Long.MIN_VALUE; long now = System.currentTimeMillis(); Upstream selectedInvoker = null; WeightedRoundRobin selectedWeightedRoundRobin = null; for (Upstream upstream : upstreamList) { String rKey = upstream.getUrl(); WeightedRoundRobin weightedRoundRobin = map.get(rKey); int weight = getWeight(upstream); if (Objects.isNull(weightedRoundRobin)) { weightedRoundRobin = new WeightedRoundRobin(); weightedRoundRobin.setWeight(weight); map.putIfAbsent(rKey, weightedRoundRobin); } if (weight != weightedRoundRobin.getWeight()) { // weight changed. weightedRoundRobin.setWeight(weight); } long cur = weightedRoundRobin.increaseCurrent(); weightedRoundRobin.setLastUpdate(now); if (cur > maxCurrent) { maxCurrent = cur; selectedInvoker = upstream; selectedWeightedRoundRobin = weightedRoundRobin; } totalWeight += weight; } ...... //erase the section which handles the time-out upstreams. if (selectedInvoker != null) { selectedWeightedRoundRobin.sel(totalWeight); return selectedInvoker; } // should not happen here return upstreamList.get(0);}
For example we assume upstreamUrl values of three server nodes is: LIST = [upstream-20, upstream-50, upstream-30]. After a round of execution, the data in newly created methodWeightMap is as follows:
For the above example LIST, assumes the weight array is [20,50,30]. the following figure shows the value change and polling selection process of the current array in WeighedRoundRobin object.
In each round, it will choose the server node with max current value.
Round1:
Traverse the server node list, initialize the weightedRoundRobin instance of each server node or update the weight value of server nodes object Upstream
Traverse the server node list, initialize the weightedRoundRobin instance of each server node or update the weight value of server nodes object Upstream
say, in this case, after traverse, the current array of the node list changes to [20, 50,30],so according to rule, the node Stream-50 would be chosen, and then the static object WeightedRoundRobin of Stream-50 executes sel(-total) , the current array is now [20,-50, 30].
Round 2: after traverse, the current array should be [40,0,60], so the Stream-30 node would be chosen, current array is now [40,0,-40].
Round 3: after traverse, current array changes to [60,50,-10], Stream-20 would be chosen,and current array is now [-40,50,-10].
When there is any inconsistence or some server crashed, for example, the lists size does not match with the elements in map, it would copy and modify the element with lock mechanism, and remove the timeout server node, the data in Map updated. Following is the fault tolerance code segment.
if (!updateLock.get() && upstreamList.size() != map.size() && updateLock.compareAndSet(false, true)) { try { // copy -> modify -> update reference. ConcurrentMap<String, WeightedRoundRobin> newMap = new ConcurrentHashMap<>(map); newMap.entrySet().removeIf(item -> now - item.getValue().getLastUpdate() > recyclePeriod); methodWeightMap.put(key, newMap); } finally { updateLock.set(false); } } if (Objects.nonNull(selectedInvoker)) { selectedWeightedRoundRobin.sel(totalWeight); return selectedInvoker; } // should not happen here. return upstreamList.get(0);
In this class, a static method calling LoadBalancer is provided, whereExtensionLoader is the entry point of Apache Shenyu SPI. That is to say, LoadBalancer module is configurable and extensible. The algorithm variable in this static method is the name enumeration type defined in LoadBalanceEnum.
/** * Selector upstream. * * @param upstreamList the upstream list * @param algorithm the loadBalance algorithm * @param ip the ip * @return the upstream */ public static Upstream selector(final List<Upstream> upstreamList, final String algorithm, final String ip) { LoadBalancer loadBalance = ExtensionLoader.getExtensionLoader(LoadBalancer.class).getJoin(algorithm); return loadBalance.select(upstreamList, ip); }
In the above section, we describe the LoadBalancerSPI and three implementation classes. Let's take a look at how the LoadBalancer to be used in Apache Shenyu. DividePlugin is a plugin in Apache Shenyu responsible for routing http request. when enable to use this plugin, it will transfer traffic according to selection data and rule data, and deliver to next plugin downstream.
@Overrideprotected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { ......}
The type of second parameter of doExecute() is ShenyuPluginChain, which represents the execution chain of plugins. For details, see the mechanism of Apache Shenyu Plugins. The third one is SelectorData type, and the fourth is RuleData type working as the rule data.
In doExecute() of DividePlugin, first verify the size of header, content length, etc, then preparing for load balancing.
Following is a code fragment usingLoadBalancer in the doExecute() method:
// find the routing server node list List<Upstream> upstreamList = UpstreamCacheManager.getInstance().findUpstreamListBySelectorId(selector.getId()); ... // the requested ip String ip = Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getAddress().getHostAddress(); //calling the Utility class and invoke the LoadBalance processing. Upstream upstream = LoadBalancerFactory.selector(upstreamList, ruleHandle.getLoadBalance(), ip);
In the above code, the output ofruleHandle.getLoadBalance() is the name variable defined in LoadBalanceEnum, that is random, hash, roundRobin, etc. It is very convenient to use LoadBalancer by LoadBalancerFactory. When adding more LoadBalancer implementing classes, the interface in plugin module will not be effect at all.
After reading through the code of LoadBalancer module, from the design perspective, it is concluded that this module has the following characteristics:
Extensibility: Interface oriented design and implemented on Apache Shenyu SPI mechanism, it can be easily extended to other dynamic load balancing algorithms (for example, least connection, fastest mode, etc), and supports cluster processing.
Scalability: Every load balancing implementation, weighted Random, consistency Hashing and weighted RoundRobin can well support increase or decrease cluster overall capacity.
More detailed design such as warm-up can bring better performance and obtain better overall stability.
In most of the plugins ( such as Dubbo, gRPC,Spring-cloud, etc) of Apache Shenyu, the routingparameters are designed to support the combination of multiple conditions. In order to realize such requirements, the parameters and behaviors are abstracted to three parts according to its SPI mechanism, and implemented in shenyu-plugin-base module.
ParameterData-parameters
PredictJudge-predicate
MatchStrategy-matching strategy
Relatively speaking, the MatchStrategy is the part that needs the least extension points. For the combined judgement of multiple conditions, the common selection rules are: All conditions are matched, at least one is matched, at least the first is met, or most of conditions satisfied. As we will need to handle various types of parameters, for example: IP, header, uri, etc.
How to make the MatchStrategy to be simple to use and extensible?
The implementation of MatchStrategy is in shenyu-plugin-base module. It is based on the SPI creation mechanism, and has used factory pattern and strategy design pattern. The class diagram of MatchStrategyis showed as follows.
Based on the interface MatchStrategy we design the implementation classes, and the abstract class AbstractMatchStrategy supplies common method, while the factory class MatchStrategyFactory provides creation functions.
The annotation @SPI means that this is an SPI interface. Where ServerWebExchange is org.springframework.web.server.ServerWebExchange, represents the request-response interactive content of HTTP. Following is the code of ConditionData, the more detail about this class can refer to code analysis of PredicteJudge
public class ConditionData { private String paramType; private String operator; private String paramName; private String paramValue;}
Second, let's look at the abstract class AbstractMatchStrategy,it has defined a buildRealData method,In this method it wraps various parameters to a unified interface through the functionality of ParameterDataFactory, which is the factory class of ParameterData. It supports a variety of types of parameters , such as Ip, Cookie, Header,uri, etc. Modifications of such parameters will not impact the calling of matching rules of MatchStrategy.
public abstract class AbstractMatchStrategy { public String buildRealData(final ConditionData condition, final ServerWebExchange exchange) { return ParameterDataFactory.builderData(condition.getParamType(), condition.getParamName(), exchange); }}
Now, let's look at the two implementation class based on the above interface in shenyu-plugin-base module , that is:
AndMatchStrategy- AND -All conditions are matched
OrMatchStrategy- OR -at least one is match
The properties file containing the SPI implementation is shown as follows, which located at the SHENYU_DIRECTORYdirectory. When starting up, the top-level SPI classes will read the key-value and load the classes and cache them.
Since the PredicateJudge interface can encapsulate different variety of Predicates , for example EqualsPredicateJudge, EndsWithPredicateJudge and so on, the ConditionData and ParamData passed to it can present with variety of parameters, for treating of multiple conditions. So usingstream and lambda expression, it can be very simple and efficient to process "AND" logic (all conditions must be matched).
@Joinpublic class AndMatchStrategy extends AbstractMatchStrategy implements MatchStrategy { @Override public Boolean match(final List<ConditionData> conditionDataList, final ServerWebExchange exchange) { return conditionDataList .stream() .allMatch(condition -> PredicateJudgeFactory.judge(condition, buildRealData(condition, exchange))); }}
The OrMatchStrategy similarly implements the "OR" logic- at least one is match.
This is the factory class of MatchStrategy,there are two methods, one is newInstance(), which will return the MatchStrategy implementation class instance cached by the SPIExtensionLoader indexed by the key-value.
AbstractShenyuPlugin is the base class of plugins in shenyu-plugin module. In this class two selection method are defined: filterSelector() and filterRule() , Both of them call the match() method of MatchStrategyFactory. The code of filterSelector() is shown as follows.
private Boolean filterSelector(final SelectorData selector, final ServerWebExchange exchange) { if (selector.getType() == SelectorTypeEnum.CUSTOM_FLOW.getCode()) { if (CollectionUtils.isEmpty(selector.getConditionList())) { return false; } return MatchStrategyFactory.match(selector.getMatchMode(), selector.getConditionList(), exchange); } return true; }
In filterSelector() method, after validation of the SelectorData, calls the match method of MatchStrategyFactory, and then this factory class will invokes the match method of corresponding implementation class.
In filterRule() it is also calls the match() method of MatchStrategyFactory. Does it look particularly concise or even simple? In the code analysis of PredicteJudge , you can see more detail about parameter processing in shenyu-plugin.
Due to the use of SPI mechanism of Apache Shenyu, the parameter selection module has the characteristic of loose coupling and extensibility. In terms of the combination of multiple conditions, MatchStrategy provides a good design. Although currently only two implementation classes are present, it can be easily used to develop more complex MatchStrategy rules in the future, such as "firstOf"-first condition must matched, or "mostOf"- most of the conditions must be matched, etc.
Interested readers can read the source code of 'shenyu-plugin' to learn more.
Apache Shenyu has been identified as a gateway application which supports a variety of protocols and microservice frameworks such as Dubbo, gRPC, Spring-Cloud, etc. To do this, the product has accomplished an elegant SPI (Service Provider Interface) as its foundation, and make the Rule data parsing and predicting program very simple , resiliency and security. As to rule data parsing processing, the SPI design increases the product's scalability. When appending new plugin, in most cases, the existing module is enough for rule data parsing , otherwise it can be rapidly carry out with tiny effort.
In Apache Shenyu, the SPI archtecure is defined in shenyu-spi module and composed of three parts: SPI interface, factory design pattern, and configuration file. There is two interface defined as annotation: @SPI and @Join. When class file with @Join annotation, it means that it will join as an SPI extension class, in other words, it is an application or registration. The @SPI denotes that the class is an SPI extension class.
Fig 1 classes in the shenyu-spi
The SPI configuration directory is META-INF/shenyu/. that is specified:
SHENYU_DIRECTORY = "META-INF/shenyu/";
When starting the gateway system , the ExtensionLoader will scan the profiles under SHENYU_DIRECTORY, in turn, load and validate and then initialize each configed class. The configuration file uses "Key = class-file" format. During operation of the system, the corresponding SPI implementation class will be invoked through the factory mechanism.
In shenyu-plugin module, various plugins for HTTP routing are implemented according to the plugin mechanism, including request, redirect, response and rewrite, etc. Plugins for microservice frameworks such as Dubbo, gRPC , Spring-Cloud and Tars have been developed in the gateway product. And plugins are still increasing. If no such dependent module fo parsing and judge routing parameters and data, each plugin is necessary to implement the parsing functions, and has to frequently modify to support their matching rules, such as wildcard, regular expression, SpEL expression, etc. Therefore, they made a high level abstraction for routing parameter data following the SPI framework in shenyu-plugin module. The rule analysis consists of three parts:
ParameterData- parameter data
PredicatJudge- predicate whether the actural data match the rule
MatchStrategy- combine multiple conditions, the final used strategy
These implementation classes are defined in shenyu-plugin-base module. In each plugin, resolution and predication of the routing parameter can be realized through AbstractShenyuPlugin using the above SPIs. That is dedicated and easy to extend, in line with SOLID principle.
This section analyzes the PredictJudge in detail. You can find the dependency to shenyu-spi in the pom.xml of this module.
PredicateJudgeSPI is used to analyze and judge various routing rules configed in Apache Shenyu gateway. The name and functions of this SPI are similar to Predicate in Java, but the acceptance behavior is further abstracted applying for routing aspect. This SPI is implemented through the Factory pattern. Let's look at the PredictejudgeSPI interface:
@SPI@FunctionalInterfacepublic interface PredicateJudge { /** * judge conditionData and realData is match. * * @param conditionData {@linkplain ConditionData} * @param realData realData * @return true is pass false is not pass. */ Boolean judge(ConditionData conditionData, String realData);}
The class diagram is as follows:
Fig 2-Predicate class diagram
The important methods of PredicateJudgeFactory are shown as follows:
Whenever need to parsing and matching routing data, you can use
public static PredicateJudge newInstance(final String operator) { return ExtensionLoader.getExtensionLoader(PredicateJudge.class).getJoin(processSpecialOperator(operator)); }
public static Boolean judge(final ConditionData conditionData, final String realData) { if (Objects.isNull(conditionData) || StringUtils.isBlank(realData)) { return false; } return newInstance(conditionData.getOperator()).judge(conditionData, realData); }
ConditionData contains of four attributes of String type: paramType, operator,paramName,paramValue
Base on the above defination , the plugin module provides the following eight PredicateJudge implemetation classes to realize the logic of these operators respectively.
Implementation class
Logic description
corespondece operator
ContainsPredicateJudge
"contain" relation, the actual data needs contain the specified string
contains
EqualsPredicateJudge
equals "="
=
MatchPredicateJudge
used for URI context path matching
match
TimerAfterPredicateJudge
Whether the local time is after the specified time
TimeAfter
TimerBeforePredicateJudge
Whether the local time is before the specified time
TimeBefore
GroovyPredicateJudge
used Groovy syntax toe set ParamName and value data
The usage of PredicateJudge SPI in Shenyu gateway#
Most plugins in Apache Shenyu are inherited from AbstractShenyuPlugin. In this abstract class, the filter functions (selection and matching) are achieved through MatchStrategySPI, and PredicateJudge will be invoked from MatchStrategy to predicate each condition data.
Fig 3- class diagram of plugins with PredicateJudge and MatchStrategySPI
The process from client request calling the routing parsing moodule is showed as following chart.
Fig 4- flow chart for Shenyu gateway filter with parameter processing
When startup, the system will load SPI classes from profile and cache them.
When the client sends a new request to the Shenyu gateway, will call the corresponding plugin within the gateway.
When analyzing real data with routing rules, the PredicateJudge implementation class will be invoked according to the contained operator.
For example, giving a ConditionData with: paramType="uri", paramValue 是 "/http/**", when using the "contains" relation: ContainsPredicateJudge, the matching result is as follows.
ConditionData (operator="contains")
real data
judge result
paramType="uri", "/http/**"
"/http/**/test"
true
"/test/http/**/other"
true
"/http1/**"
false
About other PredicateJudge implemetantion classes, you can refer to the code and test classes.
Rate limiter is a very important integral of gateway application, to deal with high traffic. When the system is attacked abnormally by a large number of traffic gathered in a short time; When there are a large number of lower priority request need to be slow down or else it will effect your high priority transactions; Or sometimes your system can not afford the regular traffic; in these scenarios, we need to start rate limiter component to protect our system, through rejection, wait, load shedding,etc, limit the requests to an acceptable quantities, or only certain domains (or services) requests can get through.
Facing above scenarios, following need to be considered when designing the rate limiter component of an gateway.
Supports a variety of rate limiter algorithms and easy to extends.
Resilient resolvers which can distinguish traffic by different way, such as ip, url, even user group etc.
High availability, can quickly get allow or reject result from rate limiter
With fault tolerance against when rate limiter is down, the gateway can continue work.
This article will first introduce the overall architecture of the rate limiter module in Apache Shenyu, and then focus on the code analysis of rate limiter SPI.
This article based on shenyu-2.4.0 version of the source code analysis.
Spring WebFlux is reactive and non-blocking web framework, which can benefit throughput and make applications more resilient. The plugin of Apache Shenyu is based on WebFlux,its rate limiter component is implemented in ratelimiter-plugin. In rate limiter process, the commonly used algorithms are token bucket, leaky bucket, etc. To speed up concurrency performance, the counting and calculation logic is treated in Redis, and Java code is responsible for the transmission of parameters. When applying Redis, the Lua script can be resident memory, and be executed as a whole, so it is atomic. Let alone the reducing of network overhead. Redis commands abstraction and automatic serialization/deserialization with Redis store is provided in Spring Data Redis. Because of based on reactive framework, the Spring Redis Reactive is used in ratelimiter-plugin.
The class diagram of this plugin is as follows, highlighting two packages related to RateLimiter SPI: resolver 和algorithm.
High performance issue is achieved through the architecture of Spring data+ Redis+Lua , two SPI are supplied in ratelimiter-plugin for the extension of algorithm and key resolver。
RateLimiterAlgorithm:used for algorithms expansion.
RateLimiterKeyResolver: used for resolver expansion, to distinguish requests by various information, including ip, url, ect.
The profile of SPI is located at directory of SHENYU_DIRECTORY (default/META-INF/shenyu).
Obtain the critical info of the request used for packet rate limiter,the interface of RateLimiterKeyResolver is follows:
@SPIpublic interface RateLimiterKeyResolver { /** * get Key resolver's name. * * @return Key resolver's name */ String getKeyResolverName(); /** * resolve. * * @param exchange exchange the current server exchange {@linkplain ServerWebExchange} * @return rate limiter key */ String resolve(ServerWebExchange exchange);}
@SPI registers the current interface as Apache Shenyu SPI. Method resolve(ServerWebExchange exchange) is used to provide the resolution way. Currently there are two key resolvers in RateLimiterKeyResolverSPI:WholeKeyResolve and RemoteAddrKeyResolver. The resolve method of RemoteAddrKeyResolveris as follows:
@Override public String resolve(final ServerWebExchange exchange) { return Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getAddress().getHostAddress(); }
Where the resolved key is ip of request. Based on SPI mechanism and its factory pattern, new resolver can be easily developed.
RateLimiterAlgorithmSPI is used to identify and define different rate limiter algorithms, following is the class diagram of this module.
In this module, factory pattern is used , providing interface, abstract class and factory class, and four implementation classes. The lua script corresponding to the implementation class is enumerated in RateLimitEnum and located in /META-INF/scripts.
@SPIpublic interface RateLimiterAlgorithm<T> { RedisScript<T> getScript(); List<String> getKeys(String id); /** * Callback string. * * @param script the script * @param keys the keys * @param scriptArgs the script args */ default void callback(final RedisScript<?> script, final List<String> keys, final List<String> scriptArgs) { }}
@SPI registers the current interface as Apache Shenyu SPI. There are three methods:
getScript() returns a RedisScript object, which will be passed to Redis.
getKeys(String id) returns a List contains with keys.
callback() the callback function which will be executed asynchronously later on, and default is an empty method.
The template method is implemented in this abstract class, and the reified generics used is List<Long>. Two abstract methods getScriptName() and getKeyName() are left for the implementation class. Following is the code to load lua script.
initialized is an AtomicBoolean type variable used to indicate whether the lua script is loaded. If has not been loaded, the system will read specified scripts form META-INF/scripts; After reading, specify the result with List type, and set initialized=true, then returning RedisScriptobject.
The code of getKeys() in AbstractRateLimiterAlgorithm is as follows:
Two strings are generated in this template method, where the tokenKey will work as Key to a Sorted map in Redis.
We can observe from above class diagram that ConcurrentRateLimiterAlgorithm and SlidingWindowRateLimiterAlgorithm override getKeys(String id) method but another two implementation classes not, and use template method in AbstractRateLimiterAlgorithm. Only in ConcurrentRateLimiterAlgorithm has override callback() method, the others not. We will do further analysis in the following.
The method getsRateLimiterAlgorithm instance by name in RateLimiterAlgorithmFactory is as follows:
public static RateLimiterAlgorithm<?> newInstance(final String name) { return Optional.ofNullable(ExtensionLoader.getExtensionLoader(RateLimiterAlgorithm.class).getJoin(name)).orElse(new TokenBucketRateLimiterAlgorithm());}
ExtensionLoader of SPI is responsible for loading SPI classes by "name", if cannot find the specified algorithm class, it will return TokenBucketRateLimiterAlgorithm by default.
Above detailed the extension interface in RateLimiterSPI. In Apache Shenyu, we use ReactiveRedisTemplate to perform Redis processing reactively, which is implemented inisAllowed() method of RedisRateLimiter class.
public Mono<RateLimiterResponse> isAllowed(final String id, final RateLimiterHandle limiterHandle) { // get parameters that will pass to redis from RateLimiterHandle Object double replenishRate = limiterHandle.getReplenishRate(); double burstCapacity = limiterHandle.getBurstCapacity(); double requestCount = limiterHandle.getRequestCount(); // get the current used RateLimiterAlgorithm RateLimiterAlgorithm<?> rateLimiterAlgorithm = RateLimiterAlgorithmFactory.newInstance(limiterHandle.getAlgorithmName()); ........ Flux<List<Long>> resultFlux = Singleton.INST.get(ReactiveRedisTemplate.class).execute(script, keys, scriptArgs); return resultFlux.onErrorResume(throwable -> Flux.just(Arrays.asList(1L, -1L))) .reduce(new ArrayList<Long>(), (longs, l) -> { longs.addAll(l); return longs; }).map(results -> { boolean allowed = results.get(0) == 1L; Long tokensLeft = results.get(1); return new RateLimiterResponse(allowed, tokensLeft); }) .doOnError(throwable -> log.error("Error occurred while judging if user is allowed by RedisRateLimiter:{}", throwable.getMessage())) .doFinally(signalType -> rateLimiterAlgorithm.callback(script, keys, scriptArgs)); }
The POJO class RateLimiterHandle wraps the parameters needed in rate limiter, they are algorithName, replenishRate, burstCapacity, requestCount, etc. First, gets the parameters that need to be passed into Redis from RateLimiterHandle class. Then obtain the current implementation class from RateLimiterAlgorithmFactory.
For convenience, we give an flow image to show the parameters I/O and execution procedure in Java and Redis respectively. On the left is the second half of isAllowed() , and on the right is the processing of Lua script.
Following is the execution process of the JAVA code.
Get two keys value in List<String> type from the getKeys() method, the first element will map to a sorted set in Redis.
Set four parameters, replenishRate , burstCapacity, timestamp (EpochSecond) and requestcount.
Calling ReactiveRedisTemplate with the scripts, keys and parameters, the return a Flux<List<Long>>
The return value is converted from Flux<ArrayList<Long>> to Mono<ArrayList<Long>> the through reduce() of Flux ,and then transform it to Mono<RateLimiterResponse> via map() function. Returned two data, one is allowed (1-allow, 0- not allowed), the other is tokensLeft, the number of available remaining request.
As for the fault tolerance, due to using of reactor non-blocking model, when an error occurs, the fallback function onErrorResume() will be executed and a new stream (1L, -1L) will generated by Flux.just, which means allow the request getting through, and log the error on the side.
After that, performs the doFinally() method, that is to execute the callback() method of the implementation class.
From above we know that how the java code works with Redis in the gateway. In this chapter we briefly analysis some code of the four rate limiter algorithms, to understand how to develop the interface of RateLimiter SPI and work efficiently with Redis.
Four rate limiter algorithms are supplied in Apache Shenyu Ratelimit SPI:
Algorithm name
Java class
Lua script file
Request rate limiter
TokenBucketRateLimiterAlgorithm
request_rate_limiter.lua
Slide window rate limiter
SlidingWindowRateLimiterAlgorithm
liding_window_request_rate_limiter.lua
Concurrent rate limiter
ConcurrentRateLimiterAlgorithm
concurrent_request_rate_limiter.lua
Leaky bucket algorithm
LeakyBucketRateLimiterAlgorithm
request_leaky_rate_limiter.lua
Token bucket rate limiter: Limiting the traffic according to the number of requests. Assuming that N requests can be passed per second, when requests exceeding N will be rejected. In implementing of the algorithm, the requests will be grouped by bucket, the tokens will be generated at an evenly rate. If the number of requests is less than the tokens in the bucket, then it is allowed to pass. The time window is 2* capacity/rate.
Slide window rate limiter: Different from token bucket algorithm, its window size is smaller than that of token bucket rate limiter, which is a capacity/rate. And move backward one time window at a time. Other rate limiter principles are similar to token bucket.
Concurrent rate limiter: Strictly limit the concurrent requests to N. Each time when there is a new request, it will check whether the number of concurrent requests is greater than N. If it is less than N, it is allowed to pass through, and the count is increased by 1. When the requests call ends, the signal is released (count minus 1).
Leaky bucket rate limiter: In contrast with token bucket algorithm, the leaky bucket algorithm can help to smooths the burst of requests and only allows a pre-defined N number of requests. This limiter can force the output flow at a constant rate of N. It is based on a leaky bucket model, the leaky water quantity is time interval*rate. if the leaky water quantity is greater than the number of has used (represented by key_bucket_count), then clear the bucket, that is, set the key_bucket_count to 0. Otherwise, set key_bucket_count minus the leaky water quantity. If the number (requests + key_bucket_count ) is less than the capacity, then allow the requests passing through.
Let's understand the functionality of callback() by reading concurrent rate limiter code, and understand the usage of getKeys() through reading the Lua script of token rate limiter and slide window rate limiter.
The second element, requestKey is a long type and non-duplicate value (generated by a distributed ID generator,it is incremented and smaller than the current time Epochsecond value). The corresponding Lua script in concurrent_request_rate_limiter.lua:
local key = KEYS[1]local capacity = tonumber(ARGV[2])local timestamp = tonumber(ARGV[3])local id = KEYS[2]
Here id is requestKey generated by getKeys() method, it is an uuid(unique value). Subsequent process is as follows:
First, using zcard command to obtain the cardinality of the sorted set, and set count equals the cardinality , if the cardinality is less than the capacity, we will add a new member id (it is an uuid) to the sorted set, with the score of current time(in seconds) . then count =count+1, the cardinality is also incremented by 1 in reality.
All of the code above is executed in Redis as an atomic transaction. If there are a large number of concurrent requests from the same key( such as ip) , the cardinality of the sorted set of this key will increasing sharply, when then capacity limit is exceeded, the service will be denied, that is allowed =0。
In concurrent requests limiter, It is required to release the semaphore when the request is completed. However, it is not included in Lua script.
Let's see the callback function of ConcurrentRateLimiterAlgorithm:
@Override @SuppressWarnings("unchecked") public void callback(final RedisScript<?> script, final List<String> keys, final List<String> scriptArgs) { Singleton.INST.get(ReactiveRedisTemplate.class).opsForZSet().remove(keys.get(0), keys.get(1)).subscribe(); }
Here gives asynchronous subscription, using ReactiveRedisTemplate to delete the elements (key,id) in Redis store. That is once the request operation ends, the semaphore will be released. This remove operation cannot be executed in Lua script. This is just what design intention of callback in RateLimiterAlgorithmSPI .
Here now is current time parameters passed in, set tokens_key to hold the string new_tokens and settokens_key to timeout after ttl of seconds. Set timestamp_key to hold the string value now, and expires after ttl seconds.
The getKeys() in SlidingWindowRateLimiterAlgorithm also overrides the parent class, and the code is consistent with the method in ConcurrentRateLimiterAlgorithm
Following is the Lua code of slide window rate limiter, the receiving of other parameters is omitted.
local timestamp_key = KEYS[2]...... local window_size = tonumber(capacity / rate)local window_time = 1
Here set the window_size to capacity/rate.
local last_requested = 0local exists_key = redis.call('exists', tokens_key)if (exists_key == 1) then last_requested = redis.call('zcard', tokens_key)end
Obtain the cardinality(last_requested) of the tokens_key in the sorted set.
local remain_request = capacity - last_requestedlocal allowed_num = 0if (last_requested < capacity) then allowed_num = 1 redis.call('zadd', tokens_key, now, timestamp_key)end
Calculate remaining available remain_request equals capacity minus last_requested . If last_requested less than capacity ,then allow current requests passing through,add element in the sorted set with (key=timestamp_key, value=now) .
Previously has set window_time=1, using zremrangebyscore command of Redis to remove all the elements in the sorted set stored at tokens_key with a score in [0,now - window_size / window_time] , that is, move the window a window size. Set the expire time of tokens_key to window_size.
In the template method getKeys(final String id) of AbstractRateLimiterAlgorithm,the second key ( represented y secondKey) is a fixed string which concat the input parameter{id}. As we can see from the above three algorithm codes, in the token bucket algorithm, secondKey will be updated to the latest time in the Lua code, so it doesn't matter what value is passed in. In the concurrent rate limiter, secondKey will be used as the key to remove Redis data in the java callback method. In the sliding window algorithm, the secondKey will be added to the sorted set as the key of a new element, and will be removed during window sliding.
That's all, when in a new rate limiter algorithm, the getKeys(final String id)method should be carefully designed according to the logic of the algorithm.
The three parameters in doExecute() method of RateLimiter plugin, exchange is an web request, chain is the execution chain of the plugins,selector is the selection parameters,rule is the policies or rules of rate limiter setting in the system.
protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { //get the `RateLimiterHandle` parameters from cache RateLimiterHandle limiterHandle = RatelimiterRuleHandleCache.getInstance() .obtainHandle(CacheKeyUtils.INST.getKey(rule)); //find the resolver name String resolverKey = Optional.ofNullable(limiterHandle.getKeyResolverName()) .flatMap(name -> Optional.of("-" + RateLimiterKeyResolverFactory.newInstance(name).resolve(exchange))) .orElse(""); return redisRateLimiter.isAllowed(rule.getId() + resolverKey, limiterHandle) .flatMap(response -> { if (!response.isAllowed()) { exchange.getResponse().setStatusCode(HttpStatus.TOO_MANY_REQUESTS); Object error = ShenyuResultWrap.error(ShenyuResultEnum.TOO_MANY_REQUESTS.getCode(), ShenyuResultEnum.TOO_MANY_REQUESTS.getMsg(), null); return WebFluxResultUtils.result(exchange, error); } return chain.execute(exchange); });}
Firstly get the RateLimiterHandle parameters from cache.
Obtains the corresponding Key resolver by RateLimiterHandle instance.
Reactively executes isAllowed() method of RedisRateLimiter.
If not allowed, error handling is performed.
If the request is allowed, dispatch it to the next process of execution chain.
RateLimiter plugin is based on Spring WebFlux,and with Apache Shen SPI, with Redis and Lua script to responsible for the critical algorithm and logic process, make it with characteristics of high concurrency and elastic. As for the RateLimiter SPI.
RateLimiterSPI provides two SPI interface, with interface oriented design and various design patterns, it's easy to develop new rate limiter algorithm and key resolver rule.
RateLimiterAlgorithmSPI supplies four rate limiter algorithms, token bucket,concurrency rate limiter, leaky bucket and sliding window rate limiter. When designing rate limiter algorithm, the KEY generation need to be carefully designed according to the algorithm characteristic. Using Lua script to realize the logic of the algorithm, and design callback() method for asynchronous processing when needed.
Reactive programming, simple and efficient implementation.
As a first-time developer in the Shenyu community, I encountered some "Pitfalls" that were not mentioned in the tutorials I followed to start and develop the project. I have documented the detailed steps I took to start shenyu, shenyu-dashboard, shenyu-website in this blog, hoping to help more new contributors in the community.
Maven is a cross-platform project management tool . As the Apache organization's top open source projects , its main service for Java-based platform project creation , dependency management and project information management.
Download maven and extract it to a path with no Chinese and no spaces.
Add the bin directory under the maven directory to the environment variables. For Windows, if the download directory is E:\apache-maven-3.9.1, add E:\apache-maven-3.9.1\bin to the Path system variable.
Verify that the installation was successful. Type mvn -v in the cmd window, and if the Maven version and Java version appear, the installation is successful. This is shown below:
To speed up the download of project-related dependencies, you need to change the Maven mirrors, here add Aliyun and other mirrors. Change the <mirrors> </mirrors> tag pair in conf/settings.xml to the following:
and add <localRepository>E:/maven_local_repository</localRepository> to the next line of </mirrors> to set the location of Maven local repository. You can specify the exact location yourself.
Configure IDEA environment. Open shenyu project with IDEA, click File -> Settings in the top left corner, and configure Maven as shown below. Where User settings file select your settings.xml directory, and then Local repository will automatically load the localRepository path set in settings.xml:
At this point, IDEA will automatically download the project-related dependencies, you need to wait for a while, when finished, as shown in the following figure:
As you can see, shenyu-e2e, shenyu-examples, shenyu-integrated-test are not marked as Maven projects by IDEA and need to be added manually. Select the pom.xml file in the package and right-click Add as Maven Project.
If the shenyu-e2e build fails, then add the <relativePath>. /pom.xml</relativePath> to <relativePath/>.
Apache ShenYu provides samples of Http, Dubbo, SpringCloud and other applications to access the shenyu gateway, located in the shenyu-example module, here the Http service is used as an example.
Start shenyu-examples-http。
At this point, shenyu-examples-http will automatically register the interface methods annotated with @ShenyuSpringMvcClient and the relevant configuration in application.yml to the gateway. We can open the admin console and see the configuration in Client List -> Proxy -> divide.
Add enablePrintApiLog: true to the shenyu-admin/src/main/resources/application.yml file in the backend repository shenyu as shown below to show the log of frontend interface calls in the backend console.
Start ShenyuAdminBootstrap
Switch to the front-end repository shenyu-dashboard, open README, click npm install, npm start or enter the above command from cmd to access the front-end interface via http://localhost:8000, and display the log of the front-end interface called in the back-end console. Realize the co-development of front-end and back-end.
Execute the npm build command in README and copy all the generated files from the dist folder to the shenyu-admin/src/main/resources/static/ directory in the backend repository.
Apache ShenYu provides examples for Http, Dubbo, SpringCloud and other applications to access the shenyu gateway, located in the shenyu-example module. Here we take the Http service as an example.
If shenyu-example is not marked as a Maven project by IDEA, you can right-click the pom.xml file in the shenyu-example directory to add it as a Maven project.
Start shenyu-examples-http。
At this time, shenyu-examples-http will automatically register the interface method annotated with @ShenyuSpringMvcClient and the related configuration in application.yml to the gateway. When we open the admin console, you can see the relevant configuration in divide and context-path.
Apache ShenYu is an asynchronous, high-performance, cross-language, responsive API gateway.
In ShenYu gateway, data synchronization refers to how to synchronize the updated data to the gateway after the data is sent in the background management system. The Apache ShenYu gateway currently supports data synchronization for ZooKeeper, WebSocket, http long poll, Nacos, etcd and Consul. The main content of this article is based on WebSocket data synchronization source code analysis.
This paper based on shenyu-2.4.0 version of the source code analysis, the official website of the introduction of please refer to the Data Synchronization Design .
Apache ZooKeeper is a software project of the Apache Software Foundation that provides open source distributed configuration services, synchronization services, and naming registries for large-scale distributed computing. ZooKeeper nodes store their data in a hierarchical namespace, much like a file system or a prefix tree structure. Clients can read and write on nodes and thus have a shared configuration service in this way.
We traced the source code from a real case, such as updating a selector data in the Divide plugin to a weight of 90 in a background administration system:
Enter the createSelector() method of the SelectorController class, which validates data, adds or updates data, and returns results.
@Validated@RequiredArgsConstructor@RestController@RequestMapping("/selector")public class SelectorController { @PutMapping("/{id}") public ShenyuAdminResult updateSelector(@PathVariable("id") final String id, @Valid @RequestBody final SelectorDTO selectorDTO) { // set the current selector data ID selectorDTO.setId(id); // create or update operation Integer updateCount = selectorService.createOrUpdate(selectorDTO); // return result return ShenyuAdminResult.success(ShenyuResultMessage.UPDATE_SUCCESS, updateCount); } // ......}
Convert data in the SelectorServiceImpl class using the createOrUpdate() method, save it to the database, publish the event, update upstream.
@RequiredArgsConstructor@Servicepublic class SelectorServiceImpl implements SelectorService { // eventPublisher private final ApplicationEventPublisher eventPublisher; @Override @Transactional(rollbackFor = Exception.class) public int createOrUpdate(final SelectorDTO selectorDTO) { int selectorCount; // build data DTO --> DO SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO); List<SelectorConditionDTO> selectorConditionDTOs = selectorDTO.getSelectorConditions(); // insert or update ? if (StringUtils.isEmpty(selectorDTO.getId())) { // insert into data selectorCount = selectorMapper.insertSelective(selectorDO); // insert into condition data selectorConditionDTOs.forEach(selectorConditionDTO -> { selectorConditionDTO.setSelectorId(selectorDO.getId()); selectorConditionMapper.insertSelective(SelectorConditionDO.buildSelectorConditionDO(selectorConditionDTO)); }); // check selector add if (dataPermissionMapper.listByUserId(JwtUtils.getUserInfo().getUserId()).size() > 0) { DataPermissionDTO dataPermissionDTO = new DataPermissionDTO(); dataPermissionDTO.setUserId(JwtUtils.getUserInfo().getUserId()); dataPermissionDTO.setDataId(selectorDO.getId()); dataPermissionDTO.setDataType(AdminConstants.SELECTOR_DATA_TYPE); dataPermissionMapper.insertSelective(DataPermissionDO.buildPermissionDO(dataPermissionDTO)); } } else { // update data, delete and then insert selectorCount = selectorMapper.updateSelective(selectorDO); //delete rule condition then add selectorConditionMapper.deleteByQuery(new SelectorConditionQuery(selectorDO.getId())); selectorConditionDTOs.forEach(selectorConditionDTO -> { selectorConditionDTO.setSelectorId(selectorDO.getId()); SelectorConditionDO selectorConditionDO = SelectorConditionDO.buildSelectorConditionDO(selectorConditionDTO); selectorConditionMapper.insertSelective(selectorConditionDO); }); } // publish event publishEvent(selectorDO, selectorConditionDTOs); // update upstream updateDivideUpstream(selectorDO); return selectorCount; } // ......}
In the Service class to persist data, i.e. to the database, this should be familiar, not expand. The update upstream operation is analyzed in the corresponding section below, focusing on the publish event operation, which performs data synchronization.
The logic of the publishEvent() method is to find the plugin corresponding to the selector, build the conditional data, and publish the change data.
Change data released by eventPublisher.PublishEvent() is complete, the eventPublisher object is a ApplicationEventPublisher class, The fully qualified class name is org.springframework.context.ApplicationEventPublisher. Here we see that publishing data is done through Spring related functionality.
ApplicationEventPublisher:
When a state change, the publisher calls ApplicationEventPublisher of publishEvent method to release an event, Spring container broadcast event for all observers, The observer's onApplicationEvent method is called to pass the event object to the observer. There are two ways to call publishEvent method, one is to implement the interface by the container injection ApplicationEventPublisher object and then call the method, the other is a direct call container, the method of two methods of publishing events not too big difference.
ApplicationEventPublisher: publish event;
ApplicationEvent: Spring event, record the event source, time, and data;
ApplicationListener: event listener, observer.
In Spring event publishing mechanism, there are three objects,
An object is a publish event ApplicationEventPublisher, in ShenYu through the constructor in the injected a eventPublisher.
The other object is ApplicationEvent , inherited from ShenYu through DataChangedEvent, representing the event object.
public class DataChangedEvent extends ApplicationEvent {//......}
The last object is ApplicationListener in ShenYu in through DataChangedEventDispatcher class implements this interface, as the event listener, responsible for handling the event object.
@Componentpublic class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean { //......}
Released when the event is completed, will automatically enter the DataChangedEventDispatcher class onApplicationEvent() method of handling events.
@Componentpublic class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean { /** * This method is called when there are data changes * @param event */ @Override @SuppressWarnings("unchecked") public void onApplicationEvent(final DataChangedEvent event) { // Iterate through the data change listener (usually using a data synchronization approach is fine) for (DataChangedListener listener : listeners) { // What kind of data has changed switch (event.getGroupKey()) { case APP_AUTH: // app auth data listener.onAppAuthChanged((List<AppAuthData>) event.getSource(), event.getEventType()); break; case PLUGIN: // plugin data listener.onPluginChanged((List<PluginData>) event.getSource(), event.getEventType()); break; case RULE: // rule data listener.onRuleChanged((List<RuleData>) event.getSource(), event.getEventType()); break; case SELECTOR: // selector data listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); break; case META_DATA: // metadata listener.onMetaDataChanged((List<MetaData>) event.getSource(), event.getEventType()); break; default: // other types throw exception throw new IllegalStateException("Unexpected value: " + event.getGroupKey()); } } }}
When there is a data change, the onApplicationEvent method is called and all the data change listeners are iterated to determine the data type and handed over to the appropriate data listener for processing.
ShenYu groups all the data into five categories: APP_AUTH, PLUGIN, RULE, SELECTOR and META_DATA.
Here the data change listener (DataChangedListener) is an abstraction of the data synchronization policy. Its concrete implementation is:
These implementation classes are the synchronization strategies currently supported by ShenYu:
WebsocketDataChangedListener: data synchronization based on Websocket;
ZookeeperDataChangedListener:data synchronization based on Zookeeper;
ConsulDataChangedListener: data synchronization based on Consul;
EtcdDataDataChangedListener:data synchronization based on etcd;
HttpLongPollingDataChangedListener:data synchronization based on http long polling;
NacosDataChangedListener:data synchronization based on nacos;
Given that there are so many implementation strategies, how do you decide which to use?
Because this paper is based on zookeeper data synchronization source code analysis, so here to ZookeeperDataChangedListener as an example, the analysis of how it is loaded and implemented.
A global search in the source code project shows that its implementation is done in the DataSyncConfiguration class.
/** * Data Sync Configuration * By springboot conditional assembly * The type Data sync configuration. */@Configurationpublic class DataSyncConfiguration { /** * zookeeper data sunc * The type Zookeeper listener. */ @Configuration @ConditionalOnProperty(prefix = "shenyu.sync.zookeeper", name = "url") // The condition property is loaded only when it is met @Import(ZookeeperConfiguration.class) static class ZookeeperListener { /** * Config event listener data changed listener. * @param zkClient the zk client * @return the data changed listener */ @Bean @ConditionalOnMissingBean(ZookeeperDataChangedListener.class) public DataChangedListener zookeeperDataChangedListener(final ZkClient zkClient) { return new ZookeeperDataChangedListener(zkClient); } /** * Zookeeper data init zookeeper data init. * @param zkClient the zk client * @param syncDataService the sync data service * @return the zookeeper data init */ @Bean @ConditionalOnMissingBean(ZookeeperDataInit.class) public ZookeeperDataInit zookeeperDataInit(final ZkClient zkClient, final SyncDataService syncDataService) { return new ZookeeperDataInit(zkClient, syncDataService); } } // other code is omitted......}
This configuration class is implemented through the SpringBoot conditional assembly class. The ZookeeperListener class has several annotations:
@ConditionalOnProperty(prefix = "shenyu.sync.zookeeper", name = "url"): attribute condition. The configuration class takes effect only when the condition is met. That is, when we have the following configuration, ZooKeeper is used for data synchronization.
When we take the initiative to configuration, use the zookeeper data synchronization, zookeeperDataChangedListener is generated. So in the event handler onApplicationEvent(), it goes to the corresponding listener. In our case, it is a selector data update, data synchronization is zookeeper, so, the code will enter the ZookeeperDataChangedListener selector data change process.
@Override @SuppressWarnings("unchecked") public void onApplicationEvent(final DataChangedEvent event) { // Iterate through the data change listener (usually using a data synchronization approach is fine) for (DataChangedListener listener : listeners) { // what kind of data has changed switch (event.getGroupKey()) { // other code logic is omitted case SELECTOR: // selector data listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); // In our case, will enter the ZookeeperDataChangedListener selector data change process break; } }
In the onSelectorChanged() method, determine the type of action, whether to refresh synchronization or update or create synchronization. Determine whether the node is in zk based on the current selector data.
/** * use ZooKeeper to publish change data */public class ZookeeperDataChangedListener implements DataChangedListener { // The selector information changed @Override public void onSelectorChanged(final List<SelectorData> changed, final DataEventTypeEnum eventType) { // refresh if (eventType == DataEventTypeEnum.REFRESH && !changed.isEmpty()) { String selectorParentPath = DefaultPathConstants.buildSelectorParentPath(changed.get(0).getPluginName()); deleteZkPathRecursive(selectorParentPath); } // changed data for (SelectorData data : changed) { // build selector real path String selectorRealPath = DefaultPathConstants.buildSelectorRealPath(data.getPluginName(), data.getId()); // delete if (eventType == DataEventTypeEnum.DELETE) { deleteZkPath(selectorRealPath); continue; } // selector parent path String selectorParentPath = DefaultPathConstants.buildSelectorParentPath(data.getPluginName()); // create parent node createZkNode(selectorParentPath); // insert or update data insertZkNode(selectorRealPath, data); } } // create zk node private void createZkNode(final String path) { // create only if it does not exist if (!zkClient.exists(path)) { zkClient.createPersistent(path, true); } } // insert zk node private void insertZkNode(final String path, final Object data) { // create zk node createZkNode(path); // write data by zkClient zkClient.writeData(path, null == data ? "" : GsonUtils.getInstance().toJson(data)); }}
As long as the changed data is correctly written to the zk node, the admin side of the operation is complete. ShenYu uses zk for data synchronization, zk nodes are carefully designed.
In our current case, updating one of the selector data in the Divide plugin with a weight of 90 updates specific nodes in the graph.
We series the above update flow with a sequence diagram.
Assume that the ShenYu gateway is already running properly, and the data synchronization mode is also Zookeeper. How does the gateway receive and process the selector data after updating it on the admin side and sending the changed data to ZK? Let's continue our source code analysis to find out.
There is a ZookeeperSyncDataService class on the gateway, which subscribing to the data node through ZkClient and can sense when the data changes.
/** * ZookeeperSyncDataService */public class ZookeeperSyncDataService implements SyncDataService, AutoCloseable {private void subscribeSelectorDataChanges(final String path) { // zkClient subscribe data zkClient.subscribeDataChanges(path, new IZkDataListener() { @Override public void handleDataChange(final String dataPath, final Object data) { cacheSelectorData(GsonUtils.getInstance().fromJson(data.toString(), SelectorData.class)); // zk node data changed } @Override public void handleDataDeleted(final String dataPath) { unCacheSelectorData(dataPath); // zk node data deleted } }); } // ...}
ZooKeeper's Watch mechanism notifies subscribing clients of node changes. In our case, updating the selector information goes to the handleDataChange() method. cacheSelectorData() is used to process data.
PluginDataSubscriber is an interface, it is only a CommonPluginDataSubscriber implementation class, responsible for data processing plugin, selector and rules.
It has no additional logic and calls the subscribeDataHandler() method directly. Within methods, there are data types (plugins, selectors, or rules) and action types (update or delete) to perform different logic.
/** * The common plugin data subscriber, responsible for handling all plug-in, selector, and rule information */public class CommonPluginDataSubscriber implements PluginDataSubscriber { //...... // handle selector data @Override public void onSelectorSubscribe(final SelectoData selectorData) { subscribeDataHandler(selectorData, DataEventTypeEnum.UPDATE); } // A subscription data handler that handles updates or deletions of data private <T> void subscribeDataHandler(final T classData, final DataEventTypeEnum dataType) { Optional.ofNullable(classData).ifPresent(data -> { // plugin data if (data instanceof PluginData) { PluginData pluginData = (PluginData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cachePluginData(pluginData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(pluginData.getName())).ifPresent(handler -> handler.handlerPlugin(pluginData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removePluginData(pluginData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(pluginData.getName())).ifPresent(handler -> handler.removePlugin(pluginData)); } } else if (data instanceof SelectorData) { // selector data SelectorData selectorData = (SelectorData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cacheSelectData(selectorData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.handlerSelector(selectorData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removeSelectData(selectorData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.removeSelector(selectorData)); } } else if (data instanceof RuleData) { // rule data RuleData ruleData = (RuleData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cacheRuleData(ruleData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(ruleData.getPluginName())).ifPresent(handler -> handler.handlerRule(ruleData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removeRuleData(ruleData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(ruleData.getPluginName())).ifPresent(handler -> handler.removeRule(ruleData)); } } }); }}
// save the data to gateway memoryBaseDataCache.getInstance().cacheSelectData(selectorData);// If each plugin has its own processing logic, then do itOptional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.handlerSelector(selectorData));
One is to save the data to the gateway's memory. BaseDataCache is the class that ultimately caches data, implemented in a singleton pattern. The selector data is stored in the SELECTOR_MAP Map. In the subsequent use, also from this data.
public final class BaseDataCache { // private instance private static final BaseDataCache INSTANCE = new BaseDataCache(); // private constructor private BaseDataCache() { } /** * Gets instance. * public method * @return the instance */ public static BaseDataCache getInstance() { return INSTANCE; } /** * A Map of the cache selector data * pluginName -> SelectorData. */ private static final ConcurrentMap<String, List<SelectorData>> SELECTOR_MAP = Maps.newConcurrentMap(); public void cacheSelectData(final SelectorData selectorData) { Optional.ofNullable(selectorData).ifPresent(this::selectorAccept); } /** * cache selector data. * @param data the selector data */ private void selectorAccept(final SelectorData data) { String key = data.getPluginName(); if (SELECTOR_MAP.containsKey(key)) { // Update operation, delete before insert List<SelectorData> existList = SELECTOR_MAP.get(key); final List<SelectorData> resultList = existList.stream().filter(r -> !r.getId().equals(data.getId())).collect(Collectors.toList()); resultList.add(data); final List<SelectorData> collect = resultList.stream().sorted(Comparator.comparing(SelectorData::getSort)).collect(Collectors.toList()); SELECTOR_MAP.put(key, collect); } else { // Add new operations directly to Map SELECTOR_MAP.put(key, Lists.newArrayList(data)); } }}
Second, if each plugin has its own processing logic, then do it. Through the IDEA editor, you can see that after adding a selector, there are the following plugins and processing. We're not going to expand it here.
After the above source tracking, and through a practical case, in the admin end to update a selector data, the ZooKeeper data synchronization process analysis is clear.
Let's series the data synchronization process on the gateway side through the sequence diagram:
The data synchronization process has been analyzed. In order to prevent the synchronization process from being interrupted, other logic is ignored during the analysis. We also need to analyze the process of Admin synchronization data initialization and gateway synchronization operation initialization.
When admin starts, the current data will be fully synchronized to zk, the implementation logic is as follows:
/** * Zookeeper data init */public class ZookeeperDataInit implements CommandLineRunner { private final ZkClient zkClient; private final SyncDataService syncDataService; /** * Instantiates a new Zookeeper data init. * * @param zkClient the zk client * @param syncDataService the sync data service */ public ZookeeperDataInit(final ZkClient zkClient, final SyncDataService syncDataService) { this.zkClient = zkClient; this.syncDataService = syncDataService; } @Override public void run(final String... args) { String pluginPath = DefaultPathConstants.PLUGIN_PARENT; String authPath = DefaultPathConstants.APP_AUTH_PARENT; String metaDataPath = DefaultPathConstants.META_DATA; // Determine whether data exists in zk if (!zkClient.exists(pluginPath) && !zkClient.exists(authPath) && !zkClient.exists(metaDataPath)) { syncDataService.syncAll(DataEventTypeEnum.REFRESH); } }}
Check whether there is data in zk, if not, then synchronize.
ZookeeperDataInit implements the CommandLineRunner interface. It is an interface provided by SpringBoot that executes the run() method after all Spring Beans initializations and is often used for initialization operations in a project.
SyncDataService.syncAll()
Query data from the database, and then perform full data synchronization, all authentication information, plugin information, selector information, rule information, and metadata information. Synchronous events are published primarily through eventPublisher. After publishing the event via publishEvent(), the ApplicationListener performs the event change operation. In ShenYu is mentioned in DataChangedEventDispatcher.
@Servicepublic class SyncDataServiceImpl implements SyncDataService { // eventPublisher private final ApplicationEventPublisher eventPublisher; /*** * sync all data * @param type the type * @return */ @Override public boolean syncAll(final DataEventTypeEnum type) { // app auth data appAuthService.syncData(); // plugin data List<PluginData> pluginDataList = pluginService.listAll(); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, type, pluginDataList)); // selector data List<SelectorData> selectorDataList = selectorService.listAll(); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, type, selectorDataList)); // rule data List<RuleData> ruleDataList = ruleService.listAll(); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.RULE, type, ruleDataList)); // metadata metaDataService.syncData(); return true; }}
The initial operation of data synchronization on the gateway side is mainly the node in the subscription zk. When there is a data change, the changed data will be received. This relies on the Watch mechanism of ZooKeeper. In ShenYu, the one responsible for zk data synchronization is ZookeeperSyncDataService, also mentioned earlier.
The function logic of ZookeeperSyncDataService is completed in the process of instantiation: the subscription to Shenyu data synchronization node in zk is completed. Subscription here is divided into two kinds, one kind is existing node data updated above, through this zkClient.subscribeDataChanges() method; Another kind is under the current node, add or delete nodes change namely child nodes, it through zkClient.subscribeChildChanges() method.
ZookeeperSyncDataService code is a bit too much, here we use plugin data read and subscribe to track, other types of data operation principle is the same.
/** * zookeeper sync data service */public class ZookeeperSyncDataService implements SyncDataService, AutoCloseable { // At instantiation time, the data is read from the ZK and the node is subscribed public ZookeeperSyncDataService(/* omit the construction argument */ ) { this.zkClient = zkClient; this.pluginDataSubscriber = pluginDataSubscriber; this.metaDataSubscribers = metaDataSubscribers; this.authDataSubscribers = authDataSubscribers; // watch plugin, selector and rule data watcherData(); // watch app auth data watchAppAuth(); // watch metadata watchMetaData(); } private void watcherData() { // plugin node path final String pluginParent = DefaultPathConstants.PLUGIN_PARENT; // all plugin nodes List<String> pluginZKs = zkClientGetChildren(pluginParent); for (String pluginName : pluginZKs) { // watch plugin, selector, rule data node watcherAll(pluginName); } //subscribing to child nodes (adding or removing a plugin) zkClient.subscribeChildChanges(pluginParent, (parentPath, currentChildren) -> { if (CollectionUtils.isNotEmpty(currentChildren)) { for (String pluginName : currentChildren) { // you need to subscribe to all plugin, selector, and rule data for the child node watcherAll(pluginName); } } }); } private void watcherAll(final String pluginName) { // watch plugin watcherPlugin(pluginName); // watch selector watcherSelector(pluginName); // watch rule watcherRule(pluginName); } private void watcherPlugin(final String pluginName) { // plugin path String pluginPath = DefaultPathConstants.buildPluginPath(pluginName); // create if not exist if (!zkClient.exists(pluginPath)) { zkClient.createPersistent(pluginPath, true); } // read the current node data on zk and deserialize it PluginData pluginData = null == zkClient.readData(pluginPath) ? null : GsonUtils.getInstance().fromJson((String) zkClient.readData(pluginPath), PluginData.class); // cached into gateway memory cachePluginData(pluginData); // subscribe plugin data subscribePluginDataChanges(pluginPath, pluginName); } private void cachePluginData(final PluginData pluginData) { //omit implementation logic, is actually the CommonPluginDataSubscriber operation, can connect with the front } private void subscribePluginDataChanges(final String pluginPath, final String pluginName) { // subscribe data changes zkClient.subscribeDataChanges(pluginPath, new IZkDataListener() { @Override public void handleDataChange(final String dataPath, final Object data) { // update //omit implementation logic, is actually the CommonPluginDataSubscriber operation, can connect with the front } @Override public void handleDataDeleted(final String dataPath) { // delete // Omit implementation logic, is actually the CommonPluginDataSubscriber operation, can connect with the front } }); }}
The above source code is given comments, I believe you can understand. The main logic for subscribing to plug-in data is as follows:
Create the current plugin path
Create a path if it does not exist
Read the current node data on zK and deserialize it
e2e (end to end), also known as end-to-end testing, is a method used to test whether the application flow performs as designed from the beginning to the end. The purpose of performing end-to-end testing is to identify system dependencies and ensure that the correct information is passed between various system components and systems. The purpose of end-to-end testing is to test the entire software for dependencies, data integrity, and communication with other systems, interfaces, and databases to simulate a complete production scenario.
e2e testing can test the integrity and accuracy of software systems in simulated real user scenarios, and can verify whether the entire system works as expected and whether different components can work together. There are several benefits of e2e testing:
Help ensure the correctness of system functions.e2e testing can simulate the interaction and operation in real user scenarios, verify whether the entire system can work as expected, and help discover potential problems and defects in the system.
Improve test coverage.e2e testing can cover the entire system, including front-end, back-end, database and other different levels and components, thereby improving test coverage and ensuring comprehensiveness and accuracy of testing.
Ensure the stability of the system.e2e testing can check the stability and robustness of the system in various situations, including system response time, error handling capabilities, concurrency, etc., to help ensure that the system is in the face of high load and abnormal conditions Still able to maintain stable operation.
Reduce testing cost.e2e testing can improve testing efficiency and accuracy, reduce testing cost and time, and thus help enterprises release and deliver high-quality software products more quickly.
In short, e2e testing is a comprehensive testing method that can verify whether the entire system works as expected, improve test coverage and test efficiency, thereby ensuring the stability and correctness of the system, and reducing testing costs and time. And effective testing methods, so we need to improve e2e related codes.
In Apache ShenYu, the main steps of e2e testing are reflected in the script of the GitHub Action workflow, as shown below, the script is located at ~/.github/workflows directory in the e2e file.
When the workflow is triggered, use the dockerfile under the shenyu-dist module to build and upload the images of the admin and bootstrap projects. When the e2e test module is running, the admin and bootstrap images can be loaded. Then build the modules in the examples, and finally execute the test method of the corresponding test module.
If you need to write e2e test cases, you first need to code and debug locally. Currently e2e supports two startup methods, one is docker startup and the other is host startup. These two modes can be switched in the @ShenYuTest annotation in the test class. The host startup method directly starts the services that need to be started locally to run the test code. Before using docker to start, you need to build the corresponding image first. Because ShenYu currently needs to support e2e testing in the github workflow, it is recommended to use the docker startup method.
Currently, the e2e module is mainly divided into four parts: case, client, common and engine.
The case module stores the test cases of the plug-in, and the client module writes the clients of admin and gateway to request corresponding interfaces. Common stores some public classes, and the engine module is the core of the framework. Relying on the testcontainer framework, use java code to start the docker container and complete the configuration operations for admin and gatewat.
Next, I will analyze the e2e startup process based on the source code.
When we execute the test method in the case, the @ShenYuTest annotation will take effect and extend the test class. Through @ShenYuTest, we can choose the startup method, configure related parameters for admin and gateway, and choose the docker-compose file to be executed. For admin and gateway, you can configure the user name, password, data synchronization method and modify the content of yaml required for login.
@ShenYuTest( mode = ShenYuEngineConfigure.Mode.DOCKER, services = { @ShenYuTest.ServiceConfigure( serviceName = "admin", port = 9095, baseUrl = "http://{hostname:localhost}:9095", parameters = { @ShenYuTest.Parameter(key = "username", value = "admin"), @ShenYuTest.Parameter(key = "password", value = "123456"), @ShenYuTest.Parameter(key = "dataSyn", value = "admin_websocket") } ), @ShenYuTest.ServiceConfigure( serviceName = "gateway", port = 9195, baseUrl = "http://{hostname:localhost}:9195", type = ShenYuEngineConfigure.ServiceType.SHENYU_GATEWAY, parameters = { @ShenYuTest.Parameter(key = "application", value = "spring.cloud.discovery.enabled:true,eureka.client.enabled:true"), @ShenYuTest.Parameter(key = "dataSyn", value = "gateway_websocket")})}, dockerComposeFile = "classpath:./docker-compose.mysql.yml")
@ShenYuTest is extended through the ShenYuExtension class, and the configuration of admin and gateway takes effect in beforeAll in ShenYuExtension. The specific effective logic is implemented in the DockerServiceCompose class.
@ShenYuTest configuration items take effect before docker starts, mainly by modifying the yaml file in the resource directory of the test module. Currently, e2e supports testing of different data synchronization methods. The principle is to use the chooseDataSyn method in the DockerServiceCompose class. In the DataSyncHandler, initialize the content that needs to be modified in various data synchronization methods, and finally start the container.
When docker is started, start testing the plug-in function. In the PluginsTest class, there are pre- and post-operations for testing.
Taking the springcloud plug-in as an example, you first need to test whether the registration center and data synchronization can work normally, then start the plug-in and delete the existing selector. To test whether the data is successfully registered into the registration center, you can call the interface of the admin client to test, and to test whether the data synchronization is successful, you can obtain the cache of the gateway for testing.
Then run the test case in the case file and get the use case through @ShenYuScenario.
For different plug-ins, we can build a Case class to store the rules to be tested. All test rules are stored in the list and tested in order. Build selectors and rules in beforeEachSpec, caseSpec stores test entities, if they meet the uri rules, they should exist, otherwise they don’t exist. We need to simulate users to add selectors and rules, because the handler rules of the selectors of each plug-in are not necessarily the same, so we need to write its handle class according to the plug-in requirements. And verify that it complies with the rules with the request. Specific test cases are mainly divided into two categories, one is to match uri rules, such as euqal, path_pattern, start_with, end_with, and the other is request types, such as get, put, post, delete.
When all eight matching conditions are tested, it can be judged that the plug-in function is normal. After the test, we need to restore the environment, delete all selectors, set the plug-in to unavailable, and finally close all containers.
Integration testing is also called E2E (End To End) testing in some projects. It is mainly used to test whether each module can meet expectations after being assembled into a system.
Apache ShenYu puts integration tests in continuous integration, using GitHub Actions to trigger each time a Pull Request or Merge is submitted to the main branch. This can greatly reduce the maintenance cost of the project and improve the stability of Apache ShenYu.
In Apache ShenYu, the main steps of integration testing are embodied in the script of the GitHub Action workflow, as shown below, which is located at ~/.github/workflows directory.
Since we specified pull_request and push.branch: master in on, this workflow will be triggered when we submit pull_request or merge branch to master (push).
For more usage of GitHub Action, you can refer to the documentation of GitHub Action, which will not be introduced in detail here.
In the above command, -P is followed by release,docker, which means that the relevant profile configuration in the pom file will be activated.
The two profiles, release and docker, currently only exist in several submodules under shenyu-dist. The following will take the shenyu-dist-admin module as an example to introduce profiles as release and docker The specific content of the configuration. Also, integration tests only use the shenyu-admin image built in this step.
When -P is followed by release, the above maven-assembly-plugin plugin is activated. In executions, the execution timing of the plugin is bound to the maven life cycle package, which means that it will be triggered when we execute mvn install.
The binary.xml we wrote is specified in the configuration, and the maven-assembly-plugin plugin will copy the required files and package them according to this file. You can click the link to view the file: [shenyu-dist/shenyu-admin-dist/src/main/assembly/binary.xml](https://github.com/apache/shenyu/blob/master/shenyu- dist/shenyu-admin-dist/src/main/assembly/binary.xml)
According to this file, the plugin will "copy" the packaged jar packages, configuration files, startup scripts, etc. under other modules, and finally make them into a compressed package in tar.gz format.
Similar to the release above, here is the activation of the dockerfile-maven-plugin plugin. When mvn install -Pdocker, the plugin will use the dockerfile we wrote to build the docker image.
It should be noted that the dockerfile-maven-plugin currently has limited support for aarch64 architecture devices, and the following error will occur when running the plugin on aarch64 architecture machines. And when I wrote this article, it has not been maintained for a long time, which means that the problem of aarch64 architecture devices using this plugin will not be solved in the short term.
[ERROR] Failed to execute goal com.spotify:dockerfile-maven-plugin:1.4.6:build (tag-latest) on project shenyu-admin-dist: Could not build image: java.util.concurrent.ExecutionException: com.spotify.docker.client.shaded.javax.ws.rs.ProcessingException: java.lang.UnsatisfiedLinkError: could not load FFI provider jnr.ffi.provider.jffi.Provider: ExceptionInInitializerError: Can't overwrite cause with java.lang.UnsatisfiedLinkError: java.lang.UnsatisfiedLinkError: /private/var/folders/w2/j27f16yj7cvf_1cxbgqb89vh0000gn/T/jffi4972193792308935312.dylib: dlopen(/private/var/folders/w2/j27f16yj7cvf_1cxbgqb89vh0000gn/T/jffi4972193792308935312.dylib, 1): no suitable image found. Did find:[ERROR] /private/var/folders/w2/j27f16yj7cvf_1cxbgqb89vh0000gn/T/jffi4972193792308935312.dylib: no matching architecture in universal wrapper[ERROR] /private/var/folders/w2/j27f16yj7cvf_1cxbgqb89vh0000gn/T/jffi4972193792308935312.dylib: no matching architecture in universal wrapper...
Here is a temporary solution:
Open a new shell, enter the following command, and use socat to route the unix socket to the tcp port
Considering the need for release, the current pom file in the project root directory does not contain the example submodule, so the examples module is additionally built in the above step.
Similar to the above, this line of command will also use the maven plugin to build an image for our subsequent docker orchestration.
In order to subdivide the integration tests of different functions of Apache ShenYu, we will build a gateway customized by the integration test module in this step. The so-called "customization" is to introduce the minimum required dependencies in the pom file, and then replace the default shenyu-bootstrap. Similar to the above two steps, this step will also build the docker image.
It is worth noting that the way of packaging and building here is slightly different from that of the shenyu-dist module, which you can find by comparing the pom file.
For example, the docker-compose.yml under the shenyu-integrated-test-http module starts zookeeper, redis, example, admin, gateway and other services in sequence. Among them, the mirrors of example, admin, and gateway are built by us before.
Among them, docker-compose uses depends_on to determine the topological relationship between services, and most services have corresponding health checks, and the next service will not be started until the health check passes.
Run the health check and wait for docker-compose to start#
-name: Wait for docker compose start up completelyif: env.SKIP_CI != 'true'run: bash ./shenyu-integrated-test/${{ matrix.case }}/script/healthcheck.sh
In this step, the host will run the healthcheck.sh script, and then use the curl command to access the health status interface /actuator/health of each service list (in the services.list file), until the service status is normal. will continue.
-name: Check test resultif: env.SKIP_CI != 'true'run:| docker-compose -f ./shenyu-integrated-test/${{ matrix.case }}/docker-compose.yml logs --tail="all" if [[ ${{steps.test.outcome}} == "failure" ]]; then echo "Test Failed" exit 1 else echo "Test Successful" exit 0 fi
When there is an error in the workflow, the log of docker compose can help us to better troubleshoot the problem, so in this step, we will print the log of docker compose.
This article is based on the source code analysis of version 'shenyu-2.6.1'
Content
Shenyu provides a mechanism to customize its own plugins or modify existing plugins, which is implemented internally through the configuration of extPlugin. It needs to meet the following two points:
Implement interface ShenyuPlugin or PluginDataHandler.
After packaging the implemented package, place it in the corresponding path of 'shenyu. extPlugin. path'.
The class that truly implements this logic is' ShenyuLoaderService '. Now let's take a look at how this class handles it.
public ShenyuLoaderService(final ShenyuWebHandler webHandler, final CommonPluginDataSubscriber subscriber, final ShenyuConfig shenyuConfig) { // Information subscription for plugin information this.subscriber = subscriber; // The WebHandler encapsulated by Shenyu contains all the plugin logic this.webHandler = webHandler; // configuration information this.shenyuConfig = shenyuConfig; // The configuration information of the extension plugin, such as path, whether it is enabled, how many threads are enabled to process, and the frequency of loading checks ExtPlugin config = shenyuConfig.getExtPlugin(); // If enabled, create a scheduled task to check and load if (config.getEnabled()) { // Create a scheduled task with a specified thread name ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(config.getThreads(), ShenyuThreadFactory.create("plugin-ext-loader", true)); // Create a task to be executed at a fixed frequency, with a default time of 30 seconds and execution every 300 seconds executor.scheduleAtFixedRate(() -> loadExtOrUploadPlugins(null), config.getScheduleDelay(), config.getScheduleTime(), TimeUnit.SECONDS); } }
This class has the following properties:
WebHandler: This class is the entry point for shenyu to process requests, referencing all plugin data. After the extension plugin is loaded, it needs to be updated.
Subscriber: This class is the entry point for the subscription of plugins, referencing the subscription processing classes of all plugins. After the extension configuration is loaded, synchronous updates are also required.
Executor: A scheduled task will be created inside' ShenyuLoaderService 'to periodically scan and load jar packages under the specified path, facilitating the loading of extended plugins and achieving dynamic discovery
By default, it will scan every 300 seconds after 30 seconds of startup.
Meanwhile, the decision to enable extension plugin functionality can be made through the configuration of shenyu. extPlugin. enabled.
The above configurations can be adjusted in the configuration file:
shenyu:extPlugin:path:# Storage directory for extension pluginsenabled:true# Is the extension function enabledthreads:1# Number of threads loaded by scanningscheduleTime:300# The frequency of task executionscheduleDelay:30# How long after the task starts to execute
Next, let's take a look at the loading logic:
public void loadExtOrUploadPlugins(final PluginData uploadedJarResource) { try { List<ShenyuLoaderResult> plugins = new ArrayList<>(); // Obtain the holding object of ShenyuPluginClassloader ShenyuPluginClassloaderHolder singleton = ShenyuPluginClassloaderHolder.getSingleton(); if (Objects.isNull(uploadedJarResource)) { // If the parameter is empty, load all jar packages from the extended directory // PluginJar: Data containing the ShenyuPlugin interface and PluginDataHandler interface List<PluginJarParser.PluginJar> uploadPluginJars = ShenyuExtPathPluginJarLoader.loadExtendPlugins(shenyuConfig.getExtPlugin().getPath()); // Traverse all pending plugins for (PluginJarParser.PluginJar extPath : uploadPluginJars) { LOG.info("shenyu extPlugin find new {} to load", extPath.getAbsolutePath()); // Use the loader of the extension plugin to load the specified plugin, facilitating subsequent loading and unloading ShenyuPluginClassLoader extPathClassLoader = singleton.createPluginClassLoader(extPath); // Using ShenyuPluginClassLoader for loading // The main logic is to determine whether to implement ShenyuPlugin interface, PluginDataHandler interface, or identify annotations such as @ Component \ @ Service. If so, register as SpringBean // Construct ShenyuLoaderResult object plugins.addAll(extPathClassLoader.loadUploadedJarPlugins()); } } else { // Load the specified jar, with the same logic as loading all PluginJarParser.PluginJar pluginJar = PluginJarParser.parseJar(Base64.getDecoder().decode(uploadedJarResource.getPluginJar())); LOG.info("shenyu upload plugin jar find new {} to load", pluginJar.getJarKey()); ShenyuPluginClassLoader uploadPluginClassLoader = singleton.createPluginClassLoader(pluginJar); plugins.addAll(uploadPluginClassLoader.loadUploadedJarPlugins()); } // Add the extended plugins to the plugin list of ShenyuWebHandler, and subsequent requests will go through the added plugin content loaderPlugins(plugins); } catch (Exception e) { LOG.error("shenyu plugins load has error ", e); } }
The logic processed by this method:
Check if the parameter uploadedJarResource has a value. If not, all will be loaded. Otherwise, load the specified resource jar package for processing.
Retrieve the specified jar package from shenyu. extPlugin. path and encapsulate it as a PluginJar object, which contains the following information about the jar package:
version: version information
groupId: The groupId of the package
artifactId: The artifactId of the package
absolutePath: Absolute path
clazzMap: Bytecode corresponding to class
resourceMap: Bytecode of jar package
Create a corresponding ClassLoader using ShenyuPluginClassloaderHolder, with the corresponding class being 'ShenyuPluginClassLoader', and load the corresponding class accordingly.
Call ShenyuPluginClassLoader. loadUploadedJarPlugins to load the corresponding class and register it as a Spring Bean, which can be managed using the Spring container
Call the loaderPlugins method to update the extended plugin to'webHandler and subscriber.
For the content in the provided jar package, the loader will only handle classes of the specified interface type, and the implementation logic is in the ShenyuPluginClassLoader.loadUploadedJarPlugins() method.
public List<ShenyuLoaderResult> loadUploadedJarPlugins() { List<ShenyuLoaderResult> results = new ArrayList<>(); // All class mapping relationships Set<String> names = pluginJar.getClazzMap().keySet(); // Traverse all classes names.forEach(className -> { Object instance; try { // Try creating objects and, if possible, add them to the Spring container instance = getOrCreateSpringBean(className); if (Objects.nonNull(instance)) { // Building the ShenyuLoaderResult object results.add(buildResult(instance)); LOG.info("The class successfully loaded into a upload-Jar-plugin {} is registered as a spring bean", className); } } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { LOG.warn("Registering upload-Jar-plugins succeeds spring bean fails:{}", className, e); } }); return results; }
This method is responsible for building all eligible objects and encapsulating them into a ShenyuLoaderResult object. This object is encapsulated for the created object and will be processed in the method buildResult().
private ShenyuLoaderResult buildResult(final Object instance) { ShenyuLoaderResult result = new ShenyuLoaderResult(); // Does the created object implement ShenyuPlugin if (instance instanceof ShenyuPlugin) { result.setShenyuPlugin((ShenyuPlugin) instance); // Does the created object implement PluginDataHandler } else if (instance instanceof PluginDataHandler) { result.setPluginDataHandler((PluginDataHandler) instance); } return result; }
Simultaneously enter the method getOrCreatSpringBean() for further analysis:
private <T> T getOrCreateSpringBean(final String className) throws ClassNotFoundException, IllegalAccessException, InstantiationException { // Confirm if it has been registered. If so, do not process it and return directly if (SpringBeanUtils.getInstance().existBean(className)) { return SpringBeanUtils.getInstance().getBeanByClassName(className); } lock.lock(); try { // Double check, T inst = SpringBeanUtils.getInstance().getBeanByClassName(className); if (Objects.isNull(inst)) { // Using ShenyuPluginClassLoader to load classes Class<?> clazz = Class.forName(className, false, this); //Exclude ShenyuPlugin subclass and PluginDataHandler subclass // without adding @Component @Service annotation // Confirm if it is a subclass of ShenyuPlugin or PluginDataHandler boolean next = ShenyuPlugin.class.isAssignableFrom(clazz) || PluginDataHandler.class.isAssignableFrom(clazz); if (!next) { // If not, confirm if @ Component and @ Service annotations are identified Annotation[] annotations = clazz.getAnnotations(); next = Arrays.stream(annotations).anyMatch(e -> e.annotationType().equals(Component.class) || e.annotationType().equals(Service.class)); } if (next) { // If the above content is met, register the bean GenericBeanDefinition beanDefinition = new GenericBeanDefinition(); beanDefinition.setBeanClassName(className); beanDefinition.setAutowireCandidate(true); beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); // Registering beans String beanName = SpringBeanUtils.getInstance().registerBean(beanDefinition, this); // create object inst = SpringBeanUtils.getInstance().getBeanByClassName(beanName); } } return inst; } finally { lock.unlock(); } }
The logic is roughly as follows:
Check if the interface ShenyuPlugin or PluginDataHandler has been implemented. If not, check if @Component or @Service has been identified`.
If the condition of 1 is met, register the object in the Spring container and return the created object.
After the plugin registration is successful, the plugin is only instantiated, but it will not take effect yet because it has not been added to Shenyu's plugin chain. The synchronization logic is implemented by the loaderPlugins() method.
private void loaderPlugins(final List<ShenyuLoaderResult> results) { if (CollectionUtils.isEmpty(results)) { return; } // Get all objects that implement the interface ShenyuPlugin List<ShenyuPlugin> shenyuExtendPlugins = results.stream().map(ShenyuLoaderResult::getShenyuPlugin).filter(Objects::nonNull).collect(Collectors.toList()); // Synchronize updating plugins in webHandler webHandler.putExtPlugins(shenyuExtendPlugins); // Get all objects that implement the interface PluginDataHandler List<PluginDataHandler> handlers = results.stream().map(ShenyuLoaderResult::getPluginDataHandler).filter(Objects::nonNull).collect(Collectors.toList()); // Synchronize updating handlers in subscriber subscriber.putExtendPluginDataHandler(handlers); }
The logic of this method processes two data points:
Synchronize the data that implements the ShenyuPlugin interface to the plugins list of webHandler.
public void putExtPlugins(final List<ShenyuPlugin> extPlugins) { if (CollectionUtils.isEmpty(extPlugins)) { return; } // Filter out newly added plugins final List<ShenyuPlugin> shenyuAddPlugins = extPlugins.stream() .filter(e -> plugins.stream().noneMatch(plugin -> plugin.named().equals(e.named()))) .collect(Collectors.toList()); // Filter out updated plugins and determine if they have the same name as the old one, then it is an update final List<ShenyuPlugin> shenyuUpdatePlugins = extPlugins.stream() .filter(e -> plugins.stream().anyMatch(plugin -> plugin.named().equals(e.named()))) .collect(Collectors.toList()); // If there is no data, skip if (CollectionUtils.isEmpty(shenyuAddPlugins) && CollectionUtils.isEmpty(shenyuUpdatePlugins)) { return; } // Copy old data // copy new list List<ShenyuPlugin> newPluginList = new ArrayList<>(plugins); // Add new plugin data // Add extend plugin from pluginData or shenyu ext-lib this.sourcePlugins.addAll(shenyuAddPlugins); // Add new data if (CollectionUtils.isNotEmpty(shenyuAddPlugins)) { shenyuAddPlugins.forEach(plugin -> LOG.info("shenyu auto add extends plugins:{}", plugin.named())); newPluginList.addAll(shenyuAddPlugins); } // Modify updated data if (CollectionUtils.isNotEmpty(shenyuUpdatePlugins)) { shenyuUpdatePlugins.forEach(plugin -> LOG.info("shenyu auto update extends plugins:{}", plugin.named())); for (ShenyuPlugin updatePlugin : shenyuUpdatePlugins) { for (int i = 0; i < newPluginList.size(); i++) { if (newPluginList.get(i).named().equals(updatePlugin.named())) { newPluginList.set(i, updatePlugin); } } for (int i = 0; i < this.sourcePlugins.size(); i++) { if (this.sourcePlugins.get(i).named().equals(updatePlugin.named())) { this.sourcePlugins.set(i, updatePlugin); } } } } // REORDER plugins = sortPlugins(newPluginList); }
Synchronize the data that implements the PluginDataHandler interface to the handlers list of the subscriber.
public void putExtendPluginDataHandler(final List<PluginDataHandler> handlers) { if (CollectionUtils.isEmpty(handlers)) { return; } // Traverse all data for (PluginDataHandler handler : handlers) { String pluginNamed = handler.pluginNamed(); // Update existing PluginDataHandler list MapUtils.computeIfAbsent(handlerMap, pluginNamed, name -> { LOG.info("shenyu auto add extends plugin data handler name is :{}", pluginNamed); return handler; }); } }
At this point, the analysis of the loading process of the extension plugin is completed.
First, look at the ContextPathPlugin#doExecute method, which is the core of this plugin.
protected Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { ... // 1. get the contextMappingHandle from the JVM cache ContextMappingHandle contextMappingHandle = ContextPathPluginDataHandler.CACHED_HANDLE.get().obtainHandle(CacheKeyUtils.INST.getKey(rule)); ... // 2. set shenyu context according to contextMappingHandle buildContextPath(shenyuContext, contextMappingHandle); return chain.execute(exchange);}
Get the contextMappingHandle from the JVM cache
The contextMappingHandle here is an instance of the ContextMappingHandle class, which has two member variables: contextPath and addPrefix
These two variables have appeared in the Rules form in the Admin before, and they are updated when the data is synchronized.
Set shenyu context according to contextMappingHandle
Below is the source code of the ContextPathPlugin#buildContextPath method
private void buildContextPath(final ShenyuContext context, final ContextMappingHandle handle) { String realURI = ""; // 1. set the context path of shenyu, remove the prefix of the real URI according to the length of the contextPath if (StringUtils.isNoneBlank(handle.getContextPath())) { context.setContextPath(handle.getContextPath()); context.setModule(handle.getContextPath()); realURI = context.getPath().substring(handle.getContextPath().length()); } // add prefix if (StringUtils.isNoneBlank(handle.getAddPrefix())) { if (StringUtils.isNotBlank(realURI)) { realURI = handle.getAddPrefix() + realURI; } else { realURI = handle.getAddPrefix() + context.getPath(); } } context.setRealUrl(realURI);}
Set the context path of shenyu, remove the prefix of the real URI according to the length of the contextPath
You may be wondering whether there is a problem with the so-called "according to the length of the contextPath" here?
In fact, such a judgment is not a problem, because the request will be processed by the plugin only after it is matched by the Selector and Rules. Therefore, under the premise of setting up Selector and Rules, it is completely possible to meet the needs of converting a specific contextPath.
Then, the ContextPathPlugin class has a more important method skip, part of the code is shown below. We can find: If it is a call to the RPC service, the context_path plugin will be skipped directly.
Finally, the context-path plugin has another class ContextPathPluginDataHandler. The function of this class is to subscribe to the data of the plug-in. When the plugin configuration is modified, deleted, or added, the data is modified, deleted, or added to the JVM cache.
The ShenYu gateway uses the divide plugin to handle http requests. You can see the official documentation Quick start with Http to learn how to use this plugin.
This article is based on shenyu-2.4.3 version for source code analysis, please refer to Http Proxy for the introduction of the official website.
Annotation scanning is done through SpringMvcClientBeanPostProcessor, which implements the BeanPostProcessor interface and is a post-processor provided by Spring.
During constructor instantiation.
Read the property configuration
Add annotations, read path information
Start the registry and register with shenyu-admin
public class SpringMvcClientBeanPostProcessor implements BeanPostProcessor { //... /** * Constructor instantiation */ public SpringMvcClientBeanPostProcessor(final PropertiesConfig clientConfig, final ShenyuClientRegisterRepository shenyuClientRegisterRepository) { // 1. read Properties Properties props = clientConfig.getProps(); this.appName = props.getProperty(ShenyuClientConstants.APP_NAME); this.contextPath = props.getProperty(ShenyuClientConstants.CONTEXT_PATH, ""); if (StringUtils.isBlank(appName) && StringUtils.isBlank(contextPath)) { String errorMsg = "http register param must config the appName or contextPath"; LOG.error(errorMsg); throw new ShenyuClientIllegalArgumentException(errorMsg); } this.isFull = Boolean.parseBoolean(props.getProperty(ShenyuClientConstants.IS_FULL, Boolean.FALSE.toString())); // 2. add annotation mappingAnnotation.add(ShenyuSpringMvcClient.class); mappingAnnotation.add(PostMapping.class); mappingAnnotation.add(GetMapping.class); mappingAnnotation.add(DeleteMapping.class); mappingAnnotation.add(PutMapping.class); mappingAnnotation.add(RequestMapping.class); // 3. start register cneter publisher.start(shenyuClientRegisterRepository); } @Override public Object postProcessAfterInitialization(@NonNull final Object bean, @NonNull final String beanName) throws BeansException { // override post process return bean; }
Rewrite post-processor logic: read annotation information, construct metadata objects and URI objects, and register them with shenyu-admin.
@Override public Object postProcessAfterInitialization(@NonNull final Object bean, @NonNull final String beanName) throws BeansException { // 1. If the all service is registered or is not a Controller class, it is not handled if (Boolean.TRUE.equals(isFull) || !hasAnnotation(bean.getClass(), Controller.class)) { return bean; } // 2. Read the annotations on the class ShenyuSpringMvcClient final ShenyuSpringMvcClient beanShenyuClient = AnnotationUtils.findAnnotation(bean.getClass(), ShenyuSpringMvcClient.class); // 2.1 build superPath final String superPath = buildApiSuperPath(bean.getClass()); // 2.2 whether to register the entire class method if (Objects.nonNull(beanShenyuClient) && superPath.contains("*")) { // build the metadata object and register it with shenyu-admin publisher.publishEvent(buildMetaDataDTO(beanShenyuClient, pathJoin(contextPath, superPath))); return bean; } // 3. read all methods final Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(bean.getClass()); for (Method method : methods) { // 3.1 read the annotations on the method ShenyuSpringMvcClient ShenyuSpringMvcClient methodShenyuClient = AnnotationUtils.findAnnotation(method, ShenyuSpringMvcClient.class); // If there is no annotation on the method, use the annotation on the class methodShenyuClient = Objects.isNull(methodShenyuClient) ? beanShenyuClient : methodShenyuClient; if (Objects.nonNull(methodShenyuClient)) { // 3.2 Build path information, build metadata objects, register with shenyu-admin publisher.publishEvent(buildMetaDataDTO(methodShenyuClient, buildApiPath(method, superPath))); } } return bean; }
If you are registering the whole service or not Controller class, do not handle it
read the annotation on the class ShenyuSpringMvcClient, if the whole class is registered, build the metadata object here and register it with shenyu-admin.
Annotation on the handler method ShenyuSpringMvcClient, build path information for the specific method, build the metadata object and then register it with shenyu-admin
There are two methods here that take path and need special instructions.
buildApiSuperPath()
Construct SuperPath: first take the path property from the annotation ShenyuSpringMvcClient on the class, if not, take the path information from the RequestMapping annotation on the current class.
private String buildApiSuperPath(@NonNull final Class<?> method) { // First take the path property from the annotation ShenyuSpringMvcClient on the class ShenyuSpringMvcClient shenyuSpringMvcClient = AnnotationUtils.findAnnotation(method, ShenyuSpringMvcClient.class); if (Objects.nonNull(shenyuSpringMvcClient) && StringUtils.isNotBlank(shenyuSpringMvcClient.path())) { return shenyuSpringMvcClient.path(); } // Take the path information from the RequestMapping annotation of the current class RequestMapping requestMapping = AnnotationUtils.findAnnotation(method, RequestMapping.class); if (Objects.nonNull(requestMapping) && ArrayUtils.isNotEmpty(requestMapping.path()) && StringUtils.isNotBlank(requestMapping.path()[0])) { return requestMapping.path()[0]; } return ""; }
buildApiPath()
Build path: first read the annotation ShenyuSpringMvcClient on the method and build it if it exists; otherwise get the path information from other annotations on the method; complete path = contextPath(context information) + superPath(class information) + methodPath(method information).
private String buildApiPath(@NonNull final Method method, @NonNull final String superPath) { // 1. Read the annotation ShenyuSpringMvcClient on the method ShenyuSpringMvcClient shenyuSpringMvcClient = AnnotationUtils.findAnnotation(method, ShenyuSpringMvcClient.class); // 1.1 If path exists, build if (Objects.nonNull(shenyuSpringMvcClient) && StringUtils.isNotBlank(shenyuSpringMvcClient.path())) { //1.2 path = contextPath+superPath+methodPath return pathJoin(contextPath, superPath, shenyuSpringMvcClient.path()); } // 2. Get path information from other annotations on the method final String path = getPathByMethod(method); if (StringUtils.isNotBlank(path)) { // 2.1 path = contextPath+superPath+methodPath return pathJoin(contextPath, superPath, path); } return pathJoin(contextPath, superPath); }
getPathByMethod()
Get path information from other annotations on the method, other annotations include.
ShenyuSpringMvcClient
PostMapping
GetMapping
DeleteMapping
PutMapping
RequestMapping
private String getPathByMethod(@NonNull final Method method) { // Iterate through interface annotations to get path information for (Class<? extends Annotation> mapping : mappingAnnotation) { final String pathByAnnotation = getPathByAnnotation(AnnotationUtils.findAnnotation(method, mapping), pathAttributeNames); if (StringUtils.isNotBlank(pathByAnnotation)) { return pathByAnnotation; } } return null; }
After the scanning annotation is finished, construct the metadata object and send the object to shenyu-admin to complete the registration.
Metadata
Includes the rule information of the currently registered method: contextPath, appName, registration path, description information, registration type, whether it is enabled, rule name and whether to register metadata.
private MetaDataRegisterDTO buildMetaDataDTO(@NonNull final ShenyuSpringMvcClient shenyuSpringMvcClient, final String path) { return MetaDataRegisterDTO.builder() .contextPath(contextPath) // contextPath .appName(appName) // appName .path(path) // Registered path, used when gateway rules match .pathDesc(shenyuSpringMvcClient.desc()) // desc info .rpcType(RpcTypeEnum.HTTP.getName()) // divide plugin, http type when default .enabled(shenyuSpringMvcClient.enabled()) // is enabled? .ruleName(StringUtils.defaultIfBlank(shenyuSpringMvcClient.ruleName(), path))//rule name .registerMetaData(shenyuSpringMvcClient.registerMetaData()) // whether to register metadata information .build(); }
The specific registration logic is implemented by the registration center, which has been analyzed in the previous articles and will not be analyzed in depth here.
ContextRegisterListener is responsible for registering the client's URI information to shenyu-admin, it implements the ApplicationListener interface, when the context refresh event ContextRefreshedEvent occurs, the onApplicationEvent() method is executed to implement the registration logic.
public class ContextRegisterListener implements ApplicationListener<ContextRefreshedEvent>, BeanFactoryAware { //...... /** * Constructor instantiation */ public ContextRegisterListener(final PropertiesConfig clientConfig) { // read Properties final Properties props = clientConfig.getProps(); this.isFull = Boolean.parseBoolean(props.getProperty(ShenyuClientConstants.IS_FULL, Boolean.FALSE.toString())); this.contextPath = props.getProperty(ShenyuClientConstants.CONTEXT_PATH); if (Boolean.TRUE.equals(isFull)) { if (StringUtils.isBlank(contextPath)) { final String errorMsg = "http register param must config the contextPath"; LOG.error(errorMsg); throw new ShenyuClientIllegalArgumentException(errorMsg); } } this.port = Integer.parseInt(Optional.ofNullable(props.getProperty(ShenyuClientConstants.PORT)).orElseGet(() -> "-1")); this.appName = props.getProperty(ShenyuClientConstants.APP_NAME); this.protocol = props.getProperty(ShenyuClientConstants.PROTOCOL, ShenyuClientConstants.HTTP); this.host = props.getProperty(ShenyuClientConstants.HOST); } @Override public void setBeanFactory(final BeanFactory beanFactory) throws BeansException { this.beanFactory = beanFactory; } // Execute application events @Override public void onApplicationEvent(@NonNull final ContextRefreshedEvent contextRefreshedEvent) { // The method is guaranteed to be executed once if (!registered.compareAndSet(false, true)) { return; } // 1. If you are registering for the entire service if (Boolean.TRUE.equals(isFull)) { // Build metadata and register publisher.publishEvent(buildMetaDataDTO()); } try { // get port final int mergedPort = port <= 0 ? PortUtils.findPort(beanFactory) : port; // 2. Constructing URI data and registering publisher.publishEvent(buildURIRegisterDTO(mergedPort)); } catch (ShenyuException e) { throw new ShenyuException(e.getMessage() + "please config ${shenyu.client.http.props.port} in xml/yml !"); } } // build URI data private URIRegisterDTO buildURIRegisterDTO(final int port) { return URIRegisterDTO.builder() .contextPath(this.contextPath) // contextPath .appName(appName) // appName .protocol(protocol) // protocol .host(IpUtils.isCompleteHost(this.host) ? this.host : IpUtils.getHost(this.host)) //host .port(port) // port .rpcType(RpcTypeEnum.HTTP.getName()) // divide plugin, default registration http type .build(); } // build MetaData private MetaDataRegisterDTO buildMetaDataDTO() { return MetaDataRegisterDTO.builder() .contextPath(contextPath) .appName(appName) .path(contextPath) .rpcType(RpcTypeEnum.HTTP.getName()) .enabled(true) .ruleName(contextPath) .build(); }}
The metadata and URI data registered by the client through the registry are processed in shenyu-admin, which is responsible for storing to the database and synchronizing to the shenyu gateway. The client registration processing logic of Divide plugin is in ShenyuClientRegisterDivideServiceImpl. The inheritance relationship is as follows.
The metadata MetaDataRegisterDTO object registered by the client through the registry is picked up and dropped in the register() method of shenyu-admin.
Build contextPath, find if the selector information exists, if it does, return id; if it doesn't, create the default selector information.
@Override public String registerDefault(final MetaDataRegisterDTO dto, final String pluginName, final String selectorHandler) { // build contextPath String contextPath = ContextPathUtils.buildContextPath(dto.getContextPath(), dto.getAppName()); // Find if selector information exists by name SelectorDO selectorDO = findByNameAndPluginName(contextPath, pluginName); if (Objects.isNull(selectorDO)) { // Create a default selector message if it does not exist return registerSelector(contextPath, pluginName, selectorHandler); } return selectorDO.getId(); }
Default Selector Information
Construct the default selector information and its conditional properties here.
//register selector private String registerSelector(final String contextPath, final String pluginName, final String selectorHandler) { // build selector SelectorDTO selectorDTO = buildSelectorDTO(contextPath, pluginMapper.selectByName(pluginName).getId()); selectorDTO.setHandle(selectorHandler); //register default Selector return registerDefault(selectorDTO); } //build private SelectorDTO buildSelectorDTO(final String contextPath, final String pluginId) { //build default SelectorDTO selectorDTO = buildDefaultSelectorDTO(contextPath); selectorDTO.setPluginId(pluginId); //build the conditional properties of the default selector selectorDTO.setSelectorConditions(buildDefaultSelectorConditionDTO(contextPath)); return selectorDTO; }
private List<SelectorConditionDTO> buildDefaultSelectorConditionDTO(final String contextPath) { SelectorConditionDTO selectorConditionDTO = new SelectorConditionDTO(); selectorConditionDTO.setParamType(ParamTypeEnum.URI.getName()); // default URI selectorConditionDTO.setParamName("/"); selectorConditionDTO.setOperator(OperatorEnum.MATCH.getAlias()); // default match selectorConditionDTO.setParamValue(contextPath + AdminConstants.URI_SUFFIX); // default /contextPath/** return Collections.singletonList(selectorConditionDTO);}
Register default selector
@Overridepublic String registerDefault(final SelectorDTO selectorDTO) { //selector info SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO); //selector condition info List<SelectorConditionDTO> selectorConditionDTOs = selectorDTO.getSelectorConditions(); if (StringUtils.isEmpty(selectorDTO.getId())) { // insert selector information into the database selectorMapper.insertSelective(selectorDO); // insert selector condition information into the database selectorConditionDTOs.forEach(selectorConditionDTO -> { selectorConditionDTO.setSelectorId(selectorDO.getId()); selectorConditionMapper.insertSelective(SelectorConditionDO.buildSelectorConditionDO(selectorConditionDTO)); }); } // Publish synchronization events to synchronize selection information and its conditional attributes to the gateway publishEvent(selectorDO, selectorConditionDTOs); return selectorDO.getId();}
Get a valid URI from the URI registered by the client, update the corresponding selector handle property, and send a selector update event to the gateway.
@Override public String doRegisterURI(final String selectorName, final List<URIRegisterDTO> uriList) { //check if (CollectionUtils.isEmpty(uriList)) { return ""; } //get selector SelectorDO selectorDO = selectorService.findByNameAndPluginName(selectorName, PluginNameAdapter.rpcTypeAdapter(rpcType())); if (Objects.isNull(selectorDO)) { throw new ShenyuException("doRegister Failed to execute,wait to retry."); } // gte valid URI List<URIRegisterDTO> validUriList = uriList.stream().filter(dto -> Objects.nonNull(dto.getPort()) && StringUtils.isNotBlank(dto.getHost())).collect(Collectors.toList()); // build handle String handler = buildHandle(validUriList, selectorDO); if (handler != null) { selectorDO.setHandle(handler); SelectorData selectorData = selectorService.buildByName(selectorName, PluginNameAdapter.rpcTypeAdapter(rpcType())); selectorData.setHandle(handler); // Update the handle property of the selector to the database selectorService.updateSelective(selectorDO); // Send selector update events to the gateway eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, DataEventTypeEnum.UPDATE, Collections.singletonList(selectorData))); } return ShenyuResultMessage.SUCCESS; }
The source code analysis on service registration is completed as well as the analysis flow chart is as follows.
The next step is to analyze how the divide plugin initiates a call to the http service based on this information.
The divide plugin is the core processing plugin used by the gateway to handle http protocol requests.
Take the case provided on the official website Quick start with Http as an example, a direct connection request is as follows.
GET http://localhost:8189/order/findById?id=100Accept: application/json
After proxying through the ShenYu gateway, the request is as follows.
GET http://localhost:9195/http/order/findById?id=100Accept: application/json
The services proxied by the ShenYu gateway are still able to request the previous services, where the divide plugin comes into play. The class inheritance relationship is as follows.
After passing the ShenYu gateway proxy, the request entry is ShenyuWebHandler, which implements the org.springframework.web.server.WebHandler interface.
public final class ShenyuWebHandler implements WebHandler, ApplicationListener<SortPluginEvent> { //...... /** * hanlde web reuest */ @Override public Mono<Void> handle(@NonNull final ServerWebExchange exchange) { // execute plugin chain Mono<Void> execute = new DefaultShenyuPluginChain(plugins).execute(exchange); if (scheduled) { return execute.subscribeOn(scheduler); } return execute; } private static class DefaultShenyuPluginChain implements ShenyuPluginChain { private int index; private final List<ShenyuPlugin> plugins; /** * Instantiating the default plugin chain */ DefaultShenyuPluginChain(final List<ShenyuPlugin> plugins) { this.plugins = plugins; } /** * Execute each plugin */ @Override public Mono<Void> execute(final ServerWebExchange exchange) { return Mono.defer(() -> { if (this.index < plugins.size()) { // get current plugin ShenyuPlugin plugin = plugins.get(this.index++); // is skip ? boolean skip = plugin.skip(exchange); if (skip) { // If skipped, execute the next return this.execute(exchange); } // execute current plugin return plugin.execute(exchange, this); } return Mono.empty(); }); } }}
Initiate the request call in the execute() method.
Get the specified timeout, number of retries
Initiate the request
Retry after failure according to the specified retry policy
public abstract class AbstractHttpClientPlugin<R> implements ShenyuPlugin { protected static final Logger LOG = LoggerFactory.getLogger(AbstractHttpClientPlugin.class); @Override public final Mono<Void> execute(final ServerWebExchange exchange, final ShenyuPluginChain chain) { // shenyu Context final ShenyuContext shenyuContext = exchange.getAttribute(Constants.CONTEXT); assert shenyuContext != null; // uri final URI uri = exchange.getAttribute(Constants.HTTP_URI); if (Objects.isNull(uri)) { Object error = ShenyuResultWrap.error(exchange, ShenyuResultEnum.CANNOT_FIND_URL, null); return WebFluxResultUtils.result(exchange, error); } // get time out final long timeout = (long) Optional.ofNullable(exchange.getAttribute(Constants.HTTP_TIME_OUT)).orElse(3000L); final Duration duration = Duration.ofMillis(timeout); // get retry times final int retryTimes = (int) Optional.ofNullable(exchange.getAttribute(Constants.HTTP_RETRY)).orElse(0); // get retry strategy final String retryStrategy = (String) Optional.ofNullable(exchange.getAttribute(Constants.RETRY_STRATEGY)).orElseGet(RetryEnum.CURRENT::getName); LOG.info("The request urlPath is {}, retryTimes is {}, retryStrategy is {}", uri.toASCIIString(), retryTimes, retryStrategy); // build header final HttpHeaders httpHeaders = buildHttpHeaders(exchange); // do request final Mono<R> response = doRequest(exchange, exchange.getRequest().getMethodValue(), uri, httpHeaders, exchange.getRequest().getBody()) .timeout(duration, Mono.error(new TimeoutException("Response took longer than timeout: " + duration))) .doOnError(e -> LOG.error(e.getMessage(), e)); // Retry Policy CURRENT, retries the current service. if (RetryEnum.CURRENT.getName().equals(retryStrategy)) { //old version of DividePlugin and SpringCloudPlugin will run on this return response.retryWhen(Retry.anyOf(TimeoutException.class, ConnectTimeoutException.class, ReadTimeoutException.class, IllegalStateException.class) .retryMax(retryTimes) .backoff(Backoff.exponential(Duration.ofMillis(200), Duration.ofSeconds(20), 2, true))) .onErrorMap(TimeoutException.class, th -> new ResponseStatusException(HttpStatus.GATEWAY_TIMEOUT, th.getMessage(), th)) .flatMap((Function<Object, Mono<? extends Void>>) o -> chain.execute(exchange)); } // Retry for other services // Exclude services that have already been called final Set<URI> exclude = Sets.newHashSet(uri); // resend return resend(response, exchange, duration, httpHeaders, exclude, retryTimes) .onErrorMap(TimeoutException.class, th -> new ResponseStatusException(HttpStatus.GATEWAY_TIMEOUT, th.getMessage(), th)) .flatMap((Function<Object, Mono<? extends Void>>) o -> chain.execute(exchange)); } private Mono<R> resend(final Mono<R> clientResponse, final ServerWebExchange exchange, final Duration duration, final HttpHeaders httpHeaders, final Set<URI> exclude, final int retryTimes) { Mono<R> result = clientResponse; // Retry according to the specified number of retries for (int i = 0; i < retryTimes; i++) { result = resend(result, exchange, duration, httpHeaders, exclude); } return result; } private Mono<R> resend(final Mono<R> response, final ServerWebExchange exchange, final Duration duration, final HttpHeaders httpHeaders, final Set<URI> exclude) { return response.onErrorResume(th -> { final String selectorId = exchange.getAttribute(Constants.DIVIDE_SELECTOR_ID); final String loadBalance = exchange.getAttribute(Constants.LOAD_BALANCE); //Check available services final List<Upstream> upstreamList = UpstreamCacheManager.getInstance().findUpstreamListBySelectorId(selectorId) .stream().filter(data -> { final String trimUri = data.getUrl().trim(); for (URI needToExclude : exclude) { // exclude already called if ((needToExclude.getHost() + ":" + needToExclude.getPort()).equals(trimUri)) { return false; } } return true; }).collect(Collectors.toList()); if (CollectionUtils.isEmpty(upstreamList)) { // no need to retry anymore return Mono.error(new ShenyuException(ShenyuResultEnum.CANNOT_FIND_HEALTHY_UPSTREAM_URL_AFTER_FAILOVER.getMsg())); } // requets ip final String ip = Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getAddress().getHostAddress(); // Load Balance final Upstream upstream = LoadBalancerFactory.selector(upstreamList, loadBalance, ip); if (Objects.isNull(upstream)) { // no need to retry anymore return Mono.error(new ShenyuException(ShenyuResultEnum.CANNOT_FIND_HEALTHY_UPSTREAM_URL_AFTER_FAILOVER.getMsg())); } final URI newUri = RequestUrlUtils.buildRequestUri(exchange, upstream.buildDomain()); // Exclude uri that has already been called exclude.add(newUri); // Make another call return doRequest(exchange, exchange.getRequest().getMethodValue(), newUri, httpHeaders, exchange.getRequest().getBody()) .timeout(duration, Mono.error(new TimeoutException("Response took longer than timeout: " + duration))) .doOnError(e -> LOG.error(e.getMessage(), e)); }); } //......}
The source code analysis in this article starts from the http service registration to the divide plugin service calls. The divide plugin is mainly used to handle http requests. Some of the source code does not enter the in-depth analysis, such as the implementation of load balancing, service probe live, will continue to analyze in the following.
Let's take a look at the structure of this plugin first, as shown in the figure below.
Guess: handler is used for data synchronization; strategy may be adapted to various request bodies, which should be the focus of this plugin; ParamMappingPlugin should be the implementation of ShenyuPlugin.
First, take a look at the ParamMappingPlugin, the focus is on the override of the doExecute method.
public Mono<Void> doExecute(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule) { ... // judge whether paramMappingHandle is null // Determine the request body type according to the contentType in the header line HttpHeaders headers = exchange.getRequest().getHeaders(); MediaType contentType = headers.getContentType(); // * return match(contentType).apply(exchange, chain, paramMappingHandle);}
The match method returns the corresponding Operator according to contentType
As can be seen from the code of the match method, there are currently three types of DefaultOperator, FormDataOperator, and JsonOperator, which support the request body in two formats: x-www-form-urlencoded and json.
So let's take a look at what the above three operators are like.
Nothing happens, its apply method just continues to execute the plug-in chain, and has no real function. When the request body does not match the Operator, it will be skipped by DefaultOperator.
This class is used to process the request body in the format of x-www-form-urlencoded.
Mainly depends on the apply method, but it looks a bit strange.
public Mono<Void> apply(final ServerWebExchange exchange, final ShenyuPluginChain shenyuPluginChain, final ParamMappingHandle paramMappingHandle) { return exchange.getFormData() .switchIfEmpty(Mono.defer(() -> Mono.just(new LinkedMultiValueMap<>()))) .flatMap(multiValueMap -> { ... });}
The code in the ellipsis is the processing of the request body, as follows.
// judge whether it is emptyif (Objects.isNull(multiValueMap) || multiValueMap.isEmpty()) { return shenyuPluginChain.execute(exchange);}// convert form-data to jsonString original = GsonUtils.getInstance().toJson(multiValueMap);LOG.info("get from data success data:{}", original);// *modify request body*String modify = operation(original, paramMappingHandle);if (StringUtils.isEmpty(modify)) { return shenyuPluginChain.execute(exchange);}...// Convert the modified json into LinkedMultiValueMap. Pay attention to this line, it will be mentioned later!LinkedMultiValueMap<String, String> modifyMap = GsonUtils.getInstance().toLinkedMultiValueMap(modify);...final BodyInserter bodyInserter = BodyInserters.fromValue(modifyMap);...// modify the request body in the exchange, and then continue to execute the plugin chainreturn bodyInserter.insert(cachedBodyOutputMessage, new BodyInserterContext()) .then(Mono.defer(() -> shenyuPluginChain.execute(exchange.mutate() .request(new ModifyServerHttpRequestDecorator(httpHeaders, exchange.getRequest(), cachedBodyOutputMessage)) .build()) )).onErrorResume((Function<Throwable, Mono<Void>>) throwable -> release(cachedBodyOutputMessage, throwable));
PS: The omitted part is to set the request first and other operations.
The more important thing above should be the modification request body of the star, that is, the call of the operation method. Here, because of the parameter type, the default method of the Operator interface will be called first (instead of being overridden by the FormDataOperator).
default String operation(final String jsonValue, final ParamMappingHandle paramMappingHandle) { DocumentContext context = JsonPath.parse(jsonValue); // call the override operation method and add addParameterKey operation(context, paramMappingHandle); // replace the related replacedParameterKey if (!CollectionUtils.isEmpty(paramMappingHandle.getReplaceParameterKeys())) { paramMappingHandle.getReplaceParameterKeys().forEach(info -> { context.renameKey(info.getPath(), info.getKey(), info.getValue()); }); } // Delete the related removeParameterKey if (!CollectionUtils.isEmpty(paramMappingHandle.getRemoveParameterKeys())) { paramMappingHandle.getRemoveParameterKeys().forEach(info -> { context.delete(info); }); } return context.jsonString();}
After sorting it out, we can find that the json tool JsonPath imported here makes the processing of the request body much simpler and clearer.
In addition, we can notice that the FormDataOperator overrides the operation(DocumentContext, ParamMappingHandle) method.
Why override it? There is a default method for handling addParameterKey in the interface.
// Default method in Operator interfacedefault void operation(final DocumentContext context, final ParamMappingHandle paramMappingHandle) { if (!CollectionUtils.isEmpty(paramMappingHandle.getAddParameterKeys())) { paramMappingHandle.getAddParameterKeys().forEach(info -> { context.put(info.getPath(), info.getKey(), info.getValue()); //不同之处 }); }}// method overridden by FormDataOperator@Overridepublic void operation(final DocumentContext context, final ParamMappingHandle paramMappingHandle) { if (!CollectionUtils.isEmpty(paramMappingHandle.getAddParameterKeys())) { paramMappingHandle.getAddParameterKeys().forEach(info -> { context.put(info.getPath(), info.getKey(), Arrays.asList(info.getValue())); }); }}
In fact, there is such a line in FormDataOperator#apply (mentioned earlier):
LinkedMultiValueMap<String, String> modifyMap = GsonUtils.getInstance().toLinkedMultiValueMap(modify);
This line converts the modified json into LinkedMultiValueMap, GsonUtils#toLinkedMultiValueMap is as follows.
public LinkedMultiValueMap<String, String> toLinkedMultiValueMap(final String json) { return GSON.fromJson(json, new TypeToken<LinkedMultiValueMap<String, String>>() { }.getType());}
The attribute targetMap in the LinkedMultiValueMap class is defined as: private final Map<K, List<V>> targetMap
Therefore, the value in the json string must be in the form of a list, otherwise Gson will throw a conversion error exception, which is why the FormDataOperator must override the operator method.
But why use LinkedMultiValueMap?
Go back to the first line exchange.getFormData of the FormDataOperator#apply method. In SpringMVC, the return value type of DefaultServerWebExchange#getFormData is Mono<MultiValueMap<String, String>>, and LinkedMultiValueMap is a subclass of MultiValueMap. And, the getFormData method is for the request body in the format of x-www-form-urlencoded.
Obviously, this class is used to process the request body in Json format.
public Mono<Void> apply(final ServerWebExchange exchange, final ShenyuPluginChain shenyuPluginChain, final ParamMappingHandle paramMappingHandle) { ServerRequest serverRequest = ServerRequest.create(exchange, MESSAGE_READERS); Mono<String> mono = serverRequest.bodyToMono(String.class).switchIfEmpty(Mono.defer(() -> Mono.just(""))).flatMap(originalBody -> { LOG.info("get body data success data:{}", originalBody); // call the default operation method to modify the request body String modify = operation(originalBody, paramMappingHandle); return Mono.just(modify); }); BodyInserter bodyInserter = BodyInserters.fromPublisher(mono, String.class); ... //process the header line CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange, headers); // modify the request body in the exchange, and then continue to execute the plugin chain return bodyInserter.insert(outputMessage, new BodyInserterContext()) .then(Mono.defer(() -> { ServerHttpRequestDecorator decorator = new ModifyServerHttpRequestDecorator(headers, exchange.getRequest(), outputMessage); return shenyuPluginChain.execute(exchange.mutate().request(decorator).build()); })).onErrorResume((Function<Throwable, Mono<Void>>) throwable -> release(outputMessage, throwable));}
The processing flow of JsonOperator is roughly similar to that of FormDataOperator.
Apache ShenYu is an asynchronous, high-performance, cross-language, responsive API gateway.
The Apache ShenYu gateway uses the dubbo plugin to make calls to the dubbo service. You can see the official documentation Dubbo Quick Start to learn how to use the plugin.
This article is based on shenyu-2.4.3 version for source code analysis, please refer to Dubbo Service Access for the introduction of the official website.
Declare the application service name, register the center address, use the dubbo protocol, declare the service interface, and the corresponding interface implementation class.
/** * DubboTestServiceImpl. */@Service("dubboTestService")public class DubboTestServiceImpl implements DubboTestService { @Override @ShenyuDubboClient(path = "/findById", desc = "Query by Id") public DubboTest findById(final String id) { return new DubboTest(id, "hello world shenyu Apache, findById"); } //......}
In the interface implementation class, use the annotation @ShenyuDubboClient to register the service with shenyu-admin. The role of this annotation and its rationale will be analyzed later.
The configuration information in the configuration file application.yml.
In the configuration file, declare the registry address used by dubbo. The dubbo service registers with shenyu-admin, using the method http, and the registration address is http://localhost:9095.
Use the annotation @ShenyuDubboClient to register the service to the gateway. The simple demo is as follows.
// dubbo sevice@Service("dubboTestService")public class DubboTestServiceImpl implements DubboTestService { @Override @ShenyuDubboClient(path = "/findById", desc = "Query by Id") // need to be registered method public DubboTest findById(final String id) { return new DubboTest(id, "hello world shenyu Apache, findById"); } //......}
annotation definition:
/** * Works on classes and methods */@Retention(RetentionPolicy.RUNTIME)@Target({ElementType.TYPE, ElementType.METHOD})@Inheritedpublic @interface ShenyuDubboClient { //path String path(); //rule name String ruleName() default ""; //desc String desc() default ""; //enabled boolean enabled() default true;}
Annotation scanning is done through the ApacheDubboServiceBeanListener, which implements the ApplicationListener<ContextRefreshedEvent> interface and starts executing the event handler method when a context refresh event occurs during the Spring container startup onApplicationEvent().
During constructor instantiation.
Read property configuration
Start the thread pool
Start the registry for registering with shenyu-admin
public class ApacheDubboServiceBeanListener implements ApplicationListener<ContextRefreshedEvent> { // ...... //Constructor public ApacheDubboServiceBeanListener(final PropertiesConfig clientConfig, final ShenyuClientRegisterRepository shenyuClientRegisterRepository) { //1.Read property configuration Properties props = clientConfig.getProps(); String contextPath = props.getProperty(ShenyuClientConstants.CONTEXT_PATH); String appName = props.getProperty(ShenyuClientConstants.APP_NAME); if (StringUtils.isBlank(contextPath)) { throw new ShenyuClientIllegalArgumentException("apache dubbo client must config the contextPath or appName"); } this.contextPath = contextPath; this.appName = appName; this.host = props.getProperty(ShenyuClientConstants.HOST); this.port = props.getProperty(ShenyuClientConstants.PORT); //2.Start the thread pool executorService = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("shenyu-apache-dubbo-client-thread-pool-%d").build()); //3.Start the registry for registering with `shenyu-admin` publisher.start(shenyuClientRegisterRepository); } /** * Context refresh event, execute method logic */ @Override public void onApplicationEvent(final ContextRefreshedEvent contextRefreshedEvent) { //...... }
Rewritten method logic: read Dubbo service ServiceBean, build metadata object and URI object, and register it with shenyu-admin.
@Override public void onApplicationEvent(final ContextRefreshedEvent contextRefreshedEvent) { //read ServiceBean Map<String, ServiceBean> serviceBean = contextRefreshedEvent.getApplicationContext().getBeansOfType(ServiceBean.class); if (serviceBean.isEmpty()) { return; } //The method is guaranteed to be executed only once if (!registered.compareAndSet(false, true)) { return; } //handle metadata for (Map.Entry<String, ServiceBean> entry : serviceBean.entrySet()) { handler(entry.getValue()); } //handle URI serviceBean.values().stream().findFirst().ifPresent(bean -> { publisher.publishEvent(buildURIRegisterDTO(bean)); }); }
handler()
In the handler() method, read all methods from the serviceBean, determine if there is a ShenyuDubboClient annotation on the method, build a metadata object if it exists, and register the method with shenyu-admin through the registry.
Constructs a metadata object where the necessary information for method registration is constructed and subsequently used for selector or rule matching.
The metadata and URI data registered by the client through the registry are processed at the shenyu-admin end, which is responsible for storing to the database and synchronizing to the shenyu gateway. The client-side registration processing logic of the Dubbo plugin is in the ShenyuClientRegisterDubboServiceImpl. The inheritance relationship is as follows.
The metadata MetaDataRegisterDTO object registered by the client through the registry is picked up and dropped in the register() method of shenyu-admin.
Construct contextPath, find if the selector information exists, if it does, return id; if it doesn't, create the default selector information.
@Override public String registerDefault(final MetaDataRegisterDTO dto, final String pluginName, final String selectorHandler) { // build contextPath String contextPath = ContextPathUtils.buildContextPath(dto.getContextPath(), dto.getAppName()); // Find if selector information exists by name SelectorDO selectorDO = findByNameAndPluginName(contextPath, pluginName); if (Objects.isNull(selectorDO)) { // Create a default selector message if it does not exist return registerSelector(contextPath, pluginName, selectorHandler); } return selectorDO.getId(); }
Default selector information
Construct the default selector information and its conditional properties here.
//register selector private String registerSelector(final String contextPath, final String pluginName, final String selectorHandler) { //build selector SelectorDTO selectorDTO = buildSelectorDTO(contextPath, pluginMapper.selectByName(pluginName).getId()); selectorDTO.setHandle(selectorHandler); //register default selector return registerDefault(selectorDTO); } //build selector private SelectorDTO buildSelectorDTO(final String contextPath, final String pluginId) { //build default SelectorDTO selectorDTO = buildDefaultSelectorDTO(contextPath); selectorDTO.setPluginId(pluginId); //build the conditional properties of the default selector selectorDTO.setSelectorConditions(buildDefaultSelectorConditionDTO(contextPath)); return selectorDTO; }
Build default selector
private SelectorDTO buildDefaultSelectorDTO(final String name) { return SelectorDTO.builder() .name(name) // name .type(SelectorTypeEnum.CUSTOM_FLOW.getCode()) // default type cutom .matchMode(MatchModeEnum.AND.getCode()) //default match mode .enabled(Boolean.TRUE) //enable .loged(Boolean.TRUE) //log .continued(Boolean.TRUE) .sort(1) .build();}
Build default selector conditional properties
private List<SelectorConditionDTO> buildDefaultSelectorConditionDTO(final String contextPath) { SelectorConditionDTO selectorConditionDTO = new SelectorConditionDTO(); selectorConditionDTO.setParamType(ParamTypeEnum.URI.getName()); // default URI selectorConditionDTO.setParamName("/"); selectorConditionDTO.setOperator(OperatorEnum.MATCH.getAlias()); // default match selectorConditionDTO.setParamValue(contextPath + AdminConstants.URI_SUFFIX); return Collections.singletonList(selectorConditionDTO);}
Register default selector
@Overridepublic String registerDefault(final SelectorDTO selectorDTO) { //selector information SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO); //selector conditional properties List<SelectorConditionDTO> selectorConditionDTOs = selectorDTO.getSelectorConditions(); if (StringUtils.isEmpty(selectorDTO.getId())) { // insert selector information into the database selectorMapper.insertSelective(selectorDO); // inserting selector conditional properties to the database selectorConditionDTOs.forEach(selectorConditionDTO -> { selectorConditionDTO.setSelectorId(selectorDO.getId()); selectorConditionMapper.insertSelective(SelectorConditionDO.buildSelectorConditionDO(selectorConditionDTO)); }); } // Publish synchronization events to synchronize selection information and its conditional attributes to the gateway publishEvent(selectorDO, selectorConditionDTOs); return selectorDO.getId();}
Get a valid URI from the URI registered by the client, update the corresponding selector handle property, and send a selector update event to the gateway.
@Override public String doRegisterURI(final String selectorName, final List<URIRegisterDTO> uriList) { //check if (CollectionUtils.isEmpty(uriList)) { return ""; } SelectorDO selectorDO = selectorService.findByNameAndPluginName(selectorName, PluginNameAdapter.rpcTypeAdapter(rpcType())); if (Objects.isNull(selectorDO)) { throw new ShenyuException("doRegister Failed to execute,wait to retry."); } // gte valid URI List<URIRegisterDTO> validUriList = uriList.stream().filter(dto -> Objects.nonNull(dto.getPort()) && StringUtils.isNotBlank(dto.getHost())).collect(Collectors.toList()); // build handle String handler = buildHandle(validUriList, selectorDO); if (handler != null) { selectorDO.setHandle(handler); SelectorData selectorData = selectorService.buildByName(selectorName, PluginNameAdapter.rpcTypeAdapter(rpcType())); selectorData.setHandle(handler); // Update the handle property of the selector to the database selectorService.updateSelective(selectorDO); // Send selector update events to the gateway eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, DataEventTypeEnum.UPDATE, Collections.singletonList(selectorData))); } return ShenyuResultMessage.SUCCESS; }
The source code analysis on service registration is completed as well as the analysis flow chart is as follows.
The next step is to analyze how the dubbo plugin initiates calls to the http service based on this information.
The dubbo plugin is the core processing plugin used by the ShenYu gateway to convert http requests into the dubbo protocol and invoke the dubbo service.
Take the case provided by the official website Quick Start with Dubbo as an example, a dubbo service is registered with shenyu-admin through the registry, and then requested through the ShenYu gateway proxy, the request is as follows.
GET http://localhost:9195/dubbo/findById?id=100Accept: application/json
The class inheritance relationship in the Dubbo plugin is as follows.
After passing the ShenYu gateway proxy, the request entry is ShenyuWebHandler, which implements the org.springframework.web.server.WebHandler interface.
public final class ShenyuWebHandler implements WebHandler, ApplicationListener<SortPluginEvent> { //...... /** * hanlde request */ @Override public Mono<Void> handle(@NonNull final ServerWebExchange exchange) { // execute default plugin chain Mono<Void> execute = new DefaultShenyuPluginChain(plugins).execute(exchange); if (scheduled) { return execute.subscribeOn(scheduler); } return execute; } private static class DefaultShenyuPluginChain implements ShenyuPluginChain { private int index; private final List<ShenyuPlugin> plugins; DefaultShenyuPluginChain(final List<ShenyuPlugin> plugins) { this.plugins = plugins; } /** * execute. */ @Override public Mono<Void> execute(final ServerWebExchange exchange) { return Mono.defer(() -> { if (this.index < plugins.size()) { // get plugin ShenyuPlugin plugin = plugins.get(this.index++); boolean skip = plugin.skip(exchange); if (skip) { // next return this.execute(exchange); } // execute return plugin.execute(exchange, this); } return Mono.empty(); }); } }}
GlobalPlugin is a global plugin that constructs contextual information in the execute() method.
public class GlobalPlugin implements ShenyuPlugin { // shenyu context private final ShenyuContextBuilder builder; //...... @Override public Mono<Void> execute(final ServerWebExchange exchange, final ShenyuPluginChain chain) { // build context information to be passed into the exchange ShenyuContext shenyuContext = builder.build(exchange); exchange.getAttributes().put(Constants.CONTEXT, shenyuContext); return chain.execute(exchange); } //......}
The RpcParamTransformPlugin is responsible for reading the parameters from the http request, saving them in the exchange and passing them to the rpc service.
In the execute() method, the core logic of the plugin is executed: get the request information from exchange and process the parameters according to the form of content passed in by the request.
Set special context information in the doDubboInvoker() method, and then start the dubbo generalization call.
public class ApacheDubboPlugin extends AbstractDubboPlugin { @Override protected Mono<Void> doDubboInvoker(final ServerWebExchange exchange, final ShenyuPluginChain chain, final SelectorData selector, final RuleData rule, final MetaData metaData, final String param) { //set the current selector and rule information, and request address for dubbo graying support RpcContext.getContext().setAttachment(Constants.DUBBO_SELECTOR_ID, selector.getId()); RpcContext.getContext().setAttachment(Constants.DUBBO_RULE_ID, rule.getId()); RpcContext.getContext().setAttachment(Constants.DUBBO_REMOTE_ADDRESS, Objects.requireNonNull(exchange.getRequest().getRemoteAddress()).getAddress().getHostAddress()); //dubbo generic invoker final Mono<Object> result = dubboProxyService.genericInvoker(param, metaData, exchange); //execute next plugin in chain return result.then(chain.execute(exchange)); }}
Gets the generalization service GenericService object.
Constructs the request parameter pair object.
Initiates an asynchronous generalization invocation.
public class ApacheDubboProxyService { //...... /** * Generic invoker object. */ public Mono<Object> genericInvoker(final String body, final MetaData metaData, final ServerWebExchange exchange) throws ShenyuException { //1.Get the ReferenceConfig object ReferenceConfig<GenericService> reference = ApacheDubboConfigCache.getInstance().get(metaData.getPath()); if (Objects.isNull(reference) || StringUtils.isEmpty(reference.getInterface())) { //Failure of the current cache information ApacheDubboConfigCache.getInstance().invalidate(metaData.getPath()); //Reinitialization with metadata reference = ApacheDubboConfigCache.getInstance().initRef(metaData); } //2.Get the GenericService object of the generalization service GenericService genericService = reference.get(); //3.Constructing the request parameter pair object Pair<String[], Object[]> pair; if (StringUtils.isBlank(metaData.getParameterTypes()) || ParamCheckUtils.dubboBodyIsEmpty(body)) { pair = new ImmutablePair<>(new String[]{}, new Object[]{}); } else { pair = dubboParamResolveService.buildParameter(body, metaData.getParameterTypes()); } //4.Initiating asynchronous generalization calls return Mono.fromFuture(invokeAsync(genericService, metaData.getMethodName(), pair.getLeft(), pair.getRight()).thenApply(ret -> { //handle result if (Objects.isNull(ret)) { ret = Constants.DUBBO_RPC_RESULT_EMPTY; } exchange.getAttributes().put(Constants.RPC_RESULT, ret); exchange.getAttributes().put(Constants.CLIENT_RESPONSE_RESULT_TYPE, ResultEnum.SUCCESS.getName()); return ret; })).onErrorMap(exception -> exception instanceof GenericException ? new ShenyuException(((GenericException) exception).getExceptionMessage()) : new ShenyuException(exception));//处理异常 } //Generalized calls, asynchronous operations private CompletableFuture<Object> invokeAsync(final GenericService genericService, final String method, final String[] parameterTypes, final Object[] args) throws GenericException { genericService.$invoke(method, parameterTypes, args); Object resultFromFuture = RpcContext.getContext().getFuture(); return resultFromFuture instanceof CompletableFuture ? (CompletableFuture<Object>) resultFromFuture : CompletableFuture.completedFuture(resultFromFuture); }}
Calling the dubbo service at the gateway can be achieved by generalizing the call.
The ReferenceConfig object is the key object to support generalization calls , and its initialization operation is done during data synchronization. There are two parts of data involved here, one is the synchronized plugin handler information and the other is the synchronized plugin metadata information.
When the plugin data is updated, the data synchronization module synchronizes the data from shenyu-admin to the gateway. The initialization operation is performed in handlerPlugin().
public abstract class AbstractDubboPluginDataHandler implements PluginDataHandler { //...... //Initializing the configuration cache protected abstract void initConfigCache(DubboRegisterConfig dubboRegisterConfig); @Override public void handlerPlugin(final PluginData pluginData) { if (Objects.nonNull(pluginData) && Boolean.TRUE.equals(pluginData.getEnabled())) { //Data deserialization DubboRegisterConfig dubboRegisterConfig = GsonUtils.getInstance().fromJson(pluginData.getConfig(), DubboRegisterConfig.class); DubboRegisterConfig exist = Singleton.INST.get(DubboRegisterConfig.class); if (Objects.isNull(dubboRegisterConfig)) { return; } if (Objects.isNull(exist) || !dubboRegisterConfig.equals(exist)) { // Perform initialization operations this.initConfigCache(dubboRegisterConfig); } Singleton.INST.single(DubboRegisterConfig.class, dubboRegisterConfig); } } //......}
When the metadata is updated, the data synchronization module synchronizes the data from shenyu-admin to the gateway. The metadata update operation is performed in the onSubscribe() method.
public class ApacheDubboMetaDataSubscriber implements MetaDataSubscriber { //local memory cache private static final ConcurrentMap<String, MetaData> META_DATA = Maps.newConcurrentMap(); //update metaData public void onSubscribe(final MetaData metaData) { // dubbo if (RpcTypeEnum.DUBBO.getName().equals(metaData.getRpcType())) { //Whether the corresponding metadata exists MetaData exist = META_DATA.get(metaData.getPath()); if (Objects.isNull(exist) || Objects.isNull(ApacheDubboConfigCache.getInstance().get(metaData.getPath()))) { // initRef ApacheDubboConfigCache.getInstance().initRef(metaData); } else { // The corresponding metadata has undergone an update operation if (!Objects.equals(metaData.getServiceName(), exist.getServiceName()) || !Objects.equals(metaData.getRpcExt(), exist.getRpcExt()) || !Objects.equals(metaData.getParameterTypes(), exist.getParameterTypes()) || !Objects.equals(metaData.getMethodName(), exist.getMethodName())) { //Build ReferenceConfig again based on the latest metadata ApacheDubboConfigCache.getInstance().build(metaData); } } //local memory cache META_DATA.put(metaData.getPath(), metaData); } } //dalete public void unSubscribe(final MetaData metaData) { if (RpcTypeEnum.DUBBO.getName().equals(metaData.getRpcType())) { //使ReferenceConfig失效 ApacheDubboConfigCache.getInstance().invalidate(metaData.getPath()); META_DATA.remove(metaData.getPath()); } }}
The source code analysis in this article starts from Dubbo service registration to Dubbo plug-in service calls. The Dubbo plugin is mainly used to handle the conversion of http requests to the dubbo protocol, and the main logic is implemented through generalized calls.
Apache ShenYu is an asynchronous, high-performance, cross-language, responsive API gateway.
In ShenYu gateway, the registration center is used to register the client information to shenyu-admin, admin then synchronizes this information to the gateway through data synchronization, and the gateway completes traffic filtering through these data. The client information mainly includes interface information and URI information.
This article is based on shenyu-2.5.0 version for source code analysis, please refer to Client Access Principles for the introduction of the official website.
When the client starts, it reads the interface information and uri information, and sends the data to shenyu-admin by the specified registration type.
The registration center in the figure requires the user to specify which registration type to use. ShenYu currently supports Http, Zookeeper, Etcd, Consul and Nacos for registration. Please refer to Client Access Configuration for details on how to configure them.
ShenYu introduces Disruptor in the principle design of the registration center, in which the Disruptor queue plays a role in decoupling data and operations, which is conducive to expansion. If too many registration requests lead to registration exceptions, it also has a data buffering role.
As shown in the figure, the registration center is divided into two parts, one is the registration center client register-client, the load processing client data reading. The other is the registration center server register-server, which is loaded to handle the server side (that is shenyu-admin) data writing. Data is sent and received by specifying the registration type.
Client: Usually it is a microservice, which can be springmvc, spring-cloud, dubbo, grpc, etc.
register-client: register the central client, read the client interface and uri information.
Disruptor: decoupling data from operations, data buffering role.
register-server: registry server, here is shenyu-admin, receive data, write to database, send data synchronization events.
registration-type: specify the registration type, complete data registration, currently supports Http, Zookeeper, Etcd, Consul and Nacos.
This article analyzes the use of Http for registration, so the specific processing flow is as follows.
On the client side, after the data is out of the queue, the data is transferred via http and on the server side, the corresponding interface is provided to receive the data and then write it to the queue.
When the client starts, it reads the attribute information according to the relevant configuration, and then writes it to the queue. Let's take the official shenyu-examples-http as an example and start the source code analysis . The official example is a microservice built by springboot. For the configuration of the registration center, please refer to the official website client access configuration .
registerType: the service registration type, fill in http.
serverList: The address of the Shenyu-Admin project to fill in for the http registration type, note the addition of http:// and separate multiple addresses with English commas.
username: The username of the Shenyu-Admin
password: The password of the Shenyu-Admin
port: the start port of your project, currently springmvc/tars/grpc needs to be filled in.
contextPath: the routing prefix for your mvc project in shenyu gateway, such as /order, /product, etc. The gateway will route according to your prefix.
appName: the name of your application, if not configured, it will take the value of spring.application.name by default.
isFull: set true to proxy your entire service, false to proxy one of your controllers; currently applies to springmvc/springcloud.
After the project starts, it will first load the configuration file, read the property information and generate the corresponding Bean.
The first configuration file read is ShenyuSpringMvcClientConfiguration, which is the http registration configuration class for the shenyu client, indicated by @Configuration which is a configuration class, and by @ImportAutoConfiguration which is a configuration class. to introduce other configuration classes. Create SpringMvcClientEventListener, which mainly handles metadata and URI information.
/** * Shenyu SpringMvc Client Configuration */@Configuration@ImportAutoConfiguration(ShenyuClientCommonBeanConfiguration.class)@ConditionalOnProperty(value = "shenyu.register.enabled", matchIfMissing = true, havingValue = "true")public class ShenyuSpringMvcClientConfiguration { // create SpringMvcClientEventListener to handle metadata and URI @Bean public SpringMvcClientEventListener springHttpClientEventListener(final ShenyuClientConfig clientConfig, final ShenyuClientRegisterRepository shenyuClientRegisterRepository) { return new SpringMvcClientEventListener(clientConfig.getClient().get(RpcTypeEnum.HTTP.getName()), shenyuClientRegisterRepository); }}
ShenyuClientCommonBeanConfiguration is a shenyu client common configuration class that will create the bean common to the registry client.
Create ShenyuClientRegisterRepository, which is created by factory class.
Create ShenyuRegisterCenterConfig, which reads the shenyu.register property configuration.
Create ShenyuClientConfig, read the shenyu.client property configuration.
/** * Shenyu Client Common Bean Configuration */@Configurationpublic class ShenyuClientCommonBeanConfiguration { // create ShenyuClientRegisterRepository by factory @Bean public ShenyuClientRegisterRepository shenyuClientRegisterRepository(final ShenyuRegisterCenterConfig config) { return ShenyuClientRegisterRepositoryFactory.newInstance(config); } // create ShenyuRegisterCenterConfig to read shenyu.register properties @Bean @ConfigurationProperties(prefix = "shenyu.register") public ShenyuRegisterCenterConfig shenyuRegisterCenterConfig() { return new ShenyuRegisterCenterConfig(); } // create ShenyuClientConfig to read shenyu.client properties @Bean @ConfigurationProperties(prefix = "shenyu") public ShenyuClientConfig shenyuClientConfig() { return new ShenyuClientConfig(); }}
The ShenyuClientRegisterRepository generated in the configuration file above is a concrete implementation of the client registration, which is an interface with the following implementation class.
HttpClientRegisterRepository: registration via http.
ConsulClientRegisterRepository: registration via Consul.
EtcdClientRegisterRepository: registration via Etcd; EtcdClientRegisterRepository: registration via Etcd.
NacosClientRegisterRepository: registration via nacos; NacosClientRegisterRepository: registration via nacos.
ZookeeperClientRegisterRepository: registration through Zookeeper.
The specific way which is achieved by loading through SPI, the implementation logic is as follows.
/** * load ShenyuClientRegisterRepository */public final class ShenyuClientRegisterRepositoryFactory { private static final Map<String, ShenyuClientRegisterRepository> REPOSITORY_MAP = new ConcurrentHashMap<>(); /** * create ShenyuClientRegisterRepository */ public static ShenyuClientRegisterRepository newInstance(final ShenyuRegisterCenterConfig shenyuRegisterCenterConfig) { if (!REPOSITORY_MAP.containsKey(shenyuRegisterCenterConfig.getRegisterType())) { // Loading by means of SPI, type determined by registerType ShenyuClientRegisterRepository result = ExtensionLoader.getExtensionLoader(ShenyuClientRegisterRepository.class).getJoin(shenyuRegisterCenterConfig.getRegisterType()); //init ShenyuClientRegisterRepository result.init(shenyuRegisterCenterConfig); ShenyuClientShutdownHook.set(result, shenyuRegisterCenterConfig.getProps()); REPOSITORY_MAP.put(shenyuRegisterCenterConfig.getRegisterType(), result); return result; } return REPOSITORY_MAP.get(shenyuRegisterCenterConfig.getRegisterType()); }}
The load type is specified by registerType, which is the type we specify in the configuration file at
We specified http, so it will go to load HttpClientRegisterRepository. After the object is successfully created, the initialization method init() is executed as follows.
Read username, password and serverLists from the configuration file, the username, password and address of sheenyu-admin, in preparation for subsequent data sending. The class annotation @Join is used for SPI loading.
SPI, known as Service Provider Interface, is a service provider discovery feature built into the JDK, a mechanism for dynamic replacement discovery.
shenyu-spi is a custom SPI extension implementation for the Apache ShenYu gateway, designed and implemented with reference to Dubbo SPI extension implementation.
Create SpringMvcClientEventListener, which is responsible for the construction and registration of client-side metadata and URI data, and its creation is done in the configuration file.
@Configuration@ImportAutoConfiguration(ShenyuClientCommonBeanConfiguration.class)public class ShenyuSpringMvcClientConfiguration { // ...... // create SpringMvcClientEventListener @Bean public SpringMvcClientEventListener springHttpClientEventListener(final ShenyuClientConfig clientConfig, final ShenyuClientRegisterRepository shenyuClientRegisterRepository) { return new SpringMvcClientEventListener(clientConfig.getClient().get(RpcTypeEnum.HTTP.getName()), shenyuClientRegisterRepository); }}
SpringMvcClientEventListener implements the AbstractContextRefreshedEventListener
The AbstractContextRefreshedEventListener is an abstract class. it implements the ApplicationListener interface and overrides the onApplicationEvent() method, which is executed when a Spring event occurs. It has several implementation classes, which support different kind of RPC styles.
public abstract class AbstractContextRefreshedEventListener<T, A extends Annotation> implements ApplicationListener<ContextRefreshedEvent> { //...... // Instantiation is done through the constructor public AbstractContextRefreshedEventListener(final PropertiesConfig clientConfig, final ShenyuClientRegisterRepository shenyuClientRegisterRepository) { // read shenyu.client.http properties Properties props = clientConfig.getProps(); // appName this.appName = props.getProperty(ShenyuClientConstants.APP_NAME); // contextPath this.contextPath = Optional.ofNullable(props.getProperty(ShenyuClientConstants.CONTEXT_PATH)).map(UriUtils::repairData).orElse(""); if (StringUtils.isBlank(appName) && StringUtils.isBlank(contextPath)) { String errorMsg = "client register param must config the appName or contextPath"; LOG.error(errorMsg); throw new ShenyuClientIllegalArgumentException(errorMsg); } this.ipAndPort = props.getProperty(ShenyuClientConstants.IP_PORT); // host this.host = props.getProperty(ShenyuClientConstants.HOST); // port this.port = props.getProperty(ShenyuClientConstants.PORT); // publish event publisher.start(shenyuClientRegisterRepository); } // This method is executed when a context refresh event(ContextRefreshedEvent), occurs @Override public void onApplicationEvent(@NonNull final ContextRefreshedEvent event) { // The contents of the method are guaranteed to be executed only once if (!registered.compareAndSet(false, true)) { return; } final ApplicationContext context = event.getApplicationContext(); // get the specific beans Map<String, T> beans = getBeans(context); if (MapUtils.isEmpty(beans)) { return; } // build URI data and register it publisher.publishEvent(buildURIRegisterDTO(context, beans)); // build metadata and register it beans.forEach(this::handle); } @SuppressWarnings("all") protected abstract URIRegisterDTO buildURIRegisterDTO(ApplicationContext context, Map<String, T> beans); protected void handle(final String beanName, final T bean) { Class<?> clazz = getCorrectedClass(bean); final A beanShenyuClient = AnnotatedElementUtils.findMergedAnnotation(clazz, getAnnotationType()); final String superPath = buildApiSuperPath(clazz, beanShenyuClient); if (Objects.nonNull(beanShenyuClient) && superPath.contains("*")) { handleClass(clazz, bean, beanShenyuClient, superPath); return; } final Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(clazz); for (Method method : methods) { handleMethod(bean, clazz, beanShenyuClient, method, superPath); } } // default implementation. build URI data and register it protected void handleClass(final Class<?> clazz, final T bean, @NonNull final A beanShenyuClient, final String superPath) { publisher.publishEvent(buildMetaDataDTO(bean, beanShenyuClient, pathJoin(contextPath, superPath), clazz, null)); } // default implementation. build metadata and register it protected void handleMethod(final T bean, final Class<?> clazz, @Nullable final A beanShenyuClient, final Method method, final String superPath) { // get the annotation A methodShenyuClient = AnnotatedElementUtils.findMergedAnnotation(method, getAnnotationType()); if (Objects.nonNull(methodShenyuClient)) { // 构建元数据,发送注册事件 publisher.publishEvent(buildMetaDataDTO(bean, methodShenyuClient, buildApiPath(method, superPath, methodShenyuClient), clazz, method)); } } protected abstract MetaDataRegisterDTO buildMetaDataDTO(T bean, @NonNull A shenyuClient, String path, Class<?> clazz, Method method);}
In the constructor, the main purpose is to read the property information and then perform the checksum.
Finally, publisher.start() is executed to start event publishing and prepare for registration.
ShenyuClientRegisterEventPublisher is implemented via singleton pattern, mainly generating metadata and URI subscribers (subsequently used for data publishing), and then starting the Disruptor queue. A common method publishEvent() is provided to publish events and send data to the Disruptor queue.
public class ShenyuClientRegisterEventPublisher { private static final ShenyuClientRegisterEventPublisher INSTANCE = new ShenyuClientRegisterEventPublisher(); private DisruptorProviderManage<DataTypeParent> providerManage; public static ShenyuClientRegisterEventPublisher getInstance() { return INSTANCE; } public void start(final ShenyuClientRegisterRepository shenyuClientRegisterRepository) { RegisterClientExecutorFactory factory = new RegisterClientExecutorFactory(); factory.addSubscribers(new ShenyuClientMetadataExecutorSubscriber(shenyuClientRegisterRepository)); factory.addSubscribers(new ShenyuClientURIExecutorSubscriber(shenyuClientRegisterRepository)); providerManage = new DisruptorProviderManage(factory); providerManage.startup(); } public <T> void publishEvent(final DataTypeParent data) { DisruptorProvider<DataTypeParent> provider = providerManage.getProvider(); provider.onData(data); }}
The logic of the constructor of AbstractContextRefreshedEventListener is analyzed, it mainly reads the property configuration, creates metadata and URI subscribers, and starts the Disruptor queue.
The onApplicationEvent() method is executed when a Spring event occurs, the parameter here is ContextRefreshedEvent, which means the context refresh event.
ContextRefreshedEvent is a Spring built-in event. It is fired when the ApplicationContext is initialized or refreshed. This can also happen in the ConfigurableApplicationContext interface using the refresh() method. Initialization here means that all Beans have been successfully loaded, post-processing Beans have been detected and activated, all Singleton Beans have been pre-instantiated, and the ApplicationContext container is ready to be used.
SpringMvcClientEventListener: the http implementation of AbstractContextRefreshedEventListener:
public class SpringMvcClientEventListener extends AbstractContextRefreshedEventListener<Object, ShenyuSpringMvcClient> { private final List<Class<? extends Annotation>> mappingAnnotation = new ArrayList<>(3); private final Boolean isFull; private final String protocol; // 构造函数 public SpringMvcClientEventListener(final PropertiesConfig clientConfig, final ShenyuClientRegisterRepository shenyuClientRegisterRepository) { super(clientConfig, shenyuClientRegisterRepository); Properties props = clientConfig.getProps(); // get isFull this.isFull = Boolean.parseBoolean(props.getProperty(ShenyuClientConstants.IS_FULL, Boolean.FALSE.toString())); // http protocol this.protocol = props.getProperty(ShenyuClientConstants.PROTOCOL, ShenyuClientConstants.HTTP); mappingAnnotation.add(ShenyuSpringMvcClient.class); mappingAnnotation.add(RequestMapping.class); } @Override protected Map<String, Object> getBeans(final ApplicationContext context) { // Configuration attribute, if isFull=true, means register the whole microservice if (Boolean.TRUE.equals(isFull)) { getPublisher().publishEvent(MetaDataRegisterDTO.builder() .contextPath(getContextPath()) .appName(getAppName()) .path(PathUtils.decoratorPathWithSlash(getContextPath())) .rpcType(RpcTypeEnum.HTTP.getName()) .enabled(true) .ruleName(getContextPath()) .build()); return null; } // get bean with Controller annotation return context.getBeansWithAnnotation(Controller.class); } @Override protected URIRegisterDTO buildURIRegisterDTO(final ApplicationContext context, final Map<String, Object> beans) { // ... } @Override protected String buildApiSuperPath(final Class<?> clazz, @Nullable final ShenyuSpringMvcClient beanShenyuClient) { if (Objects.nonNull(beanShenyuClient) && StringUtils.isNotBlank(beanShenyuClient.path())) { return beanShenyuClient.path(); } RequestMapping requestMapping = AnnotationUtils.findAnnotation(clazz, RequestMapping.class); // Only the first path is supported temporarily if (Objects.nonNull(requestMapping) && ArrayUtils.isNotEmpty(requestMapping.path()) && StringUtils.isNotBlank(requestMapping.path()[0])) { return requestMapping.path()[0]; } return ""; } @Override protected Class<ShenyuSpringMvcClient> getAnnotationType() { return ShenyuSpringMvcClient.class; } @Override protected void handleMethod(final Object bean, final Class<?> clazz, @Nullable final ShenyuSpringMvcClient beanShenyuClient, final Method method, final String superPath) { // get RequestMapping annotation final RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(method, RequestMapping.class); // get ShenyuSpringMvcClient annotation ShenyuSpringMvcClient methodShenyuClient = AnnotatedElementUtils.findMergedAnnotation(method, ShenyuSpringMvcClient.class); methodShenyuClient = Objects.isNull(methodShenyuClient) ? beanShenyuClient : methodShenyuClient; // the result of ReflectionUtils#getUniqueDeclaredMethods contains method such as hashCode, wait, toSting // add Objects.nonNull(requestMapping) to make sure not register wrong method if (Objects.nonNull(methodShenyuClient) && Objects.nonNull(requestMapping)) { getPublisher().publishEvent(buildMetaDataDTO(bean, methodShenyuClient, buildApiPath(method, superPath, methodShenyuClient), clazz, method)); } } //... // 构造元数据 @Override protected MetaDataRegisterDTO buildMetaDataDTO(final Object bean, @NonNull final ShenyuSpringMvcClient shenyuClient, final String path, final Class<?> clazz, final Method method) { //... }}
The registration logic is done through publisher.publishEvent().
The Controller annotation and the RequestMapping annotation are provided by Spring, which you should be familiar with, so I won't go into details. The ShenyuSpringMvcClient annotation is provided by Apache ShenYu to register the SpringMvc client, which is defined as follows.
@RestController@RequestMapping("/test")@ShenyuSpringMvcClient(path = "/test/**") // register the entire interfacepublic class HttpTestController { //......}
register current method
@RestController@RequestMapping("/order")@ShenyuSpringMvcClient(path = "/order")public class OrderController { /** * Save order dto. * * @param orderDTO the order dto * @return the order dto */ @PostMapping("/save") @ShenyuSpringMvcClient(path = "/save", desc = "Save order") // register current method public OrderDTO save(@RequestBody final OrderDTO orderDTO) { orderDTO.setName("hello world save order"); return orderDTO; }
publisher.publishEvent()
This method sends the data to the Disruptor queue. More details about the Disruptor queue are not described here, which does not affect the flow of analyzing the registration.
When the data is sent, the consumers of the Disruptor queue will process the data for consumption.
This method sends the data to the Disruptor queue. More details about the Disruptor queue are not described here, which does not affect the flow of analyzing the registration.
QueueConsumer
QueueConsumer is a consumer that implements the WorkHandler interface, which is created in the providerManage.startup() logic. The WorkHandler interface is the data consumption interface for Disruptor, and the only method is onEvent().
The QueueConsumer overrides the onEvent() method, and the main logic is to generate the consumption task and then go to the thread pool to execute it.
/** * * QueueConsumer */public class QueueConsumer<T> implements WorkHandler<DataEvent<T>> { // ...... @Override public void onEvent(final DataEvent<T> t) { if (t != null) { // Use different thread pools based on DataEvent type ThreadPoolExecutor executor = orderly(t); // create queue consumption tasks via factory QueueConsumerExecutor<T> queueConsumerExecutor = factory.create(); // set data queueConsumerExecutor.setData(t.getData()); // help gc t.setData(null); // put in the thread pool to execute the consumption task executor.execute(queueConsumerExecutor); } }}
QueueConsumerExecutor is the task that is executed in the thread pool, it implements the Runnable interface, and there are two specific implementation classes.
As the name implies, one is responsible for handling client-side tasks, and one is responsible for handling server-side tasks (the server side is admin, which is analyzed below).
RegisterClientConsumerExecutor
The logic of the rewritten run() is as follows.
public final class RegisterClientConsumerExecutor<T extends DataTypeParent> extends QueueConsumerExecutor<T> { //...... @Override public void run() { // get data final T data = getData(); // call the appropriate processor for processing according to the data type subscribers.get(data.getType()).executor(Lists.newArrayList(data)); }}
Different processors are called to perform the corresponding tasks based on different data types. There are two types of data, one is metadata, which records the client registration information. One is the URI data, which records the client service information.
public enum DataType { META_DATA, URI,}
ExecutorSubscriber#executor()
The actuator subscribers are divided into two categories, one that handles metadata and one that handles URIs. There are two on the client side and two on the server side, so there are four in total.
Here is the registration metadata information, so the execution class is ShenyuClientMetadataExecutorSubscriber.
ShenyuClientMetadataExecutorSubscriber#executor()
The metadata processing logic on the client side is: iterate through the metadata information and call the interface method persistInterface() to finish publishing the data.
public class ShenyuClientMetadataExecutorSubscriber implements ExecutorTypeSubscriber<MetaDataRegisterDTO> { //...... @Override public DataType getType() { return DataType.META_DATA; } @Override public void executor(final Collection<MetaDataRegisterDTO> metaDataRegisterDTOList) { for (MetaDataRegisterDTO metaDataRegisterDTO : metaDataRegisterDTOList) { // call the interface method persistInterface() to finish publishing the data shenyuClientRegisterRepository.persistInterface(metaDataRegisterDTO); } }}
The two registration interfaces get the data well and call the publish() method to publish the data to the Disruptor queue.
ShenyuServerRegisterRepository
The ShenyuServerRegisterRepository interface is a service registration interface, which has five implementation classes, indicating five types of registration.
ConsulServerRegisterRepository: registration is achieved through Consul;
EtcdServerRegisterRepository: registration through Etcd.
NacosServerRegisterRepository: registration through Nacos.
ShenyuHttpRegistryController: registration via Http; ShenyuHttpRegistryController: registration via Http.
ZookeeperServerRegisterRepository: registration through Zookeeper.
As you can see from the diagram, the loading of the registry is done by means of SPI. This was mentioned earlier, and the specific class loading is done in the client-side generic configuration file by specifying the properties in the configuration file.
/** * load ShenyuClientRegisterRepository */public final class ShenyuClientRegisterRepositoryFactory { private static final Map<String, ShenyuClientRegisterRepository> REPOSITORY_MAP = new ConcurrentHashMap<>(); /** * create ShenyuClientRegisterRepository */ public static ShenyuClientRegisterRepository newInstance(final ShenyuRegisterCenterConfig shenyuRegisterCenterConfig) { if (!REPOSITORY_MAP.containsKey(shenyuRegisterCenterConfig.getRegisterType())) { // loading by means of SPI, type determined by registerType ShenyuClientRegisterRepository result = ExtensionLoader.getExtensionLoader(ShenyuClientRegisterRepository.class).getJoin(shenyuRegisterCenterConfig.getRegisterType()); // perform initialization operations result.init(shenyuRegisterCenterConfig); ShenyuClientShutdownHook.set(result, shenyuRegisterCenterConfig.getProps()); REPOSITORY_MAP.put(shenyuRegisterCenterConfig.getRegisterType(), result); return result; } return REPOSITORY_MAP.get(shenyuRegisterCenterConfig.getRegisterType()); }}
The source code analysis in this article is based on the Http way of registration, so we first analyze the HttpClientRegisterRepository, and the other registration methods will be analyzed afterwards.
Registration by way of http is very simple, it is to call the tool class to send http requests. The registration metadata and URI are both called by the same method doRegister(), specifying the interface and type.
Constants.URI_PATH = /shenyu-client/register-metadata: the interface provided by the server for registering metadata.
Constants.META_PATH = /shenyu-client/register-uri: Server-side interface for registering URIs.
@Joinpublic class HttpClientRegisterRepository extends FailbackRegistryRepository { private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientRegisterRepository.class); private static URIRegisterDTO uriRegisterDTO; private String username; private String password; private List<String> serverList; private String accessToken; public HttpClientRegisterRepository() { } public HttpClientRegisterRepository(final ShenyuRegisterCenterConfig config) { init(config); } @Override public void init(final ShenyuRegisterCenterConfig config) { // admin username this.username = config.getProps().getProperty(Constants.USER_NAME); // admin paaword this.password = config.getProps().getProperty(Constants.PASS_WORD); // admin server address this.serverList = Lists.newArrayList(Splitter.on(",").split(config.getServerLists())); // set access token this.setAccessToken(); } /** * Persist uri. * * @param registerDTO the register dto */ @Override public void doPersistURI(final URIRegisterDTO registerDTO) { if (RuntimeUtils.listenByOther(registerDTO.getPort())) { return; } doRegister(registerDTO, Constants.URI_PATH, Constants.URI); uriRegisterDTO = registerDTO; } @Override public void doPersistInterface(final MetaDataRegisterDTO metadata) { doRegister(metadata, Constants.META_PATH, Constants.META_TYPE); } @Override public void close() { if (uriRegisterDTO != null) { uriRegisterDTO.setEventType(EventType.DELETED); doRegister(uriRegisterDTO, Constants.URI_PATH, Constants.URI); } } private void setAccessToken() { for (String server : serverList) { try { Optional<?> login = RegisterUtils.doLogin(username, password, server.concat(Constants.LOGIN_PATH)); login.ifPresent(v -> this.accessToken = String.valueOf(v)); } catch (Exception e) { LOGGER.error("Login admin url :{} is fail, will retry. cause: {} ", server, e.getMessage()); } } } private <T> void doRegister(final T t, final String path, final String type) { int i = 0; // iterate through the list of admin services (admin may be clustered) for (String server : serverList) { i++; String concat = server.concat(path); try { // 设置访问token if (StringUtils.isBlank(accessToken)) { this.setAccessToken(); if (StringUtils.isBlank(accessToken)) { throw new NullPointerException("accessToken is null"); } } // calling the tool class to send http requests RegisterUtils.doRegister(GsonUtils.getInstance().toJson(t), concat, type, accessToken); return; } catch (Exception e) { LOGGER.error("Register admin url :{} is fail, will retry. cause:{}", server, e.getMessage()); if (i == serverList.size()) { throw new RuntimeException(e); } } } }}
Serialize the data and send it via OkHttp.
public final class RegisterUtils { //...... // Sending data via OkHttp public static void doRegister(final String json, final String url, final String type) throws IOException { if (!StringUtils.hasLength(accessToken)) { LOGGER.error("{} client register error accessToken is null, please check the config : {} ", type, json); return; } Headers headers = new Headers.Builder().add(Constants.X_ACCESS_TOKEN, accessToken).build(); String result = OkHttpTools.getInstance().post(url, json, headers); if (Objects.equals(SUCCESS, result)) { LOGGER.info("{} client register success: {} ", type, json); } else { LOGGER.error("{} client register error: {} ", type, json); } }}
At this point, the logic of the client registering metadata by means of http is finished. To summarize: construct metadata by reading custom annotation information, send the data to the Disruptor queue, then consume the data from the queue, put the consumer into the thread pool to execute, and finally send an http request to the admin.
Similarly, ShenyuClientURIExecutorSubscriber is the execution class of registering URI information.
ShenyuClientURIExecutorSubscriber#executor()
The main logic is to iterate through the URI data collection and implement data registration through the persistURI() method.
public class ShenyuClientURIExecutorSubscriber implements ExecutorTypeSubscriber<URIRegisterDTO> { //...... @Override public DataType getType() { return DataType.URI; } // register URI @Override public void executor(final Collection<URIRegisterDTO> dataList) { for (URIRegisterDTO uriRegisterDTO : dataList) { Stopwatch stopwatch = Stopwatch.createStarted(); while (true) { try (Socket ignored = new Socket(uriRegisterDTO.getHost(), uriRegisterDTO.getPort())) { break; } catch (IOException e) { long sleepTime = 1000; // maybe the port is delay exposed if (stopwatch.elapsed(TimeUnit.SECONDS) > 5) { LOG.error("host:{}, port:{} connection failed, will retry", uriRegisterDTO.getHost(), uriRegisterDTO.getPort()); // If the connection fails for a long time, Increase sleep time if (stopwatch.elapsed(TimeUnit.SECONDS) > 180) { sleepTime = 10000; } } try { TimeUnit.MILLISECONDS.sleep(sleepTime); } catch (InterruptedException ex) { ex.printStackTrace(); } } } ShenyuClientShutdownHook.delayOtherHooks(); shenyuClientRegisterRepository.persistURI(uriRegisterDTO); } }}
The while(true) loop in the code is to ensure that the client has been successfully started and can connect via host and port.
The logic behind it is: add the hook function for gracefully stopping the client .
Data registration is achieved through the persistURI() method. The whole logic is also analyzed in the previous section, and ultimately it is the OkHttp client that initiates http to shenyu-admin and registers the URI by way of http.
The analysis of the registration logic of the client is finished here, and the metadata and URI data constructed are sent to the Disruptor queue, from which they are then consumed, read, and sent to admin via http.
The source code analysis of the client-side metadata and URI registration process is complete, with the following flow chart.
From the previous analysis, we know that the server side provides two interfaces for registration.
/shenyu-client/register-metadata: The interface provided by the server side is used to register metadata.
/shenyu-client/register-uri: The server-side interface is provided for registering URIs.
These two interfaces are located in ShenyuHttpRegistryController, which implements the ShenyuServerRegisterRepository interface and is the implementation class for server-side registration. It is marked with @Join to indicate loading via SPI.
@RequestMapping("/shenyu-client")@Joinpublic class ShenyuHttpRegistryController implements ShenyuServerRegisterRepository { private ShenyuServerRegisterPublisher publisher; @Override public void init(final ShenyuServerRegisterPublisher publisher, final ShenyuRegisterCenterConfig config) { this.publisher = publisher; } // register Metadata @PostMapping("/register-metadata") @ResponseBody public String registerMetadata(@RequestBody final MetaDataRegisterDTO metaDataRegisterDTO) { publisher.publish(metaDataRegisterDTO); return ShenyuResultMessage.SUCCESS; } // register URI @PostMapping("/register-uri") @ResponseBody public String registerURI(@RequestBody final URIRegisterDTO uriRegisterDTO) { publisher.publish(uriRegisterDTO); return ShenyuResultMessage.SUCCESS; }}
The exact method used is specified by the configuration file and then loaded via SPI.
In the application.yml file in shenyu-admin configure the registration method, registerType specify the registration type, when registering with http, serverLists do not need to be filled in, for more configuration instructions you can refer to the official website Client Access Configuration.
shenyu:register:registerType: http serverLists:
RegisterCenterConfiguration
After introducing the relevant dependencies and properties configuration, when starting shenyu-admin, the configuration file will be loaded first, and the configuration file class related to the registration center is RegisterCenterConfiguration.
@Configurationpublic class RegisterCenterConfiguration { @Bean @ConfigurationProperties(prefix = "shenyu.register") public ShenyuRegisterCenterConfig shenyuRegisterCenterConfig() { return new ShenyuRegisterCenterConfig(); } //create ShenyuServerRegisterRepository to register in admin @Bean(destroyMethod = "close") public ShenyuServerRegisterRepository shenyuServerRegisterRepository(final ShenyuRegisterCenterConfig shenyuRegisterCenterConfig, final List<ShenyuClientRegisterService> shenyuClientRegisterService) { // 1. get the registration type from the configuration property String registerType = shenyuRegisterCenterConfig.getRegisterType(); // 2. load the implementation class by registering the type with the SPI method ShenyuServerRegisterRepository registerRepository = ExtensionLoader.getExtensionLoader(ShenyuServerRegisterRepository.class).getJoin(registerType); // 3. get the publisher and write data to the Disruptor queue RegisterServerDisruptorPublisher publisher = RegisterServerDisruptorPublisher.getInstance(); // 4. ShenyuClientRegisterService, rpcType -> registerService Map<String, ShenyuClientRegisterService> registerServiceMap = shenyuClientRegisterService.stream().collect(Collectors.toMap(ShenyuClientRegisterService::rpcType, e -> e)); // 5. start publisher publisher.start(registerServiceMap); // 6. init registerRepository registerRepository.init(publisher, shenyuRegisterCenterConfig); return registerRepository; }}
Two beans are generated in the configuration class.
shenyuRegisterCenterConfig: to read the attribute configuration.
shenyuServerRegisterRepository: for server-side registration.
In the process of creating shenyuServerRegisterRepository, a series of preparations are also performed.
get the registration type from the configuration property.
Load the implementation class by the registration type with the SPI method: for example, if the specified type is http, ShenyuHttpRegistryController will be loaded.
Get publisher and write data to the Disruptor queue.
Register Service, rpcType -> registerService: get the registered Service, each rpc has a corresponding Service. The client for this article is built through springboot, which belongs to the http type, and other client types: dubbo, Spring Cloud, gRPC, etc.
Preparation for event publishing: add server-side metadata and URI subscribers, process the data. And start the Disruptor queue.
Initialization operation for registration: http type registration initialization operation is to save publisher.
RegisterClientServerDisruptorPublisher#publish()
The server-side publisher that writes data to the Disruptor queue , built via the singleton pattern.
public class RegisterClientServerDisruptorPublisher implements ShenyuServerRegisterPublisher { private static final RegisterClientServerDisruptorPublisher INSTANCE = new private static final RegisterClientServerDisruptorPublisher INSTANCE = new RegisterServerDisruptorPublisher();(); public static RegisterClientServerDisruptorPublisher getInstance() { return INSTANCE; } //prepare for event publishing, add server-side metadata and URI subscribers, process data. And start the Disruptor queue. public void start(final Map<String, ShenyuClientRegisterService> shenyuClientRegisterService) { RegisterServerExecutorFactory factory = new RegisterServerExecutorFactory(); // add URI data subscriber factory.addSubscribers(new URIRegisterExecutorSubscriber(shenyuClientRegisterService)); // add Metadata subscriber factory.addSubscribers(new MetadataExecutorSubscriber(shenyuClientRegisterService)); //start Disruptor providerManage = new DisruptorProviderManage(factory); providerManage.startup(); } // write data to queue @Override public <T> void publish(final DataTypeParent data) { DisruptorProvider<Object> provider = providerManage.getProvider(); provider.onData(Collections.singleton(data)); } // write data to queue on batch @Override public void publish(final Collection<? extends DataTypeParent> dataList) { DisruptorProvider<Collection<DataTypeParent>> provider = providerManage.getProvider(); provider.onData(dataList.stream().map(DataTypeParent.class::cast).collect(Collectors.toList())); } @Override public void close() { providerManage.getProvider().shutdown(); }}
The loading of the configuration file, which can be seen as the initialization process of the registry server, is described in the following diagram.
In the previous analysis of the client-side disruptor queue consumption of data over. The server side has the same logic, except that the executor performing the task changes.
The QueueConsumer is a consumer that implements the WorkHandler interface, which is created in the providerManage.startup() logic. The WorkHandler interface is the data consumption interface for disruptor, and the only method is onEvent().
The QueueConsumer overrides the onEvent() method, and the main logic is to generate the consumption task and then go to the thread pool to execute it.
/** * * QueueConsumer */public class QueueConsumer<T> implements WorkHandler<DataEvent<T>> { // ...... @Override public void onEvent(final DataEvent<T> t) { if (t != null) { // Use different thread pools based on DataEvent type ThreadPoolExecutor executor = orderly(t); // create queue consumption tasks via factory QueueConsumerExecutor<T> queueConsumerExecutor = factory.create(); // set data queueConsumerExecutor.setData(t.getData()); // help gc t.setData(null); // put in the thread pool to execute the consumption task executor.execute(queueConsumerExecutor); } }}
QueueConsumerExecutor is the task that is executed in the thread pool, it implements the Runnable interface, and there are two specific implementation classes.
RegisterClientConsumerExecutor: the client-side consumer executor.
As the name implies, one is responsible for handling client-side tasks and one is responsible for handling server-side tasks.
RegisterServerConsumerExecutor#run()
RegisterServerConsumerExecutor is a server-side consumer executor that indirectly implements the Runnable interface via QueueConsumerExecutor and overrides the run() method.
public final class RegisterServerConsumerExecutor extends QueueConsumerExecutor<List<DataTypeParent>> { // ... @Override public void run() { //get the data from the disruptor queue and check data Collection<DataTypeParent> results = getData() .stream() .filter(this::isValidData) .collect(Collectors.toList()); if (CollectionUtils.isEmpty(results)) { return; } //execute operations according to type getType(results).executor(results); } // get subscribers by type private ExecutorSubscriber<DataTypeParent> selectExecutor(final Collection<DataTypeParent> list) { final Optional<DataTypeParent> first = list.stream().findFirst(); return subscribers.get(first.orElseThrow(() -> new RuntimeException("the data type is not found")).getType()); }}
ExecutorSubscriber#executor()
The actuator subscribers are divided into two categories, one that handles metadata and one that handles URIs. There are two on the client side and two on the server side, so there are four in total.
MetadataExecutorSubscriber#executor()
In case of registering metadata, this is achieved by MetadataExecutorSubscriber#executor(): get the registered Service according to the type and call register().
public class MetadataExecutorSubscriber implements ExecutorTypeSubscriber<MetaDataRegisterDTO> { //...... @Override public DataType getType() { return DataType.META_DATA; } @Override public void executor(final Collection<MetaDataRegisterDTO> metaDataRegisterDTOList) { // Traversing the metadata list metaDataRegisterDTOList.forEach(meta -> { Optional.ofNullable(this.shenyuClientRegisterService.get(meta.getRpcType())) // Get registered Service by type .ifPresent(shenyuClientRegisterService -> { // Registration of metadata, locking to ensure sequential execution and prevent concurrent errors synchronized (shenyuClientRegisterService) { shenyuClientRegisterService.register(meta); } }); }); }}
URIRegisterExecutorSubscriber#executor()
In case of registration metadata, this is achieved by URIRegisterExecutorSubscriber#executor(): construct URI data, find Service according to the registration type, and achieve registration by the registerURI method.
public class URIRegisterExecutorSubscriber implements ExecutorTypeSubscriber<URIRegisterDTO> { //...... @Override public DataType getType() { return DataType.URI; } @Override public void executor(final Collection<URIRegisterDTO> dataList) { if (CollectionUtils.isEmpty(dataList)) { return; } findService(dataList).ifPresent(service -> { Map<String, List<URIRegisterDTO>> listMap = buildData(dataList); listMap.forEach(service::registerURI); }); final Map<String, List<URIRegisterDTO>> groupByRpcType = dataList.stream() .filter(data -> StringUtils.isNotBlank(data.getRpcType())) .collect(Collectors.groupingBy(URIRegisterDTO::getRpcType)); for (Map.Entry<String, List<URIRegisterDTO>> entry : groupByRpcType.entrySet()) { final String rpcType = entry.getKey(); // Get registered Service by type Optional.ofNullable(shenyuClientRegisterService.get(rpcType)) .ifPresent(service -> { final List<URIRegisterDTO> list = entry.getValue(); // Build URI data types and register them with the registerURI method Map<String, List<URIRegisterDTO>> listMap = buildData(list); listMap.forEach(service::registerURI); }); } } // Find Service by type private Optional<ShenyuClientRegisterService> findService(final Collection<URIRegisterDTO> dataList) { return dataList.stream().map(dto -> shenyuClientRegisterService.get(dto.getRpcType())).findFirst(); }}
ShenyuClientRegisterService#register()
ShenyuClientRegisterService is the registration method interface, which has several implementation classes.
AbstractContextPathRegisterService: abstract class, handling part of the public logic.
AbstractShenyuClientRegisterServiceImpl: : abstract class, handles part of the public logic.
From the above, we can see that each microservice has a corresponding registration implementation class. The source code analysis in this article is based on the official shenyu-examples-http as an example, it is of http registration type, so the registration implementation class for metadata and URI data is ShenyuClientRegisterDivideServiceImpl: ShenyuClientRegisterDivideServiceImpl.
register():
public abstract class AbstractShenyuClientRegisterServiceImpl extends FallbackShenyuClientRegisterService implements ShenyuClientRegisterService { //...... public String register(final MetaDataRegisterDTO dto) { // 1.register selector information String selectorHandler = selectorHandler(dto); String selectorId = selectorService.registerDefault(dto, PluginNameAdapter.rpcTypeAdapter(rpcType()), selectorHandler); // 2.register rule information String ruleHandler = ruleHandler(); RuleDTO ruleDTO = buildRpcDefaultRuleDTO(selectorId, dto, ruleHandler); ruleService.registerDefault(ruleDTO); // 3.register metadata information registerMetadata(dto); // 4.register contextPath String contextPath = dto.getContextPath(); if (StringUtils.isNotEmpty(contextPath)) { registerContextPath(dto); } return ShenyuResultMessage.SUCCESS; }}
The whole registration logic can be divided into 4 steps.
Register selector information
Register rule information
Register metadata information
Register `contextPath
This side of admin requires the construction of selectors, rules, metadata and ContextPath through the metadata information of the client. The specific registration process and details of processing are related to the rpc type. We will not continue to track down the logical analysis of the registration center, tracking to this point is enough.
The source code of the server-side metadata registration process is analyzed and the flow chart is described as follows.
registerURI()
public abstract class AbstractShenyuClientRegisterServiceImpl extends FallbackShenyuClientRegisterService implements ShenyuClientRegisterService { //...... public String registerURI(final String selectorName, final List<URIRegisterDTO> uriList) { if (CollectionUtils.isEmpty(uriList)) { return ""; } // Does the corresponding selector exist SelectorDO selectorDO = selectorService.findByNameAndPluginName(selectorName, PluginNameAdapter.rpcTypeAdapter(rpcType())); if (Objects.isNull(selectorDO)) { return ""; } // Handle handler information in the selector String handler = buildHandle(uriList, selectorDO); selectorDO.setHandle(handler); SelectorData selectorData = selectorService.buildByName(selectorName, PluginNameAdapter.rpcTypeAdapter(rpcType())); selectorData.setHandle(handler); // Update records in the database selectorService.updateSelective(selectorDO); // publish Event to gateway eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, DataEventTypeEnum.UPDATE, Collections.singletonList(selectorData))); return ShenyuResultMessage.SUCCESS; }}
After admin gets the URI data, it mainly updates the handler information in the selector, then writes it to the database, and finally publishes the event notification gateway. The logic of notifying the gateway is done by the data synchronization operation, which has been analyzed in the previous article, so we will not repeat it.
The source code analysis of the server-side URI registration process is complete and is described in the following diagram.
At this point, the server-side registration process is also analyzed, mainly through the interface provided externally, accept the registration information from the client, and then write to the Disruptor queue, and then consume data from it, and update the admin selector, rules, metadata and selector handler according to the received metadata and URI data.
This article focuses on the http registration module of the Apache ShenYu gateway for source code analysis. The main knowledge points involved are summarized as follows.
The register center is for registering client information to admin to facilitate traffic filtering.
http registration is to register client metadata information and URI information to admin.
http service access is identified by the annotation @ShenyuSpringMvcClient.
construction of the registration information mainly through the application listener ApplicationListener.
loading of the registration type is done through SPI.
The Disruptor queue was introduced to decouple data from operations, and data buffering.
The implementation of the registry uses interface-oriented programming, using design patterns such as template methods, singleton, and observer.
This article is based on the source code analysis of version 'shenyu-2.6.1'. Please refer to the official website for an introduction Data Synchronization Design.
Enter the createPlugin() method in the PluginController class, which is responsible for data validation, adding or updating data, and returning result information.
@Validated@RequiredArgsConstructor@RestController@RequestMapping("/plugin")public class PluginController { @PostMapping("") @RequiresPermissions("system:plugin:add") public ShenyuAdminResult createPlugin(@Valid @ModelAttribute final PluginDTO pluginDTO) { // Call pluginService.createOrUpdate for processing logic return ShenyuAdminResult.success(pluginService.createOrUpdate(pluginDTO)); } // ......}
Use the create() method in the PluginServiceImpl class to convert data, save it to the database, and publish events.
@RequiredArgsConstructor@Servicepublic class PluginServiceImpl implements SelectorService { // Event publishing object pluginEventPublisher private final PluginEventPublisher pluginEventPublisher; private String create(final PluginDTO pluginDTO) { // Check if there is a corresponding plugin Assert.isNull(pluginMapper.nameExisted(pluginDTO.getName()), AdminConstants.PLUGIN_NAME_IS_EXIST); // check if Customized plugin jar if (!Objects.isNull(pluginDTO.getFile())) { Assert.isTrue(checkFile(Base64.decode(pluginDTO.getFile())), AdminConstants.THE_PLUGIN_JAR_FILE_IS_NOT_CORRECT_OR_EXCEEDS_16_MB); } // Create plugin object PluginDO pluginDO = PluginDO.buildPluginDO(pluginDTO); // Insert object into database if (pluginMapper.insertSelective(pluginDO) > 0) { // publish create event. init plugin data pluginEventPublisher.onCreated(pluginDO); } return ShenyuResultMessage.CREATE_SUCCESS; } // ......}
Complete the data persistence operation in the PluginServiceImpl class, that is, save the data to the database and publish events through pluginEventPublisher.
The logic of the pluginEventPublisher.onCreated method is to publish the changed event:
Publishing change data is completed through publisher.publishEvent(), which is an 'Application EventPublisher' object with the fully qualified name of 'org. springframework. contentxt.' Application EventPublisher `. From here, we know that publishing data is accomplished through the Spring related features.
About ApplicationEventPublisher:
When there is a state change, the publisher calls the publishEvent method of ApplicationEventPublisher to publish an event, the Spring container broadcasts the event to all observers, and calls the observer's onApplicationEvent method to pass the event object to the observer. There are two ways to call the publishEvent method. One is to implement the interface, inject the ApplicationEventPublisher object into the container, and then call its method. The other is to call the container directly. There is not much difference between the two methods to publish events.
ApplicationEventPublisher:Publish events;
ApplicationEvent:Spring events,Record the source, time, and data of the event;
ApplicationListener:Event listeners, observers;
In the event publishing mechanism of Spring, there are three objects,
One is the ApplicationEventPublisher that publishes events, injecting an publisher through a constructor in ShenYu.
The other object is ApplicationEvent, which is inherited from ShenYu through DataChangedEvent, representing the event object
public class DataChangedEvent extends ApplicationEvent {//......}
The last one is ApplicationListener, which is implemented in ShenYu through the DataChangedEventDispatcher class as a listener for events, responsible for handling event objects.
@Componentpublic class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean { //......}
After the event is published, it will automatically enter the onApplicationEvent() method in the DataChangedEventDispatcher class for event processing.
@Componentpublic class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean { /** * When there is a data change, call this method * @param event */ @Override @SuppressWarnings("unchecked") public void onApplicationEvent(final DataChangedEvent event) { // Traverse data change listeners (only ApolloDataChangedListener will be registered here) for (DataChangedListener listener : listeners) { // Forward according to different grouping types switch (event.getGroupKey()) { case APP_AUTH: // authentication information listener.onAppAuthChanged((List<AppAuthData>) event.getSource(), event.getEventType()); break; case PLUGIN: // Plugin events // Calling the registered listener object listener.onPluginChanged((List<PluginData>) event.getSource(), event.getEventType()); break; case RULE: // Rule events listener.onRuleChanged((List<RuleData>) event.getSource(), event.getEventType()); break; case SELECTOR: // Selector event listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); break; case META_DATA: // Metadata events listener.onMetaDataChanged((List<MetaData>) event.getSource(), event.getEventType()); break; case PROXY_SELECTOR: // Proxy selector event listener.onProxySelectorChanged((List<ProxySelectorData>) event.getSource(), event.getEventType()); break; case DISCOVER_UPSTREAM: // Registration discovery of downstream list events listener.onDiscoveryUpstreamChanged((List<DiscoverySyncData>) event.getSource(), event.getEventType()); applicationContext.getBean(LoadServiceDocEntry.class).loadDocOnUpstreamChanged((List<DiscoverySyncData>) event.getSource(), event.getEventType()); break; default: throw new IllegalStateException("Unexpected value: " + event.getGroupKey()); } } }}
When there is a data change, call the onApplicationEvent method, then traverse all data change listeners, determine which data type it is, and hand it over to the corresponding data listeners for processing.
ShenYu has grouped all data into the following types: authentication information, plugin information, rule information, selector information, metadata, proxy selector, and downstream event discovery.
The Data Change Listener here is an abstraction of the data synchronization strategy, processed by specific implementations, and different listeners are processed by different implementations. Currently, Apollo is being analyzed
Listening, so here we only focus on ApolloDataChangedListener.
// Inheriting AbstractNodeDataChangedListenerpublic class ApolloDataChangedListener extends AbstractNodeDataChangedListener {}
ApolloDataChangedListener inherits the AbstractNodeDataChangedListener class, which mainly uses key as the base class for storage, such as Apollo, Nacos, etc., while others such as Zookeeper
Consul, etc. are searched in a hierarchical manner using a path.
// Using key as the base class for finding storage methodspublic abstract class AbstractNodeDataChangedListener implements DataChangedListener { protected AbstractNodeDataChangedListener(final ChangeData changeData) { this.changeData = changeData; }}
AbstractNodeDataChangedListener receives ChangeData as a parameter, which defines the key names for each data stored in Apollo. The data stored in Apollo includes the following data:
Plugin(plugin)
Selector(selector)
Rules(rule)
Authorization(auth)
Metadata(meta)
Proxy selector(proxy.selector)
Downstream List (discovery)
These information are specified by the ApolloDataChangedListener constructor:
public class ApolloDataChangedListener extends AbstractNodeDataChangedListener { public ApolloDataChangedListener(final ApolloClient apolloClient) { // Configure prefixes for several types of grouped data super(new ChangeData(ApolloPathConstants.PLUGIN_DATA_ID, ApolloPathConstants.SELECTOR_DATA_ID, ApolloPathConstants.RULE_DATA_ID, ApolloPathConstants.AUTH_DATA_ID, ApolloPathConstants.META_DATA_ID, ApolloPathConstants.PROXY_SELECTOR_DATA_ID, ApolloPathConstants.DISCOVERY_DATA_ID)); // Manipulating objects of Apollo this.apolloClient = apolloClient; }}
DataChangedListener defines the following methods:
// Data Change Listenerpublic interface DataChangedListener { // Call when authorization information changes default void onAppAuthChanged(List<AppAuthData> changed, DataEventTypeEnum eventType) { } // Called when plugin information changes default void onPluginChanged(List<PluginData> changed, DataEventTypeEnum eventType) { } // Called when selector information changes default void onSelectorChanged(List<SelectorData> changed, DataEventTypeEnum eventType) { } // Called when metadata information changes default void onMetaDataChanged(List<MetaData> changed, DataEventTypeEnum eventType) { } // Call when rule information changes default void onRuleChanged(List<RuleData> changed, DataEventTypeEnum eventType) { } // Called when proxy selector changes default void onProxySelectorChanged(List<ProxySelectorData> changed, DataEventTypeEnum eventType) { } // Called when downstream information changes are discovered default void onDiscoveryUpstreamChanged(List<DiscoverySyncData> changed, DataEventTypeEnum eventType) { }}
When the plugin is processed by DataChangedEventDispatcher, the method listener.onPluginChanged is called. Next, analyze the logic of the object and implement the processing by AbstractNodeDataChangedListener:
public abstract class AbstractNodeDataChangedListener implements DataChangedListener { @Override public void onPluginChanged(final List<PluginData> changed, final DataEventTypeEnum eventType) { //Configure prefix as plugin. final String configKeyPrefix = changeData.getPluginDataId() + DefaultNodeConstants.JOIN_POINT; this.onCommonChanged(configKeyPrefix, changed, eventType, PluginData::getName, PluginData.class); LOG.debug("[DataChangedListener] PluginChanged {}", configKeyPrefix); }}
Firstly, the key prefix for constructing configuration data is: plugin., Call onCommonChanged again for unified processing:
private <T> void onCommonChanged(final String configKeyPrefix, final List<T> changedList, final DataEventTypeEnum eventType, final Function<? super T, ? extends String> mapperToKey, final Class<T> tClass) { // Avoiding concurrent operations on list nodes final ReentrantLock reentrantLock = listSaveLockMap.computeIfAbsent(configKeyPrefix, key -> new ReentrantLock()); try { reentrantLock.lock(); // Current incoming plugin list final List<String> changeNames = changedList.stream().map(mapperToKey).collect(Collectors.toList()); switch (eventType) { // Delete Operation case DELETE: // delete plugin.${pluginName} changedList.stream().map(mapperToKey).forEach(removeKey -> { delConfig(configKeyPrefix + removeKey); }); // Remove the corresponding plugin name from plugin. list // The plugin.list records the currently enabled list delChangedData(configKeyPrefix, changeNames); break; case REFRESH: case MYSELF: // Overload logic // Get a list of all plugins in plugin.list final List<String> configDataNames = this.getConfigDataNames(configKeyPrefix); // Update each currently adjusted plug-in in turn changedList.forEach(changedData -> { // Publish Configuration publishConfig(configKeyPrefix + mapperToKey.apply(changedData), changedData); }); // If there is more data in the currently stored list than what is currently being passed in, delete the excess data if (configDataNames != null && configDataNames.size() > changedList.size()) { // Kick out the currently loaded data configDataNames.removeAll(changeNames); // Delete cancelled data one by one configDataNames.forEach(this::delConfig); } // Update list data again publishConfig(configKeyPrefix + DefaultNodeConstants.LIST_STR, changeNames); break; default: // Add or update changedList.forEach(changedData -> { publishConfig(configKeyPrefix + mapperToKey.apply(changedData), changedData); }); // Update the newly added plugin putChangeData(configKeyPrefix, changeNames); break; } } catch (Exception e) { LOG.error("AbstractNodeDataChangedListener onCommonMultiChanged error ", e); } finally { reentrantLock.unlock(); } }
In the above logic, it actually includes the handling of full overloading (REFRESH, MYSELF) and increment (Delete, UPDATE, CREATE)
The plugin mainly includes two nodes:
plugin.list List of currently effective plugins
plugin.${plugin.name} Detailed information on specific plugins
Finally, write the data corresponding to these two nodes into Apollo.
After starting admin, the current data information will be fully synchronized to Apollo, which is implemented by ApolloDataChangedInit:
// Inheriting AbstractDataChangedInitpublic class ApolloDataChangedInit extends AbstractDataChangedInit { // Apollo operation object private final ApolloClient apolloClient; public ApolloDataChangedInit(final ApolloClient apolloClient) { this.apolloClient = apolloClient; } @Override protected boolean notExist() { // Check if nodes such as plugin, auth, meta, proxy.selector exist // As long as one does not exist, it enters reload (these nodes will not be created, why check once?) return Stream.of(ApolloPathConstants.PLUGIN_DATA_ID, ApolloPathConstants.AUTH_DATA_ID, ApolloPathConstants.META_DATA_ID, ApolloPathConstants.PROXY_SELECTOR_DATA_ID).allMatch( this::dataIdNotExist); } /** * Data id not exist boolean. * * @param pluginDataId the plugin data id * @return the boolean */ private boolean dataIdNotExist(final String pluginDataId) { return Objects.isNull(apolloClient.getItemValue(pluginDataId)); }}
Check if there is data in apollo, and if it does not exist, synchronize it.
There is a bug here because the key determined here will not be created during synchronization, which will cause data to be reloaded every time it is restarted. PR#5435
ApolloDataChangedInit implements the CommandLineRunner interface. It is an interface provided by springboot that executes the run() method after all Spring Beans are initialized. It is commonly used for initialization operations in projects.
SyncDataService.syncAll()
Query data from the database, then perform full data synchronization, including all authentication information, plugin information, rule information, selector information, metadata, proxy selector, and discover downstream events. Mainly, synchronization events are published through eventPublisher. After publishing events through publishEvent(), ApplicationListener performs event change operations, which is referred to as DataChangedEventDispatcher in ShenYu.
@Servicepublic class SyncDataServiceImpl implements SyncDataService { // Event Publishing private final ApplicationEventPublisher eventPublisher; /*** * Full data synchronization * @param type the type * @return */ @Override public boolean syncAll(final DataEventTypeEnum type) { // Synchronize auth data appAuthService.syncData(); // Synchronize plugin data List<PluginData> pluginDataList = pluginService.listAll(); //Notify subscribers through the Spring publish/subscribe mechanism (publishing DataChangedEvent) //Unified monitoring by DataChangedEventDispatcher //DataChangedEvent comes with configuration grouping type, current operation type, and data eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, type, pluginDataList)); // synchronizing selector List<SelectorData> selectorDataList = selectorService.listAll(); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, type, selectorDataList)); // Synchronization rules List<RuleData> ruleDataList = ruleService.listAll(); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.RULE, type, ruleDataList)); // Synchronization metadata metaDataService.syncData(); // Synchronization Downstream List discoveryService.syncData(); return true; }}
The data synchronization initialization operation on the gateway side mainly involves subscribing to nodes in apollo, and receiving changed data when there are changes. This depends on the listener mechanism of apollo. In ShenYu, the person responsible for Apollo data synchronization is ApolloDataService.
The functional logic of Apollo DataService is completed during the instantiation process: subscribe to the shenyu data synchronization node in Apollo. Implement through the configService.addChangeListener() method;
public class ApolloDataService extends AbstractNodeDataSyncService implements SyncDataService { public ApolloDataService(final Config configService, final PluginDataSubscriber pluginDataSubscriber, final List<MetaDataSubscriber> metaDataSubscribers, final List<AuthDataSubscriber> authDataSubscribers, final List<ProxySelectorDataSubscriber> proxySelectorDataSubscribers, final List<DiscoveryUpstreamDataSubscriber> discoveryUpstreamDataSubscribers) { // Configure the prefix for listening super(new ChangeData(ApolloPathConstants.PLUGIN_DATA_ID, ApolloPathConstants.SELECTOR_DATA_ID, ApolloPathConstants.RULE_DATA_ID, ApolloPathConstants.AUTH_DATA_ID, ApolloPathConstants.META_DATA_ID, ApolloPathConstants.PROXY_SELECTOR_DATA_ID, ApolloPathConstants.DISCOVERY_DATA_ID), pluginDataSubscriber, metaDataSubscribers, authDataSubscribers, proxySelectorDataSubscribers, discoveryUpstreamDataSubscribers); this.configService = configService; // Start listening // Note: The Apollo method is only responsible for obtaining data from Apollo and adding it to the local cache, and does not handle listening startWatch(); // Configure listening apolloWatchPrefixes(); }}
Firstly, configure the key information that needs to be processed and synchronize it with the admin's key. Next, call the startWatch() method to process data acquisition and listening. But in the implementation of Apollo, this method is only responsible for handling data retrieval and setting it to the local cache.
Listening is handled by the apolloWatchPrefixes method
private void apolloWatchPrefixes() { // Defining Listeners final ConfigChangeListener listener = changeEvent -> { changeEvent.changedKeys().forEach(changeKey -> { try { final ConfigChange configChange = changeEvent.getChange(changeKey); // Skip if not changed if (configChange == null) { LOG.error("apollo watchPrefixes error configChange is null {}", changeKey); return; } final String newValue = configChange.getNewValue(); // skip last is "list" // If it is a Key at the end of the list, such as plugin.list, skip it because it is only a list that records the effectiveness and will not be cached locally final int lastListStrIndex = changeKey.length() - DefaultNodeConstants.LIST_STR.length(); if (changeKey.lastIndexOf(DefaultNodeConstants.LIST_STR) == lastListStrIndex) { return; } // If it starts with plugin. => Process plugin data if (changeKey.indexOf(ApolloPathConstants.PLUGIN_DATA_ID) == 0) { // delete if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { // clear cache unCachePluginData(changeKey); } else { // update cache cachePluginData(newValue); } // If it starts with selector. => Process selector data } else if (changeKey.indexOf(ApolloPathConstants.SELECTOR_DATA_ID) == 0) { if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { unCacheSelectorData(changeKey); } else { cacheSelectorData(newValue); } // If it starts with rule. => Process rule data } else if (changeKey.indexOf(ApolloPathConstants.RULE_DATA_ID) == 0) { if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { unCacheRuleData(changeKey); } else { cacheRuleData(newValue); } // If it starts with auth. => Process auth data } else if (changeKey.indexOf(ApolloPathConstants.AUTH_DATA_ID) == 0) { if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { unCacheAuthData(changeKey); } else { cacheAuthData(newValue); } // If it starts with meta. => Process meta data } else if (changeKey.indexOf(ApolloPathConstants.META_DATA_ID) == 0) { if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { unCacheMetaData(changeKey); } else { cacheMetaData(newValue); } // If it starts with proxy.selector. => Process proxy.selector meta } else if (changeKey.indexOf(ApolloPathConstants.PROXY_SELECTOR_DATA_ID) == 0) { if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { unCacheProxySelectorData(changeKey); } else { cacheProxySelectorData(newValue); } // If it starts with discovery. => Process discovery meta } else if (changeKey.indexOf(ApolloPathConstants.DISCOVERY_DATA_ID) == 0) { if (PropertyChangeType.DELETED.equals(configChange.getChangeType())) { unCacheDiscoveryUpstreamData(changeKey); } else { cacheDiscoveryUpstreamData(newValue); } } } catch (Exception e) { LOG.error("apollo sync listener change key handler error", e); } }); }; watchConfigChangeListener = listener; // Add listening configService.addChangeListener(listener, Collections.emptySet(), ApolloPathConstants.pathKeySet()); }
The logic of loading data from the previous admin will only add two keys to the plugin: plugin.list and plugin.${plugin.name}, while plugin.list is a list of all enabled plugins, and the data for this key is in the
There is no data in the local cache, only `plugin${plugin.name} will be focus.
At this point, the synchronization logic of bootstrap in apollo has been analyzed.
Apache ShenYu is an asynchronous, high-performance, cross-language, responsive API gateway.
In ShenYu gateway, data synchronization refers to how to synchronize the updated data to the gateway after the data is sent in the background management system. The Apache ShenYu gateway currently supports data synchronization for ZooKeeper, WebSocket, http long poll, Nacos, Etcd and Consul. The main content of this article is based on Etcd data synchronization source code analysis.
This paper based on shenyu-2.4.0 version of the source code analysis, the official website of the introduction of please refer to the Data Synchronization Design .
Etcd is a strongly consistent, distributed key-value store that provides a reliable way to store data that needs to be accessed by a distributed system or cluster of machines.
We traced the source code from a real case, such as updating a selector data in the Divide plugin to a weight of 90 in a background administration system:
Enter the createSelector() method of the SelectorController class, which validates data, adds or updates data, and returns results.
@Validated@RequiredArgsConstructor@RestController@RequestMapping("/selector")public class SelectorController { @PutMapping("/{id}") public ShenyuAdminResult updateSelector(@PathVariable("id") final String id, @Valid @RequestBody final SelectorDTO selectorDTO) { // set the current selector data ID selectorDTO.setId(id); // create or update operation Integer updateCount = selectorService.createOrUpdate(selectorDTO); // return result return ShenyuAdminResult.success(ShenyuResultMessage.UPDATE_SUCCESS, updateCount); } // ......}
Convert data in the SelectorServiceImpl class using the createOrUpdate() method, save it to the database, publish the event, update upstream.
@RequiredArgsConstructor@Servicepublic class SelectorServiceImpl implements SelectorService { // eventPublisher private final ApplicationEventPublisher eventPublisher; @Override @Transactional(rollbackFor = Exception.class) public int createOrUpdate(final SelectorDTO selectorDTO) { int selectorCount; // build data DTO --> DO SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO); List<SelectorConditionDTO> selectorConditionDTOs = selectorDTO.getSelectorConditions(); // insert or update ? if (StringUtils.isEmpty(selectorDTO.getId())) { // insert into data selectorCount = selectorMapper.insertSelective(selectorDO); // insert into condition data selectorConditionDTOs.forEach(selectorConditionDTO -> { selectorConditionDTO.setSelectorId(selectorDO.getId()); selectorConditionMapper.insertSelective(SelectorConditionDO.buildSelectorConditionDO(selectorConditionDTO)); }); // check selector add if (dataPermissionMapper.listByUserId(JwtUtils.getUserInfo().getUserId()).size() > 0) { DataPermissionDTO dataPermissionDTO = new DataPermissionDTO(); dataPermissionDTO.setUserId(JwtUtils.getUserInfo().getUserId()); dataPermissionDTO.setDataId(selectorDO.getId()); dataPermissionDTO.setDataType(AdminConstants.SELECTOR_DATA_TYPE); dataPermissionMapper.insertSelective(DataPermissionDO.buildPermissionDO(dataPermissionDTO)); } } else { // update data, delete and then insert selectorCount = selectorMapper.updateSelective(selectorDO); //delete rule condition then add selectorConditionMapper.deleteByQuery(new SelectorConditionQuery(selectorDO.getId())); selectorConditionDTOs.forEach(selectorConditionDTO -> { selectorConditionDTO.setSelectorId(selectorDO.getId()); SelectorConditionDO selectorConditionDO = SelectorConditionDO.buildSelectorConditionDO(selectorConditionDTO); selectorConditionMapper.insertSelective(selectorConditionDO); }); } // publish event publishEvent(selectorDO, selectorConditionDTOs); // update upstream updateDivideUpstream(selectorDO); return selectorCount; } // ......}
In the Service class to persist data, i.e. to the database, this should be familiar, not expand. The update upstream operation is analyzed in the corresponding section below, focusing on the publish event operation, which performs data synchronization.
The logic of the publishEvent() method is to find the plugin corresponding to the selector, build the conditional data, and publish the change data.
Change data released by eventPublisher.PublishEvent() is complete, the eventPublisher object is a ApplicationEventPublisher class, The fully qualified class name is org.springframework.context.ApplicationEventPublisher. Here we see that publishing data is done through Spring related functionality.
ApplicationEventPublisher:
When a state change, the publisher calls ApplicationEventPublisher of publishEvent method to release an event, Spring container broadcast event for all observers, The observer's onApplicationEvent method is called to pass the event object to the observer. There are two ways to call publishEvent method, one is to implement the interface by the container injection ApplicationEventPublisher object and then call the method, the other is a direct call container, the method of two methods of publishing events not too big difference.
ApplicationEventPublisher: publish event;
ApplicationEvent: Spring event, record the event source, time, and data;
ApplicationListener: event listener, observer.
In Spring event publishing mechanism, there are three objects,
An object is a publish event ApplicationEventPublisher, in ShenYu through the constructor in the injected a eventPublisher.
The other object is ApplicationEvent , inherited from ShenYu through DataChangedEvent, representing the event object.
public class DataChangedEvent extends ApplicationEvent {//......}
The last object is ApplicationListener in ShenYu in through DataChangedEventDispatcher class implements this interface, as the event listener, responsible for handling the event object.
@Componentpublic class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean { //......}
Released when the event is completed, will automatically enter the DataChangedEventDispatcher class onApplicationEvent() method of handling events.
@Componentpublic class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean { /** * This method is called when there are data changes * @param event */ @Override @SuppressWarnings("unchecked") public void onApplicationEvent(final DataChangedEvent event) { // Iterate through the data change listener (usually using a data synchronization approach is fine) for (DataChangedListener listener : listeners) { // What kind of data has changed switch (event.getGroupKey()) { case APP_AUTH: // app auth data listener.onAppAuthChanged((List<AppAuthData>) event.getSource(), event.getEventType()); break; case PLUGIN: // plugin data listener.onPluginChanged((List<PluginData>) event.getSource(), event.getEventType()); break; case RULE: // rule data listener.onRuleChanged((List<RuleData>) event.getSource(), event.getEventType()); break; case SELECTOR: // selector data listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); break; case META_DATA: // metadata listener.onMetaDataChanged((List<MetaData>) event.getSource(), event.getEventType()); break; default: // other types throw exception throw new IllegalStateException("Unexpected value: " + event.getGroupKey()); } } }}
When there is a data change, the onApplicationEvent method is called and all the data change listeners are iterated to determine the data type and handed over to the appropriate data listener for processing.
ShenYu groups all the data into five categories: APP_AUTH, PLUGIN, RULE, SELECTOR and META_DATA.
Here the data change listener (DataChangedListener) is an abstraction of the data synchronization policy. Its concrete implementation is:
These implementation classes are the synchronization strategies currently supported by ShenYu:
WebsocketDataChangedListener: data synchronization based on Websocket;
ZookeeperDataChangedListener:data synchronization based on Zookeeper;
ConsulDataChangedListener: data synchronization based on Consul;
EtcdDataDataChangedListener:data synchronization based on etcd;
HttpLongPollingDataChangedListener:data synchronization based on http long polling;
NacosDataChangedListener:data synchronization based on nacos;
Given that there are so many implementation strategies, how do you decide which to use?
Because this paper is based on Etcd data synchronization source code analysis, so here to EtcdDataDataChangedListener as an example, the analysis of how it is loaded and implemented.
A global search in the source code project shows that its implementation is done in the DataSyncConfiguration class.
/** * Data Sync Configuration * By springboot conditional assembly * The type Data sync configuration. */@Configurationpublic class DataSyncConfiguration { /** * The type Etcd listener. */ @Configuration @ConditionalOnProperty(prefix = "shenyu.sync.etcd", name = "url") @EnableConfigurationProperties(EtcdProperties.class) static class EtcdListener { @Bean public EtcdClient etcdClient(final EtcdProperties etcdProperties) { Client client = Client.builder() .endpoints(etcdProperties.getUrl()) .build(); return new EtcdClient(client); } /** * Config event listener data changed listener. * * @param etcdClient the etcd client * @return the data changed listener */ @Bean @ConditionalOnMissingBean(EtcdDataDataChangedListener.class) public DataChangedListener etcdDataChangedListener(final EtcdClient etcdClient) { return new EtcdDataDataChangedListener(etcdClient); } /** * data init. * * @param etcdClient the etcd client * @param syncDataService the sync data service * @return the etcd data init */ @Bean @ConditionalOnMissingBean(EtcdDataInit.class) public EtcdDataInit etcdDataInit(final EtcdClient etcdClient, final SyncDataService syncDataService) { return new EtcdDataInit(etcdClient, syncDataService); } } // other code is omitted......}
This configuration class is implemented through the SpringBoot conditional assembly class. The EtcdListener class has several annotations:
@ConditionalOnProperty(prefix = "shenyu.sync.etcd", name = "url"): attribute condition. The configuration class takes effect only when the condition is met. That is, when we have the following configuration, etcd is used for data synchronization.
shenyu: sync: etcd: url: localhost:2181
@EnableConfigurationProperties(EtcdProperties.class):import EtcdProperties; The properties in the class EtcdProperties is relative to the properties which is with shenyu.sync.etcd as prefix in the configuration file.
When the shenyu.sync.etcd.url property is set in the configuration file, Admin would use the etcd data synchronization, EtcdListener is generated and the beans with type EtcdClient, EtcdDataDataChangedListener and EtcdDataInit would also be generated.
The bean with the type EtcdClient would be generated, named etcdClient. This bean configues the connection properties of the etcd server based on the configuration file and can operate the etcdnodes directly.
The bean with the type EtcdDataDataChangedListener would be generated, named etcdDataDataChangedListener. This bean use the bean etcdClient as a member variable and so when the event is listened, etcdDataDataChangedListener would call the callback method and use the etcdClient to operate the etcd nodes.
The bean with the type EtcdDataInit would be generated, named etcdDataInit. This bean use the bean etcdClient and syncDataService as member variables, and use etcdClient to judge whether the data are initialized, if not, would use syncDataService to refresh data. We would dive into the details later.
So in the event handler onApplicationEvent(), it goes to the corresponding listener. In our case, it is a selector data update, data synchronization is etcd, so, the code will enter the EtcdDataDataChangedListener selector data change process.
@Override @SuppressWarnings("unchecked") public void onApplicationEvent(final DataChangedEvent event) { // Iterate through the data change listener (usually using a data synchronization approach is fine) for (DataChangedListener listener : listeners) { // what kind of data has changed switch (event.getGroupKey()) { // other code logic is omitted case SELECTOR: // selector data listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); // In our case, will enter the EtcdDataDataChangedListener selector data change process break; } }
In the onSelectorChanged() method, determine the type of action, whether to refresh synchronization or update or create synchronization. Determine whether the node is in etcd based on the current selector data.
/** * EtcdDataDataChangedListener. */@Slf4jpublic class EtcdDataDataChangedListener implements DataChangedListener { @Override public void onSelectorChanged(final List<SelectorData> changed, final DataEventTypeEnum eventType) { if (eventType == DataEventTypeEnum.REFRESH && !changed.isEmpty()) { String selectorParentPath = DefaultPathConstants.buildSelectorParentPath(changed.get(0).getPluginName()); etcdClient.deleteEtcdPathRecursive(selectorParentPath); } for (SelectorData data : changed) { String selectorRealPath = DefaultPathConstants.buildSelectorRealPath(data.getPluginName(), data.getId()); if (eventType == DataEventTypeEnum.DELETE) { etcdClient.delete(selectorRealPath); continue; } //create or update updateNode(selectorRealPath, data); } }}
This part is very important. The variable changed represents the SelectorData list, the variable eventType reprents the event type. When the event type is REFRESH and the SelectorData has changed, all the selector nodes under this plugin would be deleted in etcd. We should notice that the condition that the SelectorData has changed is necessary, otherwise a bug would appear that all the selector nodes would be deleted when no SelectorData data has changed.
As long as the changed data is correctly written to the etcd node, the admin side of the operation is complete.
In our current case, updating one of the selector data in the Divide plugin with a weight of 90 updates specific nodes in the graph.
We series the above update flow with a sequence diagram.
Assume that the ShenYu gateway is already running properly, and the data synchronization mode is also etcd. How does the gateway receive and process the selector data after updating it on the admin side and sending the changed data to etcd? Let's continue our source code analysis to find out.
There is a EtcdSyncDataService class on the gateway, which subscribing to the data node through etcdClient and can sense when the data changes.
/** * Data synchronize of etcd. */@Slf4jpublic class EtcdSyncDataService implements SyncDataService, AutoCloseable { private void subscribeSelectorDataChanges(final String path) { etcdClient.watchDataChange(path, (updateNode, updateValue) -> cacheSelectorData(updateValue), this::unCacheSelectorData); } //other codes omitted}
Etcd's Watch mechanism notifies subscribing clients of node changes. In our case, updating the selector information goes to the watchDataChange() method. cacheSelectorData() is used to process data.
PluginDataSubscriber is an interface, it is only a CommonPluginDataSubscriber implementation class, responsible for data processing plugin, selector and rules.
It has no additional logic and calls the subscribeDataHandler() method directly. Within methods, there are data types (plugins, selectors, or rules) and action types (update or delete) to perform different logic.
/** * The common plugin data subscriber, responsible for handling all plug-in, selector, and rule information */public class CommonPluginDataSubscriber implements PluginDataSubscriber { //...... // handle selector data @Override public void onSelectorSubscribe(final SelectoData selectorData) { subscribeDataHandler(selectorData, DataEventTypeEnum.UPDATE); } // A subscription data handler that handles updates or deletions of data private <T> void subscribeDataHandler(final T classData, final DataEventTypeEnum dataType) { Optional.ofNullable(classData).ifPresent(data -> { // plugin data if (data instanceof PluginData) { PluginData pluginData = (PluginData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cachePluginData(pluginData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(pluginData.getName())).ifPresent(handler -> handler.handlerPlugin(pluginData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removePluginData(pluginData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(pluginData.getName())).ifPresent(handler -> handler.removePlugin(pluginData)); } } else if (data instanceof SelectorData) { // selector data SelectorData selectorData = (SelectorData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cacheSelectData(selectorData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.handlerSelector(selectorData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removeSelectData(selectorData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.removeSelector(selectorData)); } } else if (data instanceof RuleData) { // rule data RuleData ruleData = (RuleData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cacheRuleData(ruleData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(ruleData.getPluginName())).ifPresent(handler -> handler.handlerRule(ruleData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removeRuleData(ruleData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(ruleData.getPluginName())).ifPresent(handler -> handler.removeRule(ruleData)); } } }); }}
// save the data to gateway memoryBaseDataCache.getInstance().cacheSelectData(selectorData);// If each plugin has its own processing logic, then do itOptional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.handlerSelector(selectorData));
One is to save the data to the gateway's memory. BaseDataCache is the class that ultimately caches data, implemented in a singleton pattern. The selector data is stored in the SELECTOR_MAP Map. In the subsequent use, also from this data.
public final class BaseDataCache { // private instance private static final BaseDataCache INSTANCE = new BaseDataCache(); // private constructor private BaseDataCache() { } /** * Gets instance. * public method * @return the instance */ public static BaseDataCache getInstance() { return INSTANCE; } /** * A Map of the cache selector data * pluginName -> SelectorData. */ private static final ConcurrentMap<String, List<SelectorData>> SELECTOR_MAP = Maps.newConcurrentMap(); public void cacheSelectData(final SelectorData selectorData) { Optional.ofNullable(selectorData).ifPresent(this::selectorAccept); } /** * cache selector data. * @param data the selector data */ private void selectorAccept(final SelectorData data) { String key = data.getPluginName(); if (SELECTOR_MAP.containsKey(key)) { // Update operation, delete before insert List<SelectorData> existList = SELECTOR_MAP.get(key); final List<SelectorData> resultList = existList.stream().filter(r -> !r.getId().equals(data.getId())).collect(Collectors.toList()); resultList.add(data); final List<SelectorData> collect = resultList.stream().sorted(Comparator.comparing(SelectorData::getSort)).collect(Collectors.toList()); SELECTOR_MAP.put(key, collect); } else { // Add new operations directly to Map SELECTOR_MAP.put(key, Lists.newArrayList(data)); } }}
Second, if each plugin has its own processing logic, then do it. Through the IDEA editor, you can see that after adding a selector, there are the following plugins and processing. We're not going to expand it here.
After the above source tracking, and through a practical case, in the admin end to update a selector data, the ZooKeeper data synchronization process analysis is clear.
Let's series the data synchronization process on the gateway side through the sequence diagram:
The data synchronization process has been analyzed. In order to prevent the synchronization process from being interrupted, other logic is ignored during the analysis. We also need to analyze the process of Admin synchronization data initialization and gateway synchronization operation initialization.
When admin starts, the current data will be fully synchronized to etcd, the implementation logic is as follows:
/** * EtcdDataInit. */@Slf4jpublic class EtcdDataInit implements CommandLineRunner { private final EtcdClient etcdClient; private final SyncDataService syncDataService; public EtcdDataInit(final EtcdClient client, final SyncDataService syncDataService) { this.etcdClient = client; this.syncDataService = syncDataService; } @Override public void run(final String... args) throws Exception { final String pluginPath = DefaultPathConstants.PLUGIN_PARENT; final String authPath = DefaultPathConstants.APP_AUTH_PARENT; final String metaDataPath = DefaultPathConstants.META_DATA; if (!etcdClient.exists(pluginPath) && !etcdClient.exists(authPath) && !etcdClient.exists(metaDataPath)) { log.info("Init all data from database"); syncDataService.syncAll(DataEventTypeEnum.REFRESH); } }}
Check whether there is data in etcd, if not, then synchronize.
EtcdDataInit implements the CommandLineRunner interface. It is an interface provided by SpringBoot that executes the run() method after all Spring Beans initializations and is often used for initialization operations in a project.
SyncDataService.syncAll()
Query data from the database, and then perform full data synchronization, all authentication information, plugin information, selector information, rule information, and metadata information. Synchronous events are published primarily through eventPublisher. After publishing the event via publishEvent(), the ApplicationListener performs the event change operation. In ShenYu is mentioned in DataChangedEventDispatcher.
@Servicepublic class SyncDataServiceImpl implements SyncDataService { // eventPublisher private final ApplicationEventPublisher eventPublisher; /*** * sync all data * @param type the type * @return */ @Override public boolean syncAll(final DataEventTypeEnum type) { // app auth data appAuthService.syncData(); // plugin data List<PluginData> pluginDataList = pluginService.listAll(); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, type, pluginDataList)); // selector data List<SelectorData> selectorDataList = selectorService.listAll(); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, type, selectorDataList)); // rule data List<RuleData> ruleDataList = ruleService.listAll(); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.RULE, type, ruleDataList)); // metadata metaDataService.syncData(); return true; }}
The initial operation of data synchronization on the gateway side is mainly the node in the subscription etcd. When there is a data change, the changed data will be received. This relies on the Watch mechanism of etcd. In ShenYu, the one responsible for etcd data synchronization is EtcdSyncDataService, also mentioned earlier.
The function logic of EtcdSyncDataService is completed in the process of instantiation: the subscription to Shenyu data synchronization node in etcd is completed. Subscription here is divided into two kinds, one kind is existing node data updated above, through this etcdClient.subscribeDataChanges() method; Another kind is under the current node, add or delete nodes change namely child nodes, it through etcdClient.subscribeChildChanges() method.
EtcdSyncDataService code is a bit too much, here we use plugin data read and subscribe to track, other types of data operation principle is the same.
/** * Data synchronize of etcd. */@Slf4jpublic class EtcdSyncDataService implements SyncDataService, AutoCloseable { /** * Instantiates a new Zookeeper cache manager. * * @param etcdClient the etcd client * @param pluginDataSubscriber the plugin data subscriber * @param metaDataSubscribers the meta data subscribers * @param authDataSubscribers the auth data subscribers */ public EtcdSyncDataService(final EtcdClient etcdClient, final PluginDataSubscriber pluginDataSubscriber, final List<MetaDataSubscriber> metaDataSubscribers, final List<AuthDataSubscriber> authDataSubscribers) { this.etcdClient = etcdClient; this.pluginDataSubscriber = pluginDataSubscriber; this.metaDataSubscribers = metaDataSubscribers; this.authDataSubscribers = authDataSubscribers; watcherData(); watchAppAuth(); watchMetaData(); } private void watcherData() { final String pluginParent = DefaultPathConstants.PLUGIN_PARENT; List<String> pluginZKs = etcdClientGetChildren(pluginParent); for (String pluginName : pluginZKs) { watcherAll(pluginName); } etcdClient.watchChildChange(pluginParent, (updateNode, updateValue) -> { if (!updateNode.isEmpty()) { watcherAll(updateNode); } }, null); } private void watcherAll(final String pluginName) { watcherPlugin(pluginName); watcherSelector(pluginName); watcherRule(pluginName); } private void watcherPlugin(final String pluginName) { String pluginPath = DefaultPathConstants.buildPluginPath(pluginName); cachePluginData(etcdClient.get(pluginPath)); subscribePluginDataChanges(pluginPath, pluginName); } private void cachePluginData(final String dataString) { final PluginData pluginData = GsonUtils.getInstance().fromJson(dataString, PluginData.class); Optional.ofNullable(pluginData) .flatMap(data -> Optional.ofNullable(pluginDataSubscriber)).ifPresent(e -> e.onSubscribe(pluginData)); } private void subscribePluginDataChanges(final String pluginPath, final String pluginName) { etcdClient.watchDataChange(pluginPath, (updatePath, updateValue) -> { final String dataPath = buildRealPath(pluginPath, updatePath); final String dataStr = etcdClient.get(dataPath); final PluginData data = GsonUtils.getInstance().fromJson(dataStr, PluginData.class); Optional.ofNullable(data) .ifPresent(d -> Optional.ofNullable(pluginDataSubscriber).ifPresent(e -> e.onSubscribe(d))); }, deleteNode -> deletePlugin(pluginName)); }}
The above source code is given comments, I believe you can understand. The main logic for subscribing to plug-in data is as follows:
Create the current plugin path
Read the current node data on etcd and deserialize it
Apache ShenYu is an asynchronous, high-performance, cross-language, responsive API gateway.
In ShenYu gateway, data synchronization refers to how to synchronize the updated data to the gateway after the data is sent in the background management system. The Apache ShenYu gateway currently supports data synchronization for ZooKeeper, WebSocket, http long poll, Nacos, etcd and Consul. The main content of this article is based on http long poll data synchronization source code analysis.
This paper based on shenyu-2.5.0 version of the source code analysis, the official website of the introduction of please refer to the Data Synchronization Design .
Here is a direct quote from the official website with the relevant description.
The mechanism of Zookeeper and WebSocket data synchronization is relatively simple, while Http long polling is more complex. Apache ShenYu borrowed the design ideas of Apollo and Nacos, took their essence, and implemented Http long polling data synchronization function by itself. Note that this is not the traditional ajax long polling!
Http Long Polling mechanism as shown above, Apache ShenYu gateway active request shenyu-admin configuration service, read timeout time is 90s, means that the gateway layer request configuration service will wait at most 90s, so as to facilitate shenyu-admin configuration service timely response to change data, so as to achieve quasi real-time push.
The Http long polling mechanism is initiated by the gateway requesting shenyu-admin, so for this source code analysis, we start from the gateway side.
The Http long polling data synchronization configuration is loaded through spring boot starter mechanism when we introduce the relevant dependencies and have the following configuration in the configuration file.
Introduce dependencies in the pom file.
<!--shenyu data sync start use http--><dependency><groupId>org.apache.shenyu</groupId><artifactId>shenyu-spring-boot-starter-sync-data-http</artifactId><version>${project.version}</version></dependency>
Add the following configuration to the application.yml configuration file.
shenyu:sync:http:url: http://localhost:9095
When the gateway is started, the configuration class HttpSyncDataConfiguration is executed, loading the corresponding Bean.
/** * Http sync data configuration for spring boot. */@Configuration@ConditionalOnClass(HttpSyncDataService.class)@ConditionalOnProperty(prefix = "shenyu.sync.http", name = "url")@EnableConfigurationProperties(value = HttpConfig.class)public class HttpSyncDataConfiguration { private static final Logger LOGGER = LoggerFactory.getLogger(HttpSyncDataConfiguration.class); /** * Rest template. * * @param httpConfig the http config * @return the rest template */ @Bean public RestTemplate restTemplate(final HttpConfig httpConfig) { OkHttp3ClientHttpRequestFactory factory = new OkHttp3ClientHttpRequestFactory(); factory.setConnectTimeout(Objects.isNull(httpConfig.getConnectionTimeout()) ? (int) HttpConstants.CLIENT_POLLING_CONNECT_TIMEOUT : httpConfig.getConnectionTimeout()); factory.setReadTimeout(Objects.isNull(httpConfig.getReadTimeout()) ? (int) HttpConstants.CLIENT_POLLING_READ_TIMEOUT : httpConfig.getReadTimeout()); factory.setWriteTimeout(Objects.isNull(httpConfig.getWriteTimeout()) ? (int) HttpConstants.CLIENT_POLLING_WRITE_TIMEOUT : httpConfig.getWriteTimeout()); return new RestTemplate(factory); } /** * AccessTokenManager. * * @param httpConfig the http config. * @param restTemplate the rest template. * @return the access token manager. */ @Bean public AccessTokenManager accessTokenManager(final HttpConfig httpConfig, final RestTemplate restTemplate) { return new AccessTokenManager(restTemplate, httpConfig); } /** * Http sync data service. * * @param httpConfig the http config * @param pluginSubscriber the plugin subscriber * @param restTemplate the rest template * @param metaSubscribers the meta subscribers * @param authSubscribers the auth subscribers * @param accessTokenManager the access token manager * @return the sync data service */ @Bean public SyncDataService httpSyncDataService(final ObjectProvider<HttpConfig> httpConfig, final ObjectProvider<PluginDataSubscriber> pluginSubscriber, final ObjectProvider<RestTemplate> restTemplate, final ObjectProvider<List<MetaDataSubscriber>> metaSubscribers, final ObjectProvider<List<AuthDataSubscriber>> authSubscribers, final ObjectProvider<AccessTokenManager> accessTokenManager) { LOGGER.info("you use http long pull sync shenyu data"); return new HttpSyncDataService( Objects.requireNonNull(httpConfig.getIfAvailable()), Objects.requireNonNull(pluginSubscriber.getIfAvailable()), Objects.requireNonNull(restTemplate.getIfAvailable()), metaSubscribers.getIfAvailable(Collections::emptyList), authSubscribers.getIfAvailable(Collections::emptyList), Objects.requireNonNull(accessTokenManager.getIfAvailable()) ); }}
HttpSyncDataConfiguration is the configuration class for Http long polling data synchronization, responsible for creating HttpSyncDataService (responsible for the concrete implementation of http data synchronization) 、 RestTemplate and AccessTokenManager (responsible for the access token processing). It is annotated as follows.
@Configuration: indicates that this is a configuration class.
@ConditionalOnClass(HttpSyncDataService.class): conditional annotation indicating that the class HttpSyncDataService is to be present.
@ConditionalOnProperty(prefix = "shenyu.sync.http", name = "url"): conditional annotation to have the property shenyu.sync.http.url configured.
@EnableConfigurationProperties(value = HttpConfig.class): indicates that the annotation @ConfigurationProperties(prefix = "shenyu.sync.http") on HttpConfig will take effect, and the configuration class HttpConfig will be injected into the Ioc container.
In the start() method, two things are done, one is to get the full amount of data, that is, to request the admin side to get all the data that needs to be synchronized, and then cache the acquired data into the gateway memory. The other is to open a multi-threaded execution of a long polling task.
public class HttpSyncDataService implements SyncDataService { // ...... private void start() { // It could be initialized multiple times, so you need to control that. if (RUNNING.compareAndSet(false, true)) { // fetch all group configs. // Initial startup, get full data this.fetchGroupConfig(ConfigGroupEnum.values()); // one backend service, one thread int threadSize = serverList.size(); // ThreadPoolExecutor this.executor = new ThreadPoolExecutor(threadSize, threadSize, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), ShenyuThreadFactory.create("http-long-polling", true)); // start long polling, each server creates a thread to listen for changes. this.serverList.forEach(server -> this.executor.execute(new HttpLongPollingTask(server))); } else { LOG.info("shenyu http long polling was started, executor=[{}]", executor); } } // ......}
ShenYu groups all the data that needs to be synchronized, there are 5 data types, namely plugins, selectors, rules, metadata and authentication data.
public enum ConfigGroupEnum { APP_AUTH, // app auth data PLUGIN, // plugin data RULE, // rule data SELECTOR, // selector data META_DATA; // meta data}
The admin may be a cluster, and here a request is made to each admin in a round-robin fashion, and if one succeeds, then the operation to get the full amount of data from the admin and cache it to the gateway is executed successfully. If there is an exception, the request is launched to the next admin.
public class HttpSyncDataService implements SyncDataService { // ...... private void fetchGroupConfig(final ConfigGroupEnum... groups) throws ShenyuException { // It is possible that admins are clustered, and here requests are made to each admin by means of a loop. for (int index = 0; index < this.serverList.size(); index++) { String server = serverList.get(index); try { // do execute this.doFetchGroupConfig(server, groups); // If you have a success, you are successful and can exit the loop break; } catch (ShenyuException e) { // An exception occurs, try executing the next // The last one also failed to execute, throwing an exception // no available server, throw exception. if (index >= serverList.size() - 1) { throw e; } LOG.warn("fetch config fail, try another one: {}", serverList.get(index + 1)); } } } // ......}
HttpSyncDataService#doFetchGroupConfig()
In this method, the request parameters are first assembled, then the request is launched through httpClient to admin to get the data, and finally the obtained data is updated to the gateway memory.
public class HttpSyncDataService implements SyncDataService { // ...... // Launch a request to the admin backend management system to get all synchronized data private void doFetchGroupConfig(final String server, final ConfigGroupEnum... groups) { // 1. build request parameters, all grouped enumeration types StringBuilder params = new StringBuilder(); for (ConfigGroupEnum groupKey : groups) { params.append("groupKeys").append("=").append(groupKey.name()).append("&"); } // admin url: /configs/fetch String url = server + Constants.SHENYU_ADMIN_PATH_CONFIGS_FETCH + "?" + StringUtils.removeEnd(params.toString(), "&"); LOG.info("request configs: [{}]", url); String json; try { HttpHeaders headers = new HttpHeaders(); // set accessToken headers.set(Constants.X_ACCESS_TOKEN, this.accessTokenManager.getAccessToken()); HttpEntity<String> httpEntity = new HttpEntity<>(headers); // 2. get a request for change data json = this.restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class).getBody(); } catch (RestClientException e) { String message = String.format("fetch config fail from server[%s], %s", url, e.getMessage()); LOG.warn(message); throw new ShenyuException(message, e); } // update local cache // 3. Update data in gateway memory boolean updated = this.updateCacheWithJson(json); if (updated) { LOG.debug("get latest configs: [{}]", json); return; } // not updated. it is likely that the current config server has not been updated yet. wait a moment. LOG.info("The config of the server[{}] has not been updated or is out of date. Wait for 30s to listen for changes again.", server); // No data update on the server side, just wait 30s ThreadUtils.sleep(TimeUnit.SECONDS, 30); } // ......}
From the code, we can see that the admin side provides the interface to get the full amount of data is /configs/fetch, so we will not go further here and put it in the later analysis.
If you get the result data from admin and update it successfully, then this method is finished. If there is no successful update, then it is possible that there is no data update on the server side, so wait 30s.
Here you need to explain in advance, the gateway in determining whether the update is successful, there is a comparison of the data operation, immediately mentioned.
HttpSyncDataService#updateCacheWithJson()
Update the data in the gateway memory. Use GSON for deserialization, take the real data from the property data and give it to DataRefreshFactory to do the update.
public class HttpSyncDataService implements SyncDataService { // ...... private boolean updateCacheWithJson(final String json) { // Using GSON for deserialization JsonObject jsonObject = GSON.fromJson(json, JsonObject.class); // if the config cache will be updated? return factory.executor(jsonObject.getAsJsonObject("data")); } // ......}
DataRefreshFactory#executor()
Update the data according to different data types and return the updated result. The specific update logic is given to the dataRefresh.refresh() method. In the update result, one of the data types is updated, which means that the operation has been updated.
public final class DataRefreshFactory { // ...... public boolean executor(final JsonObject data) { // update data List<Boolean> result = ENUM_MAP.values().parallelStream() .map(dataRefresh -> dataRefresh.refresh(data)) .collect(Collectors.toList()); // one of the data types is updated, which means that the operation has been updated. return result.stream().anyMatch(Boolean.TRUE::equals); } // ......}
AbstractDataRefresh#refresh()
The data update logic uses the template method design pattern, where the generic operation is done in the abstract method and the different implementation logic is done by subclasses. 5 data types have some differences in the specific update logic, but there is also a common update logic, and the class diagram relationship is as follows.
In the generic refresh() method, it is responsible for data type conversion, determining whether an update is needed, and the actual data refresh operation.
public abstract class AbstractDataRefresh<T> implements DataRefresh { // ...... @Override public Boolean refresh(final JsonObject data) { // convert data JsonObject jsonObject = convert(data); if (Objects.isNull(jsonObject)) { return false; } boolean updated = false; // get data ConfigData<T> result = fromJson(jsonObject); // does it need to be updated if (this.updateCacheIfNeed(result)) { updated = true; // real update logic, data refresh operation refresh(result.getData()); } return updated; } // ......}
AbstractDataRefresh#updateCacheIfNeed()
The process of data conversion, which is based on different data types, we will not trace further to see if the data needs to be updated logically. The method name is updateCacheIfNeed(), which is implemented by method overloading.
public abstract class AbstractDataRefresh<T> implements DataRefresh { // ...... // result is data protected abstract boolean updateCacheIfNeed(ConfigData<T> result); // newVal is the latest value obtained // What kind of data type is groupEnum protected boolean updateCacheIfNeed(final ConfigData<T> newVal, final ConfigGroupEnum groupEnum) { // If it is the first time, then it is put directly into the cache and returns true, indicating that the update was made this time if (GROUP_CACHE.putIfAbsent(groupEnum, newVal) == null) { return true; } ResultHolder holder = new ResultHolder(false); GROUP_CACHE.merge(groupEnum, newVal, (oldVal, value) -> { // md5 value is the same, no need to update if (StringUtils.equals(oldVal.getMd5(), newVal.getMd5())) { LOG.info("Get the same config, the [{}] config cache will not be updated, md5:{}", groupEnum, oldVal.getMd5()); return oldVal; } // The current cached data has been modified for a longer period than the new data and does not need to be updated. // must compare the last update time if (oldVal.getLastModifyTime() >= newVal.getLastModifyTime()) { LOG.info("Last update time earlier than the current configuration, the [{}] config cache will not be updated", groupEnum); return oldVal; } LOG.info("update {} config: {}", groupEnum, newVal); holder.result = true; return newVal; }); return holder.result; } // ......}
As you can see from the source code above, there are two cases where updates are not required.
The md5 values of both data are the same, so no update is needed;
The current cached data has been modified longer than the new data, so no update is needed.
In other cases, the data needs to be updated.
At this point, we have finished analyzing the logic of the start() method to get the full amount of data for the first time, followed by the long polling operation. For convenience, I will paste the start() method once more.
public class HttpSyncDataService implements SyncDataService { // ...... private void start() { // It could be initialized multiple times, so you need to control that. if (RUNNING.compareAndSet(false, true)) { // fetch all group configs. // Initial startup, get full data this.fetchGroupConfig(ConfigGroupEnum.values()); // one backend service, one thread int threadSize = serverList.size(); // ThreadPoolExecutor this.executor = new ThreadPoolExecutor(threadSize, threadSize, 60L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), ShenyuThreadFactory.create("http-long-polling", true)); // start long polling, each server creates a thread to listen for changes. this.serverList.forEach(server -> this.executor.execute(new HttpLongPollingTask(server))); } else { LOG.info("shenyu http long polling was started, executor=[{}]", executor); } } // ......}
The long polling task is HttpLongPollingTask, which implements the Runnable interface and the task logic is in the run() method. The task is executed continuously through a while() loop, i.e., long polling. There are three retries in each polling logic, one polling task fails, wait 5s and continue, 3 times all fail, wait 5 minutes and try again.
Start long polling, an admin service, and create a thread for data synchronization.
class HttpLongPollingTask implements Runnable { private final String server; HttpLongPollingTask(final String server) { this.server = server; } @Override public void run() { // long polling while (RUNNING.get()) { // Default retry 3 times int retryTimes = 3; for (int time = 1; time <= retryTimes; time++) { try { doLongPolling(server); } catch (Exception e) { if (time < retryTimes) { LOG.warn("Long polling failed, tried {} times, {} times left, will be suspended for a while! {}", time, retryTimes - time, e.getMessage()); // long polling failed, wait 5s and continue ThreadUtils.sleep(TimeUnit.SECONDS, 5); continue; } // print error, then suspended for a while. LOG.error("Long polling failed, try again after 5 minutes!", e); // 3 次都失败了,等 5 分钟再试 ThreadUtils.sleep(TimeUnit.MINUTES, 5); } } } LOG.warn("Stop http long polling."); }}
HttpSyncDataService#doLongPolling()
Core logic for performing long polling tasks.
Assembling request parameters based on data types: md5 and lastModifyTime.
Assembling the request header and request body.
Launching a request to admin to determine if the group data has changed.
Based on the group that has changed, go back and get the data.
public class HttpSyncDataService implements SyncDataService { private void doLongPolling(final String server) { // build request params: md5 and lastModifyTime MultiValueMap<String, String> params = new LinkedMultiValueMap<>(8); for (ConfigGroupEnum group : ConfigGroupEnum.values()) { ConfigData<?> cacheConfig = factory.cacheConfigData(group); if (cacheConfig != null) { String value = String.join(",", cacheConfig.getMd5(), String.valueOf(cacheConfig.getLastModifyTime())); params.put(group.name(), Lists.newArrayList(value)); } } // build request head and body HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); // set accessToken headers.set(Constants.X_ACCESS_TOKEN, this.accessTokenManager.getAccessToken()); HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(params, headers); String listenerUrl = server + Constants.SHENYU_ADMIN_PATH_CONFIGS_LISTENER; JsonArray groupJson; //Initiate a request to admin to determine if the group data has changed //Here it just determines whether a group has changed or not try { String json = this.restTemplate.postForEntity(listenerUrl, httpEntity, String.class).getBody(); LOG.info("listener result: [{}]", json); JsonObject responseFromServer = GsonUtils.getGson().fromJson(json, JsonObject.class); groupJson = responseFromServer.getAsJsonArray("data"); } catch (RestClientException e) { String message = String.format("listener configs fail, server:[%s], %s", server, e.getMessage()); throw new ShenyuException(message, e); } // Depending on the group where the change occurred, go back and get the data /** * The official website explains here. * After the gateway receives the response message, it only knows which Group has made the configuration change, and it still needs to request the configuration data of that Group again. * There may be a question here: why not write out the changed data directly? * We also discussed this issue in depth during development, because the http long polling mechanism can only guarantee quasi-real time, if the processing at the gateway layer is not timely, * or the administrator frequently updates the configuration, it is very difficult to get the information from the gateway layer. * If it is not processed in time at the gateway level, or if the administrator updates the configuration frequently, it is very likely to miss the push of a configuration change, so for security reasons, we only inform a group that the information has changed. *For security reasons, we only notify a group of changes. * Personal understanding. * If the change data is written out directly, when the administrator frequently updates the configuration, the first update will remove the client from the blocking queue and return the response information to the gateway. * If a second update is made at this time, the current client is not in the blocking queue, so this time the change is missed. * The same is true for untimely processing by the gateway layer. * This is a long polling, one gateway one synchronization thread, there may be time consuming process. * If the admin has data changes, the current gateway client is not in the blocking queue and will not get the data. */ if (groupJson != null) { // fetch group configuration async. ConfigGroupEnum[] changedGroups = GSON.fromJson(groupJson, ConfigGroupEnum[].class); if (ArrayUtils.isNotEmpty(changedGroups)) { log.info("Group config changed: {}", Arrays.toString(changedGroups)); // Proactively get the changed data from admin, depending on the grouping, and take the data in full this.doFetchGroupConfig(server, changedGroups); } } if (Objects.nonNull(groupJson) && groupJson.size() > 0) { // fetch group configuration async. ConfigGroupEnum[] changedGroups = GsonUtils.getGson().fromJson(groupJson, ConfigGroupEnum[].class); LOG.info("Group config changed: {}", Arrays.toString(changedGroups)); // Proactively get the changed data from admin, depending on the grouping, and take the data in full this.doFetchGroupConfig(server, changedGroups); } }}
One special point needs to be explained here: In the long polling task, why don't you get the changed data directly? Instead, we determine which group data has been changed, and then request admin again to get the changed data?
The official explanation here is.
After the gateway receives the response information, it only knows which Group has changed its configuration, and it needs to request the configuration data of that Group again.
There may be a question here: Why not write out the changed data directly?
We have discussed this issue in depth during development, because the http long polling mechanism can only guarantee quasi-real time, and if it is not processed in time at the gateway layer, it will be very difficult to update the configuration data.
If the gateway layer is not processed in time, or the administrator updates the configuration frequently, it is likely to miss the push of a configuration change, so for security reasons, we only inform a group that the information has changed.
My personal understanding is that.
If the change data is written out directly, when the administrator updates the configuration frequently, the first update will remove the client from blocking queue and return the response information to the gateway. If a second update is made at this time, then the current client is not in the blocking queue, so this time the change is missed. The same is true for the gateway layer's untimely processing. This is a long polling, one gateway one synchronization thread, there may be a time-consuming process. If admin has data changes, the current gateway client is not in the blocking queue and will not get the data.
We have not yet analyzed the processing logic of the admin side, so let's talk about it roughly. At the admin end, the gateway client will be put into the blocking queue, and when there is a data change, the gateway client will come out of the queue and send the change data. So, if the gateway client is not in the blocking queue when there is a data change, then the current changed data is not available.
When we know which grouping data has changed, we actively get the changed data from admin again, and get the data in full depending on the grouping. The call method is doFetchGroupConfig(), which has been analyzed in the previous section.
At this point of analysis, the data synchronization operation on the gateway side is complete. The long polling task is to keep making requests to admin to see if the data has changed, and if any group data has changed, then initiate another request to admin to get the changed data, and then update the data in the gateway's memory.
From the previous analysis, it can be seen that the gateway side mainly calls two interfaces of admin.
/configs/listener: determine whether the group data has changed.
/configs/fetch: get the changed group data.
If we analyze directly from these two interfaces, some parts may not be well understood, so let's start analyzing the data synchronization process from the admin startup process.
If the following configuration is done in the configuration file application.yml, it means that the data synchronization is done by http long polling.
shenyu:sync:http:enabled:true
When the program starts, the configuration of the data synchronization class is loaded through springboot conditional assembly. In this process, HttpLongPollingDataChangedListener is created to handle the implementation logic related to long polling.
/** * Data synchronization configuration class * Conditional assembly via springboot * The type Data sync configuration. */@Configurationpublic class DataSyncConfiguration { /** * http long polling. */ @Configuration @ConditionalOnProperty(name = "shenyu.sync.http.enabled", havingValue = "true") @EnableConfigurationProperties(HttpSyncProperties.class) static class HttpLongPollingListener { @Bean @ConditionalOnMissingBean(HttpLongPollingDataChangedListener.class) public HttpLongPollingDataChangedListener httpLongPollingDataChangedListener(final HttpSyncProperties httpSyncProperties) { return new HttpLongPollingDataChangedListener(httpSyncProperties); } }}
The data change listener is instantiated and initialized by means of a constructor. In the constructor, a blocking queue is created to hold clients, a thread pool is created to execute deferred tasks and periodic tasks, and information about the properties of long polling is stored.
public HttpLongPollingDataChangedListener(final HttpSyncProperties httpSyncProperties) { // default client (here is the gateway) 1024 this.clients = new ArrayBlockingQueue<>(1024); // create thread pool // ScheduledThreadPoolExecutor can perform delayed tasks, periodic tasks, and normal tasks this.scheduler = new ScheduledThreadPoolExecutor(1, ShenyuThreadFactory.create("long-polling", true)); // http sync properties this.httpSyncProperties = httpSyncProperties; }
In addition, it has the following class diagram relationships.
The InitializingBean interface is implemented, so the afterInitialize() method is executed during the initialization of the bean. Execute periodic tasks via thread pool: updating the data in memory (CACHE) is executed every 5 minutes and starts after 5 minutes. Refreshing the local cache is reading data from the database to the local cache (in this case the memory), done by refreshLocalCache().
public class HttpLongPollingDataChangedListener extends AbstractDataChangedListener { // ...... /** * is called in the afterPropertiesSet() method of the InitializingBean interface, which is executed during the initialization of the bean */ @Override protected void afterInitialize() { long syncInterval = httpSyncProperties.getRefreshInterval().toMillis(); // Periodically check the data for changes and update the cache // Execution cycle task: Update data in memory (CACHE) is executed every 5 minutes and starts after 5 minutes // Prevent the admin from starting up first for a while and then generating data; then the gateway doesn't get the full amount of data when it first connects scheduler.scheduleWithFixedDelay(() -> { LOG.info("http sync strategy refresh config start."); try { // Read data from database to local cache (in this case, memory) this.refreshLocalCache(); LOG.info("http sync strategy refresh config success."); } catch (Exception e) { LOG.error("http sync strategy refresh config error!", e); } }, syncInterval, syncInterval, TimeUnit.MILLISECONDS); LOG.info("http sync strategy refresh interval: {}ms", syncInterval); } // ......}
refreshLocalCache()
Update for each of the 5 data types.
public abstract class AbstractDataChangedListener implements DataChangedListener, InitializingBean { // ...... // Read data from database to local cache (in this case, memory) private void refreshLocalCache() { //update app auth data this.updateAppAuthCache(); //update plugin data this.updatePluginCache(); //update rule data this.updateRuleCache(); //update selector data this.updateSelectorCache(); //update meta data this.updateMetaDataCache(); } // ......}
The logic of the 5 update methods is similar, call the service method to get the data and put it into the memory CACHE. Take the updateRuleData method updateRuleCache() for example, pass in the rule enumeration type and call ruleService.listAll() to get all the rule data from the database.
Update the data in memory using the data in the database.
public abstract class AbstractDataChangedListener implements DataChangedListener, InitializingBean { // ...... // cache Map protected static final ConcurrentMap<String, ConfigDataCache> CACHE = new ConcurrentHashMap<>(); /** * if md5 is not the same as the original, then update lcoal cache. * @param group ConfigGroupEnum * @param <T> the type of class * @param data the new config data */ protected <T> void updateCache(final ConfigGroupEnum group, final List<T> data) { // data serialization String json = GsonUtils.getInstance().toJson(data); // pass in md5 value and modification time ConfigDataCache newVal = new ConfigDataCache(group.name(), json, Md5Utils.md5(json), System.currentTimeMillis()); // update group data ConfigDataCache oldVal = CACHE.put(newVal.getGroup(), newVal); log.info("update config cache[{}], old: {}, updated: {}", group, oldVal, newVal); } // ......}
The initialization process is to start periodic tasks to update the memory data by fetching data from the database at regular intervals.
Next, we start the analysis of two interfaces.
/configs/listener: determines if the group data has changed.
/configs/listener: determines if the group data has changed.
The interface class is ConfigController, which only takes effect when using http long polling for data synchronization. The interface method listener() has no other logic and calls the doLongPolling() method directly.
/** * This Controller only when HttpLongPollingDataChangedListener exist, will take effect. */@ConditionalOnBean(HttpLongPollingDataChangedListener.class)@RestController@RequestMapping("/configs")public class ConfigController { private final HttpLongPollingDataChangedListener longPollingListener; public ConfigController(final HttpLongPollingDataChangedListener longPollingListener) { this.longPollingListener = longPollingListener; } // Omit other logic /** * Listener. * Listen for data changes and perform long polling * @param request the request * @param response the response */ @PostMapping(value = "/listener") public void listener(final HttpServletRequest request, final HttpServletResponse response) { longPollingListener.doLongPolling(request, response); }}
Perform long polling tasks: If there are data changes, they will be responded to the client (in this case, the gateway side) immediately. Otherwise, the client will be blocked until there is a data change or a timeout.
public class HttpLongPollingDataChangedListener extends AbstractDataChangedListener { // ...... /** * Execute long polling: If there is a data change, it will be responded to the client (here is the gateway side) immediately. * Otherwise, the client will otherwise remain blocked until there is a data change or a timeout. * @param request * @param response */ public void doLongPolling(final HttpServletRequest request, final HttpServletResponse response) { // compare group md5 // Compare the md5, determine whether the data of the gateway and the data of the admin side are consistent, and get the data group that has changed List<ConfigGroupEnum> changedGroup = compareChangedGroup(request); String clientIp = getRemoteIp(request); // response immediately. // Immediate response to the gateway if there is changed data if (CollectionUtils.isNotEmpty(changedGroup)) { this.generateResponse(response, changedGroup); Log.info("send response with the changed group, ip={}, group={}", clientIp, changedGroup); return; } // No change, then the client (in this case the gateway) is put into the blocking queue // listen for configuration changed. final AsyncContext asyncContext = request.startAsync(); // AsyncContext.settimeout() does not timeout properly, so you have to control it yourself asyncContext.setTimeout(0L); // block client's thread. scheduler.execute(new LongPollingClient(asyncContext, clientIp, HttpConstants.SERVER_MAX_HOLD_TIMEOUT)); }}
To determine whether the group data has changed, the judgment logic is to compare the md5 value and lastModifyTime at the gateway side and the admin side.
If the md5 value is different, then it needs to be updated.
If the lastModifyTime on the admin side is greater than the lastModifyTime on the gateway side, then it needs to be updated.
/** * Determine if the group data has changed * @param request * @return */ private List<ConfigGroupEnum> compareChangedGroup(final HttpServletRequest request) { List<ConfigGroupEnum> changedGroup = new ArrayList<>(ConfigGroupEnum.values().length); for (ConfigGroupEnum group : ConfigGroupEnum.values()) { // The md5 value and lastModifyTime of the data on the gateway side String[] params = StringUtils.split(request.getParameter(group.name()), ','); if (params == null || params.length != 2) { throw new ShenyuException("group param invalid:" + request.getParameter(group.name())); } String clientMd5 = params[0]; long clientModifyTime = NumberUtils.toLong(params[1]); ConfigDataCache serverCache = CACHE.get(group.name()); // do check. determine if the group data has changed if (this.checkCacheDelayAndUpdate(serverCache, clientMd5, clientModifyTime)) { changedGroup.add(group); } } return changedGroup; }
LongPollingClient
No change data, then the client (in this case the gateway) is put into the blocking queue. The blocking time is 60 seconds, i.e. after 60 seconds remove and respond to the client.
class LongPollingClient implements Runnable { // omitted other logic @Override public void run() { try { // Removal after 60 seconds and response to the client this.asyncTimeoutFuture = scheduler.schedule(() -> { clients.remove(LongPollingClient.this); List<ConfigGroupEnum> changedGroups = compareChangedGroup((HttpServletRequest) asyncContext.getRequest()); sendResponse(changedGroups); }, timeoutTime, TimeUnit.MILLISECONDS); // Add to blocking queue clients.add(this); } catch (Exception ex) { log.error("add long polling client error", ex); } } /** * Send response. * * @param changedGroups the changed groups */ void sendResponse(final List<ConfigGroupEnum> changedGroups) { // cancel scheduler if (null != asyncTimeoutFuture) { asyncTimeoutFuture.cancel(false); } // Groups responding to changes generateResponse((HttpServletResponse) asyncContext.getResponse(), changedGroups); asyncContext.complete(); } }
Get the grouped data and return the result according to the parameters passed in by the gateway. The main implementation method is longPollingListener.fetchConfig().
@ConditionalOnBean(HttpLongPollingDataChangedListener.class)@RestController@RequestMapping("/configs")public class ConfigController { private final HttpLongPollingDataChangedListener longPollingListener; public ConfigController(final HttpLongPollingDataChangedListener longPollingListener) { this.longPollingListener = longPollingListener; } /** * Fetch configs shenyu result. * @param groupKeys the group keys * @return the shenyu result */ @GetMapping("/fetch") public ShenyuAdminResult fetchConfigs(@NotNull final String[] groupKeys) { Map<String, ConfigData<?>> result = Maps.newHashMap(); for (String groupKey : groupKeys) { ConfigData<?> data = longPollingListener.fetchConfig(ConfigGroupEnum.valueOf(groupKey)); result.put(groupKey, data); } return ShenyuAdminResult.success(ShenyuResultMessage.SUCCESS, result); } // Other interfaces are omitted}
AbstractDataChangedListener#fetchConfig()
Data fetching is taken directly from CACHE, and then matched and encapsulated according to different grouping types.
public abstract class AbstractDataChangedListener implements DataChangedListener, InitializingBean { // ...... /** * fetch configuration from cache. * @param groupKey the group key * @return the configuration data */ public ConfigData<?> fetchConfig(final ConfigGroupEnum groupKey) { // get data from CACHE ConfigDataCache config = CACHE.get(groupKey.name()); switch (groupKey) { case APP_AUTH: // app auth data return buildConfigData(config, AppAuthData.class); case PLUGIN: // plugin data return buildConfigData(config, PluginData.class); case RULE: // rule data return buildConfigData(config, RuleData.class); case SELECTOR: // selector data return buildConfigData(config, SelectorData.class); case META_DATA: // meta data return buildConfigData(config, MetaData.class); default: // other data type, throw exception throw new IllegalStateException("Unexpected groupKey: " + groupKey); } } // ......}
In the previous websocket data synchronization and zookeeper data synchronization source code analysis article, we know that the admin side data synchronization design structure is as follows.
Various data change listeners are subclasses of DataChangedListener.
When the data is modified on the admin side, event notifications are sent through the Spring event handling mechanism. The sending logic is as follows.
/** * Event forwarders, which forward the changed events to each ConfigEventListener. * Data change event distributor: synchronize the change data to ShenYu gateway when there is a data change in admin side * Data changes rely on Spring's event-listening mechanism: ApplicationEventPublisher --> ApplicationEvent --> ApplicationListener * */@Componentpublic class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean { // other logic omitted /** * Call this method when there are data changes * @param event */ @Override @SuppressWarnings("unchecked") public void onApplicationEvent(final DataChangedEvent event) { // Iterate through the data change listeners (it's generally good to use a kind of data synchronization) for (DataChangedListener listener : listeners) { // What kind of data has changed switch (event.getGroupKey()) { case APP_AUTH: // app auth data listener.onAppAuthChanged((List<AppAuthData>) event.getSource(), event.getEventType()); break; case PLUGIN: // plugin data listener.onPluginChanged((List<PluginData>) event.getSource(), event.getEventType()); break; case RULE: // rule data listener.onRuleChanged((List<RuleData>) event.getSource(), event.getEventType()); break; case SELECTOR: // selector data listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); // pull and save API document on seletor changed applicationContext.getBean(LoadServiceDocEntry.class).loadDocOnSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); break; case META_DATA: // meta data listener.onMetaDataChanged((List<MetaData>) event.getSource(), event.getEventType()); break; default: // other data type, throw exception throw new IllegalStateException("Unexpected value: " + event.getGroupKey()); } } }}
Suppose, the plugin information is modified and the data is synchronized by http long polling, then the actual call to listener.onPluginChanged() is org.apache.shenyu.admin.listener. AbstractDataChangedListener#onPluginChanged.
/** * In the operation of the admin, there is an update of the plugin occurred * @param changed the changed * @param eventType the event type */ @Override public void onPluginChanged(final List<PluginData> changed, final DataEventTypeEnum eventType) { if (CollectionUtils.isEmpty(changed)) { return; } // update CACHE this.updatePluginCache(); // execute change task this.afterPluginChanged(changed, eventType); }
There are two processing operations, one is to update the memory CACHE, which was analyzed earlier, and the other is to execute the change task, which is executed in the thread pool.
@Override protected void afterPluginChanged(final List<PluginData> changed, final DataEventTypeEnum eventType) { // execute by thread pool scheduler.execute(new DataChangeTask(ConfigGroupEnum.PLUGIN)); }
DataChangeTask
Data change task: remove the clients in the blocking queue in turn and send a response to notify the gateway that a group of data has changed.
class DataChangeTask implements Runnable { //other logic omitted @Override public void run() { // If the client in the blocking queue exceeds the given value of 100, it is executed in batches if (clients.size() > httpSyncProperties.getNotifyBatchSize()) { List<LongPollingClient> targetClients = new ArrayList<>(clients.size()); clients.drainTo(targetClients); List<List<LongPollingClient>> partitionClients = Lists.partition(targetClients, httpSyncProperties.getNotifyBatchSize()); // batch execution partitionClients.forEach(item -> scheduler.execute(() -> doRun(item))); } else { // execute task doRun(clients); } } private void doRun(final Collection<LongPollingClient> clients) { // Notify all clients that a data change has occurred for (Iterator<LongPollingClient> iter = clients.iterator(); iter.hasNext();) { LongPollingClient client = iter.next(); iter.remove(); // send response to client client.sendResponse(Collections.singletonList(groupKey)); Log.info("send response with the changed group,ip={}, group={}, changeTime={}", client.ip, groupKey, changeTime); } } }
At this point, the data synchronization logic on the admin side is analyzed. In the http long polling based data synchronization is, it has three main functions.
providing a data change listening interface.
providing the interface to get the changed data.
When there is a data change, remove the client in the blocking queue and respond to the result.
Finally, three diagrams describe the long polling task flow on the admin side.
/configs/listener data change listener interface.
/configs/fetch fetch change data interface.
Update data in the admin backend management system for data synchronization.
This article focuses on the source code analysis of http long polling data synchronization in the ShenYu gateway. The main knowledge points involved are as follows.
http long polling is initiated by the gateway side, which constantly requests the admin side.
change data at group granularity (authentication information, plugins, selectors, rules, metadata).
http long polling results in getting only the change group, and another request needs to be initiated to get the group data.
Whether the data is updated or not is determined by the md5 value and the modification time lastModifyTime.
Apache ShenYu is an asynchronous, high-performance, cross-language, responsive API gateway.
In ShenYu gateway, data synchronization refers to how to synchronize the updated data to the gateway after the data is sent in the background management system. The Apache ShenYu gateway currently supports data synchronization for ZooKeeper, WebSocket, http long poll, Nacos, etcd and Consul. The main content of this article is based on Nacos data synchronization source code analysis.
This paper based on shenyu-2.4.0 version of the source code analysis, the official website of the introduction of please refer to the Data Synchronization Design .
We traced the source code from a real case, such as updating a selector data in the Divide plugin to a weight of 90 in a background administration system:
Enter the createSelector() method of the SelectorController class, which validates data, adds or updates data, and returns results.
@Validated@RequiredArgsConstructor@RestController@RequestMapping("/selector")public class SelectorController { @PutMapping("/{id}") public ShenyuAdminResult updateSelector(@PathVariable("id") final String id, @Valid @RequestBody final SelectorDTO selectorDTO) { // set the current selector data ID selectorDTO.setId(id); // create or update operation Integer updateCount = selectorService.createOrUpdate(selectorDTO); // return result return ShenyuAdminResult.success(ShenyuResultMessage.UPDATE_SUCCESS, updateCount); } // ......}
Convert data in the SelectorServiceImpl class using the createOrUpdate() method, save it to the database, publish the event, update upstream.
@RequiredArgsConstructor@Servicepublic class SelectorServiceImpl implements SelectorService { // eventPublisher private final ApplicationEventPublisher eventPublisher; @Override @Transactional(rollbackFor = Exception.class) public int createOrUpdate(final SelectorDTO selectorDTO) { int selectorCount; // build data DTO --> DO SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO); List<SelectorConditionDTO> selectorConditionDTOs = selectorDTO.getSelectorConditions(); // insert or update ? if (StringUtils.isEmpty(selectorDTO.getId())) { // insert into data selectorCount = selectorMapper.insertSelective(selectorDO); // insert into condition data selectorConditionDTOs.forEach(selectorConditionDTO -> { selectorConditionDTO.setSelectorId(selectorDO.getId()); selectorConditionMapper.insertSelective(SelectorConditionDO.buildSelectorConditionDO(selectorConditionDTO)); }); // check selector add if (dataPermissionMapper.listByUserId(JwtUtils.getUserInfo().getUserId()).size() > 0) { DataPermissionDTO dataPermissionDTO = new DataPermissionDTO(); dataPermissionDTO.setUserId(JwtUtils.getUserInfo().getUserId()); dataPermissionDTO.setDataId(selectorDO.getId()); dataPermissionDTO.setDataType(AdminConstants.SELECTOR_DATA_TYPE); dataPermissionMapper.insertSelective(DataPermissionDO.buildPermissionDO(dataPermissionDTO)); } } else { // update data, delete and then insert selectorCount = selectorMapper.updateSelective(selectorDO); //delete rule condition then add selectorConditionMapper.deleteByQuery(new SelectorConditionQuery(selectorDO.getId())); selectorConditionDTOs.forEach(selectorConditionDTO -> { selectorConditionDTO.setSelectorId(selectorDO.getId()); SelectorConditionDO selectorConditionDO = SelectorConditionDO.buildSelectorConditionDO(selectorConditionDTO); selectorConditionMapper.insertSelective(selectorConditionDO); }); } // publish event publishEvent(selectorDO, selectorConditionDTOs); // update upstream updateDivideUpstream(selectorDO); return selectorCount; } // ......}
In the Service class to persist data, i.e. to the database, this should be familiar, not expand. The update upstream operation is analyzed in the corresponding section below, focusing on the publish event operation, which performs data synchronization.
The logic of the publishEvent() method is to find the plugin corresponding to the selector, build the conditional data, and publish the change data.
Change data released by eventPublisher.PublishEvent() is complete, the eventPublisher object is a ApplicationEventPublisher class, The fully qualified class name is org.springframework.context.ApplicationEventPublisher. Here we see that publishing data is done through Spring related functionality.
ApplicationEventPublisher:
When a state change, the publisher calls ApplicationEventPublisher of publishEvent method to release an event, Spring container broadcast event for all observers, The observer's onApplicationEvent method is called to pass the event object to the observer. There are two ways to call publishEvent method, one is to implement the interface by the container injection ApplicationEventPublisher object and then call the method, the other is a direct call container, the method of two methods of publishing events not too big difference.
ApplicationEventPublisher: publish event;
ApplicationEvent: Spring event, record the event source, time, and data;
ApplicationListener: event listener, observer.
In Spring event publishing mechanism, there are three objects,
An object is a publish event ApplicationEventPublisher, in ShenYu through the constructor in the injected a eventPublisher.
The other object is ApplicationEvent , inherited from ShenYu through DataChangedEvent, representing the event object.
public class DataChangedEvent extends ApplicationEvent {//......}
The last object is ApplicationListener in ShenYu in through DataChangedEventDispatcher class implements this interface, as the event listener, responsible for handling the event object.
@Componentpublic class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean { //......}
Released when the event is completed, will automatically enter the DataChangedEventDispatcher class onApplicationEvent() method of handling events.
@Componentpublic class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean { /** * This method is called when there are data changes * @param event */ @Override @SuppressWarnings("unchecked") public void onApplicationEvent(final DataChangedEvent event) { // Iterate through the data change listener (usually using a data synchronization approach is fine) for (DataChangedListener listener : listeners) { // What kind of data has changed switch (event.getGroupKey()) { case APP_AUTH: // app auth data listener.onAppAuthChanged((List<AppAuthData>) event.getSource(), event.getEventType()); break; case PLUGIN: // plugin data listener.onPluginChanged((List<PluginData>) event.getSource(), event.getEventType()); break; case RULE: // rule data listener.onRuleChanged((List<RuleData>) event.getSource(), event.getEventType()); break; case SELECTOR: // selector data listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); break; case META_DATA: // metadata listener.onMetaDataChanged((List<MetaData>) event.getSource(), event.getEventType()); break; default: // other types throw exception throw new IllegalStateException("Unexpected value: " + event.getGroupKey()); } } }}
When there is a data change, the onApplicationEvent method is called and all the data change listeners are iterated to determine the data type and handed over to the appropriate data listener for processing.
ShenYu groups all the data into five categories: APP_AUTH, PLUGIN, RULE, SELECTOR and META_DATA.
Here the data change listener (DataChangedListener) is an abstraction of the data synchronization policy. Its concrete implementation is:
These implementation classes are the synchronization strategies currently supported by ShenYu:
WebsocketDataChangedListener: data synchronization based on Websocket;
ZookeeperDataChangedListener:data synchronization based on Zookeeper;
ConsulDataChangedListener: data synchronization based on Consul;
EtcdDataDataChangedListener:data synchronization based on etcd;
HttpLongPollingDataChangedListener:data synchronization based on http long polling;
NacosDataChangedListener:data synchronization based on nacos;
Given that there are so many implementation strategies, how do you decide which to use?
Because this paper is based on nacos data synchronization source code analysis, so here to NacosDataChangedListener as an example, the analysis of how it is loaded and implemented.
A global search in the source code project shows that its implementation is done in the DataSyncConfiguration class.
/** * The type Data sync configuration. */@Configurationpublic class DataSyncConfiguration { // some codes omitted here /** * The type Nacos listener. */ @Configuration @ConditionalOnProperty(prefix = "shenyu.sync.nacos", name = "url") @Import(NacosConfiguration.class) static class NacosListener { /** * Data changed listener data changed listener. * * @param configService the config service * @return the data changed listener */ @Bean @ConditionalOnMissingBean(NacosDataChangedListener.class) public DataChangedListener nacosDataChangedListener(final ConfigService configService) { return new NacosDataChangedListener(configService); } /** * Nacos data init zookeeper data init. * * @param configService the config service * @param syncDataService the sync data service * @return the nacos data init */ @Bean @ConditionalOnMissingBean(NacosDataInit.class) public NacosDataInit nacosDataInit(final ConfigService configService, final SyncDataService syncDataService) { return new NacosDataInit(configService, syncDataService); } } // some codes omitted here}
This configuration class is implemented through the SpringBoot conditional assembly class. The NacosListener class has several annotations:
@ConditionalOnProperty(prefix = "shenyu.sync.nacos", name = "url"): attribute condition. The configuration class takes effect only when the condition is met. That is, when we have the following configuration, nacos is used for data synchronization.
shenyu: sync: nacos: url: localhost:8848
@Import(NacosConfiguration.class):import a configration class NacosConfiguration, which provides a method ConfigService nacosConfigService(final NacosProperties nacosProp) to convert the nacos properties to a bean with the ConfigService type. We would take a look at how to generate the bean and then analyze the property configuration class and the property configuration file.
/** * Nacos configuration. */@EnableConfigurationProperties(NacosProperties.class)public class NacosConfiguration { /** * register configService in spring ioc. * * @param nacosProp the nacos configuration * @return ConfigService {@linkplain ConfigService} * @throws Exception the exception */ @Bean @ConditionalOnMissingBean(ConfigService.class) public ConfigService nacosConfigService(final NacosProperties nacosProp) throws Exception { Properties properties = new Properties(); if (nacosProp.getAcm() != null && nacosProp.getAcm().isEnabled()) { // Use aliyun ACM service properties.put(PropertyKeyConst.ENDPOINT, nacosProp.getAcm().getEndpoint()); properties.put(PropertyKeyConst.NAMESPACE, nacosProp.getAcm().getNamespace()); // Use subaccount ACM administrative authority properties.put(PropertyKeyConst.ACCESS_KEY, nacosProp.getAcm().getAccessKey()); properties.put(PropertyKeyConst.SECRET_KEY, nacosProp.getAcm().getSecretKey()); } else { properties.put(PropertyKeyConst.SERVER_ADDR, nacosProp.getUrl()); if (StringUtils.isNotBlank(nacosProp.getNamespace())) { properties.put(PropertyKeyConst.NAMESPACE, nacosProp.getNamespace()); } if (StringUtils.isNotBlank(nacosProp.getUsername())) { properties.put(PropertyKeyConst.USERNAME, nacosProp.getUsername()); } if (StringUtils.isNotBlank(nacosProp.getPassword())) { properties.put(PropertyKeyConst.PASSWORD, nacosProp.getPassword()); } } return NacosFactory.createConfigService(properties); }}
There are two steps in this method. Firstly, Properties object is generated and populated with the specified nacos path value and authority values on whether the alyun ACM service is used. Secondly, the nacos factory class would use its static factory method to create a configService object via reflect methods and then populate the object with the Properties object generated in the first step.
Now, let's analyze the NacosProperties class and its counterpart property file.
/** * The type Nacos config. */@ConfigurationProperties(prefix = "shenyu.sync.nacos")public class NacosProperties { private String url; private String namespace; private String username; private String password; private NacosACMProperties acm; /** * Gets the value of url. * * @return the value of url */ public String getUrl() { return url; } /** * Sets the url. * * @param url url */ public void setUrl(final String url) { this.url = url; } /** * Gets the value of namespace. * * @return the value of namespace */ public String getNamespace() { return namespace; } /** * Sets the namespace. * * @param namespace namespace */ public void setNamespace(final String namespace) { this.namespace = namespace; } /** * Gets the value of username. * * @return the value of username */ public String getUsername() { return username; } /** * Sets the username. * * @param username username */ public void setUsername(final String username) { this.username = username; } /** * Gets the value of password. * * @return the value of password */ public String getPassword() { return password; } /** * Sets the password. * * @param password password */ public void setPassword(final String password) { this.password = password; } /** * Gets the value of acm. * * @return the value of acm */ public NacosACMProperties getAcm() { return acm; } /** * Sets the acm. * * @param acm acm */ public void setAcm(final NacosACMProperties acm) { this.acm = acm; } public static class NacosACMProperties { private boolean enabled; private String endpoint; private String namespace; private String accessKey; private String secretKey; /** * Gets the value of enabled. * * @return the value of enabled */ public boolean isEnabled() { return enabled; } /** * Sets the enabled. * * @param enabled enabled */ public void setEnabled(final boolean enabled) { this.enabled = enabled; } /** * Gets the value of endpoint. * * @return the value of endpoint */ public String getEndpoint() { return endpoint; } /** * Sets the endpoint. * * @param endpoint endpoint */ public void setEndpoint(final String endpoint) { this.endpoint = endpoint; } /** * Gets the value of namespace. * * @return the value of namespace */ public String getNamespace() { return namespace; } /** * Sets the namespace. * * @param namespace namespace */ public void setNamespace(final String namespace) { this.namespace = namespace; } /** * Gets the value of accessKey. * * @return the value of accessKey */ public String getAccessKey() { return accessKey; } /** * Sets the accessKey. * * @param accessKey accessKey */ public void setAccessKey(final String accessKey) { this.accessKey = accessKey; } /** * Gets the value of secretKey. * * @return the value of secretKey */ public String getSecretKey() { return secretKey; } /** * Sets the secretKey. * * @param secretKey secretKey */ public void setSecretKey(final String secretKey) { this.secretKey = secretKey; } }}
When the property shenyu.sync.nacos.url is set in the property file, the shenyu admin would choose the nacos to sync data. At this time, the configuration class NacosListener would take effect and a bean with the type NacosDataChangedListener and another bean with the type NacosDataInit would both be generated.
nacosDataChangedListener, the bean with the type NacosDataChangedListener , takes the bean with the type ConfigService as a member variable. ConfigService is an api provided by nacos and can be used to send request to nacos server to modify configurations once the nacosDataChangedListener has accepted an event and trigger the callback method.
nacosDataInit, the bean with the type NacosDataInit, takes the bean configService and the bean syncDataService as memeber variables. It use configService to call the Nacos api to judge whether the configurations have been initialized, and would use syncDataService to refresh them if the answer is no.
As mentioned above, some operations of the listener would be triggered in the event handle method onApplicationEvent(). In this example, we update selector data and choose nacos to sync data, so the code about logic of the selector data changes in the NacosDataChangedListener class would be called.
//DataChangedEventDispatcher.java @Override @SuppressWarnings("unchecked") public void onApplicationEvent(final DataChangedEvent event) { // Iterate through the data change listener (usually using a data synchronization approach is fine) for (DataChangedListener listener : listeners) { // What kind of data has changed switch (event.getGroupKey()) { // some codes omitted case SELECTOR: // selector data listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); break; } } }
In the onSelectorChanged() method, determine the type of action, whether to refresh synchronization or update or create synchronization. Determine whether the node is in etcd based on the current selector data.
/** * Use nacos to push data changes. */public class NacosDataChangedListener implements DataChangedListener { @Override public void onSelectorChanged(final List<SelectorData> changed, final DataEventTypeEnum eventType) { updateSelectorMap(getConfig(NacosPathConstants.SELECTOR_DATA_ID)); switch (eventType) { case DELETE: changed.forEach(selector -> { List<SelectorData> ls = SELECTOR_MAP .getOrDefault(selector.getPluginName(), new ArrayList<>()) .stream() .filter(s -> !s.getId().equals(selector.getId())) .sorted(SELECTOR_DATA_COMPARATOR) .collect(Collectors.toList()); SELECTOR_MAP.put(selector.getPluginName(), ls); }); break; case REFRESH: case MYSELF: SELECTOR_MAP.keySet().removeAll(SELECTOR_MAP.keySet()); changed.forEach(selector -> { List<SelectorData> ls = SELECTOR_MAP .getOrDefault(selector.getPluginName(), new ArrayList<>()) .stream() .sorted(SELECTOR_DATA_COMPARATOR) .collect(Collectors.toList()); ls.add(selector); SELECTOR_MAP.put(selector.getPluginName(), ls); }); break; default: changed.forEach(selector -> { List<SelectorData> ls = SELECTOR_MAP .getOrDefault(selector.getPluginName(), new ArrayList<>()) .stream() .filter(s -> !s.getId().equals(selector.getId())) .sorted(SELECTOR_DATA_COMPARATOR) .collect(Collectors.toList()); ls.add(selector); SELECTOR_MAP.put(selector.getPluginName(), ls); }); break; } publishConfig(NacosPathConstants.SELECTOR_DATA_ID, SELECTOR_MAP); } }
This is the core part. The variable changed represents the list , which needs to be updated, with the elements of the SelectorData type. The variable eventType represents the event type. The variable SELECTOR_MAP is with the type ConcurrentMap<String, List<SelectorData>>, so the key of the map is with the String type and the value is the selector list of this plugin. The value of the constant NacosPathConstants.SELECTOR_DATA_ID is shenyu.selector.json. The steps are as follows, firstly, use the method getConfig to call the api of Nacos to fetch the config with the group value of shenyu.selector.json from Nacos and call the updateSelectorMap method to use the config fetched above to update the SELECTOR_MAP so that the we refresh the selector config from Nacos. Secondly, we can update SELECTOR_MAP according to the event type and then use the publishConfig method to call the Nacos api to update all the config with the group value of shenyu.selector.json.
As long as the changed data is correctly written to the Nacos node, the admin side of the operation is complete.
In our current case, updating one of the selector data in the Divide plugin with a weight of 90 updates specific nodes in the graph.
We series the above update flow with a sequence diagram.
Assume that the ShenYu gateway is already running properly, and the data synchronization mode is also nacos. How does the gateway receive and process the selector data after updating it on the admin side and sending the changed data to nacos? Let's continue our source code analysis to find out.
The gateway side use NacosSyncDataService to watch nacos and fetch the data update, but before we dive into this part, let us take a look on how the bean with the type NacosSyncDataService is generated. The answer is it's defined in the Spring config class NacosSyncDataConfiguration. Let's focus on the annotation @ConditionalOnProperty(prefix = "shenyu.sync.nacos", name = "url") on the class NacosSyncDataConfiguration again. We have met this annotation when we analyzed the NacosListener class on the Admin side before, this config class would take effect only and if only the condition on this annotation is matched. In other words, when we have the config as below on the gateway side, the gateway would use nacos to sync data and the config class NacosSyncDataConfiguration would take effect.
shenyu: sync: nacos: url: localhost:8848
/** * Nacos sync data configuration for spring boot. */@Configuration@ConditionalOnClass(NacosSyncDataService.class)@ConditionalOnProperty(prefix = "shenyu.sync.nacos", name = "url")public class NacosSyncDataConfiguration { private static final Logger LOGGER = LoggerFactory.getLogger(NacosSyncDataConfiguration.class); /** * Nacos sync data service. * * @param configService the config service * @param pluginSubscriber the plugin subscriber * @param metaSubscribers the meta subscribers * @param authSubscribers the auth subscribers * @return the sync data service */ @Bean public SyncDataService nacosSyncDataService(final ObjectProvider<ConfigService> configService, final ObjectProvider<PluginDataSubscriber> pluginSubscriber, final ObjectProvider<List<MetaDataSubscriber>> metaSubscribers, final ObjectProvider<List<AuthDataSubscriber>> authSubscribers) { LOGGER.info("you use nacos sync shenyu data......."); return new NacosSyncDataService(configService.getIfAvailable(), pluginSubscriber.getIfAvailable(), metaSubscribers.getIfAvailable(Collections::emptyList), authSubscribers.getIfAvailable(Collections::emptyList)); } /** * Nacos config service config service. * * @param nacosConfig the nacos config * @return the config service * @throws Exception the exception */ @Bean public ConfigService nacosConfigService(final NacosConfig nacosConfig) throws Exception { Properties properties = new Properties(); if (nacosConfig.getAcm() != null && nacosConfig.getAcm().isEnabled()) { properties.put(PropertyKeyConst.ENDPOINT, nacosConfig.getAcm().getEndpoint()); properties.put(PropertyKeyConst.NAMESPACE, nacosConfig.getAcm().getNamespace()); properties.put(PropertyKeyConst.ACCESS_KEY, nacosConfig.getAcm().getAccessKey()); properties.put(PropertyKeyConst.SECRET_KEY, nacosConfig.getAcm().getSecretKey()); } else { properties.put(PropertyKeyConst.SERVER_ADDR, nacosConfig.getUrl()); if (StringUtils.isNotBlank(nacosConfig.getNamespace())) { properties.put(PropertyKeyConst.NAMESPACE, nacosConfig.getNamespace()); } if (nacosConfig.getUsername() != null) { properties.put(PropertyKeyConst.USERNAME, nacosConfig.getUsername()); } if (nacosConfig.getPassword() != null) { properties.put(PropertyKeyConst.PASSWORD, nacosConfig.getPassword()); } } return NacosFactory.createConfigService(properties); } /** * Http config http config. * * @return the http config */ @Bean @ConfigurationProperties(prefix = "shenyu.sync.nacos") public NacosConfig nacosConfig() { return new NacosConfig(); }}
Let's focus on the part of code above which is about the generation of the bean nacosSyncDataService:
@Beanpublic SyncDataService nacosSyncDataService(final ObjectProvider<ConfigService> configService, final ObjectProvider<PluginDataSubscriber> pluginSubscriber, final ObjectProvider<List<MetaDataSubscriber>> metaSubscribers, final ObjectProvider<List<AuthDataSubscriber>> authSubscribers) { LOGGER.info("you use nacos sync shenyu data......."); return new NacosSyncDataService(configService.getIfAvailable(), pluginSubscriber.getIfAvailable(), metaSubscribers.getIfAvailable(Collections::emptyList), authSubscribers.getIfAvailable(Collections::emptyList));}
As we can see, the bean is generated by the construction method of the Class NacosSyncDataService. Let's dive into the construction method.
public NacosSyncDataService(final ConfigService configService, final PluginDataSubscriber pluginDataSubscriber, final List<MetaDataSubscriber> metaDataSubscribers, final List<AuthDataSubscriber> authDataSubscribers) { super(configService, pluginDataSubscriber, metaDataSubscribers, authDataSubscribers); start();}
protected void watcherData(final String dataId, final OnChange oc) { Listener listener = new Listener() { @Override public void receiveConfigInfo(final String configInfo) { oc.change(configInfo); } @Override public Executor getExecutor() { return null; } }; oc.change(getConfigAndSignListener(dataId, listener)); LISTENERS.computeIfAbsent(dataId, key -> new ArrayList<>()).add(listener); }
As we can see, the construction method calls the start method and calls the watcherData method to create a listener which relates itself to a callback method oc, since we're analyzing the changes on the component with the selector type, the relative callback method is updateSelectorMap. This callback method is used to handle data.
PluginDataSubscriber is an interface, it is only a CommonPluginDataSubscriber implementation class, responsible for data processing plugin, selector and rules.
It has no additional logic and calls the unSelectorSubscribe()andsubscribeDataHandler() method directly. Within methods, there are data types (plugins, selectors, or rules) and action types (update or delete) to perform different logic.
/** * The common plugin data subscriber, responsible for handling all plug-in, selector, and rule information */public class CommonPluginDataSubscriber implements PluginDataSubscriber { //...... // handle selector data @Override public void onSelectorSubscribe(final SelectoData selectorData) { subscribeDataHandler(selectorData, DataEventTypeEnum.UPDATE); } @Override public void unSelectorSubscribe(final SelectorData selectorData) { subscribeDataHandler(selectorData, DataEventTypeEnum.DELETE); } // A subscription data handler that handles updates or deletions of data private <T> void subscribeDataHandler(final T classData, final DataEventTypeEnum dataType) { Optional.ofNullable(classData).ifPresent(data -> { // plugin data if (data instanceof PluginData) { PluginData pluginData = (PluginData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cachePluginData(pluginData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(pluginData.getName())).ifPresent(handler -> handler.handlerPlugin(pluginData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removePluginData(pluginData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(pluginData.getName())).ifPresent(handler -> handler.removePlugin(pluginData)); } } else if (data instanceof SelectorData) { // selector data SelectorData selectorData = (SelectorData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cacheSelectData(selectorData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.handlerSelector(selectorData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removeSelectData(selectorData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.removeSelector(selectorData)); } } else if (data instanceof RuleData) { // rule data RuleData ruleData = (RuleData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cacheRuleData(ruleData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(ruleData.getPluginName())).ifPresent(handler -> handler.handlerRule(ruleData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removeRuleData(ruleData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(ruleData.getPluginName())).ifPresent(handler -> handler.removeRule(ruleData)); } } }); }}
// save the data to gateway memoryBaseDataCache.getInstance().cacheSelectData(selectorData);// If each plugin has its own processing logic, then do itOptional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.handlerSelector(selectorData));
One is to save the data to the gateway's memory. BaseDataCache is the class that ultimately caches data, implemented in a singleton pattern. The selector data is stored in the SELECTOR_MAP Map. In the subsequent use, also from this data.
public final class BaseDataCache { // private instance private static final BaseDataCache INSTANCE = new BaseDataCache(); // private constructor private BaseDataCache() { } /** * Gets instance. * public method * @return the instance */ public static BaseDataCache getInstance() { return INSTANCE; } /** * A Map of the cache selector data * pluginName -> SelectorData. */ private static final ConcurrentMap<String, List<SelectorData>> SELECTOR_MAP = Maps.newConcurrentMap(); public void cacheSelectData(final SelectorData selectorData) { Optional.ofNullable(selectorData).ifPresent(this::selectorAccept); } /** * cache selector data. * @param data the selector data */ private void selectorAccept(final SelectorData data) { String key = data.getPluginName(); if (SELECTOR_MAP.containsKey(key)) { // Update operation, delete before insert List<SelectorData> existList = SELECTOR_MAP.get(key); final List<SelectorData> resultList = existList.stream().filter(r -> !r.getId().equals(data.getId())).collect(Collectors.toList()); resultList.add(data); final List<SelectorData> collect = resultList.stream().sorted(Comparator.comparing(SelectorData::getSort)).collect(Collectors.toList()); SELECTOR_MAP.put(key, collect); } else { // Add new operations directly to Map SELECTOR_MAP.put(key, Lists.newArrayList(data)); } }}
Second, if each plugin has its own processing logic, then do it. Through the IDEA editor, you can see that after adding a selector, there are the following plugins and processing. We're not going to expand it here.
After the above source tracking, and through a practical case, in the admin end to update a selector data, the ZooKeeper data synchronization process analysis is clear.
Let's series the data synchronization process on the gateway side through the sequence diagram:
The data synchronization process has been analyzed. In order to prevent the synchronization process from being interrupted, other logic is ignored during the analysis. We have analyzed the process of gateway synchronization operation initialization in the start method of NacosSyncDataService class. We also need to analyze the process of Admin synchronization data initialization.
On the admin side, the bean with the type NacosDataInit, is defined and generated in the NacosListener, if the configuration of the admin side decides to use nacos to sync data, when admin starts, the current data will be fully synchronized to nacos, the implementation logic is as follows:
/** * The type Nacos data init. */public class NacosDataInit implements CommandLineRunner { private static final Logger LOG = LoggerFactory.getLogger(NacosDataInit.class); private final ConfigService configService; private final SyncDataService syncDataService; /** * Instantiates a new Nacos data init. * @param configService the nacos config service * @param syncDataService the sync data service */ public NacosDataInit(final ConfigService configService, final SyncDataService syncDataService) { this.configService = configService; this.syncDataService = syncDataService; } @Override public void run(final String... args) { String pluginDataId = NacosPathConstants.PLUGIN_DATA_ID; String authDataId = NacosPathConstants.AUTH_DATA_ID; String metaDataId = NacosPathConstants.META_DATA_ID; if (dataIdNotExist(pluginDataId) && dataIdNotExist(authDataId) && dataIdNotExist(metaDataId)) { syncDataService.syncAll(DataEventTypeEnum.REFRESH); } } private boolean dataIdNotExist(final String pluginDataId) { try { String group = NacosPathConstants.GROUP; long timeout = NacosPathConstants.DEFAULT_TIME_OUT; return configService.getConfig(pluginDataId, group, timeout) == null; } catch (NacosException e) { LOG.error("Get data from nacos error.", e); throw new ShenyuException(e.getMessage()); } }}
Check whether there is data in nacos, if not, then synchronize.
NacosDataInit implements the CommandLineRunner interface. It is an interface provided by SpringBoot that executes the run() method after all Spring Beans initializations and is often used for initialization operations in a project.
SyncDataService.syncAll()
Query data from the database, and then perform full data synchronization, all authentication information, plugin information, selector information, rule information, and metadata information. Synchronous events are published primarily through eventPublisher. After publishing the event via publishEvent(), the ApplicationListener performs the event change operation. In ShenYu is mentioned in DataChangedEventDispatcher.
@Servicepublic class SyncDataServiceImpl implements SyncDataService { // eventPublisher private final ApplicationEventPublisher eventPublisher; /*** * sync all data * @param type the type * @return */ @Override public boolean syncAll(final DataEventTypeEnum type) { // app auth data appAuthService.syncData(); // plugin data List<PluginData> pluginDataList = pluginService.listAll(); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.PLUGIN, type, pluginDataList)); // selector data List<SelectorData> selectorDataList = selectorService.listAll(); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.SELECTOR, type, selectorDataList)); // rule data List<RuleData> ruleDataList = ruleService.listAll(); eventPublisher.publishEvent(new DataChangedEvent(ConfigGroupEnum.RULE, type, ruleDataList)); // metadata metaDataService.syncData(); return true; }}
In ShenYu gateway, data synchronization refers to how to synchronize the updated data to the gateway after the data is sent in the background management system. The Apache ShenYu gateway currently supports data synchronization for ZooKeeper, WebSocket, http long poll, Nacos, etcd and Consul. The main content of this article is based on WebSocket data synchronization source code analysis.
This paper based on shenyu-2.4.0 version of the source code analysis, the official website of the introduction of please refer to the Data Synchronization Design .
The WebSocket protocol was born in 2008 and became an international standard in 2011. It can be two-way communication, the server can take the initiative to push information to the client, the client can also take the initiative to send information to the server. The WebSocket protocol is based on the TCP protocol and belongs to the application layer, with low performance overhead and high communication efficiency. The protocol identifier is ws.
Convert data in the SelectorServiceImpl class using the createOrUpdate() method, save it to the database, publish the event, update upstream.
@RequiredArgsConstructor@Servicepublic class SelectorServiceImpl implements SelectorService { // eventPublisher private final ApplicationEventPublisher eventPublisher; @Override @Transactional(rollbackFor = Exception.class) public int createOrUpdate(final SelectorDTO selectorDTO) { int selectorCount; // build data DTO --> DO SelectorDO selectorDO = SelectorDO.buildSelectorDO(selectorDTO); List<SelectorConditionDTO> selectorConditionDTOs = selectorDTO.getSelectorConditions(); // insert or update ? if (StringUtils.isEmpty(selectorDTO.getId())) { // insert into data selectorCount = selectorMapper.insertSelective(selectorDO); // insert into condition data selectorConditionDTOs.forEach(selectorConditionDTO -> { selectorConditionDTO.setSelectorId(selectorDO.getId()); selectorConditionMapper.insertSelective(SelectorConditionDO.buildSelectorConditionDO(selectorConditionDTO)); }); // check selector add if (dataPermissionMapper.listByUserId(JwtUtils.getUserInfo().getUserId()).size() > 0) { DataPermissionDTO dataPermissionDTO = new DataPermissionDTO(); dataPermissionDTO.setUserId(JwtUtils.getUserInfo().getUserId()); dataPermissionDTO.setDataId(selectorDO.getId()); dataPermissionDTO.setDataType(AdminConstants.SELECTOR_DATA_TYPE); dataPermissionMapper.insertSelective(DataPermissionDO.buildPermissionDO(dataPermissionDTO)); } } else { // update data, delete and then insert selectorCount = selectorMapper.updateSelective(selectorDO); //delete rule condition then add selectorConditionMapper.deleteByQuery(new SelectorConditionQuery(selectorDO.getId())); selectorConditionDTOs.forEach(selectorConditionDTO -> { selectorConditionDTO.setSelectorId(selectorDO.getId()); SelectorConditionDO selectorConditionDO = SelectorConditionDO.buildSelectorConditionDO(selectorConditionDTO); selectorConditionMapper.insertSelective(selectorConditionDO); }); } // publish event publishEvent(selectorDO, selectorConditionDTOs); // update upstream updateDivideUpstream(selectorDO); return selectorCount; } // ......}
In the Service class to persist data, i.e. to the database, this should be familiar, not expand. The update upstream operation is analyzed in the corresponding section below, focusing on the publish event operation, which performs data synchronization.
The logic of the publishEvent() method is to find the plugin corresponding to the selector, build the conditional data, and publish the change data.
Change data released by eventPublisher.PublishEvent() is complete, the eventPublisher object is a ApplicationEventPublisher class, The fully qualified class name is org.springframework.context.ApplicationEventPublisher. Here we see that publishing data is done through Spring related functionality.
ApplicationEventPublisher:
When a state change, the publisher calls ApplicationEventPublisher of publishEvent method to release an event, Spring container broadcast event for all observers, The observer's onApplicationEvent method is called to pass the event object to the observer. There are two ways to call publishEvent method, one is to implement the interface by the container injection ApplicationEventPublisher object and then call the method, the other is a direct call container, the method of two methods of publishing events not too big difference.
ApplicationEventPublisher: publish event;
ApplicationEvent: Spring event, record the event source, time, and data;
ApplicationListener: event listener, observer.
In Spring event publishing mechanism, there are three objects,
An object is a publish event ApplicationEventPublisher, in ShenYu through the constructor in the injected a eventPublisher.
The other object is ApplicationEvent , inherited from ShenYu through DataChangedEvent, representing the event object.
public class DataChangedEvent extends ApplicationEvent {//......}
The last object is ApplicationListener in ShenYu in through DataChangedEventDispatcher class implements this interface, as the event listener, responsible for handling the event object.
@Componentpublic class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean { //......}
Released when the event is completed, will automatically enter the DataChangedEventDispatcher class onApplicationEvent() method of handling events.
@Componentpublic class DataChangedEventDispatcher implements ApplicationListener<DataChangedEvent>, InitializingBean { /** * This method is called when there are data changes * @param event */ @Override @SuppressWarnings("unchecked") public void onApplicationEvent(final DataChangedEvent event) { // Iterate through the data change listener (usually using a data synchronization approach is fine) for (DataChangedListener listener : listeners) { // What kind of data has changed switch (event.getGroupKey()) { case APP_AUTH: // app auth data listener.onAppAuthChanged((List<AppAuthData>) event.getSource(), event.getEventType()); break; case PLUGIN: // plugin data listener.onPluginChanged((List<PluginData>) event.getSource(), event.getEventType()); break; case RULE: // rule data listener.onRuleChanged((List<RuleData>) event.getSource(), event.getEventType()); break; case SELECTOR: // selector data listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); break; case META_DATA: // metadata listener.onMetaDataChanged((List<MetaData>) event.getSource(), event.getEventType()); break; default: // Other types throw exception throw new IllegalStateException("Unexpected value: " + event.getGroupKey()); } } }}
When there is a data change, the onApplicationEvent method is called and all the data change listeners are iterated to determine the data type and handed over to the appropriate data listener for processing.
ShenYu groups all the data into five categories: APP_AUTH, PLUGIN, RULE, SELECTOR and META_DATA.
Here the data change listener (DataChangedListener) is an abstraction of the data synchronization policy. Its concrete implementation is:
These implementation classes are the synchronization strategies currently supported by ShenYu:
WebsocketDataChangedListener: data synchronization based on Websocket;
ZookeeperDataChangedListener:data synchronization based on Zookeeper;
ConsulDataChangedListener: data synchronization based on Consul;
EtcdDataDataChangedListener:data synchronization based on etcd;
HttpLongPollingDataChangedListener:data synchronization based on http long polling;
NacosDataChangedListener:data synchronization based on nacos;
Given that there are so many implementation strategies, how do you decide which to use?
Because this paper is based on websocket data synchronization source code analysis, so here to WebsocketDataChangedListener as an example, the analysis of how it is loaded and implemented.
A global search in the source code project shows that its implementation is done in the DataSyncConfiguration class.
/** * Data Sync Configuration * By springboot conditional assembly * The type Data sync configuration. */@Configurationpublic class DataSyncConfiguration { /** * The WebsocketListener(default strategy). */ @Configuration @ConditionalOnProperty(name = "shenyu.sync.websocket.enabled", havingValue = "true", matchIfMissing = true) @EnableConfigurationProperties(WebsocketSyncProperties.class) static class WebsocketListener { /** * Config event listener data changed listener. * @return the data changed listener */ @Bean @ConditionalOnMissingBean(WebsocketDataChangedListener.class) public DataChangedListener websocketDataChangedListener() { return new WebsocketDataChangedListener(); } /** * Websocket collector. * Websocket collector class: establish a connection, send a message, close the connection and other operations * @return the websocket collector */ @Bean @ConditionalOnMissingBean(WebsocketCollector.class) public WebsocketCollector websocketCollector() { return new WebsocketCollector(); } /** * Server endpoint exporter * * @return the server endpoint exporter */ @Bean @ConditionalOnMissingBean(ServerEndpointExporter.class) public ServerEndpointExporter serverEndpointExporter() { return new ServerEndpointExporter(); } } //......}
This configuration class is implemented through the SpringBoot conditional assembly class. The WebsocketListener class has several annotations:
@ConditionalOnProperty(name = "shenyu.sync.websocket.enabled", havingValue = "true", matchIfMissing = true): attribute condition. The configuration class takes effect only when the condition is met. That is, when we have the following configuration, websocket is used for data synchronization. Note, however, the matchIfMissing = true attribute, which means that this configuration class will work if you don't have the following configuration. Data synchronization based on webSocket is officially recommended and the default.
When we take the initiative to configuration, use the websocket data synchronization, WebsocketDataChangedListener is generated. So in the event handler onApplicationEvent(), it goes to the corresponding listener. In our case, a selector is to increase the new data, the data by adopting the websocket, so, the code will enter the WebsocketDataChangedListener selector data change process.
@Override @SuppressWarnings("unchecked") public void onApplicationEvent(final DataChangedEvent event) { // Iterate through the data change listener (usually using a data synchronization approach is fine) for (DataChangedListener listener : listeners) { // What kind of data has changed switch (event.getGroupKey()) { // other logic is omitted case SELECTOR: // selector data listener.onSelectorChanged((List<SelectorData>) event.getSource(), event.getEventType()); // WebsocketDataChangedListener handle selector data break; } }
In the onSelectorChanged() method, the data is encapsulated into WebsocketData and then sent via webSocketCollector.send().
// selector data has been updated @Override public void onSelectorChanged(final List<SelectorData> selectorDataList, final DataEventTypeEnum eventType) { // build WebsocketData WebsocketData<SelectorData> websocketData = new WebsocketData<>(ConfigGroupEnum.SELECTOR.name(), eventType.name(), selectorDataList); // websocket send data WebsocketCollector.send(GsonUtils.getInstance().toJson(websocketData), eventType); }
In the send() method, the type of synchronization is determined and processed according to the different types.
@Slf4j@ServerEndpoint(value = "/websocket", configurator = WebsocketConfigurator.class)public class WebsocketCollector {/** * Send. * * @param message the message * @param type the type */ public static void send(final String message, final DataEventTypeEnum type) { if (StringUtils.isNotBlank(message)) { // If it's MYSELF (first full synchronization) if (DataEventTypeEnum.MYSELF == type) { // get the session from ThreadLocal Session session = (Session) ThreadLocalUtil.get(SESSION_KEY); if (session != null) { // send full data to the session sendMessageBySession(session, message); } } else { // subsequent incremental synchronization // synchronize change data to all sessions SESSION_SET.forEach(session -> sendMessageBySession(session, message)); } } } private static void sendMessageBySession(final Session session, final String message) { try { // The message is sent through the Websocket session session.getBasicRemote().sendText(message); } catch (IOException e) { log.error("websocket send result is exception: ", e); } }}
The example we give is a new operation, an incremental synchronization, so it goes
Assume ShenYu gateway is already in normal operation, using the data synchronization mode is also websocket. How does the gateway receive and process new selector data from admin and send it to the gateway via WebSocket? Let's continue our source code analysis to find out.
There is a ShenyuWebsocketClient class on the gateway, which inherits from WebSocketClient and can establish a connection and communicate with WebSocket.
public final class ShenyuWebsocketClient extends WebSocketClient { // ......}
After sending data via websocket on the admin side, ShenyuWebsocketClient can receive data via onMessage() and then process it itself.
public final class ShenyuWebsocketClient extends WebSocketClient { // execute after receiving the message @Override public void onMessage(final String result) { // handle accept data handleResult(result); } private void handleResult(final String result) { // data deserialization WebsocketData websocketData = GsonUtils.getInstance().fromJson(result, WebsocketData.class); // which data types, plug-ins, selectors, rules... ConfigGroupEnum groupEnum = ConfigGroupEnum.acquireByName(websocketData.getGroupType()); // which operation type, update, delete... String eventType = websocketData.getEventType(); String json = GsonUtils.getInstance().toJson(websocketData.getData()); // handle data websocketDataHandler.executor(groupEnum, json, eventType); }}
After receiving the data, first has carried on the deserialization operation, read the data type and operation type, then hand over to websocketDataHandler.executor() for processing.
A Websocket data handler is created in factory mode, providing one handler for each data type:
plugin --> PluginDataHandler;
selector --> SelectorDataHandler;
rule --> RuleDataHandler;
auth --> AuthDataHandler;
metadata --> MetaDataHandler.
/** * Create Websocket data handlers through factory mode * The type Websocket cache handler. */public class WebsocketDataHandler { private static final EnumMap<ConfigGroupEnum, DataHandler> ENUM_MAP = new EnumMap<>(ConfigGroupEnum.class); /** * Instantiates a new Websocket data handler. * @param pluginDataSubscriber the plugin data subscriber * @param metaDataSubscribers the meta data subscribers * @param authDataSubscribers the auth data subscribers */ public WebsocketDataHandler(final PluginDataSubscriber pluginDataSubscriber, final List<MetaDataSubscriber> metaDataSubscribers, final List<AuthDataSubscriber> authDataSubscribers) { // plugin --> PluginDataHandler ENUM_MAP.put(ConfigGroupEnum.PLUGIN, new PluginDataHandler(pluginDataSubscriber)); // selector --> SelectorDataHandler ENUM_MAP.put(ConfigGroupEnum.SELECTOR, new SelectorDataHandler(pluginDataSubscriber)); // rule --> RuleDataHandler ENUM_MAP.put(ConfigGroupEnum.RULE, new RuleDataHandler(pluginDataSubscriber)); // auth --> AuthDataHandler ENUM_MAP.put(ConfigGroupEnum.APP_AUTH, new AuthDataHandler(authDataSubscribers)); // metadata --> MetaDataHandler ENUM_MAP.put(ConfigGroupEnum.META_DATA, new MetaDataHandler(metaDataSubscribers)); } /** * Executor. * * @param type the type * @param json the json * @param eventType the event type */ public void executor(final ConfigGroupEnum type, final String json, final String eventType) { // find the corresponding data handler based on the data type ENUM_MAP.get(type).handle(json, eventType); }}
Different data types have different ways of handling data, so there are different implementation classes. But they also have the same processing logic between them, so they can be implemented through the template approach to design patterns. The same logic is placed in the handle() method of the abstract class, and the different logic is handed over to the respective implementation class.
In our case, a new selector is added, so it will be passed to the SelectorDataHandler for data processing.
Implement common logical handling of data changes: invoke different methods based on different operation types.
public abstract class AbstractDataHandler<T> implements DataHandler { /** * Convert list. * The different logic is implemented by the respective implementation classes * @param json the json * @return the list */ protected abstract List<T> convert(String json); /** * Do refresh. * The different logic is implemented by the respective implementation classes * @param dataList the data list */ protected abstract void doRefresh(List<T> dataList); /** * Do update. * The different logic is implemented by the respective implementation classes * @param dataList the data list */ protected abstract void doUpdate(List<T> dataList); /** * Do delete. * The different logic is implemented by the respective implementation classes * @param dataList the data list */ protected abstract void doDelete(List<T> dataList); // General purpose logic, abstract class implementation @Override public void handle(final String json, final String eventType) { List<T> dataList = convert(json); if (CollectionUtils.isNotEmpty(dataList)) { DataEventTypeEnum eventTypeEnum = DataEventTypeEnum.acquireByName(eventType); switch (eventTypeEnum) { case REFRESH: case MYSELF: doRefresh(dataList); //Refreshes data and synchronizes all data break; case UPDATE: case CREATE: doUpdate(dataList); // Update or create data, incremental synchronization break; case DELETE: doDelete(dataList); // delete data break; default: break; } } }}
New selector data, new operation, through switch-case into doUpdate() method.
/** * The type Selector data handler. */@RequiredArgsConstructorpublic class SelectorDataHandler extends AbstractDataHandler<SelectorData> { private final PluginDataSubscriber pluginDataSubscriber; //...... // update data @Override protected void doUpdate(final List<SelectorData> dataList) { dataList.forEach(pluginDataSubscriber::onSelectorSubscribe); }}
Iterate over the data and enter the onSelectorSubscribe() method.
PluginDataSubscriber.onSelectorSubscribe()
It has no additional logic and calls the subscribeDataHandler() method directly. Within methods, there are data types (plugins, selectors, or rules) and action types (update or delete) to perform different logic.
/** * The common plugin data subscriber, responsible for handling all plug-in, selector, and rule information */public class CommonPluginDataSubscriber implements PluginDataSubscriber { //...... // handle selector data @Override public void onSelectorSubscribe(final SelectoData selectorData) { subscribeDataHandler(selectorData, DataEventTypeEnum.UPDATE); } // A subscription data handler that handles updates or deletions of data private <T> void subscribeDataHandler(final T classData, final DataEventTypeEnum dataType) { Optional.ofNullable(classData).ifPresent(data -> { // plugin data if (data instanceof PluginData) { PluginData pluginData = (PluginData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cachePluginData(pluginData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(pluginData.getName())).ifPresent(handler -> handler.handlerPlugin(pluginData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removePluginData(pluginData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(pluginData.getName())).ifPresent(handler -> handler.removePlugin(pluginData)); } } else if (data instanceof SelectorData) { // selector data SelectorData selectorData = (SelectorData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cacheSelectData(selectorData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.handlerSelector(selectorData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removeSelectData(selectorData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.removeSelector(selectorData)); } } else if (data instanceof RuleData) { // rule data RuleData ruleData = (RuleData) data; if (dataType == DataEventTypeEnum.UPDATE) { // update // save the data to gateway memory BaseDataCache.getInstance().cacheRuleData(ruleData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(ruleData.getPluginName())).ifPresent(handler -> handler.handlerRule(ruleData)); } else if (dataType == DataEventTypeEnum.DELETE) { // delete // delete the data from gateway memory BaseDataCache.getInstance().removeRuleData(ruleData); // If each plugin has its own processing logic, then do it Optional.ofNullable(handlerMap.get(ruleData.getPluginName())).ifPresent(handler -> handler.removeRule(ruleData)); } } }); }}
Adding a selector will enter the following logic:
// save the data to gateway memoryBaseDataCache.getInstance().cacheSelectData(selectorData);// If each plugin has its own processing logic, then do itOptional.ofNullable(handlerMap.get(selectorData.getPluginName())).ifPresent(handler -> handler.handlerSelector(selectorData));
One is to save the data to the gateway's memory. BaseDataCache is the class that ultimately caches data, implemented in a singleton pattern. The selector data is stored in the SELECTOR_MAP Map. In the subsequent use, also from this data.
public final class BaseDataCache { // private instance private static final BaseDataCache INSTANCE = new BaseDataCache(); // private constructor private BaseDataCache() { } /** * Gets instance. * public method * @return the instance */ public static BaseDataCache getInstance() { return INSTANCE; } /** * A Map of the cache selector data * pluginName -> SelectorData. */ private static final ConcurrentMap<String, List<SelectorData>> SELECTOR_MAP = Maps.newConcurrentMap(); public void cacheSelectData(final SelectorData selectorData) { Optional.ofNullable(selectorData).ifPresent(this::selectorAccept); } /** * cache selector data. * @param data the selector data */ private void selectorAccept(final SelectorData data) { String key = data.getPluginName(); if (SELECTOR_MAP.containsKey(key)) { // Update operation, delete before insert List<SelectorData> existList = SELECTOR_MAP.get(key); final List<SelectorData> resultList = existList.stream().filter(r -> !r.getId().equals(data.getId())).collect(Collectors.toList()); resultList.add(data); final List<SelectorData> collect = resultList.stream().sorted(Comparator.comparing(SelectorData::getSort)).collect(Collectors.toList()); SELECTOR_MAP.put(key, collect); } else { // Add new operations directly to Map SELECTOR_MAP.put(key, Lists.newArrayList(data)); } }}
Second, if each plugin has its own processing logic, then do it. Through the IDEA editor, you can see that after adding a selector, there are the following plugins and processing. We're not going to expand it here.
After the above source tracing, and through a practical case, in the admin side to add a selector data, will websocket data synchronization process analysis cleared.
Let's use the following figure to concatenate the data synchronization process on the gateway side:
The data synchronization process has been analyzed, but there are still some problems that have not been analyzed, that is, how does the gateway establish a connection with admin?
4. The Gateway Establishes a Websocket Connection with Admin#
websocket config
With the following configuration in the gateway configuration file and the dependency introduced, the websocket related service is started.
shenyu:file:enabled:truecross:enabled:truedubbo:parameter: multisync:websocket:# Use websocket for data synchronizationurls: ws://localhost:9095/websocket # websocket address of adminallowOrigin: ws://localhost:9195
Add a dependency on websocket in the gateway.
<!--shenyu data sync start use websocket--><dependency><groupId>org.apache.shenyu</groupId><artifactId>shenyu-spring-boot-starter-sync-data-websocket</artifactId><version>${project.version}</version></dependency>
Websocket Data Sync Config
The associated bean is created by conditional assembly of springboot. In the gateway started, if we configure the shenyu.sync.websocket.urls, then websocket data synchronization configuration will be loaded. The dependency loading is done through the springboot starter.
/** * WebsocketSyncDataService * Conditional injection is implemented through SpringBoot * Websocket sync data configuration for spring boot. */@Configuration@ConditionalOnClass(WebsocketSyncDataService.class)@ConditionalOnProperty(prefix = "shenyu.sync.websocket", name = "urls")@Slf4jpublic class WebsocketSyncDataConfiguration { /** * Websocket sync data service. * @param websocketConfig the websocket config * @param pluginSubscriber the plugin subscriber * @param metaSubscribers the meta subscribers * @param authSubscribers the auth subscribers * @return the sync data service */ @Bean public SyncDataService websocketSyncDataService(final ObjectProvider<WebsocketConfig> websocketConfig, final ObjectProvider<PluginDataSubscriber> pluginSubscriber, final ObjectProvider<List<MetaDataSubscriber>> metaSubscribers, final ObjectProvider<List<AuthDataSubscriber>> authSubscribers) { log.info("you use websocket sync shenyu data......."); return new WebsocketSyncDataService(websocketConfig.getIfAvailable(WebsocketConfig::new), pluginSubscriber.getIfAvailable(), metaSubscribers.getIfAvailable(Collections::emptyList), authSubscribers.getIfAvailable(Collections::emptyList)); } /** * Config websocket config. * * @return the websocket config */ @Bean @ConfigurationProperties(prefix = "shenyu.sync.websocket") public WebsocketConfig websocketConfig() { return new WebsocketConfig(); }}
Start a new spring.factories file in the resources/META-INF directory of your project and specify the configuration classes in the file.
WebsocketSyncDataService
The following things are done in 'WebsocketSyncDataService' :
Read configuration urls, which represent the admin side of the synchronization address, if there are more than one, use "," split;
Create a scheduling thread pool, with each admin assigned one to perform scheduled tasks;
Create ShenyuWebsocketClient, assign one to each admin, set up websocket communication with admin;
Start connection with admin end websocket;
Executes a scheduled task every 10 seconds. The main function is to determine whether the websocket connection has been disconnected, if so, try to reconnect. If not, a ping-pong test is performed.
/** * Websocket sync data service. */@Slf4jpublic class WebsocketSyncDataService implements SyncDataService, AutoCloseable { private final List<WebSocketClient> clients = new ArrayList<>(); private final ScheduledThreadPoolExecutor executor; /** * Instantiates a new Websocket sync cache. * @param websocketConfig the websocket config * @param pluginDataSubscriber the plugin data subscriber * @param metaDataSubscribers the meta data subscribers * @param authDataSubscribers the auth data subscribers */ public WebsocketSyncDataService(final WebsocketConfig websocketConfig, final PluginDataSubscriber pluginDataSubscriber, final List<MetaDataSubscriber> metaDataSubscribers, final List<AuthDataSubscriber> authDataSubscribers) { // If there are multiple synchronization addresses on the admin side, use commas (,) to separate them String[] urls = StringUtils.split(websocketConfig.getUrls(), ","); // Create a scheduling thread pool, one for each admin executor = new ScheduledThreadPoolExecutor(urls.length, ShenyuThreadFactory.create("websocket-connect", true)); for (String url : urls) { try { //Create a WebsocketClient and assign one to each admin clients.add(new ShenyuWebsocketClient(new URI(url), Objects.requireNonNull(pluginDataSubscriber), metaDataSubscribers, authDataSubscribers)); } catch (URISyntaxException e) { log.error("websocket url({}) is error", url, e); } } try { for (WebSocketClient client : clients) { // Establish a connection with the WebSocket Server boolean success = client.connectBlocking(3000, TimeUnit.MILLISECONDS); if (success) { log.info("websocket connection is successful....."); } else { log.error("websocket connection is error....."); } // Run a scheduled task every 10 seconds // The main function is to check whether the WebSocket connection is disconnected. If the connection is disconnected, retry the connection. // If it is not disconnected, the ping-pong test is performed executor.scheduleAtFixedRate(() -> { try { if (client.isClosed()) { boolean reconnectSuccess = client.reconnectBlocking(); if (reconnectSuccess) { log.info("websocket reconnect server[{}] is successful.....", client.getURI().toString()); } else { log.error("websocket reconnection server[{}] is error.....", client.getURI().toString()); } } else { client.sendPing(); log.debug("websocket send to [{}] ping message successful", client.getURI().toString()); } } catch (InterruptedException e) { log.error("websocket connect is error :{}", e.getMessage()); } }, 10, 10, TimeUnit.SECONDS); } /* client.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyaddress", 80)));*/ } catch (InterruptedException e) { log.info("websocket connection...exception....", e); } } @Override public void close() { // close websocket client for (WebSocketClient client : clients) { if (!client.isClosed()) { client.close(); } } // close threadpool if (Objects.nonNull(executor)) { executor.shutdown(); } }}
ShenyuWebsocketClient
The WebSocket client created in ShenYu to communicate with the admin side. After the connection is successfully established for the first time, full data is synchronized and incremental data is subsequently synchronized.
/** * The type shenyu websocket client. */@Slf4jpublic final class ShenyuWebsocketClient extends WebSocketClient { private volatile boolean alreadySync = Boolean.FALSE; private final WebsocketDataHandler websocketDataHandler; /** * Instantiates a new shenyu websocket client. * @param serverUri the server uri * @param pluginDataSubscriber the plugin data subscriber * @param metaDataSubscribers the meta data subscribers * @param authDataSubscribers the auth data subscribers */ public ShenyuWebsocketClient(final URI serverUri, final PluginDataSubscriber pluginDataSubscriber,final List<MetaDataSubscriber> metaDataSubscribers, final List<AuthDataSubscriber> authDataSubscribers) { super(serverUri); this.websocketDataHandler = new WebsocketDataHandler(pluginDataSubscriber, metaDataSubscribers, authDataSubscribers); } // Execute after the connection is successfully established @Override public void onOpen(final ServerHandshake serverHandshake) { // To prevent re-execution when reconnecting, use alreadySync to determine if (!alreadySync) { // Synchronize all data, type MYSELF send(DataEventTypeEnum.MYSELF.name()); alreadySync = true; } } // Execute after receiving the message @Override public void onMessage(final String result) { // handle data handleResult(result); } // Execute after shutdown @Override public void onClose(final int i, final String s, final boolean b) { this.close(); } // Execute after error @Override public void onError(final Exception e) { this.close(); } @SuppressWarnings("ALL") private void handleResult(final String result) { // Data deserialization WebsocketData websocketData = GsonUtils.getInstance().fromJson(result, WebsocketData.class); // Which data types, plugins, selectors, rules... ConfigGroupEnum groupEnum = ConfigGroupEnum.acquireByName(websocketData.getGroupType()); // Which operation type, update, delete... String eventType = websocketData.getEventType(); String json = GsonUtils.getInstance().toJson(websocketData.getData()); // handle data websocketDataHandler.executor(groupEnum, json, eventType); }}
This paper through a practical case, the data synchronization principle of websocket source code analysis. The main knowledge points involved are as follows:
WebSocket supports bidirectional communication and has good performance. It is recommended.
Complete event publishing and listening via Spring;
Support multiple synchronization strategies through abstract DataChangedListener interface, interface oriented programming;
Use factory mode to create WebsocketDataHandler to handle different data types;
Implement AbstractDataHandler using template method design patterns to handle general operation types;
Use singleton design pattern to cache data class BaseDataCache;
Loading of configuration classes via conditional assembly of SpringBoot and starter loading mechanism.
Recently,when I read the source code of open source project Apache Shenyu API gateway,I find and many core components of the gateway are loaded with the SPI module. Here I will analyzes the source code of SPI module in Shenyu gateway.
SPI means 'Service Provider Interface', which is a dynamic Service discovery mechanism. We can dynamically load the implementation class of the Interface based on the runtime of the Interface (that is, a development mode of Interface programming + strategy pattern + configuration file) with it. The most common is the built-in database Driver interface 'java.sql.Driver' in JDK. Different vendors can implement this interface differently. For example, 'MySQL' ('com.mysql.jdbc.Driver' in the 'MySQL' Driver package),' PostgreSQL' ('org.postgresql.driver' in the 'PostgreSQL' Driver package), etc.
The JDK's built-in 'SPI' can be used as follows:
In the 'META-INF/services' directory of the classpath, create a file named with the fully qualified name of the interface (essentially a 'properties' file) whose implementation classes you want the SPI loader to load , for example if you want to load the SQL driver implementation classes mentioned above then create a file named 'java.sql.Driver' since those classes implement the 'java.sql.driver' interface.
In this file we can add entries for all the specific implementations of the interface . For example for the above driver class scenario we would add entries as shown in below code snippet in the file META-INF/services/java.sql.Driver
# content of file META-INF/services/java.sql.Drivercom.mysql.jdbc.Driverorg.postgresql.Driver
Finally load the file with 'java.util.ServiceLoader' to instantiate the corresponding implementation class of the interface
The underlying implementation involves classloading, the parent delegate model, and so on, which I won't expand here. Based on this design idea, many mainstream frameworks self-implemented a set of 'SPI' extension, such as 'Dubbo SPI' extension module, which would read the 'META-INF/services/dubbo' directory file content in the classppath for class loading. The 'shenyu-spi' module also follows this similar design idea.
'ExtensionFactory' : 'SPI' loader Factory, used to load an 'ExtensionLoader' instance based on the 'SPI' mechanism and to obtain the default 'SPI' identity implementation based on the 'ExtensionLoader' instance
'SpiExtensionFactory' : is an implementation of 'ExtensionFactory'
'SPI' : identification annotation, used to identify 'SPI', used on the interface
'Join' : identification annotation, used on the implementation class, used to identify the class joining the SPI system
'ExtensionLoader' : 'SPI' loader, analogous to 'java.util.ServiceLoader', used to load the implementation class of the interface in 'SPI'
org.apache.shenyu.spi.SPI is an identification annotation which is used for identifying an interface as a 'SPI' interface.That is, only interfaces that use '@SPI' annotation can be loaded by 'shenyu-spi'. The class's annotation describes the implementation of Apache Dubbo, a reference to all the SPI systems (which makes sense, since the SPI extension is already a mature scheme with much the same implementation). This annotation has only one method:
@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface SPI { /** * Value string. * * @return the string */ String value() default "";}
The unique 'value()' method is used to specify the default 'SPI' implementation (optional), as will be shown later when analyzing 'ExtensionLoader'.
org.apache.shenyu.spi.Join is an identification annotation too. When this annotation is used on a class it specifies that the class contains 'SPI' implementation and to indicate that the class is added to the SPI system and can be loaded by ExtensionLoader. This annotation has two methods:
@Documented@Retention(RetentionPolicy.RUNTIME)@Target(ElementType.TYPE)public @interface Join { /** * It will be sorted according to the current serial number.. * @return int. */ int order() default 0; /** * Indicates that the object joined by @Join is a singleton, * otherwise a completely new instance is created each time. * @return true or false. */ boolean isSingleton() default true;}
The unique 'order()' method is used to specify the specific sequence number. If a single interface that annotated with '@SPI' has multiple implementation classes that annotated with '@Join', the sequence number determines the order of these implementation class instances (the smaller one comes first).
The isSingleton() method indicates whether the class that the implementation class is a singleton class or not. That is if it is a singleton class it will be instantiated only once else it will create a new instance everytime .
'ExtensionLoader' is the core of the 'SPI' module. Look at it's attributes first:
public final class ExtensionLoader<T> { // SLF4J日志句柄 private static final Logger LOG = LoggerFactory.getLogger(ExtensionLoader.class); // SPI配置文件基于类路径下的相对目录 private static final String SHENYU_DIRECTORY = "META-INF/shenyu/"; // @SPI标识接口类型 -> ExtensionLoader实例的缓存 => 注意这个是一个全局的静态变量 private static final Map<Class<?>, ExtensionLoader<?>> LOADERS = new ConcurrentHashMap<>(); // 当前@SPI标识接口类型 private final Class<T> clazz; // 类加载器实例 private final ClassLoader classLoader; // 当前ExtensionLoader缓存的已加载的实现类信息,使用值持有器包装,是一个HashMap,映射关系:实现类别名 -> 实现类信息 private final Holder<Map<String, ClassEntity>> cachedClasses = new Holder<>(); // 当前ExtensionLoader缓存的已加载的实现类实例的值包装器,使用值持有器包装,映射关系:实现类别名 -> 值持有器包装的实现类实体 private final Map<String, Holder<Object>> cachedInstances = new ConcurrentHashMap<>(); // 当前ExtensionLoader缓存的已加载的实现类实例,使用值持有器包装,映射关系:实现类类型 -> 实现类实体 private final Map<Class<?>, Object> joinInstances = new ConcurrentHashMap<>(); // 缓存默认名称,来源于@SPI注解的value()方法非空白返回值,用于加载默认的接口实现 private String cachedDefaultName; // Holder比较器,按照Holder的order降序,也就是顺序号小的排在前面 private final Comparator<Holder<Object>> holderComparator = Comparator.comparing(Holder::getOrder); // ClassEntity比较器,按照ClassEntity的order降序,也就是顺序号小的排在前面 private final Comparator<ClassEntity> classEntityComparator = Comparator.comparing(ClassEntity::getOrder); // 暂时省略其他代码 // 值持有器,简单VO,用来存储泛型值和值加载顺序 public static class Holder<T> { // 这里的值引用是volatile修饰,便于某线程更变另一线程马上读到最新的值 private volatile T value; private Integer order; private boolean isSingleton; // 省略setter和getter代码 } // 类实体,主要存放加载的实现类的信息 static final class ClassEntity { // 名称,这里是指SPI实现类的别名,不是类名 private String name; // 加载顺序号 private Integer order; private Boolean isSingleton; // SPI实现类 private Class<?> clazz; private ClassEntity(final String name, final Integer order, final Class<?> clazz, final boolean isSingleton) { this.name = name; this.order = order; this.clazz = clazz; this.isSingleton = isSingleton; } // 省略setter和getter代码 }}
After analyzing the attributes, it is not difficult to find the following points:
'ExtensionLoader' There will be a global static cache 'LOADERS' to cache already created instances of 'ExtensionLoader' to prevent the performance overhead of repeated creation
Each '@SPI' interface that is loaded using 'ExtensionLoader' generates a new instance of 'ExtensionLoader'
'@SPI' interfaces that have multiple implementations are eventually acquired in order
Then look at it's constructors and static factory methods:
// 私有构造函数,需要入参为@SPI标识的接口类型和类加载器实例private ExtensionLoader(final Class<T> clazz, final ClassLoader cl) { // 成员变量clazz赋值 this.clazz = clazz; // 成员变量classLoader赋值 this.classLoader = cl; // 这里对于非ExtensionFactory接口类型会懒加载一个用于加载ExtensionFactory的ExtensionLoader if (!Objects.equals(clazz, ExtensionFactory.class)) { ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getExtensionClassesEntity(); }}// 实例化getExtensionLoader,静态工厂方法,需要入参为@SPI标识的接口类型和类加载器实例public static <T> ExtensionLoader<T> getExtensionLoader(final Class<T> clazz, final ClassLoader cl) { // 前缀校验,接口类型必须非空并且必须存在@SPI注解,否则抛出异常中断 Objects.requireNonNull(clazz, "extension clazz is null"); if (!clazz.isInterface()) { throw new IllegalArgumentException("extension clazz (" + clazz + ") is not interface!"); } if (!clazz.isAnnotationPresent(SPI.class)) { throw new IllegalArgumentException("extension clazz (" + clazz + ") without @" + SPI.class + " Annotation"); } // 从缓存LOADERS中加载ExtensionLoader实例,不存在则创建,典型的懒加载模式 ExtensionLoader<T> extensionLoader = (ExtensionLoader<T>) LOADERS.get(clazz); if (Objects.nonNull(extensionLoader)) { return extensionLoader; } LOADERS.putIfAbsent(clazz, new ExtensionLoader<>(clazz, cl)); return (ExtensionLoader<T>) LOADERS.get(clazz);}// 实例化getExtensionLoader,静态工厂方法,需要入参为@SPI标识的接口类型,使用ExtensionLoader类的类加载器public static <T> ExtensionLoader<T> getExtensionLoader(final Class<T> clazz) { return getExtensionLoader(clazz, ExtensionLoader.class.getClassLoader());}
'ExtensionLoader' uses a private constructor, static factory methods, and lazy loading mode. Class loading is not triggered after initializing 'ExtensionLoader'. The actual scanning and loading is delayed until the 'getJoin' series methods are called, where the code is swept and all the method call chains that implement class information are loaded:
Processing with the chain of method 'getExtensionClassesEntity - > loadExtensionClass - > loadDirectory - > loadResources - > LoadClass',it will create a mapping of 'alias' to 'implementation class information' for subsequent instantiations, as shown in the 'getJoin()' method:
// 基于别名获取实现类实例public T getJoin(final String name) { // 别名必须为非空白字符串 if (StringUtils.isBlank(name)) { throw new NullPointerException("get join name is null"); } // 这里也使用DCL去cachedInstances缓存中取别名对应的值持有器,值持有器为空则创建 Holder<Object> objectHolder = cachedInstances.get(name); if (Objects.isNull(objectHolder)) { cachedInstances.putIfAbsent(name, new Holder<>()); objectHolder = cachedInstances.get(name); } Object value = objectHolder.getValue(); if (Objects.isNull(value)) { synchronized (cachedInstances) { // 加锁后再次判断值持有器中的值是否存在,不存在的时候则进行实现类实例化 value = objectHolder.getValue(); if (Objects.isNull(value)) { Holder<T> pair = createExtension(name); value = pair.getValue(); int order = pair.getOrder(); // 实例化完成后更新值持有器缓存 objectHolder.setValue(value); objectHolder.setOrder(order); } } } return (T) value;}// 基于别名搜索已经加载的实现类信息,并且实例化对应的实现类进行值包装private Holder<T> createExtension(final String name) { // 加载该@SPI标识接口的所有实现类信息并且获取对应别名的实现类信息 ClassEntity classEntity = getExtensionClassesEntity().get(name); if (Objects.isNull(classEntity)) { throw new IllegalArgumentException("name is error"); } Class<?> aClass = classEntity.getClazz(); // 如果实现类实例缓存中已经存在,则直接封装为值包装器返回,否则进行实例化 Object o = joinInstances.get(aClass); if (Objects.isNull(o)) { try { // 反射实例化并且缓存该实现类实例 joinInstances.putIfAbsent(aClass, aClass.newInstance()); o = joinInstances.get(aClass); } catch (InstantiationException | IllegalAccessException e) { throw new IllegalStateException("Extension instance(name: " + name + ", class: " + aClass + ") could not be instantiated: " + e.getMessage(), e); } } Holder<T> objectHolder = new Holder<>(); objectHolder.setOrder(classEntity.getOrder()); objectHolder.setValue((T) o); return objectHolder;}
As you can see from the 'createExtension()' method, we end up using reflection to instantiate the implementation class. The reflection method 'newInstance()' requires that the class must provide a no-argument constructor because of an implicit convention: The 'SPI' implementation class must provide a no-argument constructor or the instantiation will fail. The rest methods, such as 'getDefaultJoin()' and 'getJoins()' are uncomplicated extensions of 'getJoin()', so we won't analyze them here. In addition, the 'getJoin()' method uses a multilevel cache:
'cachedInstances' : Search for the corresponding implementation class instance by alias
'joinInstances' : If the alias lookup fails, load all the implementation class information, locate the implementation class type by the alias, and update the' cachedInstances' cache by either finding the implementation class type or creating and caching the implementation class instance
This completes the source code analysis of 'ExtensionLoader'. Here's another example of an 'ExtensionLoader' instance member property memory layout diagram to help you understand:
'ExtensionFactory' is the factory interface inside the factory pattern, which defines a method to get an instance of the 'SPI' implementation (the default implementation, or the only implementation) :
@SPI("spi")public interface ExtensionFactory { /** * Gets Extension. * * @param <T> the type parameter * @param key 此参数暂时没有使用,猜测是预留用于映射@SPI的value() * @param clazz @SPI标识的接口类型 * @return the extension */ <T> T getExtension(String key, Class<T> clazz);}
Let's look the class 'SpiExtensionFactory' :
@Joinpublic class SpiExtensionFactory implements ExtensionFactory { @Override public <T> T getExtension(final String key, final Class<T> clazz) { return Optional.ofNullable(clazz) // 入参clazz非空 .filter(Class::isInterface) // 入参clazz必须是接口 .filter(cls -> cls.isAnnotationPresent(SPI.class)) // 入参clazz必须被@SPI标识 .map(ExtensionLoader::getExtensionLoader) // 基于clazz这个接口类型实例化ExtensionLoader .map(ExtensionLoader::getDefaultJoin) // 获取该@SPI标识接口的默认实现,不存在则返回NULL .orElse(null); }}
It's worth noting here that the 'ExtensionFactory' itself is part of the 'SPI' system. So when using 'ExtensionFactory' you can instantiate it directly:
ExtensionFactory extensionFactory = new SpiExtensionFactory();
It can also be loaded based on an 'ExtensionLoader':
# the content of META-INF/services/shenyu/org.apache.shenyu.spi.ExtensionFactoryspi=org.apache.shenyu.spi.SpiExtensionFactory# then load it with ExtensionLoaderExtensionFactory extensionFactory = ExtensionLoader.getExtensionLoader(ExtensionFactory.class).getDefaultJoin();
Once you have an 'ExtensionFactory' instance, you can load an instance of its default implementation class based on the '@SPI' interface.
The 'SPI' extension framework based on the design idea of 'Java' native 'SPI' has the characteristics of loose coupling, high usability and high scalability, a loading instance cache system, concurrency security and other features to fill some defects of 'SPI' in the native 'JDK', Shenyu SPI module is the same. Base on this powerful 'SPI' module, other modules in 'Shenyu' such as the 'Plugin' module can be configured quickly and pluggable, making it easier to load a newly developed Plugin instance.