一、簡介
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/
從圖中我們可以看出,還沒有任何服務註冊至註冊中心。
附項目目錄截圖: