As we know in microservice architecture we have many application and every application have some end points. But it is difficult if we integrate our application with any front end technology. For that we have to define many end point and at some point it becomes hard to maintain the endpoints. So we have to give a common API address to other front end technology and that is also secure because now our Zuul API gateway address is open to others and our rest of application end Points are secured on private IP server . So spring cloud provide Netflix zuul Server for this that is used for routing the other application end Points. To use Netflix Zuul API gateway we have to follow below steps:
- Add required dependency in your pom.xml file.
- Use @EnableZuulProxy annotation in your main class.
- Register this Zuul API gateway application With Eureka Server.
- Provide routing endPoints property in your application.properties file.
- Run Eureka Server and all other application and use your zuul gateway url to access all other application end Points.
Summary of Project: In this Project we have created Four Application.
- An Eureka Server application that is used to monitor other application.
- An Student application that is registered with Eureka Server and have a GET end Point name /student.
- An Teacher application that is registered with Eureka Server and have a GET end Point name /teacher
- An Zuul API Gateway that is also register with Eureka Server and provide URL using which we can access Teacher and Student end points.
First Three Steps of Project Summary we have already know how to create if not than see my blogs on Microservices ( CLICK ME ). In this we focus on Fourth point how to create zuul API gateway. So now flow above given steps:
1- Add required dependency in your pom.xml file.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency>
2- Use @EnableZuulProxy annotation in your main class.
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableEurekaClient @EnableZuulProxy @SpringBootApplication public class AccountServiceApplication { public static void main(String[] args) { SpringApplication.run(AccountServiceApplication.class, args); } }
3- Now in this steps we have to register our Zuul API gateway with Eureka Server and also provide routing to others endPoint. So this use below properties in your application.properties file.
server.port=8082 spring.application.name=gateway-service eureka.client.service-url.default-zone=http://localhost:8761/eureka zuul.routes.student-service.path=/studentGateway/** zuul.routes.teacher-service.path=/teacherGateway/**
In the above properties zuul.routes.student-service.path
zuul.routes is fixed student-service is the name of application that you have defined for Student application in your Student application.properties file after that append path and in value you can give any prefix that you want here we give /studentGateway/** Now our property become
zuul.routes.student-service.path=/studentGateway/**
Same is For teacher property Now you have to hit the below URL for access Student and Teacher end point response.
To Access Student URL hit below URL
http://localhost:8082/studentGateway/student
To Access teacher URL hit below URL
http://localhost:8082/teacherGateway/teacher/
If You want to give any prefix also you can add that in your Zuul API gateway application.properties file.
zuul.prefix=/gatewayAPI
Now your application.properties look like
server.port=8082 spring.application.name=gateway-service eureka.client.service-url.default-zone=http://localhost:8761/eureka zuul.prefix=/gatewayAPI zuul.routes.student-service.path=/studentGateway/** zuul.routes.teacher-service.path=/teacherGateway/**
Now your Student and Teacher URL become.
http://localhost:8082/gatewayAPI/studentGateway/student http://localhost:8082/gatewayAPI/teacherGateway/teacher/
Complete Code On GitHub : Zull Gateway Routing


