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;
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现WebSocket的反向代理,可以使用Spring Boot提供的WebSocketStompClient来实现。首先,需要添加以下依赖到你的项目中: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> ``` 然后,你需要配置你的WebSocket拦截器,可以通过添加`@ServerEndpoint`注解和设置configurator来获取Filter中的数据。这里需要注意的是,要加上断点注解:`@ServerEndpoint(value = "/websocket", configurator = WebSocketConfigurator.class)`。这样,你就可以通过session来获取Filter中的数据了。 接下来是分布式部署和共享的问题。在分布式部署中,WebSocket的共享问题可以通过使用消息代理来解决。你可以使用Spring的消息中间件,如RabbitMQ或ActiveMQ,来实现WebSocket的消息代理。通过配置消息代理,你可以确保WebSocket的消息在多个节点之间共享。 最后,如果你遇到了无法注入对象的问题,可以检查你的WebSocket配置是否正确,并确保你的对象被正确注入。你可以使用`@Autowired`注解来注入对象。如果仍然无法注入对象,可能需要检查你的配置和代码逻辑是否正确。 总结起来,要实现Spring Boot的WebSocket反向代理,你需要添加必要的依赖和配置WebSocket拦截器。在分布式部署中,你可以使用消息代理来解决WebSocket的共享问题。如果遇到无法注入对象的问题,可以检查配置和代码逻辑是否正确。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值