我有一个Spring Cloud Gateway,一个eureka服务注册表和两个微服务。我使用:http://localhost:8081/auth直接向服务发送请求没有问题。当我想使用网关http://localhost:8080/auth时,我总是得到404错误响应。服务和网关都连接到尤里卡服务器。下面是我的代码:
网关
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)
}
}
}
Microservice
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问题,改变了很多,没有结果,并恢复了它。我只是看不出我错过了什么。希望你能帮助我。
我真丢脸。问题是我有错误的依赖于我的网关。我只有spring-cloud-starter-config,但我需要spring-cloud-starter-gateway…花了我一个多星期。