几个java proxy servlet 工具

HTTP-Proxy-Servlet

这个工具使用比较简单,可以通过配置,或者代码的方式 https://github.com/mitre/HTTP-Proxy-Servlet

  • servlet 配置方式
<servlet>
  <servlet-name>clusterProxy</servlet-name>
  <servlet-class>org.mitre.dsmiley.httpproxy.URITemplateProxyServlet</servlet-class>
  <init-param>
    <param-name>targetUri</param-name>
    <param-value>http://{_subHost}.behindfirewall.mycompany.com:{_port}/{_path}</param-value>
  </init-param>
  <init-param>
    <param-name>log</param-name>
    <param-value>true</param-value>
  </init-param>
</servlet>
<servlet-mapping>
  <servlet-name>clusterProxy</servlet-name>
  <url-pattern>/mywebapp/cluster/*</url-pattern>
</servlet-mapping>
  • spring boot 代码方式
@Configuration
public class SolrProxyServletConfiguration implements EnvironmentAware {
  @Bean
  public ServletRegistrationBean servletRegistrationBean(){
    ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), propertyResolver.getProperty("servlet_url"));
    servletRegistrationBean.addInitParameter(ProxyServlet.P_TARGET_URI, propertyResolver.getProperty("target_url"));
    servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, propertyResolver.getProperty("logging_enabled", "false"));
    return servletRegistrationBean;
  }
  private RelaxedPropertyResolver propertyResolver;
  @Override
  public void setEnvironment(Environment environment) {
    this.propertyResolver = new RelaxedPropertyResolver(environment, "proxy.solr.");
  }
}
  

yaml 配置

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

Jetty's ProxyServlet:

这个是最全也是比较复杂的一个实现
https://www.eclipse.org/jetty/documentation/9.4.x/proxy-servlet.html

netflix zuul

spring cloud netflix zuul gateway 使用的技术https://github.com/Netflix/zuul,目前主要包括了两个版本1.x 以及 2差异还是比较大的
zuul 不同filter 阶段处理的方式很不错,比较灵活

charon-spring-boot-starter

功能很全,很方便的一个工具
https://github.com/mkopylec/charon-spring-boot-starter/wiki

  • 简单使用
 
import static com.github.mkopylec.charon.configuration.CharonConfigurer.charonConfiguration;
import static com.github.mkopylec.charon.configuration.RequestMappingConfigurer.requestMapping;
import static com.github.mkopylec.charon.forwarding.interceptors.rewrite.RequestServerNameRewriterConfigurer.requestServerNameRewriter;
@Configuration
class CharonConfiguration {
    @Bean
    CharonConfigurer charonConfigurer() {
        return charonConfiguration()
                .set(requestServerNameRewriter().outgoingServers("host1:8080", "host2:8081"))
                .add(requestMapping("all requests mapping"));
    }
}
 

posted on 2019-08-25 16:26  荣锋亮  阅读(1664)  评论(0编辑  收藏  举报

导航