Spring Boot 如何从URL中删除端口http://< ip>:8080

olhwl3o2  于 2023-05-28  发布在  Spring
关注(0)|答案(1)|浏览(259)

我的Sping Boot Web应用程序部署在GCP中,可以在URL http://sampleip:8080/.我想从URL中删除端口,以便可以通过URL http://sampleip/访问应用程序。尝试与祖鲁与下面的配置,但不工作。我错过了什么,请建议如何才能实现?
下面是我的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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.verofax</groupId>
    <artifactId>ABIMVP</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>ABIMVP</name>
    <description>ABI POC</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.cloud.tools/appengine-maven-plugin -->
<dependency>
    <groupId>com.google.cloud.tools</groupId>
    <artifactId>appengine-maven-plugin</artifactId>
    <version>2.4.0</version>
</dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-validation</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.8</version>
</dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-openfeign -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-openfeign</artifactId>
    <version>3.0.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter</artifactId>
    <version>3.0.4</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.16</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    <version>2.2.8.RELEASE</version>
</dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

public class AbimvpApplication {

public static void main(String[] args) {
        SpringApplication.run(AbimvpApplication.class, args);
    }

下面是我的财产档案

ribbon.eureka.enabled=false
zuul.routes.userservice.path=/**
zuul.routes.userservice.url=http://localhost:8080/
nbnkbykc

nbnkbykc1#

在阅读了所有的评论,和你的问题,嗯,主要的解决方案将是改变您的端口到80...但是如果你有一个以上的服务运行,这将是一个痛苦...
所以我推荐你使用SpringCloud Gateway,它使用起来很简单,你可以Map里面的一切来隐藏端口...只需在配置类中执行类似的操作(您也可以在属性yml中执行类似的操作)

package com.verofax.gateway.config;

import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SpringCloudConfig {

    @Bean
    public RouteLocator gatewayRoutes(RouteLocatorBuilder builder) {
        return builder.routes()
                .route(r -> r.path("/SomeFolder/**")
                        .uri("http://YourIp:8080/"))

                .route(r -> r.path("/SomeFolder/OtherFolder/**")
                        .uri("http://YourURL:8085/"))

                .route(r -> r.path("/**")
                        .uri("http://IP_ADDRESS:8087/"))
                .build();
    }
}

因此,First路由会将流量从8080重定向到SomeFolder。最后一个路由将从8087(它可以是任何端口,甚至8080)重定向流量到您的主网站,因此任何人点击http://sampleip:8080/与最后一个路由将能够到达http://sampleip/没有任何努力。
如果你有一个以上的服务运行(不同的端口与网站),我建议你把所有的图像和CSS文件在项目中,你会离开路径/**(没有文件夹名称),以避免创建任何静态文件夹或静态项目...否则你的网站会有坏掉的css/images。
希望这个适合你:)

相关问题