Spring Cloud app - 在Tomcat

时间:2016-03-09 18:07:47

标签: spring-cloud

我尝试使用Spring Cloud设置一些服务,直到我将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

然而,它完全可以从日食中完成。它甚至可以在我从Tomcat运行发现和网关服务时运行,并从eclipse运行Eureka客户端服务。但是一旦我在tomcat上运行相同的服务,我就会收到错误。

我使用的是Brixton.M5,Java 8和Tomcat 8。

同样,代码似乎有效,问题是它在部署到Tomcat后无法正常工作。

我有一个用于Discovery和Gateway服务的Tomcat实例,以及一个用于Eureka客户端服务的第二个Tomcat实例。

这里有一些代码和配置..

DiscoveryServerApp

@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

DiscoveryServer - bootstrap.yml

spring:
  application:
    name: discovery
  jmx: 
    default-domain: com.example.cloud.discovery

GatewayApplication

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

GatewayApplication - application.yml

# 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

GatewayApplication - bootstrap.yml

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

encrypt:
  failOnError: false

DummyApplication

@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

# 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 properties
spring:
  application:
     name: hello-service  # Service registers under this name
  jmx: 
    default-domain: com.example.cloud.hello

encrypt:
  failOnError: false

3 个答案:

答案 0 :(得分:2)

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

所以无论如何,答案是:确保application.yml中的server.port与目标容器上的端口匹配。

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

答案 1 :(得分:0)

你应该将你的弓箭手指向4444(网关),而不是1111(尤里卡)。

答案 2 :(得分:0)

好的,@SpringCloudApplication包裹@EnableDiscoveryClient会导致DummyApplication在启动时向Eureka注册。您可以通过Eureka仪表板确认这种情况。

假设DummyApplication在Eureka注册为服务名称" hello-service",则Zuul / Ribbon将为该服务名称创建路由。因此,您的" / hello-resource"端点应通过Zuul代理:  http://localhost:4444/api/hello-service/hello-resource/