这个错误的原因可能是eureka服务未启动,一定要先启动eureka服务,在将普通服务注册到eureka中。
将其他服务注册到eureka很简单,就是在原有的工程,pom文件中加入eureka依赖
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
然后在启动类上添加服务发现注解@EnableDiscoveryClient
在配置文件中添加配置。
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
相比服务端的配置,可以发现缺少了两项配置,其他的配置没有任何差异
registerWithEureka: false
fetchRegistry: false
eureka:
instance:
hostname: localhost
client:
#声明自己是服务端
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/