Recently, the department is going to develop a simple APP, and some data are already existing in existing projects. In order to facilitate maintenance, we hope to provide only one interactive entry, and the rules of the protocol remain unchanged.
Based on this requirement, there are two solutions:
1. Encapsulate the existing api with code, do not make any changes to the request data and return data, just transfer, and then deploy it in a project together with the new data interface;
2. Use IIS to forward requests, call existing interfaces to respond to requests, develop new APIs for the rest, deploy them in a project, and use URL Rewrite for filtering and distribution.
The first solution is very traditional, and there is nothing to evaluate. Here we mainly talk about the implementation of the second solution. The advantage of the second solution is that it can save time and cost, and it needs to rely on the IIS plug-in (Application Request Routing + URL Rewrite).
First download ARR and URL Rewrite for installation. During use, it is found that ARR depends on the "directory browsing" function of IIS (not verified, if it cannot be used, you can check whether the "directory browsing" function is installed):
http://www.iis.net/downloads/microsoft/application-request-routing#additionalDownloads
http://www.iis.net/downloads/microsoft/url-rewrite#additionalDownloads
Install the plugin and reopen IIS
Double-click the IIS root directory
双击Application Request Routing Cache
Double-click on Server Proxy Settings on the right
勾上 Enable proxy
After clicking "Apply", create a new site to accept requests for forwarding
Double-click the site, double-click URL Rewrite -> Add Rules -> Blank rule
Name: fill in your rule name
Match URL is the rule that matches the Requested URL
http://www.test.com?name=michael&age=30
host: www.test.com
requested url: ?name=michael&age=30
query string: name=michael&age=30
Requested URL select Matches the Pattern (match urls that match the rules)
Using Select Regular Expressions (use regular expressions to match)
Fill in ^(.*) in Pattern. Regular expressions are not explained here, you can understand them yourself if you need them.
Check Ignore case to ignore case
Expand the Conditions filter
Logical grouping 选择 Match Any
Match All is that all rules in the list must be matched to match (and)
Match Any is a rule in the list that matches (or)
track capture group across conditions Track capture group, this function is related to regularity, you don't need to uncheck it here, you can query the keyword capture group for details
Click Add to add conditions
Fill in {HTTP_HOST} in Condition input, HTTP_HOST represents the host in the request header, which is the www.test.com part in the above example. For more filter conditions, query Server Variables to understand by yourself
Check if input string 选择 Matches the Pattern
Fill in ^arrtest.com$ for Pattern, which means that if the host is arrtest.com, the match is passed, for example: http://arrtest.com?asdf=1234
If ^www.arrtest.com$ is filled in here, it will match http://www.arrtest.com?asdf=1234
Check Ignore case to ignore case
Double click to expand the Action section
Action type Select Rewrite to rewrite and forward
Fill in https://cn.bing.com/{R:1} as the forwarding destination address in the Rewrite URL, {R:1} represents the Request URL matched by the Match URL part
Check Append query string to append the query string
At the end of this configuration, save this rule and visit http://arrtest.com/search?q=test in the browser is equivalent to visiting https://cn.bing.com/search?q=test
In order to prevent other interfaces under the site from being forwarded by this rule mindlessly, we need to add a forwarding condition
The existing API formats that need to be forwarded are as follows http://arrtest.com?PROTOID=123456
Other interfaces do not have the keyword PROTOID, and the values after PROTOID are all numbers, so this keyword is used here to filter the requests that need to be forwarded
Go back to the Conditions section just now, click Add to add a new condition
Condition input Fill in {QUERY_STRING}
Check if input string 选择 Matches the Pattern
Filling in the pattern PROTOID=\d+ means that the matching query string starts with PROTOID and the parameter value is a number (for example: http://arrtest.com/?PROTOID=456789)
Check Ignore case to ignore case
OK to save, modify the matching logic to Match All (and), all the rules in the list match, the request will pass
Now only requests whose QueryString starts with a PROTOID parameter value will be forwarded
Example: http://arrtest.com/search?PROTOID=4564&q=test => http://cn.bing.com/search?PROTOID=4564&q=test
http://arrtest.com/search?q=test&PROTOID=4564 will not be forwarded
At this point, the function of request forwarding has been realized. In addition, the powerful ARR + URL Rewrite can also achieve high-availability load balancing.