• servlet实现简单的反向代理


    项目基于Spring
    须要的依赖为:

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

    只需要添加一个java文件即可,原理有待继续研究

    package *.*.*.*;
    
    import org.mitre.dsmiley.httpproxy.ProxyServlet;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * Created by czz on 2018/11/13.
     */
    @Configuration
    public class SolrProxyServletConfiguration {
        @Bean
        public ServletRegistrationBean servletRegistrationBean(){
            ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/s/*");
            servletRegistrationBean.addInitParameter("targetUri", "http://www.baidu.com/s");
            servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
            return servletRegistrationBean;
        }
    }
    

    还可以通过配置多个bean来达到代理多个服务的目的

    
    package *.*.*.*;
    
    import org.mitre.dsmiley.httpproxy.ProxyServlet;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    /**
     * Created by czz on 2018/11/13.
     */
    @Configuration
    public class SolrProxyServletConfiguration {
        @Bean
        public ServletRegistrationBean servletRegistrationBean(){
            ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/s/*");
            servletRegistrationBean.setName("baidu");
            servletRegistrationBean.addInitParameter("targetUri", "http://www.baidu.com/s");
            servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
            return servletRegistrationBean;
        }
    
        @Bean
        public ServletRegistrationBean servletRegistrationBean1(){
            ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/bootstrap/*");
            servletRegistrationBean.setName("runoob");
            servletRegistrationBean.addInitParameter("targetUri", "http://www.runoob.com/bootstrap");
            servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
            return servletRegistrationBean;
        }
    
    
        @Bean
        public ServletRegistrationBean servletRegistrationBean2(){
            ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new ProxyServlet(), "/otheragent/*");
            servletRegistrationBean.setName("***");
            servletRegistrationBean.addInitParameter("targetUri", "http://***/otheragent");
            servletRegistrationBean.addInitParameter(ProxyServlet.P_LOG, "false");
            return servletRegistrationBean;
        }
    }
    
    

    参考文章:Spring boot使用ProxyFilter进行服务代理

    希望可以帮助到大家,大家也可关注我的公众号方便在手机上进行查看

  • 相关阅读:
    Spring Cloud Gateway配置自定义异常返回
    C#开机启动,托盘图标等小功能
    微信内置浏览器搞事情之调试模式
    物联网架构成长之路(56)-SpringCloudGateway+JWT实现网关鉴权
    物联网架构成长之路(55)-Gateway+Sentinel实现限流、熔断
    物联网架构成长之路(53)-Sentinel流量控制中间件入门
    物联网架构成长之路(54)-基于Nacos+Gateway实现动态路由
    物联网架构成长之路(52)-基于Nacos+prometheus+grafana的监控
    物联网架构成长之路(51)-Nacos微服务配置中心、服务注册服务发现
    物联网架构成长之路(50)-EMQ配置SSL证书,实现MQTTs协议
  • 原文地址:https://www.cnblogs.com/caozz/p/9953903.html
Copyright © 2020-2023  润新知