Method of zuul modifying request parameter information in spring cloud
Zuul is a load balancer based on JVM routing and server produced by Netflix
Zuul function:
Zuul's rule engine allows rules and filters to be written in any JVM language, supporting Java and groovy based builds.
Configure properties zuul max.host. Connections has been replaced by two new configuration properties, zuul host. The default values for maxtotalconnections and zuul.host.maxperrouteconnections are 200 and 20, respectively
I Why use this
In the microservice system built based on spring cloud, the gateway zuul is usually used for some filtering operations such as user authentication. For example, the user stores a token in the header or URL parameter. The gateway layer needs to use the token to find out the user's userid and store it in the request, so that the subsequent microservices can be used directly and avoid using token query again.
II Basic knowledge
In addition to routing, the biggest usage in zuul is the filter. The user-defined filter needs to implement the interface zuulfilter, which can be used in the run () method
The request is obtained, but there is only getparameter () and no setparameter () method in the request, so it is not feasible to directly modify the URL parameter. In addition, setAttribute () can be used in reqeust, but the attributes set here may not be obtained in subsequent microservices due to different scopes, so other methods must be considered.
III specific working means
Finally, the feasible method is to use
To reconstruct the request in the context. The code is as follows:
The idea is to get the input stream of the request and rewrite it, that is, rewrite the JSON parameters.
In the controller of subsequent microservices, you can use shape similarity
In this way, get the userid passed in zuulfilter
IV Some attempts
When rewriting the httpservletrequestwrapper, I tried to rewrite the getparameternames () and getparametermap () methods, hoping to rewrite the URL parameters, but it didn't take effect.
summary
The above is the method of zuul modifying request parameter information in springcloud introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!