java.lang.IllegalStateException: block()/blockFirst()/blockLast() are blocking, which is not supported in thread reactor-http-nio-3 at reactor.core.publisher.BlockingSingleSubscriber.blockingGet(BlockingSingleSubscriber.java:83) Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
throw newIllegalStateException("block()/blockFirst()/blockLast() are blocking, which is not supported in thread " + Thread.currentThread().getName());
1
public <T> ServiceInstance choose(String serviceId, Request<T> request) { ReactiveLoadBalancer<ServiceInstance> loadBalancer = this.loadBalancerClientFactory.getInstance(serviceId); if (loadBalancer == null) { returnnull; } else { Response<ServiceInstance> loadBalancerResponse = (Response)Mono.from(loadBalancer.choose(request)).block(); return loadBalancerResponse == null ? null : (ServiceInstance)loadBalancerResponse.getServer(); } } final T blockingGet() { if (Schedulers.isInNonBlockingThread()) { thrownew IllegalStateException("block()/blockFirst()/blockLast() are blocking, which is not supported in thread " + Thread.currentThread().getName()); } if (getCount() != 0) { try { await(); } catch (InterruptedException ex) { dispose(); throw Exceptions.propagate(ex); } } Throwable e = error; if (e != null) { RuntimeException re = Exceptions.propagate(e); //this is ok, as re is always a new non-singleton instance re.addSuppressed(new Exception("#block terminated with an error")); throw re; } return value; }
解决方式
问题找到了,解决起来就简单了,我们只需要重写获取实例的方法就好了,上代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
@<!-- toc --> <metaname="referrer"content="no-referrer" /> BeanpublicBlockingLoadBalancerClientblockingLoadBalancerClient(ReactiveLoadBalancer.Factory < ServiceInstance > loadBalancerClientFactory, DiscoveryClient discoveryClient) { returnnewBlockingLoadBalancerClient(loadBalancerClientFactory) {@ Overridepublic < T > ServiceInstancechoose(String serviceId, Request < T > request) { List < ServiceInstance > instanceList = discoveryClient.getInstances(serviceId); returnloadBalancerInstance(instanceList); } }; } privatestaticServiceInstanceloadBalancerInstance(List < ServiceInstance > instanceList) { if (CollUtil.isEmpty(instanceList)) { returnnull; } if (instanceList.size() == 1) { return instanceList.get(0); } // 随机负载 int index = RandomUtil.randomInt(0, instanceList.size()); return instanceList.get(index); }