在 Spring Boot 中创建 PROXY 服务,监听多个端口,并将 GET 请求重定向到新 URL

Posted

技术标签:

【中文标题】在 Spring Boot 中创建 PROXY 服务,监听多个端口,并将 GET 请求重定向到新 URL【英文标题】:Creating a PROXY service in Spring Boot, that listens on multiple ports, and redirects the GET requests to a new URL 【发布时间】:2021-11-04 06:09:55 【问题描述】:

在 Spring Boot 中创建一个 PROXY 服务,监听多个端口,并将 GET 请求重定向到一个新的 URL

请求仍然来自的旧 URL - http://localhost:7010/DNTService?cptyXXX=89959623213123&portfolioXXX=03880774324234 到新网址 - http://wasp-uat-na.net/DNTService?cptyXXX=89959623213123&portfolioXXX=03880774324234

Spring 中有没有办法可以帮助我实现这一目标???

到目前为止,使用此“https://bitbucket.org/asimio/springboot-tomcat-multiple-ports/src/master/”中的步骤,我能够监听多个端口,但添加了 spring-云网关我收到以下错误:

块引用 2021-09-08 17:48:21.737 INFO 22192 --- [main] c.p.wasp.proxyservice.WaspProxyService:以下配置文件处于活动状态:本地 2021-09-08 17:48:22.281 INFO 22192 --- [主] os.cloud.context.scope.GenericScope:BeanFactory id=092bb2d8-41fb-3da8-943b-0ff67624e05e 2021-09-08 17:48:22.287 INFO 22192 --- [main] faultConfiguringBeanFactoryPostProcessor:没有明确定义名为“errorChannel”的 bean。因此,将创建一个默认的 PublishSubscribeChannel。 2021-09-08 17:48:22.296 INFO 22192 --- [main] faultConfiguringBeanFactoryPostProcessor:没有明确定义名为“taskScheduler”的 bean。因此,将创建一个默认的 ThreadPoolTask​​Scheduler。 2021-09-08 17:48:22.301 INFO 22192 --- [main] faultConfiguringBeanFactoryPostProcessor:没有明确定义名为“integrationHeaderChannelRegistry”的 bean。因此,将创建一个默认的 DefaultHeaderChannelRegistry。 2021-09-08 17:48:22.365 INFO 22192 --- [main] trationDelegate$BeanPostProcessorChecker:[org.springframework.integration.config.IntegrationManagementConfiguration] 类型的 Bean 'org.springframework.integration.config.IntegrationManagementConfiguration' 不符合条件被所有 BeanPostProcessor 处理(例如:不符合自动代理的条件) 2021-09-08 17:48:22.369 INFO 22192 --- [main] trationDelegate$BeanPostProcessorChecker:Bean 'org.springframework.boot.autoconfigure.integration.IntegrationAutoConfiguration$IntegrationJmxConfiguration' 类型 [org.springframework.boot.autoconfigure.integration .IntegrationAutoConfiguration$IntegrationJmxConfiguration] 不符合被所有 BeanPostProcessor 处理的条件(例如:不符合自动代理的条件) 2021-09-08 17:48:22.375 INFO 22192 --- [main] trationDelegate$BeanPostProcessorChecker:Bean 'org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration' 类型为 [org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration ] 不符合被所有 BeanPostProcessor 处理的条件(例如:不符合自动代理的条件) 2021-09-08 17:48:22.378 INFO 22192 --- [main] trationDelegate$BeanPostProcessorChecker:类型为 [com.sun.jmx.mbeanserver.JmxMBeanServer] 的 Bean 'mbeanServer' 不符合所有 BeanPostProcessor 处理的条件(对于示例:不符合自动代理的条件) 2021-09-08 17:48:22.387 INFO 22192 --- [main] trationDelegate$BeanPostProcessorChecker:[org.springframework.integration.support.channel.BeanFactoryChannelResolver] 类型的 Bean 'integrationChannelResolver' 不符合所有 BeanPostProcessor 处理的条件(例如:不符合自动代理的条件) 2021-09-08 17:48:22.388 INFO 22192 --- [main] trationDelegate$BeanPostProcessorChecker:[org.springframework.integration.config.annotation.Disposables] 类型的 Bean 'integrationDisposableAutoCreatedBeans' 不符合所有 BeanPostProcessor 处理的条件(例如:不符合自动代理的条件) 2021-09-08 17:48:22.396 INFO 22192 --- [main] trationDelegate$BeanPostProcessorChecker:Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' 类型 [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$ $fd890f3c] 不符合被所有 BeanPostProcessor 处理的条件(例如:不符合自动代理的条件) 2021-09-08 17:48:22.586 INFO 22192 --- [main] osbwembedded.tomcat.TomcatWebServer:Tomcat 使用端口初始化:7010 (http) 7001 (http) 7002 (http) 7011 (http) 6152 (http) 6151 (http) 6158 (http) 6156 (http) 2021-09-08 17:48:22.609 INFO 22192 --- [main] o.apache.catalina.core.StandardService:启动服务 [Tomcat] 2021-09-08 17:48:22.610 INFO 22192 --- [main] org.apache.catalina.core.StandardEngine:启动 Servlet 引擎:[Apache Tomcat/9.0.31] 2021-09-08 17:48:22.739 INFO 22192 --- [main] o.a.c.c.C.[Tomcat].[localhost].[/]:初始化 Spring 嵌入式 WebApplicationContext 2021-09-08 17:48:22.739 INFO 22192 --- [main] w.s.c.ServletWebServerApplicationContext:根 WebApplicationContext:初始化在 990 毫秒内完成 2021-09-08 17:48:22.782 INFO 22192 --- [main] c.p.wasp.proxyservice.WaspProxyService:***初始化成功完成*** 2021-09-08 17:48:22.790 WARN 22192 --- [main] ConfigServletWebServerApplicationContext:上下文初始化期间遇到异常-取消刷新尝试:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“globalErrorWebExceptionHandler”的bean时定义在文件[C:\wasp-proxy-service\target\classes\com\potty\wasp\proxyservice\webservices\GlobalErrorWebExceptionHandler.class]:通过构造函数参数2表示的不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有“org.springframework.http.codec.ServerCodecConfigurer”类型的合格 bean 可用:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注释: 2021-09-08 17:48:22.791 INFO 22192 --- [main] o.apache.catalina.core.StandardService:停止服务 [Tomcat] 2021-09-08 17:48:22.800 INFO 22192 --- [主要] ConditionEvaluationReportLoggingListener:

启动 ApplicationContext 时出错。要显示条件报告,请在启用“调试”的情况下重新运行您的应用程序。 2021-09-08 17:48:22.944 错误 22192 --- [主] o.s.b.d.LoggingFailureAnalysisReporter:


应用程序启动失败


说明:

com.potty.wasp.proxyservice.webservices.GlobalErrorWebExceptionHandler 中构造函数的参数 2 需要一个无法找到的 'org.springframework.http.codec.ServerCodecConfigurer' 类型的 bean。

行动:

考虑在你的配置中定义一个“org.springframework.http.codec.ServerCodecConfigurer”类型的bean。

进程以退出代码 1 结束

POM 文件:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.potty.wasp</groupId>
    <artifactId>wasp-proxy-service</artifactId>
    <version>5.0.0-SNAPSHOT</version>
    <description>Proxy service used to route calls to WASP on GKP</description>

    <properties>
        <project.artifactId>wasp-proxy-service</project.artifactId>
        <project.version>5.0.0-SNAPSHOT</project.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
        <janus.janusweb-j2ee-version-gaia>5.0.0.0</janus.janusweb-j2ee-version-gaia>
        <janus.janusweb-j2ee-version>6.0.0.1</janus.janusweb-j2ee-version>
        <janus.giam-ip-common.version>14.10.19</janus.giam-ip-common.version>
        <commons-beanutils.version>1.9.3</commons-beanutils.version>
        <json.version>20140107</json.version>
        <spring-data-geode.version>2.1.0.RELEASE</spring-data-geode.version>
        <spring-shell.version>1.2.0.RELEASE</spring-shell.version>
        <jackson.version>2.9.10.20200103</jackson.version>
        <xstream.version>1.4.11.1</xstream.version>
        <tomcat.version>9.0.31</tomcat.version>
        <sonar.coverage.exclusions>
            **/db/model/*,
            **/json/model/*,
            **/datamodel/**/*,
            **/enumeration/*,
            **/*Literals.*,
            **/*Constants.*
        </sonar.coverage.exclusions>
        <sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
        <sonar.dynamicAnalysis>reuseReports</sonar.dynamicAnalysis>
        <sonar.jacoco.reportPath>$project.basedir/target/jacoco.exec</sonar.jacoco.reportPath>
        <sonar.language>java</sonar.language>
        <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
        <jacoco.version>0.8.6</jacoco.version>
        <jacoco.skip.instrument>true</jacoco.skip.instrument>
    </properties>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.4.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.jacoco</groupId>
                <artifactId>org.jacoco.agent</artifactId>
                <version>$jacoco.version</version>
                <classifier>runtime</classifier>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>$spring-cloud.version</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.logging.log4j</groupId>
                    <artifactId>log4j-to-slf4j</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.springframework</groupId>
                    <artifactId>spring-jcl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <type>pom</type>
        </dependency>-->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>com.vaadin.external.google</groupId>
                    <artifactId>android-json</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.jacoco</groupId>
            <artifactId>org.jacoco.agent</artifactId>
            <classifier>runtime</classifier>
            <scope>test</scope>
        </dependency>
        <!--~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~-->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>14.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
        </dependency>
        <!--~~~~~~~~~~~~~~~SPRING CLOUD~~~~~~~~~~~~~~~-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-contract-stub-runner</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-web</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <directory>target</directory>
        <outputDirectory>target/classes</outputDirectory>
        <finalName>$artifactId-$version</finalName>
        <testOutputDirectory>target/test-classes</testOutputDirectory>
        <sourceDirectory>src/main/java</sourceDirectory>
        <testSourceDirectory>src/test/java</testSourceDirectory>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>src/test/resources</directory>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <systemPropertyVariables>
                        <jacoco-agent.destfile>$project.build.directory/jacoco.exec</jacoco-agent.destfile>
                    </systemPropertyVariables>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>$jacoco.version</version>
                <executions>
                    <execution>
                        <id>default-instrument</id>
                        <goals>
                            <goal>instrument</goal>
                        </goals>
                        <configuration>
                            <skip>$jacoco.skip.instrument</skip>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-restore-instrumented-classes</id>
                        <goals>
                            <goal>restore-instrumented-classes</goal>
                        </goals>
                        <configuration>
                            <skip>$jacoco.skip.instrument</skip>
                        </configuration>
                    </execution>
                    <execution>
                        <id>report</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                        <configuration>
                            <skip>$jacoco.skip.instrument</skip>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <goal>build-info</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

【问题讨论】:

【参考方案1】:

spring-cloud-gateway 就是你要找的东西

【讨论】:

但是spring cloud gateway可以监听多个端口;因为我在 Spring-Cloud-Gateway 中有一个相同的实现;但它只能监听一个端口。 我刚刚更新了问题并添加了更多细节;我是否需要排除 spring-cloud-dependencies 中的某些内容才能正常工作

以上是关于在 Spring Boot 中创建 PROXY 服务,监听多个端口,并将 GET 请求重定向到新 URL的主要内容,如果未能解决你的问题,请参考以下文章

在 Spring Boot 中创建动态服务

如何从 JSON 数组在 DB 中创建表以在 Spring Boot 中创建 REST API

如何在 Spring Boot 中创建自定义错误页面

在 Spring Boot 中创建 ResponseEntity 类的对象的方法

在 Spring Boot 中创建自定义查询时出错

我如何在 Spring Boot/MVC 中创建错误处理程序(404、500...)