Sunday, 28 July 2019

Configuring Edge (proxy) server – Zuul

Zuul is an edge server or proxy server, and serves the requests of external applications such as UI client, Android/iOS app, or any third-party consumer of APIs offered by the product or service. Conceptually, it is a door to external applications.

Zuul allows dynamic routing and monitoring of requests. It also performs security operations like authentication. It can identify authentication requirements for each resource and reject any request that does not satisfy them.

In this Tutorial You’ll write a simple microservice application and then build a reverse proxy application that uses Netflix Zuul to forward requests to the service application. You’ll also see how to use Zuul to filter requests made through the proxy service.

Here I have created two projects Profile Service and Zuul Gateway. I am going to make all the changes to the Zuul Gateway Project. I can access any Profile Service using their port also using this proxy.

I will show here only the ZuulGateway Setup. InProfile Service just configure any sample service.

Gradle dependencies.

buildscript {
ext {
springBootVersion = '1.4.0.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'spring-boot'
apply plugin: 'io.spring.dependency-management'

jar {
baseName = 'gateway'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
}


dependencies {
compile('org.springframework.cloud:spring-cloud-starter-zuul')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Brixton.SR5"
}
}


eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}

Create a Filter class.

package com.example.sayhello;

import javax.servlet.http.HttpServletRequest;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.ZuulFilter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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;
  }

}

Inject the Filter class in the Main class. Also Enable Zuul Proxy.

package com.example.sayhello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@RestController
@EnableZuulProxy
public class SayHelloApplication {
@Bean
  public SimpleFilter simpleFilter() {
    return new SimpleFilter();
  }

public static void main(String[] args) {
SpringApplication.run(SayHelloApplication.class, args);
}

}

Configuration.

Below are the configuration in the ZuulGateway.

/ZuulGateway/src/main/resources/application.properties

zuul.routes.profileService.url=http://localhost:8080

ribbon.eureka.enabled=false

server.port=8093

spring.application.name=ZuulGateway

Here profileservice is the name of the spring boot application. and Url is the Url of the Project profileService.

Now I can access the Profile service using the Following Url.

Original Url: http://localhost:8080/user/name/sarath

Proxy Url: http://localhost:8093/profileService/user/name/sarath

We get the Following log information from the filter class.

2019-07-28 19:08:53.410  INFO 20184 --- [io-8093-exec-10] com.example.sayhello.SimpleFilter        : GET request to http://localhost:8093/profileService/user/name/sarath

Happy Learning !!!! 

No comments:
Write comments