Spring cloud Spring云应用程序-在Tomcat上部署后zuul超时

Spring cloud Spring云应用程序-在Tomcat上部署后zuul超时,spring-cloud,Spring Cloud,我正在尝试使用SpringCloud设置一些服务,在我将Eureka客户机服务部署到Tomcat之前,一切似乎都很正常。当我通过网关应用程序调用服务时,出现以下错误: o.s.c.n.z.filters.post.SendErrorFilter : Error during filtering com.netflix.zuul.exception.ZuulException: Forwarding error ... Caused by: com.netflix.hystrix.except

我正在尝试使用SpringCloud设置一些服务,在我将Eureka客户机服务部署到Tomcat之前,一切似乎都很正常。当我通过网关应用程序调用服务时,出现以下错误:

o.s.c.n.z.filters.post.SendErrorFilter   : Error during filtering
com.netflix.zuul.exception.ZuulException: Forwarding error
...
Caused by: com.netflix.hystrix.exception.HystrixRuntimeException: hello timed-out and no fallback available.
...
Caused by: java.util.concurrent.TimeoutException: null
然而,它在eclipse中工作得非常完美。当我从Tomcat运行发现和网关服务,并从eclipse运行Eureka客户机服务时,它甚至可以工作。但只要我在tomcat上运行相同的服务,我就会得到错误

我使用的是Brixton.M5、Java8和Tomcat8

同样,代码似乎是有效的,问题是它在部署到Tomcat之后就不起作用了

我有一个Tomcat实例用于发现和网关服务,第二个Tomcat实例用于Eureka客户端服务

下面是一些代码和配置

发现服务器应用程序

@SpringBootApplication
@EnableEurekaServer
public class DiscoveryServerApp extends SpringBootServletInitializer
{
    public static void main(String[] args)
    {
        SpringApplication.run(DiscoveryServerApp.class, args);
    }
}
DiscoveryServer-application.yml

# Configure this Discovery Server
eureka:
  instance:
    hostname: discovery
  client:  # Not a client, don't register with yourself
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:1111/discovery/eureka/

server:
  port: 1111   # HTTP (Tomcat) port
  context-path: /discovery
# Discovery Server Access
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/discovery/eureka/
  instance:
    instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}

# HTTP Server
server:
  port: 4444   # HTTP (Tomcat) port
  context-path: /api
# Discovery Server Access
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/discovery/eureka/
  instance:
    instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}} # Unique id for multiple instances

# HTTP Server
server:
  port: 3333   # HTTP (Tomcat) port
  context-path: /hello-context
DiscoveryServer-bootstrap.yml

spring:
  application:
    name: discovery
  jmx: 
    default-domain: com.example.cloud.discovery
# Spring properties
spring:
  application:
    name: gateway-service  # Identify this application
  jmx: 
    default-domain: com.example.cloud.gateway

encrypt:
  failOnError: false
# Spring properties
spring:
  application:
     name: hello-service  # Service registers under this name
  jmx: 
    default-domain: com.example.cloud.hello

encrypt:
  failOnError: false
网关应用程序

@SpringCloudApplication
@EnableZuulProxy
public class GatewayApplication extends SpringBootServletInitializer
{
    public static void main(String[] args)
    {
        SpringApplication.run(GatewayApplication.class, args);
    }
}
@SpringCloudApplication
@RestController
public class DummyApplication extends SpringBootServletInitializer
{
    public static void main(String[] args)
    {
        SpringApplication.run(DummyApplication.class, args);
    }

    @RequestMapping( path = "/hello-resource", method = RequestMethod.GET )
    public String hello()
    {
        return "hello";
    }
}
网关应用程序-application.yml

# Configure this Discovery Server
eureka:
  instance:
    hostname: discovery
  client:  # Not a client, don't register with yourself
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:1111/discovery/eureka/

server:
  port: 1111   # HTTP (Tomcat) port
  context-path: /discovery
# Discovery Server Access
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/discovery/eureka/
  instance:
    instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}

# HTTP Server
server:
  port: 4444   # HTTP (Tomcat) port
  context-path: /api
# Discovery Server Access
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/discovery/eureka/
  instance:
    instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}} # Unique id for multiple instances

# HTTP Server
server:
  port: 3333   # HTTP (Tomcat) port
  context-path: /hello-context
网关应用程序-bootstrap.yml

spring:
  application:
    name: discovery
  jmx: 
    default-domain: com.example.cloud.discovery
# Spring properties
spring:
  application:
    name: gateway-service  # Identify this application
  jmx: 
    default-domain: com.example.cloud.gateway

encrypt:
  failOnError: false
# Spring properties
spring:
  application:
     name: hello-service  # Service registers under this name
  jmx: 
    default-domain: com.example.cloud.hello

encrypt:
  failOnError: false
dummy应用程序

@SpringCloudApplication
@EnableZuulProxy
public class GatewayApplication extends SpringBootServletInitializer
{
    public static void main(String[] args)
    {
        SpringApplication.run(GatewayApplication.class, args);
    }
}
@SpringCloudApplication
@RestController
public class DummyApplication extends SpringBootServletInitializer
{
    public static void main(String[] args)
    {
        SpringApplication.run(DummyApplication.class, args);
    }

    @RequestMapping( path = "/hello-resource", method = RequestMethod.GET )
    public String hello()
    {
        return "hello";
    }
}
DummyApplication-application.yml

# Configure this Discovery Server
eureka:
  instance:
    hostname: discovery
  client:  # Not a client, don't register with yourself
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://localhost:1111/discovery/eureka/

server:
  port: 1111   # HTTP (Tomcat) port
  context-path: /discovery
# Discovery Server Access
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/discovery/eureka/
  instance:
    instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}}

# HTTP Server
server:
  port: 4444   # HTTP (Tomcat) port
  context-path: /api
# Discovery Server Access
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:1111/discovery/eureka/
  instance:
    instanceId: ${spring.application.name}:${spring.application.instance_id:${random.value}} # Unique id for multiple instances

# HTTP Server
server:
  port: 3333   # HTTP (Tomcat) port
  context-path: /hello-context
DummyApplication-bootstrap.yml

spring:
  application:
    name: discovery
  jmx: 
    default-domain: com.example.cloud.discovery
# Spring properties
spring:
  application:
    name: gateway-service  # Identify this application
  jmx: 
    default-domain: com.example.cloud.gateway

encrypt:
  failOnError: false
# Spring properties
spring:
  application:
     name: hello-service  # Service registers under this name
  jmx: 
    default-domain: com.example.cloud.hello

encrypt:
  failOnError: false

您应该将bowser指向端口4444(网关),而不是1111(eureka)

您应该将bowser指向端口4444(网关),而不是1111(尤里卡)

好,
@SpringCloudApplication
包装
@EnableDiscoveryClient
,这会导致
DummyApplication
在启动时向Eureka注册自身。您可以通过Eureka仪表板确认这一点

假设
DummyApplication
在Eureka注册为服务名“hello service”,那么Zuul/Ribbon将为该服务名创建路由。因此,您的“/hello resource”端点应该通过Zuul在以下位置进行代理:
http://localhost:4444/api/hello-服务/hello resource/
正常,
@SpringCloudApplication
包装
@EnableDiscoveryClient
,这会导致
DummyApplication
在启动时向Eureka注册自身。您可以通过Eureka仪表板确认这一点

假设
DummyApplication
在Eureka注册为服务名“hello service”,那么Zuul/Ribbon将为该服务名创建路由。因此,您的“/hello resource”端点应该通过Zuul在以下位置进行代理:
http://localhost:4444/api/hello-服务/hello resource/
我偶然发现了。。。事实证明,server.port的值需要与部署它的Tomcat实例的端口匹配。现在看起来很明显,但我想Spring会从它运行的容器中神奇地发现这一点。我想从外部位置读取配置以处理不同的环境是一个好主意,而不必进行“代码更改”

总之,答案是:确保application.yml中的server.port与目标容器上的端口匹配


感谢所有花时间帮助我的人

我是偶然发现的。。。事实证明,server.port的值需要与部署它的Tomcat实例的端口匹配。现在看起来很明显,但我想Spring会从它运行的容器中神奇地发现这一点。我想从外部位置读取配置以处理不同的环境是一个好主意,而不必进行“代码更改”

总之,答案是:确保application.yml中的server.port与目标容器上的端口匹配


感谢所有花时间帮助我的人

你从客户端/浏览器调用什么URL?获取localhost:1111/api/hello/hello/hello是的,我知道,我只是喜欢问候。。谢谢你的快速回复!你从客户端/浏览器调用什么URL?获取localhost:1111/api/hello/hello/hello是的,我知道,我只是喜欢问候。。谢谢你的快速回复!我的错。。1111上的tomcat实例既有网关又有eureka服务器。当我从eclipse运行所有东西时,我会在4444上调用它。不过,在您的示例中,我看不到第三个/hello。港口是4444。第一个上下文路径是/api,然后是来自网关的/hello,然后是来自REST的/hello。第三个/你好来自哪里?另外,为了调试的目的,尝试从eureka中删除/发现,只需指向ip/eureka即可。我知道这会让人困惑。。我更新了配置,所以现在我的调用看起来像localhost:port/api/hello service/hello context/hello resource..我也尝试了您的建议,将/discovery移动到ROOT,但不幸的是,我仍然得到相同的行为my bad。。1111上的tomcat实例既有网关又有eureka服务器。当我从eclipse运行所有东西时,我会在4444上调用它。不过,在您的示例中,我看不到第三个/hello。港口是4444。第一个上下文路径是/api,然后是来自网关的/hello,然后是来自REST的/hello。第三个/你好来自哪里?另外,为了调试的目的,尝试从eureka中删除/发现,只需指向ip/eureka即可。我知道这会让人困惑。。我更新了配置,所以现在我的调用看起来像localhost:port/api/hello service/hello context/hello resource..我也尝试了你的建议,将/discovery移到ROOT,但不幸的是,我仍然得到了与你输入相同的行为库。。记住Hello应用程序部署在
/Hello context/
下,因此Zuul端点看起来像
http://localhost:4444/api/hello-服务/你好上下文/你好资源
谢谢您的输入。。记住Hello应用程序部署在
/Hello context/
下,因此Zuul端点看起来像
http://localhost:4444/api/hello-service/hello context/hello resource
,或者从命令行使用嵌入式TC服务器运行Spring引导应用程序。是的,但这正是我试图避免的。。我想用尽可能少的jvm运行多个微服务,或者只运行Spring B