欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

nginx反向代理java項(xiàng)目方式

 更新時(shí)間:2024年12月31日 10:15:14   作者:liuyuinsdu  
文章簡(jiǎn)要介紹了如何使用Nginx作為反向代理來(lái)部署Java項(xiàng)目,核心在于配置proxy_pass指令

nginx反向代理java項(xiàng)目

server
{
    listen 80;
    server_name ***.***.com;
    
    location / {
    	proxy_pass http://127.0.0.1:8686;
  	}
    
    #一鍵申請(qǐng)SSL證書(shū)驗(yàn)證目錄相關(guān)設(shè)置
    location ~ \.well-known{
        allow all;
    }
    
    access_log  /www/wwwlogs/***.***.com.log;
    error_log  /www/wwwlogs/***.***.com.error.log;
}

很簡(jiǎn)單,核心就是 proxy_pass

Java實(shí)現(xiàn)反向代理的坑

公司是做車(chē)輛物聯(lián)網(wǎng)相關(guān)業(yè)務(wù)的,需要一個(gè)網(wǎng)關(guān)系統(tǒng)對(duì)設(shè)備上傳的報(bào)文進(jìn)行解析。最近換了個(gè)一個(gè)新的開(kāi)源網(wǎng)關(guān)系統(tǒng),里面集成了一些對(duì)設(shè)備操作的API接口。由于網(wǎng)關(guān)系統(tǒng)不能對(duì)外,所以需要另一個(gè)系統(tǒng)鑒權(quán)后將接口反向代理到網(wǎng)關(guān)的API接口,類(lèi)似于Nginx的反向代理。

在網(wǎng)上找了半天,發(fā)現(xiàn) smiley-http-proxy-servlet可以實(shí)現(xiàn)類(lèi)似功能且實(shí)現(xiàn)非常簡(jiǎn)單,實(shí)現(xiàn)步驟如下:

導(dǎo)入smiley-http-proxy-servletMaven依賴(lài)

<dependency>
    <groupId>org.mitre.dsmiley.httpproxy</groupId>
    <artifactId>smiley-http-proxy-servlet</artifactId>
    <version>1.12.1</version>
</dependency>

在配置文件中編寫(xiě)代理的前綴和目標(biāo)地址

proxy:
    solr:
        servlet_url: /proxy/*
        target_url: http://solrserver:8983/proxy

編寫(xiě)配置類(lèi)

@Configuration
public class SolrProxyServletConfiguration implements EnvironmentAware {

    @Bean
    public ServletRegistrationBean servletRegistrationBean() {
        Properties properties= (Properties) bindResult.get();
        // 設(shè)置代理前綴
        ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), properties.getProperty("servlet_url"));
        // 設(shè)置代理目標(biāo)地址
        servletRegistrationBean.addInitParameter(ProxyServlet.P_TARGET_URI, properties.getProperty("target_url"));
        // 設(shè)置是否打印日志
        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;
    }
}

假設(shè)當(dāng)前代理服務(wù)器地址為 127.0.0.1:8001,當(dāng)訪(fǎng)問(wèn) http://127.0.0.1:8001/proxy/test 這個(gè)請(qǐng)求回被代理服務(wù)器轉(zhuǎn)發(fā)到 http://solrserver:8983/proxy/test 這樣我們就實(shí)現(xiàn)了一個(gè)請(qǐng)求的代理了。

當(dāng)時(shí)我喜滋滋的認(rèn)為大功告成開(kāi)始測(cè)試時(shí),發(fā)現(xiàn)當(dāng)時(shí)使用 POST 請(qǐng)求且攜帶的參數(shù)類(lèi)型 x-www-form-urlencoded 會(huì)導(dǎo)致請(qǐng)求超時(shí)。后面在百度搜了半天也沒(méi)找到解決方法,于是我就想著去這個(gè)項(xiàng)目的GitHub主頁(yè)哪里看下有沒(méi)有大神遇到這個(gè)問(wèn)題。

當(dāng)我用 x-www-form-urlencoded作為關(guān)鍵詞在issues里搜索時(shí),真好發(fā)現(xiàn)有大佬也遇到了這個(gè)問(wèn)題。

出現(xiàn)這個(gè)問(wèn)題的原因很簡(jiǎn)單就是SpringBoot中過(guò)濾器太多,導(dǎo)致 ResponseBody 被提前消費(fèi)了,無(wú)法返回到客戶(hù)端。

在這條 issues下有個(gè)大佬提出來(lái)解決方案,就是繼承 ProxyServlet 并重寫(xiě) newProxyRequestWithEntity方法,代碼如下:

  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也需要重新設(shè)置使用的 ProxyServlet

ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new MyProxyServlet(), properties.getProperty("servlet_url"));

這就是我在使用 smiley-http-proxy-servlet遇到的坑,后續(xù)在使用過(guò)程中還有其他的坑還是會(huì)與大家分享。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論