其他分享
首页 > 其他分享> > 网关_zuul_Route三种过滤器使用方式

网关_zuul_Route三种过滤器使用方式

作者:互联网

1、网关通过url到服务

  

 1 @Override
 2     public Object run() throws ZuulException {
 3         RequestContext currentContext = RequestContext.getCurrentContext();
 4         HttpServletRequest request = currentContext.getRequest();
 5         //获取访问地址
 6         String remoteAddr = request.getRequestURI();
 7 
 8         //和老地址做匹配
 9         if(remoteAddr.contains("/send/sms-test31")){
10             //服务具体路径
11             currentContext.set(FilterConstants.REQUEST_URI_KEY,"/send/sms-test3");
12             //服务实例
13             currentContext.set(FilterConstants.SERVICE_ID_KEY,"service-sms");
14         }
15 
16 
17         return null;
18     }

 

2、网关访问具体url服务

访问指定url跳转具体的后端服务

@Override
    public Object run() throws ZuulException {
        RequestContext currentContext = RequestContext.getCurrentContext();
        HttpServletRequest request = currentContext.getRequest();
        //获取访问地址
        String remoteAddr = request.getRequestURI();
        //和老地址做匹配
        if(remoteAddr.contains("/zuul-test")){
            currentContext.set(FilterConstants.SERVICE_ID_KEY,"service-sms");
            try{
                currentContext.setRouteHost(new URI("http://localhost:8003/send/sms-test3").toURL());
            }catch (Exception e){

            }
        }
        return null;
    }

  

3、网关的自我转发

yml:

 

zuul:
  routes:
    aaa:
      path: /forward1/**
      url:  forward:/myyml

 

java:

访问:http://localhost:9100/forward1,完成自我转发

@RequestMapping(value = "/myyml")
    public String test(){
        return "myyml";
    }

 

标签:网关,zuul,RequestContext,url,request,Route,sms,currentContext
来源: https://www.cnblogs.com/fch2w/p/15367751.html