我正在尝试使用简单的微服务注册到Eureka Server,然后让Zuul代理到微服务。我得到微服务注册到Eureka服务器。但是,每当我带上Zuul服务时,我都会看到它没有注册到Eureka服务器。每当我尝试路由到微服务时,我都会得到以下异常:
负载均衡器没有可用于客户端的服务器:microservice
我无法弄清问题在哪里。任何帮助将不胜感激
以下是每个组件的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
答案 0 :(得分:5)
添加@EnableEurekaClient
解决了这个问题。 Zuul应用程序现在可以在Eureka服务器中被发现,并且能够路由请求。
答案 1 :(得分:1)
我在Zuul应用程序@EnableZuulClient上具有正确的注释,但仍然出现该错误。
问题是我同时启动了所有应用程序(Eureka,Zuul和第三个应用程序),而不是一个一个地启动。因此,他们都需要时间进行注册。一段时间后,错误消失了,我可以通过Zuul客户端访问我的服务。
答案 2 :(得分:0)
错误的spring.application.name可能是导致此错误的原因,而不是 在Zuul应用程序类中添加@EnableEurekaClient。