Zuul (Proxy & API Gateway)
This commit is contained in:
@@ -127,7 +127,7 @@ GET http://localhost:8070/event/member/hyori
|
||||
---
|
||||
|
||||
***- Zuul (Proxy & API Gateway)***<br />
|
||||
자세한 설명은 [여기](https://assu10.github.io/dev/2020/08/26/netflix-zuul/) 를 참고
|
||||
자세한 설명은 [여기](https://assu10.github.io/dev/2020/08/26/netflix-zuul/)와 [여기](https://assu10.github.io/dev/2020/09/05/netflix-zuul2/) 를 참고
|
||||
|
||||
```shell script
|
||||
HOW TO RUN
|
||||
@@ -156,6 +156,9 @@ http://localhost:5555/api/evt/event/member/hyori
|
||||
-- 주울을 통해 회원 서비스의 REST API 호출 (RestTemplate)
|
||||
http://localhost:5555/api/mb/member/gift/flower
|
||||
|
||||
-- 사전/사후 필터 동작 확인 (상관관계 ID 생성 및 하위 서비스에 전파/응답 헤더에 상관관계 ID 삽입)
|
||||
http://localhost:5555/api/mb/member/name/hyori
|
||||
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
@@ -11,8 +11,8 @@ import org.springframework.web.bind.annotation.RestController;
|
||||
@RequestMapping("/event")
|
||||
public class EventController {
|
||||
|
||||
private CustomConfig customConfig;
|
||||
private MemberFeignClient memberFeignClient;
|
||||
private final CustomConfig customConfig;
|
||||
private final MemberFeignClient memberFeignClient;
|
||||
|
||||
public EventController(CustomConfig customConfig, MemberFeignClient memberFeignClient) {
|
||||
this.customConfig = customConfig;
|
||||
|
||||
@@ -13,8 +13,8 @@ import javax.servlet.ServletRequest;
|
||||
@RequestMapping("/member")
|
||||
public class MemberController {
|
||||
|
||||
private CustomConfig customConfig;
|
||||
private EventRestTemplateClient eventRestTemplateClient;
|
||||
private final CustomConfig customConfig;
|
||||
private final EventRestTemplateClient eventRestTemplateClient;
|
||||
|
||||
public MemberController(CustomConfig customConfig, EventRestTemplateClient eventRestTemplateClient) {
|
||||
this.customConfig = customConfig;
|
||||
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.assu.cloud.zuulserver.filters;
|
||||
|
||||
import com.assu.cloud.zuulserver.utils.FilterUtils;
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import com.netflix.zuul.exception.ZuulException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 사후 필터
|
||||
* 서비스 호출자에게 다시 전달될 HTTP 응답 헤더에 상관관계 ID 를 삽입하여 사용자 트랜잭션과 연관된 로깅을 연결 지음
|
||||
*/
|
||||
@Component
|
||||
public class PostFilter extends ZuulFilter {
|
||||
/** 해당 타입의 다른 필터와 비교해 실행되어야 하는 순서 */
|
||||
private static final int FILTER_ORDER = 1;
|
||||
|
||||
/** 필터 활성화 여부 */
|
||||
private static final boolean SHOULD_FILTER = true;
|
||||
private static final Logger logger = LoggerFactory.getLogger(PostFilter.class);
|
||||
|
||||
private final FilterUtils filterUtils;
|
||||
|
||||
public PostFilter(FilterUtils filterUtils) {
|
||||
this.filterUtils = filterUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* 구축하려는 필터의 타입 지정 (사전, 라우팅, 사후)
|
||||
*/
|
||||
@Override
|
||||
public String filterType() {
|
||||
return FilterUtils.POST_FILTER_TYPE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 해당 타입의 다른 필터와 비교해 실행되어야 하는 순서
|
||||
*/
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return FILTER_ORDER;
|
||||
}
|
||||
|
||||
/**
|
||||
* 필터 활성화 여부
|
||||
*/
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
return SHOULD_FILTER;
|
||||
}
|
||||
|
||||
/**
|
||||
* 필터의 비즈니스 로직 구현
|
||||
* 원래 HTTP 요청에서 전달된 상관관계 ID 를 가져와 응답에 삽입
|
||||
*/
|
||||
@Override
|
||||
public Object run() {
|
||||
RequestContext ctx = RequestContext.getCurrentContext();
|
||||
|
||||
logger.debug("============ Adding the correlation id to the outbound headers. {}", filterUtils.getCorrelationId());
|
||||
// 원래 HTTP 요청에서 전달된 상관관계 ID 를 가져와 응답에 삽입
|
||||
ctx.getResponse().addHeader(FilterUtils.CORRELATION_ID, filterUtils.getCorrelationId());
|
||||
logger.debug("============ Completing outgoing request for {}.", ctx.getRequest().getRequestURI());
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -18,12 +18,6 @@ import java.util.UUID;
|
||||
@Component
|
||||
public class PreFilter extends ZuulFilter {
|
||||
|
||||
private FilterUtils filterUtils;
|
||||
|
||||
public PreFilter(FilterUtils filterUtils) {
|
||||
this.filterUtils = filterUtils;
|
||||
}
|
||||
|
||||
/** 해당 타입의 다른 필터와 비교해 실행되어야 하는 순서 */
|
||||
private static final int FILTER_ORDER = 1;
|
||||
|
||||
@@ -31,6 +25,12 @@ public class PreFilter extends ZuulFilter {
|
||||
private static final boolean SHOULD_FILTER = true;
|
||||
private static final Logger logger = LoggerFactory.getLogger(PreFilter.class);
|
||||
|
||||
private final FilterUtils filterUtils;
|
||||
|
||||
public PreFilter(FilterUtils filterUtils) {
|
||||
this.filterUtils = filterUtils;
|
||||
}
|
||||
|
||||
/**
|
||||
* 구축하려는 필터의 타입 지정 (사전, 라우팅, 사후)
|
||||
*/
|
||||
@@ -71,7 +71,7 @@ public class PreFilter extends ZuulFilter {
|
||||
|
||||
/**
|
||||
* 필터의 비즈니스 로직 구현
|
||||
* 서비스가 필터를 통과할때마다 실행되는 메서드
|
||||
* 서비스가 필터를 통과할 때마다 실행되는 메서드
|
||||
* 상관관계 ID의 존재 여부 확인 후 없다면 생성하여 헤더에 설정
|
||||
*/
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user