【问题标题】:Spring Eureka Ribbon method executed twiceSpring Eureka Ribbon 方法执行了两次
【发布时间】:2019-01-16 22:34:52
【问题描述】:

我的 Spring 引导应用程序遇到了一个非常奇怪的问题。 我正在使用 Eureka/Ribbon 开发用于服务发现的微服务应用程序,该应用程序是一个统计生成器,为 csv 上传提供 API,并链接到用于数据检索、比较和持久性的数据库。

有问题的流程非常简单:

  1. 检查上传目录中未处理的文件
  2. 获取未处理文件的路径并放入数组中
  3. 对于这个数组的每个元素,将它们从 Csv 转换为 pojo
  4. ???
  5. 致富

调用是通过调用转换器微服务的 api(通过邮递员和浏览器尝试)进行的。 转换器微服务通过上传直接列出每个未处理的文件。 然后在同一个微服务中使用该列表进行处理。首先,转换器调用返回三个对象列表的数据检索器微服务。然后使用它们来构建基于 csvs 的 pojos。

这就是问题出现的地方,这个方法执行了两次,而所有其他方法只执行了一次。

Csv-uploader 微服务(入口端点)

@GetMapping("/proxy/csv/process")
public void processProxyCsv(){
    logger.info("Called for processing uploaded CSVs");
    csvConverterServiceProxy.processPendingCsv();

}

以及使用的代理:

@FeignClient(name="bbx-rev-csv-converter")
@RibbonClient(name="bbx-rev-csv-converter")
public interface CsvConverterServiceProxy {
    @GetMapping("/csv/process/running")
    public String processPendingCsv();
}

到目前为止一切顺利,一切似乎都运行良好,日志只出现一次。

然后我们转到 csv-converter 微服务,所有的魔法都发生了(还有 bug),入口端点之前调用过:

@GetMapping("/csv/process/running")
public ResponseEntity<String> processPendingCsv() {
    HttpHeaders headers = new HttpHeaders();
    HttpStatus status = HttpStatus.BAD_REQUEST;
    String reason = "Pending...";

    logger.info("API call for processing pending CSVs.");
    if (fileHandlerService.checkForNonProcessedFiles()) {
        logger.info("Check over, returning to entry flow.");
        logger.info("file path : " + fileHandlerService.getUploadedFilePath().toString());
        csvConverterService.convertAllCsv(fileHandlerService.getUploadedFilePath());
        reason = "Processing pending CSVs in progress...";
    } else {
        reason = "No pending files found.";
    }
    status = HttpStatus.OK;

    return new ResponseEntity<String>(reason, headers, status);

}

从这里开始,根据日志,这被执行了两次。

我查看了所有代码,没有两次调用或循环错误地调用此代理方法(或者至少没有找到它,真的,我希望我做了一些愚蠢的事情)。

它甚至变得更奇怪了,当我调用同一个端点时(几分钟后)所有服务都运行了一段时间后,调用只进行了一次。

这里是日志:

首先是 Csv-uploader 日志:

2018-08-09 14:35:17.407  INFO 4588 --- [nio-8000-exec-7] Csv-up-ctrl : Called for processing uploaded CSVs

还有 csv-converter 日志...

2018-08-09 14:35:17.470  INFO 17280 --- [nio-8006-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring FrameworkServlet 'dispatcherServlet'
2018-08-09 14:35:17.470  INFO 17280 --- [nio-8006-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization started
2018-08-09 14:35:17.504  INFO 17280 --- [nio-8006-exec-1] o.s.web.servlet.DispatcherServlet        : FrameworkServlet 'dispatcherServlet': initialization completed in 34 ms
2018-08-09 14:35:17.547  INFO 17280 --- [nio-8006-exec-1] Csv converter controller                 : API call for processing pending CSVs.
2018-08-09 14:35:17.548  INFO 17280 --- [nio-8006-exec-1] File checker service                     : Checking for unprocessed files ...
2018-08-09 14:35:17.555  INFO 17280 --- [nio-8006-exec-1] File checker service                     : Found 1 unprocessed files : [test_file.csv]
2018-08-09 14:35:17.555  INFO 17280 --- [nio-8006-exec-1] Csv converter controller                 : Check over, returning to entry flow.
2018-08-09 14:35:17.555  INFO 17280 --- [nio-8006-exec-1] Csv converter controller                 : file path : E:\workspace\Blackbox-reversements\csv-uploader\uploads\todo
2018-08-09 14:35:17.555  INFO 17280 --- [nio-8006-exec-1] Csv converter service                    : convertAllCsv call
2018-08-09 14:35:17.561  INFO 17280 --- [nio-8006-exec-1] s.c.a.AnnotationConfigApplicationContext : Refreshing SpringClientFactory-bbx-rev-ext-data-retriever: startup date [Thu Aug 09 14:35:17 CEST 2018]; parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@7d18bd1a
2018-08-09 14:35:17.659  INFO 17280 --- [nio-8006-exec-1] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
2018-08-09 14:35:18.002  INFO 17280 --- [nio-8006-exec-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: bbx-rev-ext-data-retriever.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2018-08-09 14:35:18.034  INFO 17280 --- [nio-8006-exec-1] c.n.u.concurrent.ShutdownEnabledTimer    : Shutdown hook installed for: NFLoadBalancer-PingTimer-bbx-rev-ext-data-retriever
2018-08-09 14:35:18.079  INFO 17280 --- [nio-8006-exec-1] c.netflix.loadbalancer.BaseLoadBalancer  : Client: bbx-rev-ext-data-retriever instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=bbx-rev-ext-data-retriever,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
2018-08-09 14:35:18.090  INFO 17280 --- [nio-8006-exec-1] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
2018-08-09 14:35:18.125  INFO 17280 --- [nio-8006-exec-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: bbx-rev-ext-data-retriever.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
2018-08-09 14:35:18.127  INFO 17280 --- [nio-8006-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client bbx-rev-ext-data-retriever initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=bbx-rev-ext-data-retriever,current list of Servers=[192.168.0.130:8036],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone;    Instance count:1;   Active connections count: 0;    Circuit breaker tripped count: 0;   Active connections per server: 0.0;]
},Server stats: [[Server:192.168.0.130:8036;    Zone:defaultZone;   Total Requests:0;   Successive connection failure:0;    Total blackout seconds:0;   Last connection made:Thu Jan 01 01:00:00 CET 1970;  First connection made: Thu Jan 01 01:00:00 CET 1970;    Active Connections:0;   total failure count in last (1000) msecs:0; average resp time:0.0;  90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;  max resp time:0.0;  stddev resp time:0.0]
]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@11bc0eb
2018-08-09 14:35:18.414  INFO 17280 --- [nio-8006-exec-5] Csv converter controller                 : API call for processing pending CSVs.
2018-08-09 14:35:18.414  INFO 17280 --- [nio-8006-exec-5] File checker service                     : Checking for unprocessed files ...
2018-08-09 14:35:18.415  INFO 17280 --- [nio-8006-exec-5] File checker service                     : Found 1 unprocessed files : [test_file.csv]
2018-08-09 14:35:18.415  INFO 17280 --- [nio-8006-exec-5] Csv converter controller                 : Check over, returning to entry flow.
2018-08-09 14:35:18.415  INFO 17280 --- [nio-8006-exec-5] Csv converter controller                 : file path : E:\workspace\Blackbox-reversements\csv-uploader\uploads\todo
2018-08-09 14:35:18.415  INFO 17280 --- [nio-8006-exec-5] Csv converter service                    : convertAllCsv call
2018-08-09 14:35:18.989  INFO 17280 --- [nio-8006-exec-5] Csv converter service                    : CSVs path will go here
2018-08-09 14:35:19.050  INFO 17280 --- [nio-8006-exec-1] Csv converter service                    : CSVs path will go here
2018-08-09 14:35:19.095  INFO 17280 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty  : Flipping property: bbx-rev-ext-data-retriever.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647

所以不太好。会不会是配置问题?对于我的微服务配置,我一直保持原样:

对于主要课程:

@SpringBootApplication
@EnableFeignClients("com.telemaque.blackbox.reversement.csvuploader")
@EnableDiscoveryClient
public class CsvUploaderApplication {...

对于 application.properties 文件:

spring.application.name=bbx-rev-csv-converter
server.port=8006
eureka.client.service-url.default-zone=http://localhost:8761/eureka

以及命名服务器的 application.properties :

spring.application.name=bbx-rev-naming-server
server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

对于命名服务器:

@SpringBootApplication
@EnableEurekaServer
public class NamingServerApplication {...

对于代理:

@FeignClient(name="bbx-rev-ext-data-retriever")
@RibbonClient(name="bbx-rev-ext-data-retriever")
public interface ExternalDataRetrieverProxy {...

我一直在看这里,尤里卡 git,看着春天的一面,仍然无法弄清楚。这让我发疯了,如果你有任何想法我很想听!

非常感谢您的帮助!

编辑:又做了一些测试,我在命名服务器中发现了一个奇怪的日志:

2018-08-09 14:55:08.320  INFO 15068 --- [nio-8761-exec-2] c.n.e.registry.AbstractInstanceRegistry  : Registered instance BBX-REV-CSV-CONVERTER/192.168.0.130:bbx-rev-csv-converter:8006 with status UP (replication=false)
2018-08-09 14:55:08.832  INFO 15068 --- [io-8761-exec-10] c.n.e.registry.AbstractInstanceRegistry  : Registered instance BBX-REV-CSV-CONVERTER/192.168.0.130:bbx-rev-csv-converter:8006 with status UP (replication=true)

另外,当我直接调用 API(不是通过第一个微服务)时,它只正确执行一次。

【问题讨论】:

    标签: spring spring-boot microservices netflix-eureka netflix-ribbon


    【解决方案1】:

    这可能是因为 Ribbon 在连接超时时会重试。有关该检查的详细信息spring retry。要解决此问题,您可以按照以下步骤操作:-

    • application.properties 您的 csv-converter 微服务使用者 feignClientName.ribbon.OkToRetryOnAllOperations=false

    您还可以在这里查看更多属性application.yml

    【讨论】:

      猜你喜欢
      • 2018-10-24
      • 2015-12-12
      • 2020-10-15
      • 2016-08-21
      • 2015-07-02
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2021-11-02
      相关资源
      最近更新 更多