使用IDEA创建新的项目
在Idea里依次点击:File >>> New >>> Project
选择Spring Initializr 初始化 Spring Boot 项目,IDEA会自动生成包名与pom文件配置相关依赖,点击next下一步
根据实际项目需要配置好以下相关参数
左侧选择Spring Cloud Discovery 右边选择Spring Cloud所依赖的Spring Boot版本,点击下一步
输入项目名与文件存储地址,点击Finish完成操作,IDEA会自动在pom文件中加入Eureka依赖包与Spring Boot的依赖包
pom.xml文件配置内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.18.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>eureka</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka</name>
<description>Eureka project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Greenwich.SR6</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application启动类
package com.example.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) {
SpringApplication.run(EurekaApplication.class, args);
}
}
application.yml配置
server:
# 服务器端口
port: 7001
spring:
profiles:
active: dev
application:
# Spring项目名称
name: eureka-server
eureka:
instance:
# Eureka注册服务器名称
hostname: localhost
#
preferIpAddress: true
client:
# 是否检索服务,多节点时配置true,开启服务之间同步
fetchRegistry: false
# 是否注册给服务中心,如需注册到自己本身服务上则设置为true
registerWithEureka: false
# 治理客户端服务域,以下为单节点配置
# 多节点集群配置,以逗号隔开http://localhost:7002/eureka/,http://localhost:7003/eureka/
serviceUrl:
defaultZone: http://localhost:7001/eureka/
server:
enableSelfPreservation: false
eviction-interval-timer-in-ms: 10000