1、eureka 服务中心
依赖只需
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
依赖管理中申明:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.M9</version> <type>pom</type> <scope>import</scope> </dependency>
这样引入的spring-boot是最新的2.0.1版本。
服务中心配置:application.yml
server: port: 8761 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
Application.java中加入注解:
@EnableEurekaServer
有坑的地方:官方文档是
registerWithEureka,fetchRegistry。如果这么写没有效果,老是会报错说statusoverride属性找不到。
2、eureka客户端
加入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
application.yml中加入:
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka
注意:新版的加入spring-cloud-starter-netflix-eureka-client后,无需在Application启动类中加入client的注解,会自动识别为eureka客户端。
启动后,就会将服务注册到服务中心