Как вызвать другого клиента eureka на сервере Zuul

приложение.свойства

zuul.routes.commonservice.path=/root/path/commonservice/**
zuul.routes.commonservice.service-id=commonservice

zuul.routes.customer.path=/root/path/customer/**
zuul.routes.customer.service-id=customer

zuul.routes.student.path=/root/path/student/**
zuul.routes.student.service-id=student 

и ниже мой пользовательский фильтр

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.openreach.gateway.common.constant.CommonConstant;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class HeaderFilter extends ZuulFilter {

    private static final Logger log = LoggerFactory.getLogger(HeaderFilter.class);

    @Override
    public String filterType() {
        return "pre";
    }
    @Override
    public int filterOrder() {
        return 1;
    }
    @Override
    public boolean shouldFilter() {
        return true;
    }
    @Override
    public Object run() {
        RequestContext context = RequestContext.getCurrentContext();

        HttpSession httpSession = context.getRequest().getSession();
        String idOrEmail = context.getRequest().getHeader("coustom");

        if (httpSession.getAttribute("someAttributes") == null) {
            if (idOrEmail != null) {
                //call the common-service and get details and set it first
                //then call the customer service with common-service details
            } else {
                //call the customer service
            }

        } else {
            log.info("data excits");
            // routrs the request to the backend with the excisting data details
        }

        context.addZuulResponseHeader("Cookie", "JSESSIONID=" + httpSession.getId());


        return null;
    }
}

Я использую балансировщик нагрузки ленты с zuul. Моя проблема в том, как мне сначала позвонить в службу общего пользования? Мне нужно, чтобы все мои запросы проверяли значение заголовка, а затем вызывали фактическую конечную точку службы.


person user2567005    schedule 25.06.2017    source источник
comment
Я думаю, вы можете использовать Feign client для вызова common-service.   -  person Bhushan    schedule 29.06.2017


Ответы (1)


Во-первых, используйте квалификатор @LoadBalanced, чтобы создать bean-компонент RestTemplate с балансировкой нагрузки.

@LoadBalanced
@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

И введите боб в фильтр

@Autowired
RestTemplate restTemplate;

Затем вы можете получить результат с помощью метода restTemplate, как показано ниже.

String result = restTemplate.postForObject("http://commonservice/url", object, String.class);

ссылка: http://cloud.spring.io/spring-cloud-static/spring-cloud.html#_spring_resttemplate_as_a_load_balancer_client

person Azarea    schedule 14.09.2017