In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
In web development, with the update and iteration of the version, it is usually necessary to maintain multiple versions of api in the system, and multiple versions of api often have different data structures. Today, let's learn how the Scheme mechanism in kubernetes solves this problem and how to deserialize with the help of the data in the HTTP request.
1. Web request processing flow 1.1 HTTP request processing flow
Usually, webServer first processes the Http protocol, and then parses it into a Http request object within the underlying webServer, which usually holds the request header of the corresponding request and the underlying corresponding byte sequence (read from the socket stream)
Then, first of all, we usually deserialize according to the corresponding coding format, complete the mapping from the byte sequence to the business model of the current interface, and then give it to the business logic for processing, resulting in persistent storage. The focus of this article is also on the deserialization part.
two。 Implementation 2.1 of model mapping describes resource version information / api/ {version} / {resource} / {action}
The above is a basic web url. Usually we register a corresponding URL for each version, which contains two key pieces of information, namely version and resource. Through these two messages, we can usually know that this may be the version of a resource. If we package the later action, we can usually know the specific operation of the corresponding resource.
2.2 Group group information
Today, when micro-services are popular, we usually split micro-services according to business functions. In essence, a micro-service may be a collection of functions that implement a specific business scenario. For example, the user system usually contains all the related operations of the user. In kubernetes, there is a similar concept called Group.
POST / apis/batch/v1beta1/namespaces/ {namespace} / cronjobsPOST / apis/apps/v1/namespaces/ {namespace} / daemonsets
Let's take a look at an example. This is a url that creates daemonsets and cronjobs. If you split it according to Group, resource, and version, you can split it into the following: batch, v1beta1, cronjobs and apps, v1, daemonsets, that is, the GroupVersionKind you try, where kind corresponds to resource.
2.3 implementation of model mapping
We get the GroupVersionKind information of the resource through url. How can we map it to a specific type? This seems to be very simple, just combine reflection and map to do it. We get the corresponding GVK information through url, and then through our mapping table, we know which model the corresponding model is, and then we just need to transform it.
GvkToType map[schema.GroupVersionKind] reflect.Type3. Implementation of 3.1 Decoding Mechanism by deserialization
Then how to deserialize the data flow in the corresponding Http into an internal object? don't forget that it is the Http protocol, which must be the information in the header header. We can know the corresponding encoding format through the serialization in the header header. We only need to call the decoding of the corresponding format.
Content-Type: "application/json" 3.2 default object
If we want to decode a byte array in json format, we usually do the following. We need to pass in a pointer to the target object, and then the json parses the corresponding byte data into the target object. We also need an object to store the deserialized result.
Func Unmarshal (data [] byte, v interface {}) error {}
Is it OK if I provide another object constructor corresponding to the current version? The answer is yes.
Func () Object {return target object}, 4. Design summary
First of all, when registering with url, we construct the version information of the resource mapped by url, namely GroupVersionKind. For many subsequent operations, we can obtain the corresponding target operation or object through the corresponding version mapping, then obtain the corresponding decoder through the fields in Header, and decode the byte sequence in Body to the target object, so that we can realize the mapping and deserialization of multi-version resources.
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.