2016년 8월 13일 토요일

Routing and Filtering by using Zuul proxy

You can route and filter request by using zuul proxy as below.

First of all, I set the eureka server that use port as 8761,
and set the client server that process the request actually and use port as 8080.
And the I set the proxy server that use port as 8880.

By using eureka, the proxy server can know the client server's ip and port automatically.

- Structure















- build.gradle

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

apply plugin: 'java'
apply plugin: 'spring-boot'

repositories {
    mavenCentral()
}

dependencies {
    compile('org.springframework.boot:spring-boot-starter-web:1.4.0.RELEASE')
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-zuul', version: '1.1.5.RELEASE'
    compile group: 'org.projectlombok', name: 'lombok', version: '1.16.10'
    compile group: 'org.springframework.cloud', name: 'spring-cloud-starter-eureka', version: '1.1.5.RELEASE'
}


- application.yml

spring:
  application:
    name: proxy-server
server:
  port: 8880
zuul:
  routes:
   client-server:
    path: /client/**


- SimpleFilter.java

package org.blog.test.proxy;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SimpleFilter extends ZuulFilter {
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.addZuulRequestHeader("zuul-proxy-header", "zuul-header-value");
        return null;
    }
}


- ProxyApplication.java

package org.blog.test;

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;

@SpringBootApplication
@EnableZuulProxy
@EnableEurekaClient
public class ProxyApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProxyApplication.class, args);
    }
}

- result

Proxy server add the header as zuul-proxy-header when requesting step,
and routing the request to the client server.



댓글 없음 :

댓글 쓰기