错误 HTTP 状态 405 ?使用 GCP 负载平衡器时不允许的方法

Posted

技术标签:

【中文标题】错误 HTTP 状态 405 ?使用 GCP 负载平衡器时不允许的方法【英文标题】:ERROR HTTP Status 405 ? Method Not Allowed when using GCP load balancer 【发布时间】:2021-09-22 23:37:26 【问题描述】:

我在 Spring Boot 中制作了一个应用程序,它从远程 PostgreSQL 数据库中恢复数据。它在本地(从本地应用程序到本地数据库)、从本地主机到远程数据库以及 GCP 云上的所有资源(带有托管应用程序的 tomcat 服务器的 vm 和用于 PostgreSQL 数据库的云 SQL)都运行良好。我的 PoC 的最后一部分是将我的应用程序托管在我的实例组中,并附加了负载均衡器。当我到达我的负载均衡器时,我可以看到我的欢迎页面,我使用 spring security 登录(从同一个 postgreSQL 数据库恢复凭据),但它不起作用,我收到下一个错误:

LB error page

当我检查我的 catalina.out 日志时,它会显示下一个错误:

11:34 ERROR 893 --- [io-8080-exec-11] o.s.b.w.servlet.support.ErrorPageFilter  : Cannot forward to error page for request [/login] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false
11:35  WARN 893 --- [nio-8080-exec-9] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]

如果有帮助,我将分享我的 LB terraform 代码,'lb 部分似乎是我的 PoC 中的问题。

provider "google-beta" 
  project     = var.project
  region      = "us-central1"
  credentials = "C:/Users/jperezgarcia/Desktop/Terraform/GCP/credentials/mario.json"


resource "google_compute_region_ssl_certificate" "ssl-crt" 
  provider    = google-beta
  project     = var.project
  name_prefix = "my-certificate-"
  region      = var.lb_region
  private_key = file("lb_http/certificate/privateKey.key")
  certificate = file("lb_http/certificate/certificate.crt")

  lifecycle 
    create_before_destroy = true
  


resource "google_compute_forwarding_rule" "lb-front-HTTP" 
  provider              = google-beta
  project               = var.project
  name                  = var.lb_front_name
  load_balancing_scheme = "INTERNAL_MANAGED"
  port_range            = var.lb_front_port_range
  target                = google_compute_region_target_http_proxy.lb-proxy-http.self_link
  region                = var.lb_region
  network               = var.lb_network
  subnetwork            = var.lb_subnetwork
  # ip_address            = "10.10.20.5"


resource "google_compute_forwarding_rule" "lb-front-HTTPS" 
  provider              = google-beta
  project               = var.project
  name                  = "lb-https-front"
  port_range            = "443"
  load_balancing_scheme = "INTERNAL_MANAGED"
  # ip_address            = "10.10.20.5"
  target     = google_compute_region_target_https_proxy.lb-proxy-https.self_link
  region     = var.lb_region
  network    = var.lb_network
  subnetwork = var.lb_subnetwork



resource "google_compute_region_target_http_proxy" "lb-proxy-http" 
  provider = google-beta
  name     = var.lb_proxy_name
  region   = var.lb_region
  project  = var.project
  url_map  = google_compute_region_url_map.lb_url_map.self_link


resource "google_compute_region_target_https_proxy" "lb-proxy-https" 
  provider         = google-beta
  name             = "test-proxy"
  region           = var.lb_region
  project          = var.project
  url_map          = google_compute_region_url_map.lb_url_map.self_link
  ssl_certificates = [google_compute_region_ssl_certificate.ssl-crt.id]



resource "google_compute_region_url_map" "lb_url_map" 
  provider        = google-beta
  project         = var.project
  name            = var.url_map_name
  region          = var.lb_region
  default_service = google_compute_region_backend_service.lb-backend.self_link



resource "google_compute_region_backend_service" "lb-backend" 
  provider              = google-beta
  name                  = var.lb_backend_name
  region                = var.lb_region
  project               = var.project
  load_balancing_scheme = "INTERNAL_MANAGED"
  port_name             = var.lb_backend_port_name
  protocol              = var.lb_backend_protocol
  timeout_sec           = var.lb_backend_timeout
  health_checks         = [var.healthcheck_output]
  locality_lb_policy    = "ROUND_ROBIN"
  session_affinity      = "GENERATED_COOKIE"
  affinity_cookie_ttl_sec= 3600
  log_config 
    enable = true
  

  backend 
    group           = var.ig_id
    balancing_mode  = "UTILIZATION"
    capacity_scaler = 1.0
  

感谢您的帮助。

【问题讨论】:

9 out of 10 a 405 是因为您正在对 GET 端点进行 POST,或者对 POST 端点等进行 GET。 【参考方案1】:

我通过负载平衡器本身生成的 cookie 解决了它配置粘性会话的问题。我试图使用循环 LB,但如果你必须保持会话打开,那没有任何意义,你必须使用环哈希。我会分享脚本(看后端服务):

  provider "google-beta" 
  project     = var.project
  region      = var.region
  credentials = var.credentials


resource "google_compute_region_ssl_certificate" "ssl-crt" 
  provider    = google-beta
  project     = var.project
  name_prefix = var.certificate_name
  region      = var.lb_region
  private_key = file("lb_http/certificate/privateKey.key")
  certificate = file("lb_http/certificate/certificate.crt")

  lifecycle 
    create_before_destroy = true
  


resource "google_compute_forwarding_rule" "lb-front-HTTP" 
  provider              = google-beta
  project               = var.project
  name                  = var.lb_http_front_name
  load_balancing_scheme = "INTERNAL_MANAGED"
  port_range            = var.lb_front_port_range
  target                = google_compute_region_target_http_proxy.lb-proxy-http.self_link
  region                = var.lb_region
  network               = var.lb_network
  subnetwork            = var.lb_subnetwork
  # ip_address            = "10.10.20.5"


resource "google_compute_forwarding_rule" "lb-front-HTTPS" 
  provider              = google-beta
  project               = var.project
  name                  = "lb-https-front"
  port_range            = "443"
  load_balancing_scheme = "INTERNAL_MANAGED"
  # ip_address            = "10.10.20.5"
  target     = google_compute_region_target_https_proxy.lb-proxy-https.self_link
  region     = var.lb_region
  network    = var.lb_network
  subnetwork = var.lb_subnetwork



resource "google_compute_region_target_http_proxy" "lb-proxy-http" 
  provider = google-beta
  name     = var.lb_proxy_name
  region   = var.lb_region
  project  = var.project
  url_map  = google_compute_region_url_map.lb_url_map.self_link


resource "google_compute_region_target_https_proxy" "lb-proxy-https" 
  provider         = google-beta
  name             = "test-proxy"
  region           = var.lb_region
  project          = var.project
  url_map          = google_compute_region_url_map.lb_url_map.self_link
  ssl_certificates = [google_compute_region_ssl_certificate.ssl-crt.id]



resource "google_compute_region_url_map" "lb_url_map" 
  provider        = google-beta
  project         = var.project
  name            = var.url_map_name
  region          = var.lb_region
  default_service = google_compute_region_backend_service.lb-backend.self_link



resource "google_compute_region_backend_service" "lb-backend" 
  provider                        = google-beta
  name                            = var.lb_backend_name
  region                          = var.lb_region
  project                         = var.project
  load_balancing_scheme           = "INTERNAL_MANAGED"
  port_name                       = var.lb_backend_port_name
  protocol                        = var.lb_backend_protocol
  timeout_sec                     = var.lb_backend_timeout
  health_checks                   = [var.healthcheck_output]
  locality_lb_policy              = "RING_HASH"
  session_affinity                = "GENERATED_COOKIE"
  affinity_cookie_ttl_sec         = 3600
  connection_draining_timeout_sec = 300
  log_config 
    enable = true
  

  consistent_hash 
    minimum_ring_size = 3
    http_cookie 
      ttl 
        seconds = 11
        nanos   = 1111
      
      name = "mycookie"
    
  

  backend 
    group           = var.ig_id
    balancing_mode  = "UTILIZATION"
    capacity_scaler = 1.0
  

【讨论】:

以上是关于错误 HTTP 状态 405 ?使用 GCP 负载平衡器时不允许的方法的主要内容,如果未能解决你的问题,请参考以下文章

DioError [DioErrorType.RESPONSE]: Http 状态错误 [405] [已解决]

在带有 HTTP 状态代码 405 的 REST API 中返回啥错误消息?

HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET - RequestDispatcher 出现错误

nginx错误码405是啥状态

REST - HTTP 状态 405;方法不允许;请求方法“PUT”不支持错误

Servlet 错误:HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET [重复]