1. Overview

This article is about to Spring boot request routing example using zuul API. zuul API is used to route request which is specially use for micro service architecture, We can take zuul routing advantages as bellow:

  • Migration of old service to new service
  • Redirect specific request to another domain or submain for manage load
  • Redirect request to specific services means single point of entry in application.
    • i.e request start with /account redirect account service
    • i.e request start with /admin redirect to admin service.
    • Syntax : zuul.routers.[SERVICE_NAE]:[URL_PATTERN]
    • In bellow example all request /users/* redirect to user service.
   users: /users/**

2. Example

Spring boot request routing example

Spring boot request routing example

2.1 pom.xml

Zuul API require of group org.springframework.cloud and artifact Id spring-cloud-starter-netflix-zuul in classpath.

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <description>Spring boot request routing example</description>
    <!-- Inherit defaults from Spring Boot -->

        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-zuul -->
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>      <!-- zuul api has been used for spring boot request routing  -->


    <!-- Package as an executable jar -->

2.2 application.yml

/api-1.0/** redirect to /api-2.0/** means that /api-1.0/demo to /api-2.0/demo for more details refer spring boot document

      path: /api-1.0/**
      url: forward:/api-2.0

# /api-1.0/** redirect to /api-2.0/** means that /api-1.0/demo to /api-2.0/demo

2.3 SpringBootConfig

@EnableZuulProxy annotation will enable zuul configuration.

package com.javadeveloperzone;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.ComponentScan;

 * Created by JavaDeveloperZone on 19-07-2017.

@ComponentScan // Using a root package also allows the @ComponentScan annotation to be used without needing to specify a basePackage attribute
@EnableZuulProxy        // to enable zuul request routing
public class SpringBootConfig {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringBootConfig.class, args);            // it wil start application

2.4 ZuulAPIController

This controller will full-fill both request new API as well as old API means that /api-1.0/demo and /api-2.0/demo will be handle here:

package com.javadeveloperzone.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

 * Created by javadeveloperzone on 15-12-2017.
public class ZuulAPIController {

    public String getData(){
        return "Response from API-2.0";

2.5 Output:

http://localhost:8080/api-1.0/demo is pointing (routing) to http://localhost:8080/api-2.0/demo

Spring boot request routing example output

Spring boot request routing example output

Let Check Endpoints

1./routes endpoint will show information about configure routers in zuul:



2./routes?format=details will display more specific information about routing in zuul:



3. References


Was this post helpful?
Let us know, if you liked the post. Only in this way, we can improve us.

Leave a Reply

Your email address will not be published. Required fields are marked *