spring云网关未路由(仅404)

xt0899hw  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(285)

我有一个spring云网关,一个eureka服务注册中心和两个微服务。我可以通过以下方式将请求直接发送到服务:http://localhost:8081/认证。当我想使用网关时http://localhost:8080/auth,我总是得到404错误响应。服务和网关都连接到eureka服务器。这是我的密码:
网关
应用程序属性:

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)
        }
    }
}

Eureka
应用程序属性:

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)
        }
    }
}

微服务
应用程序属性:

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问题,改变了很多,但没有结果,并恢复了它。我只是看不到我错过了什么。希望你能帮助我。

zkure5ic

zkure5ic1#

我真丢脸。问题是我对我的网关有错误的依赖关系。我只有SpringCloudStarter配置,但我需要SpringCloudStarter网关。。。花了我一个多星期。

相关问题