Spring Cloud Zuul - redutan/redutan.github.io GitHub Wiki
- ๋ทํ๋ฆญ์ค์์ ๋ง๋ API Gateway
- https://github.com/Netflix/zuul
Zuul is a gateway service that provides dynamic routing, monitoring, resiliency, security, and more.
- ๋ทํ๋ฆญ์ค์์ ๋ง๋ฌ
- java๋ก ๋์ด ์๊ธฐ ๋๋ฌธ์ proxy ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ฉด์๋ ViewTemplate(ex:FreeMarker)๋ฅผ ์ด์ฉํด์ ํ์ด์ง๋ฅผ ์ง์ ์๋น์คํ ์๋ ์๋ค.
- spring-boot starter์ด ๊ธฐ๋ณธ ์ ๊ณต๋์ด์ application.yml์ ์ค์ ์ ํตํฉํ ์ ์๋ค.
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'
}
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
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())));
}
}