Spring 云网关中每个备用请求的 404 错误



我在春季云网关中遇到了一个非常特殊的问题。每个备用请求都返回 404。这发生在我在 API 网关中配置的所有服务中,无一例外。我什至不知道从哪里开始调试这个问题。

这是我的通用配置的应用程序.yml文件。

server:
port: 8080
ssl:
enabled: true
key-store: classpath:keystore.p12
key-store-password: password
key-store-type: pkcs12
key-alias: tomcat
security:
require-ssl=true:
logging:
level:
org:
springframework:
cloud.gateway: DEBUG
http.server.reactive: DEBUG
web.reactive: DEBUG
spring:
application:
name: api-gateway
cloud:
gateway:
httpclient:
ssl:
useInsecureTrustManager: true

这是我的 java 配置文件


@EnableWebFluxSecurity
public class SecurityConfig {
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http,
ReactiveClientRegistrationRepository clientRegistrationRepository) {
// Authenticate through configured OpenID Provider
http.oauth2Login();
// Also logout at the OpenID Connect provider
http.logout(logout -> logout.logoutSuccessHandler(new OidcClientInitiatedServerLogoutSuccessHandler(
clientRegistrationRepository)));
// Require authentication for all requests
http.authorizeExchange().anyExchange().authenticated();
// Allow showing /home within a frame
http.headers().frameOptions().mode(Mode.SAMEORIGIN);
// Disable CSRF in the gateway to prevent conflicts with proxied service CSRF
http.csrf().disable();
return http.build();
}
}

这是特定于 spring 配置文件的配置文件,该文件加载在通用 application.yml 文件之上。

spring:
security:
oauth2:
client:
provider:
keycloak:
issuerUri: http://localhost:9080/auth/realms/mylocal
userNameAttribute: preferred_username
registration:
keycloak:
clientId: api-gateway-client
clientSecret: abcdefgh-ijkl-mnop-qrst-uvwxyz5d6a9
cloud:
gateway:
default-filters:
- TokenRelay
routes:
- id: news
uri: http://localhost:8082/news
predicates:
- Path= /news/**
- id: customers
uri: http://localhost:8083/customers
predicates:
- Path= /boards/**
- id: search
uri: http://localhost:8085/search
predicates:
- Path= /search/**

嘿,我最近也遇到了这个问题,我发现这是负载平衡问题。确保您尝试联系的微服务 spring.application.name 全部为大写字母 (EXAMPLE(,尤其是在使用 Eureka 时。(希望有帮助(

我也有类似的问题。原因是在我的 Eureka 服务器中以相同的应用程序名称注册了多个服务。就像这样: 尤里卡实例屏幕截图

这些是完全不同的服务,但它们在一个应用程序名称下注册。因此,当通过负载均衡器发出请求时,后者会尝试同时使用这两个服务 URL。一个服务能够正确处理请求,但另一个服务可能对请求的路径一无所知,这就是返回 404 的原因。

检查是否有两个或多个微服务使用相同的spring.application.name并在 Eureka 注册。

我知道这是一个老问题...但可能会对未来的读者有所帮助。

嘿,这是我在浏览器中为自己找到的最简单的解决方案 URL,而不是"本地主机",而是提供系统的名称。

前任: http://hp:8083/SERVICE-NAME/customers/**

还要确保以大写形式命名所有 Spring 应用程序。

相关内容

最新更新