一、简介
Eureka是一个云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移。用它我们可以实现服务注册与发现功能
二、项目实例
1、创建Maven工程microservice-eureka-server,并在pom.xml中加入以下依赖包
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Camden.SR5</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2、创建应用启动类EurekaServerApplication.java
package com.baibei.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * @author: 会跳舞的机器人 * @email:[email protected] * @date: 17/2/15 下午5:39 * @description:启动主类 */ @SpringBootApplication @EnableEurekaServer public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class, args); } }
使用@EnableEurekaServer注解开启服务注册功能
3、配置application.yml
在resource目录下创建application.yml文件,内容如下:
spring: application: name: microservice-eureka-server server: port: 8761 eureka: instance: hostname: eureka-server client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://eureka-server:8761/eureka/
4、启动运行
运行EurekaServerApplication的main方法,启动成功后,访问:http://localhost:8761/
从图中我们可以看出,还没有任何服务注册至注册中心。
附项目目录截图: