Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to modify Spring Cloud Zuul Gateway to short connection

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

Today, the editor will share with you the relevant knowledge about how to modify the Spring Cloud Zuul gateway to a short connection. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.

I. problem analysis

Previously, when using the zuul gateway, I made several requests and then the connection was disconnected. The reason is that after http1.1, the default is connection=keep-alive persistent connection. But no heartbeat was maintained, and the heart was disconnected every minute. But RestFul usually just takes a short connection. So I just want to change the connection property of the header.

Is to modify the RequestContext ctx object of Zuul in the filter

/ / set the request to short connection ctx.addZuulRequestHeader ("connection", "close")

After making another request, I found out

Nani, it's still a long connection. Then it may be that my settings do not work or are overwritten. When you encounter problems, in addition to du Niang, it is also the best way to follow the source code. It took me more than half a day to finally find out why.

The reason is that there is a piece of logic in the proxy class ProxyRequestHelper.

Public boolean isIncludedHeader (String headerName) {String name = headerName.toLowerCase (); RequestContext ctx = RequestContext.getCurrentContext (); if (ctx.containsKey (IGNORED_HEADERS)) {Object object = ctx.get (IGNORED_HEADERS) If (object instanceof Collection & ((Collection) object) .clients (name)) {return false }} switch (name) {case "host": if (addHostHeader) {return true } case "connection": case "content-length": case "server": case "transfer-encoding": case "x-application-context": return false; default: return true }}

If there are "connection": "content-length": "server": "transfer-encoding": "x-application-context" headers in the header, they are all skipped. Will not be set successfully. It should be a mechanism of zuul itself.

II. Solution

So if the problem is found, how to fix it? Then you can only customize the route. Inherit and rewrite this class.

1. Implement custom routing configuration CustomZuulConfig

@ Componentpublic class CustomZuulConfig {@ Autowired ZuulProperties zuulProperties; @ Value ("${servletPath}") private String servletPath; @ Bean public CustomRouteLocator routeLocator () {CustomRouteLocator routeLocator = new CustomRouteLocator (servletPath, this.zuulProperties); return routeLocator;}}

2. Implement the custom request agent tool class to override the isIncludedHeader method.

Public class CustomRequestHelper extends ProxyRequestHelper {@ Override public boolean isIncludedHeader (String headerName) {String name = headerName.toLowerCase (); RequestContext ctx = RequestContext.getCurrentContext (); if (ctx.containsKey (IGNORED_HEADERS)) {Object object = ctx.get (IGNORED_HEADERS); if (object instanceof Collection & & (Collection) object) .requests (name)) {return false } switch (name) {case "content-length": case "host": case "server": case "transfer-encoding": case "x-application-context": return false; default: return true;}

3. Override the implementation proxy configuration class

@ Configuration@EnableZuulProxypublic class CustomZuulProxyConfig extends ZuulProxyAutoConfiguration {@ Bean @ Override public SimpleHostRoutingFilter simpleHostRoutingFilter (ProxyRequestHelper helper, ZuulProperties zuulProperties, ApacheHttpClientConnectionManagerFactory connectionManagerFactory, ApacheHttpClientFactory httpClientFactory) {CustomRequestHelper customRequestHelper = new CustomRequestHelper (); return new SimpleHostRoutingFilter (customRequestHelper, zuulProperties, connectionManagerFactory, httpClientFactory);}}

Then restart to view the request header.

These are all the contents of the article "how to modify the Spring Cloud Zuul Gateway to a short connection". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report