zoukankan      html  css  js  c++  java
  • 微服务网关---调用其他微服务

     


    首先写一个过滤器继承ZuulFilter ,然后重写其中的run()方法, 1、获取request对象的信息:
    RequestContext ctx = RequestContext.getCurrentContext();
    HttpServletRequest request = ctx.getRequest();
    String servletPath = request.getServletPath();
    拿到request对象之后就能获得请求的信息:
    String accessSys = request.getParameter("accessSys");
    String paramData = getRequestBody(request);

    private String getRequestBody(HttpServletRequest request) {
    try {
    InputStream v_inputstream = request.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    int x = 0;
    while ((x = v_inputstream.read()) != -1) {
    baos.write(x);
    }
    baos.flush();
    return new String(baos.toByteArray(), UTF_8);
    } catch (Exception e) {
    logger.error(e.getMessage(), e);
    }
    return "";
    }

     2、遍历routes路由信息   路由的配置信息如下:

    @ConfigurationProperties(prefix="application.myroutes")
    public class MyRoutesConfig {
    
    	
    	private Map<String, String> routes;
    
    	public Map<String, String> getRoutes() {
    		return routes;
    	}
    
    	public void setRoutes(Map<String, String> routes) {
    		this.routes = routes;
    	}
    }
    配置文件的信息如下:

    #自定义过滤器 路由,参考:RestFaceFilter.java
    application:
    myroutes:
    routes: {
    service: 'bbbbbb',
    proxyservice: 'bbbbbbb'
    }
     if (routes != null) {
                Set<String> keySet = routes.getRoutes().keySet();
                for (String str : keySet) {
    //                logger.info("get route key: {}", str);
                    if (servletPath.startsWith("/" + str + "/")) {
                        restface_forward = routes.getRoutes().get(str);
                        break;
                    }
                }
            }
    

      

  • 相关阅读:
    anything vs everything
    cong
    invalid initialization of non-const reference of type与discards qualifiers
    gvew
    3.2存储器层次结构 -- 《深入理解计算机系统》☆☆☆☆☆
    2.2优化编译器的能力和局限性
    2.1.2优化程序性能
    2.1.1优化程序性能
    linux中获取堆栈空间大小的方法
    优美的英文诗歌Beautiful English Poetry
  • 原文地址:https://www.cnblogs.com/otways/p/11411598.html
Copyright © 2011-2022 走看看