In a previous article, we have declared/defined the Zuul routes by providing the service details (URL) manually (Click here to visit that article). That means we have provided the domain name or ip address and port number of each service. Just think of a situation where the application contains a large number of microservices. Do you think that it is practical to find (manually) the server details (ip address/domain) and port details of every service? If that is difficult, then how do we declare the zuul route mapping to expose each service through the Zuul Proxy?
“The solution is to perform the Zuul routes mapping with serviceId registered in the Eureka Server“.
Here i am not going to discuss about the importance of Netflix Zuul Proxy or Netflix Eureka server. I have already written two separate articles on both of those areas. If you need to refresh your knowledge on those areas, please refer the relevant articles.
- Click here to view the article on Netflix Zuul Proxy.
- Click here to view the article on Netflix Eureka Server.
What we are going to do here?
In order to demonstrate the serviceId based Zuul route mapping, we will be creating following set of applications.
- Eureka Server :- Spring Boot Applciation to act as Eureka Server. All the microservices will be registered here.
- Zuul Proxy: – Spring Boot Application to act as Zuul Reverse Proxy. This is the centralized gateway for directing all the requests for the misroservices. Zuul proxy will communicate with Eureka server to get the details (ip address and port) of the relevant microservice for delegating the client request.
- student-service :- just dummy microservice for representing the backend business service.
Lets create them one by one. The full source code of this application can be found at GitHub.
Eureka Server
Eureka Server is just another spring boot application with Spring Cloud Netflix Eureka dependency. Then annotate the main spring boot configuration class with @EnableEurekaServer annotation.
Therefore create a spring boot application with Eureka dependency.
Then add the @EnableEurekaServer annotation to the main Spring Boot Application configuration class (That is the class annotated with @SpringBootApplication annotation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.springframework.boot.SpringApplication; | |
import org.springframework.boot.autoconfigure.SpringBootApplication; | |
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; | |
@EnableEurekaServer | |
@SpringBootApplication | |
public class EurekaServerApplication { | |
public static void main(String[] args) { | |
SpringApplication.run(EurekaServerApplication.class, args); | |
} | |
} |
application.properties (Eureka Server)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server.port=8761 | |
eureka.client.register-with-eureka=false | |
eureka.client.fetch-registry=false | |
logging.level.com.netflix.eureka=OFF | |
logging.level.com.netflix.discovery=OFF |