Microservice: API Gateway

 

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

Start it. It will register in the discovery server.

 Now, use the url of the API-GATEWAY can arrive to product-service.
For instance: 
api-gateway is in port 8080, product-service is in 6479. Use http://localhost:8080/api/product can get the response of the product service

 

Use api-gateway to arrive dicovery-server page. Set in the application.properties of the api-gateway

 Now, we can access the discovery-server with url: localhost:8080/eureka/web

 

posted @ 2023-10-30 00:34  小白冲冲  阅读(51)  评论(0)    收藏  举报