How to Modify request parameter information by zuul in springcloud, springcloudzuul

Source: Internet
Author: User

How to Modify request parameter information by zuul in springcloud, springcloudzuul

Zuul is a Load balancer developed by Netflix Based on JVM routing and server.

 Zuul functions:

  • Authentication
  • Stress Testing
  • Canary Test
  • Dynamic Routing
  • Load Reduction
  • Security
  • Static Response Processing
  • Active/active exchange management

Zuul's Rule Engine allows you to write rules and filters in any JVM language and supports Java and Groovy-based construction.

Configure the attribute zuul. max. host. connections has been replaced by two new configuration attributes, zuul. host. maxTotalConnections (total connections) and zuul. host. maxPerRouteConnections. The default values are 200 and 20, respectively.

I. Why should I use this?

In a springcloud-based microservice system, the gateway zuul is usually used for user authentication and Other filtering operations. For example, the user stores the token in the header or url parameter, the gateway layer needs to use this token to identify the user's userId and store it in the request, so that the microservice can use it directly in the future without using the token for query.

Ii. Basic Knowledge

In addition to routing, the maximum usage of zuul is the filter. The custom filter must implement the interface ZuulFilter. In the run () method, you can use

RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); 

The request is obtained, but only the getParameter () method is available in the request, so it is not feasible to directly modify the url parameter. In reqeust, although setAttribute () can be used (), however, due to the different scopes, the attribute set here cannot be obtained in subsequent microservices. Therefore, another method must be considered.

Iii. Specific Practices

The final feasible method is to use

ctx.setRequest(new HttpServletRequestWrapper(request) {}) 

To reconstruct the request in the context. The Code is as follows:

Import javax. servlet. http. httpServletRequestWrapper; // Add userId try {InputStream in = ctx to the json parameter. 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 response (request) {@ Override public ServletInputStream getInputStream () throws IOException {return new response (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 input stream of the request and rewrite it, that is, rewrite the json parameter.

In the subsequent microservice controller

@RequestBody Map<String,Object> body ======= body.get("userId"); 

In this way, obtain the userId passed in zuulFilter.

4. Some attempts

When rewriting HttpServletRequestWrapper, I tried to rewrite it. getParameterNames() And getParameterMap() Method, but it does not take effect.

Summary

The above section describes how to modify the request parameter information of zuul in springcloud. I hope it will be helpful to you. If you have any questions, please leave a message for me, the editor will reply to you in a timely manner. Thank you very much for your support for the help House website!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.