msc-eureka-server
创建Eureka Server模块,用来管理以后创建的Eureka Client实例。
模块创建
右键New —> Module,
选择使用Springboot脚手架,选择JDK,然后点击Next。
填写GroupId、ArtifactId、Version,然后Next。
选择Eureka Server。这里会默认使用Springboot版本2.2.7,这个没大碍,我们已经在父工程里已经配置了Springboot版本,后面会同一使用父工程里的版本。
点击finish按钮后,加载依赖需要一定的时间,耐心等待一下。
配置pom文件
<?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>team.one.msc</groupId>
<artifactId>msc-parent</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../msc-parent/pom.xml</relativePath> <!-- lookup parent from repository -->
</parent>
<artifactId>msc-eureka-server</artifactId>
<version>1.0-SNAPSHOT</version>
<name>msc-eureka-server</name>
<description>Demo project for Spring Boot</description>
<!-- 打包方式 -->
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Java版本 -->
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</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>
启用Eureka Server
启动类加Eureka Server注解
package team.one.msc;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
/**
* @EnableEurekaServer 表示开启Eureka Server功能
*/
@EnableEurekaServer
@SpringBootApplication
public class MscEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(MscEurekaServerApplication.class, args);
}
}
配置application.yml文件
新创建的项目默认的配置文件是application.properties文件,现在一般使用application.yml作为配置文件。
server:
# eureka-server服务的端口号
port: 7001
spring:
application:
# 服务名称,在微服务架构中具有唯一性,使用服务名可以找到对应的服务
name: msc-eureka-server
# spring-cloud-starter-netflix-eureka-server依赖中既有eureka-server,也有eureka-client,
# 就是说msc-eureka-server项目既是eureka-server,也是eureka-client
eureka:
instance:
# eureka服务端的实例名称,本地开发时需要在host文件中将 eureka01 映射到 127.0.0.1
hostname: eureka01
# 优先使用ip
prefer-ip-address: true
# 租期,即心跳,当前client每隔这个时间会主动向server发起一次心跳,表明当前服务仍正常存活,单位秒
# eureka server配置这个属性是为了后面做集群
lease-renewal-interval-in-seconds: 2
# server监听的某个client A 如果距离上次心跳时间超过 lease-expiration-duration-in-seconds 后仍未监听到 A 的心跳,
# 则认为 A 挂掉了,并将其从正常实例中剔除,单位秒
lease-expiration-duration-in-seconds: 6
client:
# 表示是否向(别的)注册中心注册自己
register-with-eureka: false
# client端是否去server端拉取注册实例信息,在这里是server端,所以配置false
fetch-registry: false
server:
# 自我保护机制开启,如果在15分钟内超过85%的节点都没有正常的心跳,那么Eureka就认为客户端与注册中心出现了网络故障
enable-self-preservation: true
renewal-percent-threshold: 0.85
启动
当日志中显示上面的内容,则表示项目启动成功。
在浏览器中输入 http://localhost:7001/ 回车会跳转如下页面。