Spring Cloud Gateway с Eureka Discovery не маршрутизирует запросы к микросервисамJAVA

Программисты JAVA общаются здесь
Ответить Пред. темаСлед. тема
Гость
 Spring Cloud Gateway с Eureka Discovery не маршрутизирует запросы к микросервисам

Сообщение Гость »


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 
ApiGatewayMain:

Код: Выделить всё

 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);     } } 
pom.xml:

Код: Выделить всё

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                                                                                           
Both my microservices (order-service and product-service) have server.port=0 for random port selection and are registered with Eureka:

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 
OrderController:

Код: Выделить всё

@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 ... } 
ProductController:

Код: Выделить всё

@RestController @RequestMapping("/api/product") @RequiredArgsConstructor public class ProductController {     private final ProductService productService;     // ...  methods ... } 
Issue:

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" } 
However, directly calling the order-service using its dynamically assigned port, such as

Код: Выделить всё

http://localhost:54062/api/order
, works correctly and I receive a

Код: Выделить всё

Order placed successfully
response.

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
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «JAVA»