Getway中StripPrefix作用

    先说一下,我之前用的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 validateByUrl(Long userId, String authUrl) {
       logger.info("userId={},authUrl={}",userId,authUrl);
        return authService.queryAuthByUrl(userId,authUrl);
    }
    
}

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;
   }

可参考:

https://segmentfault.com/a/1190000015269873

你可能感兴趣的:(Gateway)