|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
server { listen 80; server_name ***.***.com;
location / { proxy_pass http://127.0.0.1:8686; }
#一键申请SSL证书验证目录相关设置 location ~ \.well-known{ allow all; }
access_log /www/wwwlogs/***.***.com.log; error_log /www/wwwlogs/***.***.com.error.log; } |
很简单,核心就是 proxy_pass
公司是做车辆物联网相关业务的,需要一个网关系统对设备上传的报文进行解析。最近换了个一个新的开源网关系统,里面集成了一些对设备操作的API接口。由于网关系统不能对外,所以需要另一个系统鉴权后将接口反向代理到网关的API接口,类似于Nginx的反向代理。
在网上找了半天,发现 smiley-http-proxy-servlet可以实现类似功能且实现非常简单,实现步骤如下:
|
1 2 3 4 5 |
<dependency> <groupId>org.mitre.dsmiley.httpproxy</groupId> <artifactId>smiley-http-proxy-servlet</artifactId> <version>1.12.1</version> </dependency> |
|
1 2 3 4 |
proxy: solr: servlet_url: /proxy/* target_url: http://solrserver:8983/proxy |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
@Configuration public class SolrProxyServletConfiguration implements EnvironmentAware {
@Bean public ServletRegistrationBean servletRegistrationBean() { Properties properties= (Properties) bindResult.get(); // 设置代理前缀 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), properties.getProperty("servlet_url")); // 设置代理目标地址 servletRegistrationBean.addInitParameter(ProxyServlet.P_TARGET_URI, properties.getProperty("target_url")); // 设置是否打印日志 servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, properties.getProperty("logging_enabled", "false")); return servletRegistrationBean; }
private BindResult bindResult;
@Override public void setEnvironment(Environment environment) { Iterable sources = ConfigurationPropertySources.get(environment); Binder binder = new Binder(sources); BindResult bindResult = binder.bind("proxy.solr", Properties.class); this.bindResult = bindResult; } } |
假设当前代理服务器地址为 127.0.0.1:8001,当访问 http://127.0.0.1:8001/proxy/test 这个请求回被代理服务器转发到 http://solrserver:8983/proxy/test 这样我们就实现了一个请求的代理了。
当时我喜滋滋的认为大功告成开始测试时,发现当时使用 POST 请求且携带的参数类型 x-www-form-urlencoded 会导致请求超时。后面在百度搜了半天也没找到解决方法,于是我就想着去这个项目的GitHub主页哪里看下有没有大神遇到这个问题。
当我用 x-www-form-urlencoded作为关键词在issues里搜索时,真好发现有大佬也遇到了这个问题。
出现这个问题的原因很简单就是SpringBoot中过滤器太多,导致 ResponseBody 被提前消费了,无法返回到客户端。
在这条 issues下有个大佬提出来解决方案,就是继承 ProxyServlet 并重写 newProxyRequestWithEntity方法,代码如下:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
protected HttpRequest newProxyRequestWithEntity( String method, String proxyRequestUri, HttpServletRequest servletRequest) throws IOException { HttpEntityEnclosingRequest eProxyRequest = new BasicHttpEntityEnclosingRequest(method, proxyRequestUri);
String contentType = servletRequest.getContentType();
boolean isFormPost = (contentType != null && contentType.contains("application/x-www-form-urlencoded") && "POST".equalsIgnoreCase(servletRequest.getMethod()));
if (isFormPost) { List<NameValuePair> queryParams = Collections.emptyList(); String queryString = servletRequest.getQueryString(); if (queryString != null) { queryParams = URLEncodedUtils.parse(queryString, Consts.UTF_8); }
Map<String, String[]> form = servletRequest.getParameterMap(); List<NameValuePair> params = new ArrayList<>();
OUTER_LOOP: for (Iterator<String> nameIterator = form.keySet().iterator(); nameIterator.hasNext(); ) { String name = nameIterator.next();
// skip parameters from query string for (NameValuePair queryParam : queryParams) { if (name.equals(queryParam.getName())) { continue OUTER_LOOP; } }
String[] value = form.get(name); if (value.length != 1) { throw new RuntimeException("expecting one value in post form"); } params.add(new BasicNameValuePair(name, value[0])); } eProxyRequest.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
} else { eProxyRequest.setEntity( new InputStreamEntity(servletRequest.getInputStream(), getContentLength(servletRequest))); } return eProxyRequest; } |
SolrProxyServletConfiguration也需要重新设置使用的 ProxyServlet
|
1 |
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyProxyServlet(), properties.getProperty("servlet_url")); |
这就是我在使用 smiley-http-proxy-servlet遇到的坑,后续在使用过程中还有其他的坑还是会与大家分享。