Microservice combat series (8)-gateway springcloud gateway custom rules

1. Scene description

First explain that the gateway used in the next project is: springcloud gateway, because it needs to provide custom configuration routing rules for each gateway service system, it takes effect in real time, without restarting the gateway (restart risk is high), and it has been implemented: dynamic loading of custom routing files, Dynamically load the routing rules in the routing file.

2. Solution

2.1 Solutions

Create a new general monitoring category, monitor the gateway service routing rule configuration file, and then each routing configuration file monitors whether the content of its own file content changes, and dynamically loads it into the springcloud gateway.

2.2 Description of configuration rules

bc-ruanjianlaowang-all.yaml is the name of the configuration file that needs to be loaded. It is created with monitoring. When a new custom routing rule file is added, the custom rule will be automatically refreshed.

gateways: bc-ruanjianlaowang-demo.json,bc-ruanjianlaowang-auth.json

Create a custom routing rule file. At present, two new gateway service files have been created, bc-ruanjianlaowang-demo.json and bc-ruanjianlaowang-auth.json. You only need to configure custom rules. There will be other gateway services in the future. Add a custom rule json file, and then configure the file name in bc-ruanjianlaowang-all.yaml. It will automatically load and take effect in real time without restarting the gateway.

2.2.1 Rule example description
[
    {
       "id": "demo-router3","uri":"lb://demo","order": 0,
       "filters": [{"name":"StripPrefix","args":{"_genkey_0":"1"}}],
       "predicates": 
       [{"args": {"pattern":"/demo/test/v?1"},"name":"Path"}]
    } 
]

Description:

(1) "id": "demo-router3", try to use the center name + router + serial number as the id, and it must be unique;

(2) "uri": "lb://demo", here can be the service name of other centers in the registration center, if there are multiple services, the default load balance, polling access to the corresponding service provided later; also possible Configuration: "uri": "http://10.192.168.10.:8010/", this configuration is for the old service not registered in the registry, and the path needs to be configured.

(3) "filters": [{"name":"StripPrefix","args":{"_genkey_0":"1"}}], the meaning here is that removing the first layer path by default is equivalent to removing the demo for access

(4) [{"args": {"pattern":"/demo/test/v?1"},"name":"Path"}] Here are the specific custom configuration rules, follow the rules below Configuration.

2.2.2 Description of path customization rules

Because springcloud gateway uses spring's AntPathMatcher for path matching, simply copy the custom configuration rules to explain, or you can directly look at it on Baidu.

(1) URLs can be matched, the rules are as follows

? Match a character

*Match 0 or more characters

**Match 0 or more directories

如果你觉得文章对你有些帮助,欢迎微信搜索「软件老王」第一时间阅读或交流!

(2) The use case is as follows

/trip/api/*x       匹配 /trip/api/x,/trip/api/ax,/trip/api/abx ;但不匹配 /trip/abc/x;
/trip/a/a?x        匹配 /trip/a/abx;但不匹配 /trip/a/ax,/trip/a/abcx
/**/api/alie       匹配 /trip/api/alie,/trip/dax/api/alie;但不匹配 /trip/a/api
/**/*.htmlm        匹配所有以.htmlm结尾的路径

/app/*.x	         匹配(Matches)所有在app路径下的.x文件
/app/p?ttern	     匹配(Matches) /app/pattern 和 /app/pXttern,但是不包括/app/pttern
/**/example	       匹配(Matches) /app/example, /app/foo/example, 和 /example
/app/**/dir/file.  匹配(Matches) /app/dir/file.jsp,    /app/foo/dir/file.html,/app/foo
									/bar/dir/file.pdf, 和 /app/dir/file.java
/**/*.jsp	    匹配(Matches)任何的.jsp 文件
i'm 软件老王 

(3) In addition

Springcloud or spring is not very good for path matching and regular support, but it can also be used, just take it briefly.

符号 {spring:[a-z]+}
示例代码:
@RequestMapping("/index/{username:[a-b]+}")
@ResponseBody
public String index(@PathVariable("username") String username){
    System.out.println(username);
    return username;
}
结果:

index/ab        true  输出 ab
index/abbaaa    true  输出 abbaaa
index/a         false 404错误
index/ac        false 404错误 

The original code of the gateway is to open all services by default. After the new code, it will follow the custom rules. If it is not configured, the jump will be prohibited.


For more knowledge, please pay attention to the public account: "Software King" , share IT technology and related dry goods, reply to keywords to get the corresponding dry goods, java , send 10 must-see "Martial Arts Cheats"; pictures , send more than 1 million copies for commercial use High-definition pictures; interview , send java interview questions with a monthly salary of "20k" just after graduation, soft test , send official pdf books and customs clearance papers, follow-up will continue to update, such as " tools ", " videos ", etc., have been sorted out.
Insert picture description here

Guess you like

Origin blog.csdn.net/wjg8209/article/details/108851758