spring cloud ribbon irule如何获取到request里的内容?

开源之夏第三届火热来袭,高校学生参与赢万元奖金!>>>

spring cloud ribbon irule如何获取到request里的内容?

spring cloud ribbon irule如何获取到request里的内容?

回答

你好,问题解决了吗?irule策略中如何获取 request

你好,请问这个问题解决了吗

@Override
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
   ILoadBalancer loadBalancer = getLoadBalancer(serviceId);
   // 问题解答:方法调用时,未传入request对象,所以在IRule中也获取不到
Server server = getServer(loadBalancer);
   if (server == null) {
      throw new IllegalStateException("No instances available for " + serviceId);
   }
   RibbonServer ribbonServer = new RibbonServer(serviceId, server, isSecure(server,
         serviceId), serverIntrospector(serviceId).getMetadata(server));
   return execute(serviceId, ribbonServer, request);
}

解决方案:有很多种解决方案,可以添加 一个 @Bean

实现 LoadBalancerClient 继承 RibbonLoadBalancerClient
重写,通过request对象获取参数
@Override
public <T> T execute(String serviceId, LoadBalancerRequest<T> request) throws IOException {
   ILoadBalancer loadBalancer = getLoadBalancer(serviceId);
// 通过 request 到的request对象
   // Server server = getServer(loadBalancer);
// request对象向下传递
   Server server = getServer(loadBalancer, request);
   if (server == null) {
      throw new IllegalStateException("No instances available for " + serviceId);
   }
   RibbonServer ribbonServer = new RibbonServer(serviceId, server, isSecure(server,
         serviceId), serverIntrospector(serviceId).getMetadata(server));

   return execute(serviceId, ribbonServer, request);
}
protected Server getServer(ILoadBalancer loadBalancer, request) {
   if (loadBalancer == null) {
      return null;
   }
// chooseServerapi传递是Object,在chooseServer的方法里面强转即可
   return loadBalancer.chooseServer(request); // TODO: better handling of key
}

自定义的类继承了RibbonLoadBalancerClient,但是代码却走不进来,这个可能是啥问题引起的啊IRule如何获取request里的参数呢,(想根据request参数去指定访问某个服务)

自定义的类继承了RibbonLoadBalancerClient,但是代码却走不进来,这个可能是啥问题引起的啊