先说一下,我之前用的zuul,配置如下,通过url调用接口,如果路径以/payment/auth/**开头,则找到serviceid: payment-auth,再通过负载均衡调用auth服务,且controller中地址必须以/payment/auth/开头,才能找到接口。
现在用到getway,如2, 首先配置转发路径:path= /anji-open/user/**,但是controller中并没有以/anji-open/user/**开头,这样能找到具体接口吗,是可以的,原理是通过这个配置实现:
filters:
- stripprefix=2,
转发到具体服务,自动去掉/anji-open/user,如下
原始访问路径:
http://uat-open-external-146479425.cn-north-1.elb.amazonaws.com.cn/anji-open/user/jobuser/pagelist?sort=id&order=desc&pagenumber=1&pagesize=10
处理后路径:
http://uat-open-external-146479425.cn-north-1.elb.amazonaws.com.cn/jobuser/pagelist?sort=id&order=desc&pagenumber=1&pagesize=10
所以可以找到以controller中/jobuser开头的接口。
1,zuul配置及controller映射路径
paymentauth:
path: /payment/auth/**
stripprefix: false
serviceid: payment-auth
@restcontroller
@requestmapping("/archives/auth")
public class authcontroller {
private logger logger = logmanager.getlogger(authcontroller.class);
@autowired
private authservice authservice;
/**
* 用户提供给网关进行鉴权服务
* @param userid
* @param authurl
* @return
*/
@requestmapping("/validate/url")
public boolean validateby {
logger.info("userid={},authurl={}",userid,authurl);
return authservice.queryauthby;
}
}
2,getway配置及controller路径映射
gateway:
routes:
- id: user-center
uri: lb://open-user-test
predicates:
- path= /anji-open/user/**
filters:
- stripprefix=2
@restcontroller
@requestmapping("/jobuser")
public class jobusercontroller {
@requestmapping(value = "/pagelist",method = requestmethod.post)
public responsebean> pagelist(jobuserparam param) {
list userroles = jobuserservice.getuserroles(cookieutil.getloginuser());
if(userroles.contains(apiconstants.role_admin)) {
param.setadminrole(apiconstants.role_admin);
}
pageinfo jobuserpageinfo = jobuserservice.pagelist(param);
responsebean> responsebean = new responsebean<>(jobuserpageinfo);
return responsebean;
}