In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you what the working principle of the Gateway gateway is, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
1. What is API Gateway (API Gateway) distributed Services Architecture, Micro Services Architecture and API Gateway
In the micro-service architecture, the granularity of services is further subdivided, and each business service can be designed, developed, tested, deployed and managed independently. At this time, each independent deployment unit can be maintained by different development and test teams, and can be designed with different programming languages and technical platforms. this requires that a language and platform-independent service protocol must be used as a way of communication between units.
Definition of API Gateway
The role of the gateway is to act as an API architecture to protect, enhance, and control access to API services.
API gateway is a system in front of applications or services (providing REST API interface services), which is used to manage authorization, access control and traffic restrictions, so that REST API interface services are protected by API gateways and transparent to all callers. As a result, business systems hidden behind API gateways can focus on creating and managing services without having to deal with these strategic infrastructure.
Functions of API Gateway
Classification and function of API Gateway
2. What is Gateway?
Spring Cloud Gateway is the gateway officially developed by Spring based on Spring 5.0 Spring Boot 2.0 and Project Reactor technologies. Spring Cloud Gateway aims to provide a simple and effective unified API route management method for micro-service architecture. As a gateway in Spring Cloud ecosystem, Spring Cloud Gateway aims to replace ZUUL. It not only provides unified routing, but also provides basic functions of gateway based on Filter chain, such as security, monitoring / burying point, and current restriction.
3. Why use Gateway?
Spring Cloud Gateway can be seen as an upgrade and replacement of Zuul 1.x, which uses Netty to implement asynchronous IO earlier than Zuul 2, thus implementing a simple, more efficient API gateway that works closely with Spring Cloud than Zuul 1.x.
There is a clear distinction between Router and Filter in Spring Cloud Gateway, and a big feature is that there are a lot of out-of-the-box features that can be used through SpringBoot configuration or hand-coded chain calls.
For example, there are 10 kinds of Router built in, so that we can configure them directly and do routing according to Header, or Path, or Host, or Query.
For example, the distinction between general Filter and global Filter, built-in 20 kinds of Filter and 9 kinds of global Filter, can also be used directly. Of course, customizing Filter is also very convenient.
The most important concepts
4. How to use Gateway
To put it bluntly, the purpose of Predicate is to implement a set of matching rules so that the request can come and find the corresponding Route for processing. Next, we will take over the use of several Predicate built into Spring Cloud GateWay.
By time matching
Predicate supports setting a time that can be forwarded before or after the request is forwarded. For example, we now set it to be forwarded to my website only on January 1, 2019. If we don't forward it before then, I can configure it like this:
Spring: cloud: gateway: routes:-id: time_route uri: http://ityouknow.com predicates:-After=2018-01-20T06:06:06+08:00 [Asia/Shanghai]
Spring compares time through ZonedDateTime. ZonedDateTime is a class used to represent date and time information with time zone in Java 8. ZonedDateTime supports setting time through time zone. The time zone in China is Asia/Shanghai.
After Route Predicate means that requests after this time are forwarded to the destination address. The above example means that all requests after 06:06:06 on January 20, 2018 are forwarded to the address http://ityouknow.com. + 08:00 means that there is an eight-hour difference between time and UTC time, and the time area is Asia/Shanghai.
After the routing rule is added, the access address http://localhost:8080 is automatically forwarded to http://ityouknow.com.
Before Route Predicate, on the contrary, forwards requests that are requested before a certain time. Let's change the After in the routing rule above to Before, as follows:
Spring: cloud: gateway: routes:-id: after_route uri: http://ityouknow.com predicates:-Before=2018-01-20T06:06:06+08:00 [Asia/Shanghai]
It means that routing can be carried out before this time, stop routing after this time, and restart the project to visit the address http://localhost:8080 again after modification. The page will report that no address was found.
In addition to before or after time, Gateway also supports limiting routing requests to a certain time range, which can be implemented using Between Route Predicate.
Spring: cloud: gateway: routes:-id: after_route uri: http://ityouknow.com predicates:-Between=2018-01-20T06:06:06+08:00 [Asia/Shanghai], 2019-01-20T06:06:06+08:00 [Asia/Shanghai]
This setting means that the route can be matched within this time period, but not beyond this time period. The ability to match routing through time is cool and can be used in time-limited panic buying scenarios.
Match through Cookie
Cookie Route Predicate can receive two parameters, one is Cookie name and the other is regular expression. Routing rules will match by obtaining the corresponding Cookie name value and regular expression. If there is a match, the route will be executed, and if there is no match, it will not be executed.
Spring: cloud: gateway: routes:-id: cookie_route uri: http://ityouknow.com predicates:-Cookie=ityouknow, kee.e
Using the curl test, enter on the command line:
Curl http://localhost:8080-cookie "ityouknow=kee.e"
The page code will be returned. If-cookie "ityouknow=kee.e" is removed, the background will report a 404 error.
Header Route Predicate, like Cookie Route Predicate, receives two parameters, an attribute name in header and a regular expression, which is executed if the attribute value matches the regular expression.
Spring: cloud: gateway: routes:-id: header_route uri: http://ityouknow.com predicates:-Header=X-Request-Id,\ d+
Using the curl test, enter on the command line:
Curl http://localhost:8080-H "X-Request-Id:666666"
The page code is returned to prove that the match is successful. Change the parameter-H "X-Request-Id:666666" to-H "X-Request-Id:neo" when executed again and return 404 to prove that there is no match.
Match through Host
Host Route Predicate receives a set of parameters, a list of matching domain names, this template is an ant-separated template, with. Number as a delimiter. It uses the host address in the parameter as the matching rule.
Spring: cloud: gateway: routes:-id: host_route uri: http://ityouknow.com predicates:-Host=**.ityouknow.com
Using the curl test, enter on the command line:
Curl http://localhost:8080-H "Host: www.ityouknow.com" curl http://localhost:8080-H "Host: md.ityouknow.com"
After testing, both of the above two host can match to the host_route route, and if the host parameter is removed, a 404 error will be reported.
Match by request
It can be routed through different request methods such as POST, GET, PUT, DELETE, and so on.
Spring: cloud: gateway: routes:-id: method_route uri: http://ityouknow.com predicates:-Method=GET
Using the curl test, enter on the command line:
# curl defaults to requesting curl http://localhost:8080 by GET
The test returns the page code to prove that it matches the route, and then we request the test in the form of POST.
# curl defaults to requesting curl-X POST http://localhost:8080 by GET
Return 404 is not found, which proves that there is no matching route.
Match by request path
Path Route Predicate receives a parameter that matches the path to determine whether to walk or not.
Spring: cloud: gateway: routes:-id: host_route uri: http://ityouknow.com predicates:-Path=/foo/ {segment}
If the request path meets the requirements, the route will match, for example: / foo/1 or / foo/bar.
Using the curl test, enter on the command line:
Curl http://localhost:8080/foo/1curl http://localhost:8080/foo/xxcurl http://localhost:8080/boo/xx
After testing, the first and second commands can get the return value of the page normally, and the last command reports 404, which proves that the route is matched by the specified route.
Match through request parameters
Query Route Predicate supports passing in two parameters, one is the attribute name and the other is the attribute value, which can be a regular expression.
Spring: cloud: gateway: routes:-id: query_route uri: http://ityouknow.com predicates:-Query=smile
In this configuration, the route can be matched as long as the request contains the parameter of the smile attribute.
Using the curl test, enter on the command line:
Curl localhost:8080?smile=x&id=2
After testing, it is found that the route will be matched as long as the request summary has the smile parameter, and it will not match without the smile parameter.
You can also configure the value of Query as a key-value pair, so that the attribute value and regularity will be matched when the request comes over.
Spring: cloud: gateway: routes:-id: query_route uri: http://ityouknow.com predicates:-Query=keep, pu.
This will be matched and routed only if the request contains the keep attribute and the parameter value is a three-digit string that begins with pu.
Using the curl test, enter on the command line:
Curl localhost:8080?keep=pub
The test can return the page code and change the property value of keep to pubx and it will report 404 when it is accessed again, proving that the route needs to match the regular expression to route.
Match by requesting an ip address
Predicate also supports routing requests by setting an ip interval number range. RemoteAddr Route Predicate accepts a list of cidr symbol (IPv4 or IPv6) strings (the minimum size is 1), such as 192.168.0.1 Universe 16 (where 192.168.0.1 is the IP address and 16 is the subnet mask).
Spring: cloud: gateway: routes:-id: remoteaddr_route uri: http://ityouknow.com predicates:-RemoteAddr=192.168.1.1/24
You can set this address to the native ip address for testing.
If the requested remote address is 192.168.1.10, this route will match.
Combined use
In order to demonstrate the use of each Predicate above, we do configuration testing individually, but we can actually use various Predicate together.
For example:
Spring: cloud: gateway: routes:-id: host_foo_path_headers_to_httpbin uri: http://ityouknow.com predicates:-Host=**.foo.org-Path=/headers-Method=GET-Header=X-Request-Id,\ d +-Query=foo, ba. -Query=baz-Cookie=chocolate, ch.p-After=2018-01-20T06:06:06+08:00 [Asia/Shanghai]
When all kinds of Predicates exist on the same route at the same time, the request must meet all the conditions at the same time before it can be matched by this route.
When a request satisfies the predicate condition of multiple routes, the request will only be forwarded by the first successfully matched route.
These are all the contents of the article "how does the Gateway Gateway work?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.