Create a Simple Gateway for the MicroService Architecture with Spring Netflix Zuul

Create a new project. In this example I called it Gateway. You can download the complete code that contains 5 microServices and the gateway from GitHub:

In the Gateway

  1. Add the following Maven dependencies:
  2.     <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.5.12.RELEASE</version>
            <relativePath/>
        </parent>
    
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zuul</artifactId>   
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>    
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Camden.SR7</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
            
        
    
    If you have issues to launch the service in linux read the article Installing Spring Boot services in Amazon AWS as an init.d (I) your maven build part may be incorrect
  3. application.properties: Add the gateway port, this will be the port you call and that will redirect to the microservice port. Add the url, the port (underline), and the name of the microservice (underline).
  4. zuul.routes.metadata.url=http://localhost:7170
    ribbon.eureka.enabled=false
    server.port=8443
    
  5. Create the class Application.java:
    @EnableZuulProxy
    @SpringBootApplication
    public class Application {
    
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    
        @Bean
        public SimpleFilter simpleFilter() {
            return new SimpleFilter();
        }
    
    }
    
    
  6. Create the class SimpleFilter.java:
  7. public class SimpleFilter extends ZuulFilter {
    
      private static Logger log = LoggerFactory.getLogger(SimpleFilter.class);
    
      @Override
      public String filterType() {
        return "pre";
      }
    
      @Override
      public int filterOrder() {
        return 1;
      }
    
      @Override
      public boolean shouldFilter() {
        return true;
      }
    
      @Override
      public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        HttpServletRequest request = ctx.getRequest();
    
        log.info(String.format("%s request to %s", request.getMethod(), 
            request.getRequestURL().toString()));
    
        return null;
      }
    
    }
    
    

In the MicroService

  1. application.properties, notice the name and the port should match with the underlined ones in the Zuul gateway:
    spring.application.name=metadata
    server.port=7170
    
  2. Last, check with the browser that everything is working. You will call the Gateway port 8443 and this will be redirected to the service port 7170. Add as many services as needed. Remember the underlined name is taken from the given name in the application.properties. You have to add this name in front of the REST controller URI:
    http://loclhost:8443/metadata/test
    Metadata up & running!
    

Comments