前言
Eureka是Spring Cloud的核心模块之一,用于服务的注册与发现。这一篇主要介绍如何配置和启动一个Eureka。
正文
第一步,依赖
Maven的.pom文件中配置启动Eureka所需依赖:
<dependencies>
<!-- 1.3.1.RELEASE 对应spring版本为4.3.8 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka-server</artifactId>
<version>1.3.1.RELEASE</version>
</dependency>
</dependencies>
每一个eureka版本都同样对应着不同的spring的版本,我之所以选择1.3.1.RELEASE是因为本地spring版本为4.3.8.RELEASE。
第二步,配置
定义配置文件,命名为application.yml或application.properties均可。
server:
port: 8762
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
第三步,启动
构建启动类:
package com.silence.spring.cloud.eureka;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(EurekaApplication.class, args);
}
}
Spring Cloud依赖于Spring Boot,因此简化了相关配置工作。
验证
启动后,浏览器中键入链接地址:http://localhost:8762/,如果出现如下图,证明Eureka启动成功。