Microservice: API Gateway

发布时间 2023-10-30 00:36:49作者: 小白冲冲

 

How to use Spring cloud gateway

1. create a maven modele for the API gateway

2. add the dependency (Search "gateway" in the start.spring.io) into the pom.xml

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

also add the dependency of the eureka client

<dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

add the annotation into Application

 and add these into application.properties

 3. define a route

 

spring.cloud.gateway.routes[0].id=product-service
spring.cloud.gateway.routes[0].uri=lb://product-service *lb means use load balancing
spring.cloud.gateway.routes[0].predicates[0]=Path=/api/product

So, when receive the request coincide the predicates then send it to the product service