前提
系统安装jdk1.8及以上,配置好maven的ide(这里用idea进行演示,maven版本3.5,配置阿里云源)
项目搭建
新建一个maven项目,创建最简单的那种就好,项目名这里为EurekaServerDemo,包名什么的随意,项目打包方式为jar,
也可以使用spring官方的生成器,官方的生成器会创建基础的springboot项目结构。这里为了演示,都可以
修改pom文件,参考如下,版本推荐和本文相同,springboot和cloud版本的坑很多
<&#63;xml version="1.0" encoding="UTF-8"&#63;>4.0.0 com.hellxz EurekaServerDemo 0.0.1-SNAPSHOT jar org.springframework.cloud spring-cloud-netflix-eureka-server 1.3.5.RELEASE EurekaServerDemo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE org.springframework.cloud spring-cloud-dependencies Camden.SR3 pom import org.springframework.cloud spring-cloud-config-server org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-maven-plugin org.apache.maven.plugins maven-compiler-plugin 1.8
新建一个主类,用于启动项目
package com.hellxz.EurekaServerDemo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; /** * @Author : Hellxz * @Description: EurekaServer * @Date : 2018/4/13 16:53 */ @EnableEurekaServer @SpringBootApplication public class EurekaServerDemoApplication { public static void main(String[] args) { //启动这个springboot应用 SpringApplication.run(EurekaServerDemoApplication.class,args); } }
在resources目录下新建一个application.properties文件,用于配置EurekaServer相关参数,也可以使用yaml文件
#提供服务端口 server.port=1111 #提供服务的域名,本地可以使用localhost或者配置hosts测试 eureka.instance.hostname=localhost #关闭向注册中心注册自己 eureka.client.register-with-eureka=false #关闭发现注册服务,注册中心仅用于维护节点 eureka.client.fetch-registry=false #配置注册中心提供服务的url(这里引用上边的配置) eureka.client.serviceUrl.defaultZOne=http://${eureka.instance.hostname}:${server.port}/eureka/
启动这个项目测试一下
测试
因为配置的是localhost:1111作为访问路径,这里启动项目后直接访问就好,如图
至此Eureka注册中心搭建完毕
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。