Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feign request filter - Using eureka metadata #17

Open
SeungpilPark opened this issue Mar 8, 2018 · 1 comment
Open

Feign request filter - Using eureka metadata #17

SeungpilPark opened this issue Mar 8, 2018 · 1 comment

Comments

@SeungpilPark
Copy link
Member

SeungpilPark commented Mar 8, 2018

feign 클라이언트 호출시 동작

Profile 필터

유레카에 등록된 앱중 metadata.profile 이 자신의 spring.profiles.active 와 동일한 것을 선택하여 필터링한다.

FeignRequestInterceptor.java

private void updateRibbonFilterContextHolder() {
        ApplicationContext applicationContext = ApplicationContextRegistry.getApplicationContext();
        Environment environment = applicationContext.getBean(Environment.class);
        String[] activeProfiles = environment.getActiveProfiles();

        //필터 초기화
        RibbonFilterContextHolder.getCurrentContext().remove("profile");

        String[] list = new String[]{"local", "dev", "stg", "prod"};
        List<String> filterProfiles = Arrays.asList(list);

        //자신의 액티브 프로파일을 필터에 추가
        for (String activeProfile : activeProfiles) {
            if (filterProfiles.contains(activeProfile)) {
                RibbonFilterContextHolder.getCurrentContext()
                        .add("profile", activeProfile);
            }
        }
    }

토큰 전달

TenantAwareFilter 에서 받은 토큰이 있다면, Feign 클라이언트 호출시 그대로 전달한다.

FeignRequestInterceptor.java

    public void apply(RequestTemplate template) {
.
.
        //오리지널 요청에 토큰이 있다면 토큰 전달
        String token = TokenContext.getThreadLocalInstance().getToken();
        if (!StringUtils.isEmpty(token)) {
            template.header("access_token", token);
        }
    }

빈 등록

FeignRequestInterceptorMetaworks4WebConfig 에 빈 등록.

    @Bean
    public FeignRequestInterceptor feignRequestInterceptor() {
        return new FeignRequestInterceptor();
    }
@SeungpilPark
Copy link
Member Author

6c9e11a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant