一 新建maven项目加以下依赖
test etst 1.0-SNAPSHOT UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-parent 1.5.6.RELEASE org.springframework.boot spring-boot-starter org.springframework.cloud spring-cloud-starter-eureka-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Dalston.SR3 pom import org.springframework.boot spring-boot-maven-plugin
依赖版本可能会很快更新,所以上面的版本如果想获取最新的版本,请参照 获取最新版本的依赖
二 新建类如下
package test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@EnableEurekaServer@SpringBootApplicationpublic class Eurekaserver { public static void main(String[] args) { SpringApplication.run(Eurekaserver.class, args); }}
1. 注意 此类应放在某一个包例如test下,因为spring boot不希望扫描作用于所有的包,如果在默认default的包下会报 Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.错误
2. 另一个解决办法是在此类加入ComponentScan注解来限制包的扫描范围限制即可
三 在resource目录下的application.properties添加以下内容
#指定注册中心端口server.port=8761#服务器ipeureka.instance.hostname=localhost#是否将eureka自身作为应用注册到eureka注册中心eureka.client.registerWithEureka=false#为true时,可以启动,但报异常:Cannot execute request on any known servereureka.client.fetchRegistry=false#访问地址 http://localhost:8761eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/# 设为false,关闭自我保护#eureka.server.enable-self-preservation=false
最后,运行并访问即可