目录

​1. 概述​

​2. Eureka服务端的搭建​

​3. Eureka客户端(Demo)的搭建​

​4. Eureka Server 高可用​

​5. 综述​

​6. 个人公众号​


1. 概述

老话说的好:遇见困难,首先要做的是积极的想解决办法,而不是先去泄气、抱怨或生气。

言归正传,微服务是当今非常流行的一种架构方式,其中 SpringCloud 是我们常用的一种微服务框架。

今天我们来聊聊 SpringCloud 中的服务治理组件 Eureka。

2. Eureka服务端的搭建

2.1 新建项目

打开IDEA,选择 File —> New —> Project...

SpringCloud 2020.0.4 系列之Eureka_eureka

2.2 填写项目信息

选择 Spring Initializr,填写项目信息

SpringCloud 2020.0.4 系列之Eureka_eureka_02

2.3 选择依赖项

选择 Spring Cloud Discovery —> Eureka Server

SpringCloud 2020.0.4 系列之Eureka_spring cloud_03

2.4 主要依赖

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2020.0.4</spring-cloud.version>
</properties>

<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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

2.5 配置 Eureka Server

spring:
application:
name: my-eureka
server:
port: 35000
eureka:
instance:
hostname: localhost # 应用实例主机名
client:
register-with-eureka: false # 是否发起服务注册
fetch-registry: false # 是否拉取服务注册表
server:
enable-self-preservation: false # 是否开启服务自我保护,建议关闭,开启自我保护机制后,实例宕机也被不会剔除
eviction-interval-timer-in-ms: 10000 # 每隔多久触发一次服务剔除,默认是60秒

建议关闭服务自我保护。

2.6 启动类中增加 @EnableEurekaServer 注解

SpringCloud 2020.0.4 系列之Eureka_java_04

2.7 启动 Eureka Server

启动 Eureka Server 项目,在浏览器输入 ​​http://Eureka服务器IP:35000/​​,可以查看 Eureka Server 控制台。

SpringCloud 2020.0.4 系列之Eureka_eureka_05

3. Eureka客户端(Demo)的搭建

3.1 新建项目,选择依赖项

选择 Spring Cloud Discovery —> Eureka Discorvery Client

SpringCloud 2020.0.4 系列之Eureka_spring cloud_06

3.2 主要依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 健康检查 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

3.3 配置 Eureka Client

spring:
application:
name: my-eureka-client
server:
port: 36000
eureka:
client:
service-url:
defaultZone: http://192.168.1.22:35000/eureka/ # Eureka Server的地址
healthcheck:
enabled: true # 开启健康检查, 依赖于 spring-boot-starter-actuator
instance:
lease-renewal-interval-in-seconds: 5 # 发出续约指令的间隔,默认30秒
lease-expiration-duration-in-seconds: 30 # 租期到期时间,默认90秒

3.4 启动类中增加 @EnableDiscoveryClient 注解

SpringCloud 2020.0.4 系列之Eureka_java_07

3.5 启动 Eureka Client

启动 Eureka Client 项目,在之前的 Eureka Server 控制台可以看到注册的服务

SpringCloud 2020.0.4 系列之Eureka_微服务_08

4. Eureka Server 高可用

4.1 概述

如果 Eureka Server 是单点应用,则宕机后,整个链路都会瘫痪。

因此我们采用多 Eureka Server 互相注册的方式,实现 Eureka Server 的高可用。

这里以两台 Eureka Server 为例。

4.2 配置服务器的 hostname

# vi /etc/hostname

例如: zhuifengren1、zhuifengren2

4.3 配置hosts

配置服务器节点的 hosts,使IP地址与hostname对应

# vi /etc/hosts

例如:

192.168.1.22 zhuifengren1

192.168.1.12 zhuifengren2

4.4 修改 Eureka Server1 的配置文件

spring:
application:
name: my-eureka
server:
port: 35000
eureka:
instance:
hostname: zhuifengren1 # 应用实例主机名
client:
service-url:
defaultZone: http://zhuifengren2:35001/eureka/ # Eureka Server的地址
server:
enable-self-preservation: false # 是否开启自我保护,建议关闭,开启自我保护机制后,实例宕机也被不会剔除
eviction-interval-timer-in-ms: 10000 # 每隔多久触发一次服务剔除,默认是60秒

4.5 修改 Eureka Server2 的配置文件

spring:
application:
name: my-eureka
server:
port: 35001
eureka:
instance:
hostname: zhuifengren2 # 应用实例主机名
client:
service-url:
defaultZone: http://zhuifengren1:35000/eureka/ # Eureka Server的地址
server:
enable-self-preservation: false # 是否开启自我保护,建议关闭,开启自我保护机制后,实例宕机也被不会剔除
eviction-interval-timer-in-ms: 10000 # 每隔多久触发一次服务剔除,默认是60秒

注意:两台 Eureka Server 的 spring.application.name 必须相同

4.6 修改 Eureka Client 的配置文件

server:
port: 36000
eureka:
client:
service-url:
defaultZone: http://zhuifengren1:35000/eureka/,http://zhuifengren2:35001/eureka/ # Eureka Server的地址
healthcheck:
enabled: true # 开启健康检查, 依赖于 spring-boot-starter-actuator
instance:
lease-renewal-interval-in-seconds: 5 # 发出续约指令的间隔,默认30秒
lease-expiration-duration-in-seconds: 30 # 租期到期时间,默认90秒

4.7 启动服务

启动后,任意停止某一个 Eureka Server,不影响 Eureka Client 服务的注册。

SpringCloud 2020.0.4 系列之Eureka_spring cloud_09

5. 综述

今天聊了一下 Eureka的相关知识,希望可以对大家的工作有所帮助。

欢迎帮忙点赞、评论、转发、加关注 :)

关注追风人聊Java,每天更新Java干货。

6. 个人公众号

追风人聊Java,欢迎大家关注

SpringCloud 2020.0.4 系列之Eureka_eureka_10