In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand decoupling in detailed software architecture". Interested friends may wish to take a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to understand decoupling in fine counting software architecture".
1. Definition of architecture
Architecture is the category of software methodology, which solves the problem of software organization and does not solve the problem of software algorithm. The difference between the two can be compared with the building blocks shown below:
The algorithm is like building blocks, such as green columns, blue triangles, red squares and so on. The architecture is to assemble all kinds of building blocks into a castle and a small train. To build the castle or train, the architect needs to have a drawing in mind that defines what kinds of building blocks are needed and how to assemble them. This job is very much like an architect, and it is indeed called architect in English.
With this analogy, it is easy to mislead entrepreneurs who do not understand technology and think that architects are better than algorithmic engineers. In fact, this is not the case, these are two sub-areas of talent. I wonder if you noticed the chimney on the front of the small train? It is a curved shape like Coprinus comatus. Watering the mold is much more difficult than triangles and squares. It needs more esoteric geometry support, which can be visually seen as a problem solved by algorithm engineers.
two。 Meaning of Architectur
Architecture solves the problem of software organization. What value can it create for the enterprise? In other words, what's the difference between a good software organization and a bad software organization in terms of business value creation? The author thinks that the value of architecture lies in usability and agility, but today we are talking about agility. Agility refers to the rapid, low-cost, high-quality response to the differentiated needs of the expanding market. Enterprises have accumulated a lot of software assets in the start-up period, and these assets have been demonstrated to achieve market performance in the original market environment. However, with the expansion of enterprises, the market will be more refined and scene-oriented, which will put forward new requirements for our software. Enterprises need to take advantage of the first-mover advantages accumulated in this field in previous years, on the one hand, to quickly occupy the subdivided market; on the other hand, reuse the accumulated assets and give full play to the economies of scale of the assets.
For example, JD.com e-commerce, starting from high-value, standardized 3C digital, established a proprietary e-commerce model; then began to expand products, do low-value, but high-frequency, still standardized daily department store users stickiness; then do relatively non-standard clothing to develop female users and ecological models, etc., directly to the key areas of industry competition In addition to the expansion, it is accompanied by scene expansion, such as 2B enterprise business, sinking market buying business, Thailand-Indonesia international business and so on. The category expansion from the supply point of view and the scene expansion from the demand angle constitute JD.com 's matrix vertical business line. It is precisely by reusing the software infrastructure of retail China Taiwan that they have expanded rapidly to some extent.
3. The soul of architecture
Since the value of a software organization is so important, what are the criteria for a good software organization? How do you do that? The standard of good or bad is decoupling. The opposite of decoupling is coupling, coupling refers to the dependence that hinders change; decoupling is to deal with possible changes on the basis of dependence. Dependence is essential, and the essence of dependence is division of labor. As Adam Smith's Theory of Wealth of Nations discusses, division of labor contributes to specialization and efficiency. It's too abstract! Having said so much, it is not clear what decoupling is. Indeed, the author also believes that such an explanation can only allow people who have already understood to agree again, and can not make people who do not understand become understandable. This is meaningless! How should I interpret it? In fact, many truths are based on induction. The advantage of induction is that it can be seen naturally (induction seems to be an instinct of the human brain). For example, as long as you are familiar with 300 Tang poems, you can recite them even if you don't know how to recite them. If you don't believe me, there is no idea to start with an article called "solitary smoke in the Desert". If you read another article called "Qingshan is particularly clear after a new rain," you will feel a little bit. Finally, you will know "small bridge and flowing water's family" yourself. How to write a poem with artistic conception? when you open your mouth, you come to "there is bright moonlight in front of the bed". You didn't write it yourself. If you go to the prairie to touch the scene at night, an impromptu "Mingyue bonfire roast fat sheep" will be comparable to "purple smoke from the incense burner peak illuminated by the sun". So the author thinks that the best way is to count the decoupling in those software architectures, so that readers can find their own feelings from the laid-out examples.
The author is divided into 3 categories and 6 groups (each category is divided into intra-process application layer and inter-process architecture layer) to give you an example:
Plus the middle Naming resolution and Proxy agent fusion CNAME alias, a total of 7 cases.
Middle layer mapping
The design idea of middle-tier mapping is that when An is dependent on B, A does not directly rely on B, but abstracts an intermediate layer, making A rely on the middle layer, and then mapping from the middle layer to B, so that when B becomes C, there is no need to modify A. just adjust the mapping relationship of the middle layer. The middle layer mapping is interface-oriented dynamic binding in the application layer and Naming parsing dynamic binding in the architecture layer.
Application layer-interface-oriented dynamic binding
The core idea of interface-oriented programming is "figure out what to do first, and then who you want to do it." What do you mean, what do you do when you figure it out? The interface is used to describe what is input and what is output, but the interface describes more about the syntax level, and the semantic level description needs to be combined with unit tests and their assertions (technically called Test Driven), as well as documentation. This is similar to the idea of "beginning with the end" in the seven habits of highly effective people that entrepreneurs often read. Who will do it? It involves runtime dynamic binding. For example, the following figure:
In Java object-oriented language, users depict its bidding documents through Provider interface Response doService (Request r). Then three suppliers, LocalProvider, RemoteProvider and AsyncProvider, respond to the bid. The user only uses the Provider interface, and as to which specific Provider it binds to, it can be replaced dynamically at the time of "purchase".
The decoupling of interface-oriented dynamic binding is reflected in that the user abstracts the dependent service into an interface and relies on the abstract interface, rather than on the specific service provider, in order to cope with the possibility of service provider change.
Architecture layer-Naming parses dynamic binding
The figure above shows the schematic process of domain name service DNS. The client does not access Provider#An or B directly through the IP address, but first asks the Naming service, and then accesses Provider#An or B based on the list of services returned. If a Provider fails, it can be replaced and transferred to another Provider. For performance reasons, the results of Naming can also be cached on the client side and equipped with a cache update mechanism.
The application layer name service based on ZooKeeper is similar to DNS in thought. The difference is that it implements Server Push based on TCP long links and refreshes the list of services in a timely manner.
The decoupling of Naming parsing dynamic binding is reflected in that the user abstracts the dependent object or network process as a name, and the specific service provider represented by the name is returned through the Lookup mechanism, so that if the provider changes, as long as the result of the Lookup is changed, there is no need to change the user code.
Anterior and posterior segment implantation
The design concept implanted in the front and back section is that the server is the collection of processes, and the process is the sequence of links. The behavior of changing a process can be achieved by inserting a new link before and after it. The front and back sections are implanted in Chain interception mode in the application layer and Proxy proxy mode in the architecture layer.
Application layer-Chain intercept mode
The above figure shows the architecture of Strtus2. The execution of each Action is wrapped in a series of Interceptor to form a processing chain Chain, and each Interceptor is processed by PreHandler and PostHandler. The Interceptor here can be added, deleted, or replaced to achieve scalability. For example, you can do authentication, log, performance statistics, current limit and so on in Interceptor.
The dynamic binding of Chain plug and unplug transforms the past 1:1 processing relationship between URL and Action into the processing chain of URL N by adding, deleting and replacing MRV. A class of requests (a URL) that can be processed by multiple Interceptor; a single Interceptor can also handle multiple types of requests.
By the way, what Strtus2 calls "dynamic binding" here is configured relative to hard coding. Strictly speaking, the binding here is compile-time, not run-time, but static. Similar architectures include Spring AOP and Servlet Filters mechanisms.
Architecture layer-Proxy proxy pattern
The figure above is a Proxy architecture pattern, which is widely used. Such as HTTP's Nginx,SQL 's Apache Calcite,memcached and redis's twitter/twemproxy. Why? Because Proxy is the traffic entry and middleman for Backend, it can act as the AOP mechanism at the architecture level, and its scalability is very strong.
When a request comes in, Proxy forwards it to Backend#An at first. But with the development of business, Proxy can also be forwarded to Backend#B to achieve load balancing, and more importantly, An and B can also be different versions to achieve grayscale release. You can also implant PrePlugin and PostPlugin:
In PrePlugin, you can do permission control, flow control, request rewriting, cache acceleration, malicious traffic interception, PV statistics, performance Profile, ChaosMonkey chaotic event implantation and so on.
In PostPlugin, you can also do response message rewriting, secure encryption (the back end does not have to consider data security, unified encryption processing), compression acceleration, and so on.
An example of the fusion of the two-CNAME alias
The figure above is a mixed mode: both Naming parsing and Proxy proxies. And Naming services, in order to support extensibility, also introduced a parent-child level, the end of the Naming service, can be delegated to the next level of Naming service.
In DNS, we often see the domain name resolution of www.example.org and the alias of CNAME to www.example.org.cdnprovider.com (which is a subdomain name of cdnprovider.com), so that the client still accesses www.example.org without modification, but the corresponding back-end service no longer accesses Provider#An or B directly, but CNAME Proxy is implanted in the middle, and the Proxy forwards it to ask Provider#An or B according to the decision of Plugin.
This design is amazing! It enables the commercial company cdnprovider.com to provide CDN services to www.example.org without any intrusions. It only needs to modify the domain name resolution of www.example.org at the domain name service provider. This operation represents that www.example.org agrees that cdnprovider.com provides CDN services for them, representing authorization. All this stems from the decoupling of dynamic binding based on Naming parsing. Similarly, in addition to CDN, our malicious traffic cleaning, grayscale release, performance analysis and so on can all use this way to achieve zero intrusive plug and plug.
4. Event stream subscription
The design idea of event stream subscription is to transform instantaneous procedural calls into playable instructions, and the response to instructions can no longer be predefined. Event flow subscriptions are represented as Mediator mediation patterns in the application layer and Broker message patterns in the architecture layer.
Application layer-Mediator Mediation Model
A calls B directly, which means that A has a strong dependence on B. Of course, we can reduce this dependency by interface-oriented programming to rely only on the interface and not on the implementation. To put it simply, we only rely on the result of the matter, not on how to achieve it.
But that's not enough, because we also rely on interfaces, which mean a description of processing semantics. In reality, even the semantic description will change, that is, the interface will change, how to further decouple? As shown below:
A does not call B directly, but decouples in two steps through mediation Mediator:
First, A calls Mediator: a holds the reference to Mediator and executes the method of Mediator, which is mediator.publish (e).
Then Mediator calls B: in order to decouple Mediator's dependence on the outside world, we use interface-oriented EventHandler to implement dependency inversion. Let B implement EventHandler, of course, if B already exists, or has more say, it should still follow the principle of dependency inversion, but the promoter of Mediator mode can implement another Adaptor to help the existing B adapt to EventHandler.
With the above design patterns, the specific implementation is divided into three steps:
Subscriptions: register future event handling with Mediator in advance through mediator.subscribe (b).
Release: a publishes its own events to Mediator. Note that this idea is particularly important. An only publishes what happened, and A does not directly call B to declare the handling of the matter. That is, An is no longer dependent on B's interface! For example, for new employees, at the beginning, they have to apply for magnetic stripe cards, but the supplier who applies for magnetic stripe cards may be An or B. This is called interface-oriented programming, but it is not enough, because with the development of the company, new employees now have face recognition, there is no need to apply for magnetic stripe cards, but to register for face recognition, and employee benefits are even better. new employees on business trips will also be paid a settlement fee, which is not described in the previous "interface".
Execution: when mediator receives an event from A (A calls mediator.publish (e)), mediator calls back and forth the processing class that is pre-registered through mediator.subscribe (b) through EventHandler.
The above Mediator has some limitations, for all Event, there can only be one kind of EventHandler. If we upgrade Mediator to a general processing mechanism, a platform, there will naturally be a variety of Event, and we will naturally classify or group Event. We call the classification or grouping of Event as Topic; and understand Event as a concrete instance in the class Topic. And maintains a set of processors from Topic to EventHandler in Mediator. As shown in the following figure:
You can see that the above architecture maintains a set of processes from Topic to EventHandler through Map resolver. Why List and not EventHandler? In order to be more flexible, for example, as mentioned above, "now that new employees are employed, there is face recognition, there is no need to apply for magnetic stripe cards, but to register for face recognition, in addition, employee benefits are better. A settlement fee will also be paid to new employees who are on business in other places."
Architecture layer-Broker message pattern
The Broker pattern in the figure above is not fundamentally different from the Mediator pattern, but Broker highlights the implementation of asynchronism with the help of message middleware MQ. The client submits a delegate, and the Broker persistence completes, and replies to ACK, indicating that the delegate has been received. Then the entrusted consumption processing can be offline. It is usually necessary to support the Group mechanism: multiple Instance within the Group are load balanced, and they share the processing of delegated messages together; while the Group are redundant and replicated, and they consume their own and are isolated from each other, which helps to achieve business scalability.
For example, when a new employee enters the job, it produces a "new employee entry" event, and then the administrative department will prepare work card for it, the finance department will prepare a wage card for it, and the HR department will pay social security for it. Of course, as the company's business develops, it may increase, such as business training for business units, compliance training for risk control departments, and so on.
Similar to the Proxy patterns mentioned earlier, they all achieve extensibility at the architectural level. The difference is that Proxy mode supports PreHandler and PostHandler; while Broker mode supports MidHandler.
At this point, I believe you have a deeper understanding of "how to understand decoupling in detailed software architecture". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.