Skip to content

SpringCloud-LoadBalancer: How to get Header from Request? #1090

@alice-index

Description

@alice-index

I'm using SpringCloud-LoadBalancer and want to code a loadBalancer which select instance by header "x-area".Here is the code:

public class CustomLoadBalancer extends RoundRobinLoadBalancer{
...
...
    @Override
    public Mono<Response<ServiceInstance>> choose(Request request){
        DefaultRequestContext requestContext = (DefaultRequestContext) request.getContext();
        RequestData clientRequest = (RequestData) requestContext.getClientRequest();
        HttpHeaders headers = clientRequest.getFirst("x-area");
        .....
    }
...
...
}

But the requestContext.getClientRequest() cannot cast to RequestData,and here is the debug info:
捕获

The LoadBalancerRequestFactory$lambda cannot cast to RequestData,But the arg$2 is what I need,how can I get It? or There is another way which can get header?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions