1
votes

I have a Spring Cloud Gateway, a eureka service registry and two microservives. I have no problems with sending the request directely to the sevice with: http://localhost:8081/auth. When i want to use the Gateway http://localhost:8080/auth, i always get a 404 error response. The services and the gateway all connect to the eureka server. Here is my code:

Gateway

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/ 

Main:

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

application properties:

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/

Main:

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/

Controller :

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> {
        ...........
    }
}

Main:

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

I already gone through nearly every related stackoverflow question, changed a lot with no result and reverted it. I just cant see what am i missing. Hope you can help me.

1

1 Answers

2
votes

Shame on me. Problem was that i had the wrong dependencies for my gateway. I only had spring-cloud-starter-config, but i needed spring-cloud-starter-gateway... Took me over a week.