Skip to content
정명주(myeongju.jung) edited this page Feb 26, 2018 · 2 revisions

Zuul

Zuul is a gateway service that provides dynamic routing, monitoring, resiliency, security, and more.

Zull을 선택한 이유?

  • 넷플릭스에서 만듬
  • java로 되어 있기 때문에 proxy 기능을 사용하면서도 ViewTemplate(ex:FreeMarker)를 이용해서 페이지를 직접 서비스할 수도 있다.
  • spring-boot starter이 기본 제공되어서 application.yml에 설정을 통합할 수 있다.

Zuul in Netflix’s Cloud Architecture

Zuul in Netflix’s Cloud Architecture

Zuul Filters

Zuul Filters Lifecycle

Dependencies

build.gradle

compile('org.springframework.cloud:spring-cloud-starter-zuul') {
    exclude group: 'com.google.code.findbugs', module: 'annotations'
    exclude group: 'com.google.code.findbugs', module: 'jsr305'
}

Configuration

ZuulConfiguration.java

/**
 * Zuul api-gw 설정
 * 입맛에 맞게 필터들을 추가하면 된다.
 * @author myeongju.jung
 */
@Configuration("zuulCustomConfiguration")   // Bean명칭 충돌방지
@EnableZuulProxy
public class ZuulConfiguration {
 
    @Bean
    Servlet30WrapperFilter servlet30WrapperFilter() {
        return new Servlet30WrapperFilter();
    }
 
    @Bean
    DebugFilter debugFilter() {
        return new DebugFilter();
    }
 
    @Bean
    ApiFirstPreFilter apiFirstPreFilter() {
        return new ApiFirstPreFilter();
    }
}

application.yml

# zuul api-gw
# zuul api-gw
zuul:
  routes:
    post:
      path: /api/post/**
      url: http://www.post.com/api

Test

PostApiRootGwTest.java

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class PostApiRootGwTest {
    @Autowired
    TestRestTemplate restTemplate;
 
    @Ignore("해당 통합 테스트를 진행하기 위해서는 post-api 서버가 기동되어 있어야 합니다.")
    @Test
    public void samples() throws Exception {
        // given
        // when
        ResponseEntity<Collection<Sample>> responseEntity =
            restTemplate.exchange("/api/post/samples", HttpMethod.GET, HttpEntity.EMPTY,
                new ParameterizedTypeReference<Collection<Sample>>() {
                });
        Collection<Sample> samples = responseEntity.getBody();
        // then
        assertThat(samples.isEmpty(), is(false));
        samples.forEach(this::assertSample);
    }
 
    private void assertSample(Sample sample) {
        System.out.println(sample);
        assertThat(sample.getSeq(), is(greaterThan(0L)));
        assertThat(sample.getTitle(), is(notNullValue()));
        assertThat(sample.getContent(), is(notNullValue()));
        assertThat(sample.getHit(), is(greaterThanOrEqualTo(0)));
        assertThat(sample.getCreatedAt(), is(lessThan(ZonedDateTime.now())));
    }
}
Clone this wiki locally