Gateway Services Custom routing rules (springcloud + nacos)

1. Scene Description

Need to give each class a gateway service to provide custom configuration of routing rules, with immediate effect, without restarting the gateway (restart risky), has implemented dynamic load custom file routing, dynamic routing rule to load the file, simply rule file configure rules on it

2. Solution

2.1 Solutions

New general category of total surveillance, monitoring service gateway routing rule configuration file, then each routing configuration file and then monitor the content of the file contents themselves whether changes dynamically loaded into the springclou gateway.

2.2 configuration rules Description

bc-ruanjianlaowang-all.yaml is the configuration file name to load, there is listening created when adding a new custom routing rules file, it will automatically go to brush custom rules.

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

Create custom routing rules file, the file has already built two gateway services, bc-ruanjianlaowang-demo.json, bc-ruanjianlaowang-auth.json, simply configure custom rules can be, there are other gateways follow-up service is available new custom rules json file and configuration file name in the bc-ruanjianlaowang-all.yaml, do not restart the gateway, it will automatically take effect in real-time dynamic loading.

2.2.1 Rules illustration
[
    {
       "id": "demo-router3","uri":"lb://demo","order": 0,
       "filters": [{"name":"StripPrefix","args":{"_genkey_0":"1"}}],
       "predicates": 
       [{"args": {"pattern":"/demo/test/v?1"},"name":"Path"}]
    } 
]
i'm 软件老王 

Description:

(1) "id": "demo-router3", id far as possible with the present Center Name + router + ID to be unique;

(2) "uri": "lb: // demo", there may be other centers in the registry service name, if there is more than one service, the default load balancing, polling access the corresponding service provided by the back; this can be configuration: "uri": "http://10.192.168.10.:8010/", such a configuration is not registered in the registry for the old service, you need to configure the path.

(3) "filters": [{ "name": "StripPrefix", "args": { "_ genkey_0": "1"}}], where the meaning is the default path is equivalent to removing the first layer to remove access demo

(4) [{ "args": { "pattern": "/ demo / test / v 1?"}, "Name": "Path"}], this is specific custom configuration rules, and conducted in accordance with the following rules configuration.

2.2.2 Path Custom Rules Help

Because springcloud gateway using a spring path of AntPathMatcher be matched, simple to customize the configuration rules copied instructions can also be directly Baidu look.

(1) can be done URLs matching rule is as follows

? Matches one character

* Matches zero or more characters

** matches zero or more directories

(2) using for example

/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) Further Description

springcloud or spring path match for the regular support is not very good, but can also be used, simply took a look at it.

符号 {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错误
i'm 软件老王 

Gateway source code is open all the default service, the new code will be in accordance with the custom rules, if not configured, will prohibit jump.


I'm "Software Pharaoh," I felt that if I may, to focus the next chant, subsequent updates seconds to know! Welcome forum, No. namesake public message exchange!

Guess you like

Origin www.cnblogs.com/ruanjianlaowang/p/11713655.html