I'm working on a project with multiple microservices registered with a Eureka Discovery Server. I've set up a Spring Cloud Gateway as an Eureka client and configured the routes accordingly. However, my requests through the Gateway result in 404 errors, while direct requests to the microservices succeed.
API Gateway application.properties:
Код: Выделить всё
eureka.client.service-url.defaultZone= http://localhost:8761/eureka/ spring.application.name=api-gateway server.port=8080 ##Product Service Route spring.cloud.gateway.routes[0].id=product-service spring.cloud.gateway.routes[0].uri=lb://product-service spring.cloud.gateway.routes[0].predicates[0]=Path=/api/product ##Order Service Route spring.cloud.gateway.routes[1].id=order-service spring.cloud.gateway.routes[1].uri=lb://order-service spring.cloud.gateway.routes[1].predicates[0]=Path=/api/order
Код: Выделить всё
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ApiGatewayApplication { public static void main(String[] args) { SpringApplication.run(ApiGatewayApplication.class, args); } }
Код: Выделить всё
api-gateway 21 21 UTF-8 2023.0.0 org.springframework.cloud spring-cloud-starter-gateway-mvc org.springframework.cloud spring-cloud-starter-netflix-eureka-client org.springframework.boot spring-boot-starter-actuator org.springframework.cloud spring-cloud-dependencies ${spring-cloud.version} pom import org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
order-service and product-service application.properties:
Код: Выделить всё
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/ spring.application.name=order-service # for order-service # spring.application.name=product-service # for product-service
Код: Выделить всё
@RestController @RequestMapping("/api/order") @RequiredArgsConstructor public class OrderController { private final OrderService orderService; @PostMapping @ResponseStatus(HttpStatus.CREATED) public String placeOrder(@RequestBody OrderRequest orderRequest) { orderService.placeOrder(orderRequest); return "Order placed successfully"; } // ... other methods ... }
Код: Выделить всё
@RestController @RequestMapping("/api/product") @RequiredArgsConstructor public class ProductController { private final ProductService productService; // ... methods ... }
When making a POST request to http://localhost:8080/api/order via Postman, I receive the following error:
Код: Выделить всё
{ "timestamp": "2024-03-08T20:08:59.340+00:00", "status": 404, "error": "Not Found", "path": "/api/order" }
Код: Выделить всё
http://localhost:54062/api/order
Код: Выделить всё
Order placed successfully
What am I missing in my Gateway setup that's preventing it from routing requests to the microservices? Any insights or troubleshooting tips would be greatly appreciated.
Источник: https://stackoverflow.com/questions/781 ... roservices