In this tutorial, you will learn how to configure your Spring Cloud API Gateway to automatically create routes based on services registered with Discovery clients like Eureka, Consul, or Zookeeper.
Enable Discovery Locator
A very quick way to enable Spring Cloud API Gateway to route HTTP requests to a Microservice registered with discovery service is to simply enable the Discovery Locator.
To enable the automatic mapping of routes in your Spring Cloud API Gateway project, open the application.properties file and add the following configuration properties.
spring.cloud.gateway.discovery.locator.enabled = true spring.cloud.gateway.discovery.locator.lowerCaseServiceId = true
By enabling the discovery locator you can skip creating API Gateway routes manually.
Let’s assume we have a Spring Boot Microservice registered with Eureka with the name “users-ws” as is illustrated in the image below.
If you enable Discovery Locator in the application.properties of your Spring Cloud API Gateway, then the HTTP request URL to this microservice will look like in the CURL command below.
curl --location --request GET 'http://localhost:8082/users-ws/check'
- 8082 – is a port number of which Spring Cloud API Gateway is running,
- users-ws – is an application name of Spring Boot microservice registered with Eureka,
- /check – is a web service endpoint exposed by users-ws microservice.
I hope this short tutorial was of good value to you. If you are interested to learn more about building RESTful Web Services with Spring Boot and Spring Cloud, then check Spring Cloud tutorials on this website.