Spring Cloud Gateway 不路由(只有404)

时间:2021-03-09 09:47:10

标签: spring spring-boot netflix-eureka spring-cloud-gateway

我有一个 Spring Cloud Gateway、一个 eureka 服务注册中心和两个微服务。我将请求直接发送到服务没有问题:http://localhost:8081/auth。当我想使用网关 http://localhost:8080/auth 时,我总是收到 404 错误响应。服务和网关都连接到 eureka 服务器。 这是我的代码:

网关

application.properties:

server.port=8080
spring.application.name=gateway-service

spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

spring.cloud.gateway.routes[0].id=auth-service
spring.cloud.gateway.routes[0].uri=lb://AUTH-SERVICE
spring.cloud.gateway.routes[0].predicates[0]=Path=/auth/**
spring.cloud.gateway.routes[1].id=songs-service
spring.cloud.gateway.routes[1].uri=lb://SONGS-SERVICE
spring.cloud.gateway.routes[1].predicates[0]=Path=/songs/**
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ 

主要内容:

package htw.kbe_beleg

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.cloud.netflix.eureka.EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
class GatewayApplication {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            SpringApplication.run(GatewayApplication::class.java, *args)
        }
    }
} 

尤里卡

应用属性:

server.port= 8761

spring.application.name=discovery-service
eureka.instance.hostname=localhost


eureka.client.register-with-eureka=false   
eureka.client.fetch-registry=false

eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/

主要内容:

package htw.kbe_beleg

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer

@SpringBootApplication
@EnableEurekaServer
class EurekaServiceRegistration {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            SpringApplication.run(EurekaServiceRegistration::class.java, *args)
        }
    }
}

微服务

application.properties:

spring.datasource.url= jdbc:mysql://localhost:3306/authdb
spring.datasource.username= admin


server.port= 8081

spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQLDialect
spring.jpa.properties.hibernate.show_sql= true
spring.jpa.hibernate.naming.implicit-strategy= org.hibernate.boot.model.naming.ImplicitNamingStrategyLegacyJpaImpl
spring.jpa.hibernate.naming.physical-strategy= org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl

spring.application.name= auth-service
eureka.client.service-url.defaultZone= http://localhost:8761/eureka/

控制器:

package htw.kbe_beleg.controller

import htw.kbe_beleg.model.Auth
import htw.kbe_beleg.repository.AuthRepository
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class AuthController(val authRepository: AuthRepository) {

    @PostMapping("/auth", consumes = [MediaType.APPLICATION_JSON_VALUE], produces = [MediaType.TEXT_PLAIN_VALUE])
    fun authenticateUser(@RequestBody authUser: Auth): ResponseEntity<String> {
        ...........
    }
}

主要内容:

package htw.kbe_beleg

import org.springframework.boot.SpringApplication
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.cloud.netflix.eureka.EnableEurekaClient

@SpringBootApplication
@EnableEurekaClient
class AuthMain {
    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            SpringApplication.run(AuthMain::class.java, *args)
        }
    }
}

我已经完成了几乎所有相关的 stackoverflow 问题,更改了很多但没有结果并恢复了它。我只是看不到我错过了什么。希望你能帮助我。

1 个答案:

答案 0 :(得分:1)

为我感到羞耻。问题是我的网关依赖错误。我只有 spring-cloud-starter-config,但我需要 spring-cloud-starter-gateway... 花了我一个多星期。

相关问题