繁体   English   中英

com.netflix.discovery.shared.transport.TransportException:无法在任何已知服务器上执行请求

[英]com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

我对微服务非常陌生,并尝试从链接运行代码: https://dzone.com/articles/advanced-microservices-security-with-spring-and-oa 当我简单地运行代码时,我看到出现以下错误。

问题是什么?

com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:111) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1030) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:944) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient.refreshRegistry(DiscoveryClient.java:1468) [eureka-client-1.4.12.jar:1.4.12]
    at com.netflix.discovery.DiscoveryClient$CacheRefreshThread.run(DiscoveryClient.java:1435) [eureka-client-1.4.12.jar:1.4.12]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [na:1.8.0_144]
    at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_144]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_144]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_144]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_144]

2017-09-09 18:53:11.909 ERROR 16268 --- [tbeatExecutor-0] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error

我没有在系统上安装任何特殊的东西。 请让我知道我需要安装什么?

在此处输入图像描述

我发现我必须将这两个应用程序添加到 appllication.properties,它才能工作。只有一个是不够的。

eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false

发生这种情况是因为它正在尝试连接到任何已知服务器,因此要停止该错误,已知服务器是端口 8761 上的 eureka 服务器,这是其默认端口,您必须使用以下内容更新 application.properties

server.port=8761

为了避免尤里卡注册自己,你把它添加到 application.properties

eureka.client.register-with-eureka=false

确保启用了 EurekaServer,例如使用 spring boot,您在主类上编写以下内容。

@EnableEurekaServer

请原谅我使用 .properties 文件提供解决方案,但这是我使用的,但 .yml 配置不应该有太大的不同。

您需要创建另一个微服务 Eureka Registry Server。 它是 SpringBoot 应用程序的主类,应该有 @EnableEurekaServer 注释。

然后在您的 Eureka 客户端中,您必须在 appliation.yml 中提及注册表服务器 URL,如下所示:

spring:
  application:
    name: stock-service

server:
  port: 8083


eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8084/eureka/
  instance:
    hostname: localhost

defaultzone 应该保存你的 Eureka Registry 的值。

完成所有这些配置后,您需要启动 Eureka Registry 微服务,然后才能启动 Eureka 客户端。 一旦您的注册表启动,您将不会遇到此异常。

此特定消息只是一个警告。 您的应用程序正在尝试向 Eureka 注册,但 Eureka 没有响应。 您可以启动 Eureka 实例或通过将以下内容添加到 application.yml 来禁用自动注册。

eureka:
  client:
    register-with-eureka: false

当我的 Eureka 客户端(即微服务试图向 Eureka Server 注册自己)时,我遇到了同样的错误。

客户端注册eureka服务失败注册失败无法在任何已知服务器上执行请求

出现此错误消息是因为以下两种可能性。

1) defaultZone 地址不正确,要么拼写错误,要么后面的空格: .

2) 在 Eureka 服务器上实现的 Spring 安全性要求每个请求都发送一个有效的 CSRF 令牌。 Eureka 客户端通常不会拥有有效的跨站请求伪造 (CSRF) 令牌。 因此,您需要为 /eureka/** 端点禁用此要求。

在使用以下代码行禁用 CSRF 令牌后,我的 Eureka 客户端成功地将它们自己注册到 Eureka Server。

@EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().ignoringAntMatchers("/eureka/**");
        super.configure(http);
    }
}

下面还有来自 spring 文档的链接。

https://cloud.spring.io/spring-cloud-static/Greenwich.SR2/single/spring-cloud.html#_securing_the_eureka_server

我得到了同样的错误。 就我而言,在我的客户端应用程序的application.properties文件中,我拼错了defaultZone单词。 我写了default-zone但它必须是defaultZone

客户端应用application.properties

eureka.client.service-url.defaultZone=http://localhost:8030/eureka/

根据您的情况,主机名和端口可能会有所不同。

如果您的 eureka 服务器在没有用户名和密码的情况下部署/运行,请使用以下属性。

spring.application.name=Hello-World
eureka.client.serviceUrl.defaultZone=http://eurekaserver:password@localhost:9100/eureka
server.port=9200
eureka.client.fetch-registry=true

如果您的 eureka 服务器使用用户名和密码部署/运行,请使用以下属性。

spring.application.name=Hello-World
eureka.client.serviceUrl.defaultZone=http://eurekaserverusername:eurekaserverpassword@localhost:9100/eureka
server.port=9200
eureka.client.fetch-registry=true

依赖版本:

  • 春季启动2.5.6
  • 2020.0.42020.0.4

尤里卡服务器应用

application.yaml

server:
  port: 8010
eureka:
  client:
    fetch-registry: false
    register-with-eureka: false
    service-url:
      defaultZone: http://localhost:8010/eureka

MyEurekaServerApplication.java

@EnableEurekaServer
@SpringBootApplication
public class MyEurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyEurekaServerApplication.class, args);
    }
}

尤里卡客户端应用程序

application.yaml

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8010/eureka

如果使用default-zone而不是defaultZone ,则Eureka Client AppEureka Server上注册时会引发异常。

MyEurekaClientApplication.java

@EnableEurekaClient
@SpringBootApplication
public class MyEurekaClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyEurekaClientApplication.class, args);
    }
}

在 ZuulLoggingFiler.java 中查找 filterType()。 我有同样的问题。 然后我看到我的过滤器返回 null。 所以我将其更改为“发布”并且它起作用了。

@Override
public String filterType() {
    // TODO Auto-generated method stub
    return "post";
}

检查“eureka.client.serviceUrl.defaultZone”属性中提供的 eureka 服务器的 URL 和端口号。

当您在 defaultZone 的值中拼错任何内容时,将引发类似的错误。 例如:有人将 eureka 拼错为 euraka。 仔细检查一下。

defaultZone : http://localhost:8761/eureka

确保您的功能区客户端正确无误

package com.in28minutes.microservices.currencyconversionservice;

import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@FeignClient(name="curreenter code herency-exchange-service")
@RibbonClient(name="currency-exchange-service") 
public interface CurrencyExchangeServiceProxy { 
    @GetMapping("/currency-exchange/from/{from}/to/{to}")
    public CurrencyConversionBean retrieveExchangeValue
        (@PathVariable("from") String from, @PathVariable("to") String to); //mention correct pathvariables
}

对我来说,它在连接到互联网后工作,因为它需要第一次下载一些依赖项。

在 application.properties 添加以下行解决了它:

server.port=8761

eureka.client.register-with-eureka=false

eureka.client.fetch-registry=false

第一个是可选的。

其他两个属性用于告诉 Eureka Server “嘿,你是这个上下文中唯一的 Eureka Server,不要试图找到其他 Eureka Server”

如果我们不提及这些,我们的 Eureka Server 会尝试在其他 Eureka Server 上查找并注册自己,最终会抛出 Transport Exception。

尝试在要与命名服务器连接的微服务中添加此属性:

eureka.instance.hostname=localhost

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM