【发布时间】:2017-06-05 02:52:16
【问题描述】:
我正在尝试让一个简单的微服务注册到 Eureka 服务器,然后让 Zuul 代理微服务。我让微服务注册到 Eureka 服务器。但是,每当我启动 Zuul 服务时,我都会看到它没有注册到 Eureka 服务器。每当我尝试路由到微服务时,都会出现以下异常:
负载均衡器没有可用于客户端的服务器:微服务
我无法弄清楚问题出在哪里。任何帮助将不胜感激
下面是我为每个组件提供的 Spring Boot 类。
微服务组件
MicroserviceApplication.java
@SpringBootApplication(scanBasePackages = { "some.package.controller", "some.package.service" })
@EnableEurekaClient
@EnableJpaRepositories("some.package.repository")
@EntityScan("some.package.entity")
public class MicroserviceApplication {
public static void main(String[] args) {
SpringApplication.run(MicroserviceApplication.class, args);
}
}
bootstrap.yml
server:
port: 8090
info:
component: Core Services
spring:
application:
name: microservice
application.yml
eureka:
instance:
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
healthcheck:
enabled: true
lease:
duration: 5
#some other logging and jpa properties below
Eureka 服务器组件
EurekaServerApplication.java
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
bootstrap.yml
spring:
application:
name: discovery-server
application.yml
server:
port: 8761
info:
component: Discovery Server
eureka:
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
instance:
hostname: localhost
server:
waitTimeInMsWhenSyncEmpty: 0
API 网关组件
ZuulGatewayApplication.java
@SpringBootApplication
@EnableZuulProxy
public class ZuulGatewayApplication {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApplication.class, args);
}
@Bean
public ZuulGatewayPreFilter gatewayPreFilter() {
return new ZuulGatewayPreFilter();
}
}
bootstrap.yml
spring:
application:
name: api-gateway
application.yml
server:
port: 8888
info:
component: API Gateway
eureka:
instance:
leaseRenewalIntervalInSeconds: 1
leaseExpirationDurationInSeconds: 2
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
zuul:
routes:
microservice:
path: /microservice/**
serviceId: microservice
【问题讨论】:
-
eureka 怎么说(仪表板和
/eureka/apps?微服务注册了吗?这么短的续订和过期时间实际上可能会导致流失问题。您是否尝试过延长它们? -
你用的是什么版本?
-
微服务注册得很好,在 Zuul 应用程序类中添加
@EnableEurekaClient注释为我解决了这个问题,Zuul 现在作为注册服务显示在 eureka 仪表板上,并且还代理到微服务就好了。我没有指定任何特定版本,但基于 Spring Cloud 版本 Camden.SR4。对于续订和到期间隔,您有什么建议?
标签: spring-boot spring-cloud netflix-zuul netflix-eureka