springboot后端当成反向代理服务器

思路是创建一个servlet,解析路径映射,在其中实现请求消息体,消息头的转发。响应消息体消息头的转发。

实现工具https://mvnrepository.com/artifact/org.mitre.dsmiley.httpproxy/smiley-http-proxy-servlet/1.12.1

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

yml:

复制代码
# 自定义代理相关配置
# 代理的本地路由
proxy.servlet_url: /arcgisProxy/*
# 要代理的地址
proxt.target_url: http://www.baidu.com

配置类:

复制代码
@Configuration
public class ProxyConfig {
    // 读取配置文件中路由设置
    @Value("${proxy.servlet_url}")
    private String servlet_url;
    // 读取配置中代理目标地址
    @Value("${proxt.target_url}")
    private String target_url;



    @Bean
    public Servlet createProxyServlet(){
        // 创建新的ProxyServlet
        return new ProxyServlet();
    }
    @Bean
    public ServletRegistrationBean proxyServletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(createProxyServlet(), servlet_url);
        //设置网址以及参数
        Map<String, String> params = ImmutableMap.of(
                ProxyServlet.P_TARGET_URI, target_url,
                ProxyServlet.P_CONNECTTIMEOUT, "20000",
                ProxyServlet.P_READTIMEOUT, "20000",
                ProxyServlet.P_CONNECTIONREQUESTTIMEOUT, "20000",
                "log", "false");
        registrationBean.setInitParameters(params);
        return registrationBean;
    }
}

多个代理:

复制代码
package com.municipal.promotion.second_phase.config;

import com.google.common.collect.ImmutableMap;
import org.mitre.dsmiley.httpproxy.ProxyServlet;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Servlet;
import java.util.Map;

/**
 * @author yaoct
 * @date 2023/11/13 10:31
 * @description:
 */
@Configuration
public class ProxyConfig {
    // 读取配置文件中路由设置
    @Value("${proxy.servlet_url}")
    private String servlet_url;
    // 读取配置中代理目标地址
    @Value("${proxt.target_url}")
    private String target_url;

    // 读取配置文件中路由设置
    @Value("${proxy.servlet_url2}")
    private String servlet_url2;
    // 读取配置中代理目标地址
    @Value("${proxt.target_url2}")
    private String target_url2;



    @Bean
    public Servlet createProxyServlet(){
        // 创建新的ProxyServlet
        return new ProxyServlet();
    }
    @Bean
    public ServletRegistrationBean proxyServletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new ProxyServlet(), servlet_url);
        registrationBean.setName("proxyServlet1");//名字需要不一样!
        //设置网址以及参数
        Map<String, String> params = ImmutableMap.of(
                ProxyServlet.P_TARGET_URI, target_url,
                ProxyServlet.P_CONNECTTIMEOUT, "20000",
                ProxyServlet.P_READTIMEOUT, "20000",
                ProxyServlet.P_CONNECTIONREQUESTTIMEOUT, "20000",
                "log", "false");
        registrationBean.setInitParameters(params);
        return registrationBean;
    }


//    @Bean
//    public Servlet createProxyServlet2(){
//        // 创建新的ProxyServlet
//        return new ProxyServlet();
//    }
    @Bean
    public ServletRegistrationBean proxyServletRegistration2(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new ProxyServlet(), servlet_url2);
        registrationBean.setName("proxyServlet2");//名字需要不一样!
        //设置网址以及参数
        Map<String, String> params = ImmutableMap.of(
                ProxyServlet.P_TARGET_URI, target_url2,
                ProxyServlet.P_CONNECTTIMEOUT, "20000",
                ProxyServlet.P_READTIMEOUT, "20000",
                ProxyServlet.P_CONNECTIONREQUESTTIMEOUT, "20000",
                "log", "false");
        registrationBean.setInitParameters(params);
        return registrationBean;
    }
}
相关推荐
YuTaoShao1 小时前
【LeetCode 热题 100】56. 合并区间——排序+遍历
java·算法·leetcode·职场和发展
程序员张31 小时前
SpringBoot计时一次请求耗时
java·spring boot·后端
llwszx4 小时前
深入理解Java锁原理(一):偏向锁的设计原理与性能优化
java·spring··偏向锁
云泽野4 小时前
【Java|集合类】list遍历的6种方式
java·python·list
二进制person5 小时前
Java SE--方法的使用
java·开发语言·算法
小阳拱白菜6 小时前
java异常学习
java
程序员岳焱7 小时前
Java 与 MySQL 性能优化:Java 实现百万数据分批次插入的最佳实践
后端·mysql·性能优化
FrankYoou7 小时前
Jenkins 与 GitLab CI/CD 的核心对比
java·docker
麦兜*7 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了8 小时前
JAVA-springboot 整合Redis
java·spring boot·redis