注意:(请选择一个统一版本)
springboot版本2.0.1对应springCloud版本是Finchley.RC1
springboot版本2.1.1对应springCloud版本是Greenwich.M3(本次演示使用了该版本)
1.创建项目
2.选择springboot版本2.1.1,和添加Eureka服务的依赖
3.添加可用依赖(查看是否具有该依赖)
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.M3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<!-- Eureka服务端 -->
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<!-- SpringCloud依赖,一定要放到dependencyManagement中,起到管理版本的作用即可 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
4.编写启动类:
@SpringBootApplication
@EnableEurekaServer // 声明这个应用是一个EurekaServer
public class EurekaDemoApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaDemoApplication.class, args);
}
}
5. 编写配置application.yml:
server:
port: 10086 #eureka端口号
spring:
application:
name: eureka-server #eureka服务的名字
eureka:
server:
enable-self-preservation: true # Eureka自我保护机制,true打开/false禁用,默认打开状态,建议生产环境打开此配置。
eviction-interval-timer-in-ms: 5000 # 修改检查失效服务的时间
client:
fetch-registry: true # 定期的更新客户端的服务清单,以保证服务访问的正确性
register-with-eureka: true # 是否将自己注册为服务
service-url:
defaultZone: http://127.0.0.1:${server.port}/eureka # eureka服务的开放地址
6.启动服务,并访问:http://127.0.0.1:10086/eureka