Spring Cloud Gateway 兼容 Open Feign 接口远程调用

问题描述

有时候我们需要在spring cloud gateway中进行一些远程接口的调用,比如网关的鉴权,需要调用rpc接口查询用户信息,如果直接调用feign接口,会出现如下报错:

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: 

问题原因

这是因为新版的 gateway 采用的是非阻塞式编程,但是在获取注册中心实例时,调用了block()方法阻塞式获取实例,只要你敢 block,就会

throw new IllegalStateException("block()/blockFirst()/blockLast() are blocking, which is not supported in thread " + Thread.currentThread().getName());
    public <T> ServiceInstance choose(String serviceId, Request<T> request) {
        ReactiveLoadBalancer<ServiceInstance> loadBalancer = this.loadBalancerClientFactory.getInstance(serviceId);
        if (loadBalancer == null) {
            return null;
        } else {
            Response<ServiceInstance> loadBalancerResponse = (Response)Mono.from(loadBalancer.choose(request)).block();
            return loadBalancerResponse == null ? null : (ServiceInstance)loadBalancerResponse.getServer();
        }
    }

	final T blockingGet() {
		if (Schedulers.isInNonBlockingThread()) {
			throw new 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;
	}

解决方式

问题找到了,解决起来就简单了,我们只需要重写获取实例的方法就好了,上代码

    @Bean
    public BlockingLoadBalancerClient blockingLoadBalancerClient(ReactiveLoadBalancer.Factory<ServiceInstance> loadBalancerClientFactory,
                                                                 DiscoveryClient discoveryClient) {
        return new BlockingLoadBalancerClient(loadBalancerClientFactory) {
            @Override
            public <T> ServiceInstance choose(String serviceId, Request<T> request) {
                List<ServiceInstance> instanceList = discoveryClient.getInstances(serviceId);
                return loadBalancerInstance(instanceList);
            }
        };
    }

    private static ServiceInstance loadBalancerInstance(List<ServiceInstance> instanceList) {
        if (CollUtil.isEmpty(instanceList)) {
            return null;
        }
        if (instanceList.size() == 1) {
            return instanceList.get(0);
        }
        // 随机负载
        int index = RandomUtil.randomInt(0, instanceList.size());
        return instanceList.get(index);
    }

这里的负载均衡是随机负载的,各位可以根据自己的机器数量,设置不同的负载均衡策略(我这生产其实只有一个节点,负载不负载无所谓)

### 回答1: Spring Cloud Gateway 本身并不能直接使用 FeignFeign 是一个用于声明式、模板化 HTTP 客户端的库,它可以帮助我们更方便地编写基于 HTTP 的服务调用代码。但是 Spring Cloud Gateway 更多地是作为一个 API 网关来使用,用于在微服务架构中进行请求路由、限流、熔断等操作。 虽然 Spring Cloud GatewayFeign 都是基于 Spring 框架的,但它们的使用场景和功能不同,不能直接进行集成。不过,在实际的项目中,我们通常会同时使用 Spring Cloud GatewayFeign 来完成不同的任务。比如,我们可以使用 Spring Cloud Gateway 进行路由、过滤等操作,而使用 Feign 进行服务间的调用。 ### 回答2: Spring Cloud Gateway 确实无法直接使用 Feign 进行服务间的通信。这是因为 Spring Cloud GatewayFeign 是两个不同的组件,具有不同的设计理念和用途。 Spring Cloud Gateway 是一个基于 Spring WebFlux 和 Reactor 的网关服务,用于处理请求路由和转发。它支持使用各种方式定义路由规则,并且具有强大的过滤器功能,可以对请求进行修改和验证。Spring Cloud Gateway 更加注重对请求的处理和转发,而不是直接进行服务间的通信。 相比之下,Feign 是一个基于注解的轻量级 HTTP 客户端,用于服务间的通信。通过定义接口和注解,Feign 可以帮助我们简化 HTTP 请求的编写和发送,从而实现服务之间的互通。Feign 的设计初衷是为了简化服务间通信的代码编写和维护,而不是用于路由和转发请求。 然而,我们可以在 Spring Cloud Gateway 中使用 WebClient 进行服务间的通信。WebClient 是 Spring WebFlux 提供的用于发送 HTTP 请求的非阻塞客户端。我们可以在 Gateway 的过滤器中使用 WebClient 发送请求到目标服务,然后将响应返回给客户端。 综上所述,尽管 Spring Cloud Gateway 无法直接使用 Feign 进行服务间的通信,但我们仍然可以利用 WebClient 在 Gateway 中实现服务间的通信。这种方式可以更好地符合 Spring Cloud Gateway 的设计理念和用途。 ### 回答3: Spring Cloud GatewayFeign是两个完全不同的组件,分别属于Spring Cloud的不同子项目。Spring Cloud Gateway是一个基于Spring Framework5,Spring Boot2和Project Reactor的新一代微服务网关,主要用于路由请求和进行过滤处理。而Feign是一个声明式的HTTP客户端,用于在微服务之间进行远程服务调用。 不能说Spring Cloud Gateway无法集成Feign,因为它们并不是相互替代的关系。在使用Spring Cloud Gateway时,一般是将Feign用作微服务之间的远程服务调用工具,而不是直接集成到Spring Cloud Gateway中。 当需要在Spring Cloud Gateway中进行远程服务调用时,可以使用Spring WebFlux提供的WebClient来实现。WebClient是一个响应式的HTTP客户端,可以用于发送请求并接收响应。通过使用WebClient,可以轻松地在Spring Cloud Gateway中进行远程服务调用。 总结来说,虽然Spring Cloud Gateway本身不直接支持Feign,但可以使用Spring WebFlux提供的WebClient在Spring Cloud Gateway中进行远程服务调用。在使用Spring Cloud Gateway时,可以结合Feign在微服务之间进行远程服务调用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值