Check out the full series of Questions Management tutorial here.
After creating a support-edge-server project, now is the time to go into the API Gateway implementation using the Zuul Proxy of Spring Cloud Netflix for Questions Management application.
As I introduced Zuul Proxy in this tutorial, first, what we need to do is add @EnableZuulProxy annotation to the SupportEdgeServerApplication class as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
package com.huongdanjava.qm.edgeserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy @SpringBootApplication public class SupportEdgeServerApplication { public static void main(String[] args) { SpringApplication.run(SupportEdgeServerApplication.class, args); } } |
Next, we will register the API Gateway with Eureka Server by configuring in the application.properties file as follows:
1 2 3 4 5 6 |
spring.application.name=Support Edge Server eureka.client.service-url.default-zone=http://localhost:8761 eureka.client.fetchRegistry=true server.port=8380 |
Our Questions Management application has 3 services that will interact directly with the outside, currently is API Question Service, API Category Service, API Option Service. Now we will declare these services in the API Gateway with Zuul Proxy as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
zuul.prefix=/qm zuul.routes.optionservice.serviceId=API Option Service zuul.routes.optionservice.path=/option/** zuul.routes.optionservice.strip-prefix=false zuul.routes.categoryservice.service-id=API Category Service zuul.routes.categoryservice.path=/category/** zuul.routes.categoryservice.strip-prefix=false zuul.routes.questionservice.service-id=API Question Service zuul.routes.questionservice.path=/question/** zuul.routes.questionservice.strip-prefix=false |
Here, I have configured all services with the URL request to start with “/qm” and we will keep the prefix path that we have declared. You can read more this tutorial to understand the above configurations! Here we have completed the construction of API Gateway already!