Zuul is a load balancer based on JVM routing and server-side produced by Netflix.
Zuul functions:
Zuul's rules engine allows writing rules and filters through any JVM language, and supports Java and Groovy-based construction.
The configuration properties zuul.max.host.connections have been replaced by two new configuration properties, zuul.host.maxTotalConnections (total number of connections) and zuul.host.maxPerRouteConnections, (number of connections per route) have default values of 200 and 20, respectively.
1. Why use this
In microservice systems built on springcloud, gateway zuul is usually used to perform some filtering operations such as user verification. For example, the user stores tokens in the header or url parameters. The gateway layer needs to use this token to find the user's userId and store it in the request so that subsequent microservices can be used directly to avoid querying with tokens.
2. Basic knowledge
The biggest usage in zuul is in addition to routing, which is filter. Custom filters need to implement the interface ZuulFilter. In the run() method, you can use it
RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest();
Get the request, but there is only getParameter() in the request but no setParameter() method, so it is not feasible to directly modify the url parameter. In addition, although setAttribute() can be used in reqeust, it may be that due to the different scopes, the attribute set here cannot be obtained in subsequent microservices, so another method must be considered.
3. Specific practices
The final method is to use
ctx.setRequest(new HttpServletRequestWrapper(request) {})The way to reconstruct the request in the context, the code is as follows:
import javax.servlet.http.HttpServletRequestWrapper; // Add userId try { InputStream in = ctx.getRequest().getInputStream(); String body = StreamUtils.copyToString(in, Charset.forName("UTF-8")); System.out.println("body:" + body); JSONObject json = JSONObject.fromObject(body); json.put("userId", userId); String newBody = json.toString(); System.out.println("newBody:" + newBody); final byte[] reqBodyBytes = newBody.getBytes(); ctx.setRequest(new HttpServletRequestWrapper(request){ @Override public ServletInputStream getInputStream() throws IOException { return new ServletInputStreamWrapper(reqBodyBytes); } @Override public int getContentLength() { return reqBodyBytes.length; } @Override public long getContentLengthLong() { return reqBodyBytes.length; } }); } catch (IOException e) { e.printStackTrace(); }The idea is to get the requested input stream and rewrite it, that is, rewrite the json parameters.
In subsequent microservice controllers, shapes can be used
@RequestBody Map<String,Object> body ====== body.get("userId");In this way, get the userId passed in zuulFilter
Four. Some attempts
When rewriting the HttpServletRequestWrapper, I tried rewriting getParameterNames() and getParameterMap() methods, hoping to rewrite the url parameters, but it did not take effect.
Summarize
The above is the method of modifying the request parameter information in springcloud introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!