一、简介
Eureka是一个云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移。用它我们可以实现服务注册与发现功能
二、项目实例
1、创建Maven工程microservice-eureka-server,并在pom.xml中加入以下依赖包
org.springframework.boot
spring-boot-starter-parent
1.4.4.RELEASE
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-eureka-server
org.springframework.cloud
spring-cloud-dependencies
Camden.SR5
pom
import
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:2268549298@qq.com
* @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/
Paste_Image.png
从图中我们可以看出,还没有任何服务注册至注册中心。
附项目目录截图:
Paste_Image.png
本文介绍了如何使用Java和Spring Cloud搭建Eureka服务注册中心。通过创建Maven工程,添加相关依赖,创建启动类并配置application.yml,实现了Eureka Server的启动。启动后,可以在8761端口访问,但目前注册中心尚未有服务注册。
1041

被折叠的 条评论
为什么被折叠?



