parent
c8fc5e389c
commit
e36e635db7
@ -0,0 +1,35 @@
|
||||
package com.github.cloud.netflix.zuul.filters;
|
||||
|
||||
/**
|
||||
* 路由
|
||||
* @author derek(易仁川)
|
||||
* @date 2022/6/28
|
||||
*/
|
||||
public class Route {
|
||||
|
||||
private String path;
|
||||
|
||||
private String location;
|
||||
|
||||
public Route(String path, String location) {
|
||||
this.path = path;
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
public String getLocation() {
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(String location) {
|
||||
this.location = location;
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
package com.github.cloud.netflix.zuul.filters;
|
||||
|
||||
/**
|
||||
* 路由定位器
|
||||
* @author derek(易仁川)
|
||||
* @date 2022/6/28
|
||||
*/
|
||||
public interface RouteLocator {
|
||||
|
||||
/**
|
||||
* 获取匹配的路由
|
||||
* @param path
|
||||
* @return
|
||||
*/
|
||||
Route getMatchingRoute(String path);
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.github.cloud.netflix.zuul.filters;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
|
||||
/**
|
||||
* 路由定位器实现类
|
||||
* @author derek(易仁川)
|
||||
* @date 2022/6/28
|
||||
*/
|
||||
public class SimpleRouteLocator implements RouteLocator {
|
||||
|
||||
private ZuulProperties zuulProperties;
|
||||
|
||||
private PathMatcher pathMatcher = new AntPathMatcher();
|
||||
|
||||
public SimpleRouteLocator(ZuulProperties zuulProperties) {
|
||||
this.zuulProperties = zuulProperties;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Route getMatchingRoute(String path) {
|
||||
for (Map.Entry<String, ZuulProperties.ZuulRoute> entry : zuulProperties.getRoutes().entrySet()) {
|
||||
ZuulProperties.ZuulRoute zuulRoute = entry.getValue();
|
||||
String pattern = zuulRoute.getPath();
|
||||
if (pathMatcher.match(pattern, path)) {
|
||||
return new Route(path, zuulRoute.getServiceId());
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue