eureka是一个注册中心,与zookeeper不同的是,eureka是restful格式的调用,zk是rpc,还有就是zk保证一致和容错,eureka则是可用和容错.
使用时首先要加入依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
yml文件里
server: port: 7001 eureka: instance: hostname: localhost #eureka服务端的实例名称 client: register-with-eureka: false #falsege表示不向注册 中心注册自己 fetch-registry: false #不需要去检索服务 service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #设置与eureka server交互的地址查询服务和注册 服务的地址
主启动类里面
@SpringBootApplication @EnableEurekaServer //标注为eureka服务端 public class EurekaServerApp { public static void main(String[] args) { SpringApplication.run(EurekaServerApp.class,args); } }
接着可以启动,启动好后在浏览器里输入http://locahost:7001就看到eureka的页面了。
注意:
有时候会报错,可能是引入包冲突了,官网给了如下建议:
Finchley builds and works with Spring Boot 2.0.x, and is not expected to work with Spring Boot 1.5.x.
Note: The Dalston release train will reach end-of-life in December 2018. Edgware will follow the end-of-life cycle of Spring Boot 1.5.x.
The Dalston and Edgware release trains build on Spring Boot 1.5.x, and are not expected to work with Spring Boot 2.0.x.
NOTE: The Camden release train was marked end-of-life.
The Camden release train builds on Spring Boot 1.4.x, but is also tested with 1.5.x.
NOTE: The Brixton and Angel release trains were marked end-of-life (EOL) in July 2017.
The Brixton release train builds on Spring Boot 1.3.x, but is also tested with 1.4.x.
The Angel release train builds on Spring Boot 1.2.x, and is incompatible in some areas with Spring Boot 1.3.x. Brixton builds on Spring Boot 1.3.x and is similarly incompatible with 1.2.x. Some libraries and most apps built on Angel will run fine on Brixton, but changes will be required anywhere that the OAuth2 features from spring-cloud-security 1.0.x are used (they were mostly moved to Spring Boot in 1.3.0).