Spring Cloud Load Balancer在使用Eureka 时向不可用的服务发送请求

gwo2fgha  于 2023-05-17  发布在  Spring
关注(0)|答案(1)|浏览(123)

我正在使用Sping Boot 3.0.5和Spring Cloud Release Train 2022.0.2。
我目前的架构如下图所示:我正在尝试使用负载平衡发现服务将传入的外部请求路由到内部服务。

我一直有这样的问题,如果我创建三个“外部服务”的示例,它们将注册到Eureka ,但如果其中一个失败,后续请求将循环,并最终尝试命中失败的示例。
在将我的所有项目升级到Spring Cloud 2022.0.2之后,最终发现服务/负载均衡器将在网关服务中修复,但不是在一段不平凡的时间之后。
我尝试过的事情:
1.我尝试创建我自己的负载均衡器客户端,如这里所述,但我无法弄清楚如何使网关使用的负载均衡器成为我自己的自定义配置的负载均衡器。(我尤其不理解@LoadBalancerClient(value = "stores", configuration = CustomLoadBalancerConfiguration.class)中客户端服务id的用途
1.我尝试使用以下配置切换到health-check预制负载均衡器

loadbalancer:
      configurations: health-check

由于某种原因,当我这样做时,当我向网关发出请求时,无法找到外部服务。
在应用程序中设置Eureka 服务器和Eureka客户端的推荐方法是什么,以便在发出请求时进行负载平衡,并且不会将停止的服务路由到?
下面是我的Eureka Jmeter 板的示例屏幕截图和网关服务的控制台日志,当请求被路由到一个关闭的外部服务时。我在Eureka 服务器中将enable-self-preservation选项设置为false,现在它似乎有时会驱逐服务,但现在它会给出一个可怕的警告。

server:
    enable-self-preservation: false

到目前为止,Eureka 与Sping Boot 的集成非常不透明:在Spring Docs网站上没有太多的解释。我是否错过了一些信息或信息来源?
堆栈跟踪:

io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: /172.64.0.29:9007
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    *__checkpoint ⇢ org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter [DefaultWebFilterChain]
    *__checkpoint ⇢ org.springframework.web.filter.reactive.ServerHttpObservationFilter [DefaultWebFilterChain]
    *__checkpoint ⇢ HTTP POST "/ticketing/v1/stop" [ExceptionHandlingWebHandler]
Original Stack Trace:
Caused by: java.net.ConnectException: Connection refused
    at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
    at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:672) ~[na:na]
    at java.base/sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:973) ~[na:na]
    at io.netty.channel.socket.nio.NioSocketChannel.doFinishConnect(NioSocketChannel.java:337) ~[netty-transport-4.1.90.Final.jar:4.1.90.Final]
    at io.netty.channel.nio.AbstractNioChannel$AbstractNioUnsafe.finishConnect(AbstractNioChannel.java:334) ~[netty-transport-4.1.90.Final.jar:4.1.90.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:776) ~[netty-transport-4.1.90.Final.jar:4.1.90.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:724) ~[netty-transport-4.1.90.Final.jar:4.1.90.Final]
    at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:650) ~[netty-transport-4.1.90.Final.jar:4.1.90.Final]
    at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:562) ~[netty-transport-4.1.90.Final.jar:4.1.90.Final]
    at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997) ~[netty-common-4.1.90.Final.jar:4.1.90.Final]
    at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74) ~[netty-common-4.1.90.Final.jar:4.1.90.Final]
    at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.90.Final.jar:4.1.90.Final]
    at java.base/java.lang.Thread.run(Thread.java:1589) ~[na:na]

2023-04-04T16:24:41.184-04:00 DEBUG 53465 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_DSIMS-GATEWAY/172.64.0.29:dsims-gateway:80 - Heartbeat status: 200
2023-04-04T16:24:41.507-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Got delta update with apps hashcode DOWN_1_UP_3_
2023-04-04T16:24:41.508-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Added instance 172.64.0.29:rampaging-railgun:9009 to the existing apps in region null
2023-04-04T16:24:41.508-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Added instance 172.64.0.29:rampaging-railgun:9008 to the existing apps in region null
2023-04-04T16:24:41.508-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Added instance 172.64.0.29:rampaging-railgun:9007 to the existing apps in region null
2023-04-04T16:24:41.509-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : The total number of instances fetched by the delta processor : 3
2023-04-04T16:24:41.511-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : The total number of all instances in the client now is 4
2023-04-04T16:24:41.512-04:00 TRACE 53465 --- [freshExecutor-0] o.s.c.netflix.eureka.CloudEurekaClient   : onCacheRefreshed called with count: 18
2023-04-04T16:24:41.530-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Completed cache refresh task for discovery. All Apps hash code is Local region apps hashcode: DOWN_1_UP_3_, is fetching remote regions? false 
2023-04-04T16:25:11.192-04:00 DEBUG 53465 --- [tbeatExecutor-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_DSIMS-GATEWAY/172.64.0.29:dsims-gateway:80 - Heartbeat status: 200
2023-04-04T16:25:11.537-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Got delta update with apps hashcode DOWN_1_UP_3_
2023-04-04T16:25:11.537-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Added instance 172.64.0.29:rampaging-railgun:9009 to the existing apps in region null
2023-04-04T16:25:11.537-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Added instance 172.64.0.29:rampaging-railgun:9008 to the existing apps in region null
2023-04-04T16:25:11.537-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Added instance 172.64.0.29:rampaging-railgun:9007 to the existing apps in region null
2023-04-04T16:25:11.537-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : The total number of instances fetched by the delta processor : 3
2023-04-04T16:25:11.537-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : The total number of all instances in the client now is 4
2023-04-04T16:25:11.538-04:00 TRACE 53465 --- [freshExecutor-0] o.s.c.netflix.eureka.CloudEurekaClient   : onCacheRefreshed called with count: 19
2023-04-04T16:25:11.548-04:00 DEBUG 53465 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : Completed cache refresh task for discovery. All Apps hash code is Local region apps hashcode: DOWN_1_UP_3_, is fetching remote regions? false

m1m5dgzv

m1m5dgzv1#

我遇到了类似的问题,api-gateway向一个已经停止的服务发送了一个请求,所以我得到了白标签错误。
这个链接有很多有用的信息,我使用了重试模式,它工作得很好。https://github.com/danielsobrado/spring-cloud-kafka-microservices
下面是我的配置:
.yml:

spring:
  application:
    name: api-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      default-filters:
        - name: Retry
          args:
            retries: 3
            methods: GET, POST
            series: SERVER_ERROR
            exceptions: java.io.IOException
            backoff:
              factor: 2

或.properties:

spring.cloud.gateway.default-filters[0].name=Retry
spring.cloud.gateway.default-filters[0].args.retries=3
spring.cloud.gateway.default-filters[0].args.methods=GET, POST, PUT, DELETE
spring.cloud.gateway.default-filters[0].args.series=SERVER_ERROR
spring.cloud.gateway.default-filters[0].args.exceptions=java.io.IOException
spring.cloud.gateway.default-filters[0].args.backoff.factor=2

希望能帮上忙

相关问题