如何在Zuul服务器中调用其他eureka客户端

时间:2022-08-24 15:40:05

application.properties

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 

and below is my custom filter

以下是我的自定义过滤器

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;
    }
}

I'm using the ribbon load balancer with zuul. My problem is that how should I call the common-service first? I need all my requests to check the header value and then call the actual service end point.

我正在使用带有zuul的色带负载平衡器。我的问题是我应该如何首先调用公共服务?我需要所有请求来检查标头值,然后调用实际的服务端点。

1 个解决方案

#1


0  

First, use the @LoadBalanced qualifier to create your RestTemplate bean which is load balanced.

首先,使用@LoadBalanced限定符来创建负载平衡的RestTemplate bean。

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

And Inject the bean into the filter

并将豆子注入过滤器

@Autowired
RestTemplate restTemplate;

Then you can get result by restTemplate's method like below

然后你可以通过restTemplate的方法获得结果,如下所示

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

ref: http://cloud.spring.io/spring-cloud-static/spring-cloud.html#_spring_resttemplate_as_a_load_balancer_client

#1


0  

First, use the @LoadBalanced qualifier to create your RestTemplate bean which is load balanced.

首先,使用@LoadBalanced限定符来创建负载平衡的RestTemplate bean。

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

And Inject the bean into the filter

并将豆子注入过滤器

@Autowired
RestTemplate restTemplate;

Then you can get result by restTemplate's method like below

然后你可以通过restTemplate的方法获得结果,如下所示

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

ref: http://cloud.spring.io/spring-cloud-static/spring-cloud.html#_spring_resttemplate_as_a_load_balancer_client