Eureka是Netflix公司開源的產品,提供了完整的Service Registry和Service Discovery實作,也是Spring Cloud體系中最重要最核心的組件之一,可以應用在任何需要使用注冊中心的場景,本文主要介紹Eureka的概念及基本使用,文中使用到的軟體版本:Spring Boot 2.2.5.RELEASE、Spring Cloud Hoxton.SR3、Java 1.8.0_191,
1、Eureka架構

1.1、Eureka Server
Eureka Server提供如下功能:
服務注冊
服務提供者啟動時,會通過Eureka Client向Eureka Server注冊資訊,Eureka Server會存盤該服務的資訊,其內部有二層快取機制來維護整個注冊表,
提供注冊表
服務消費者在呼叫服務時,如果Eureka Client沒有快取注冊表的話,會從Eureka Serve 獲取最新的注冊表
同步狀態
Eureka Client通過注冊、心跳機制和Eureka Server同步當前客戶端的狀態,
1.2、Eureka Client
Eureka Client用于簡化與Eureka Server的互動,它會拉取、更新和快取 Eureka Server 中的資訊;因此當所有的Eureka Server節點都宕掉,服務消費者依然可以使用快取中的資訊找到服務提供者,但是當服務有更改的時候會出現資訊不一致,
Eureka Client又分為:Application Service(Service Provider)和Application Client(Service Consumer),
1.2.1、Application Service
服務提供方,是注冊到Eureka Server中的服務,
1.2.2、Application Client
服務消費方,通過Eureka Server發現服務,并消費,
一個應用可以同時是Application Service和Application Client,
1.3、路徑
Register(服務注冊):把自己的IP和埠等資訊注冊給Eureka Server,
Renew(服務續約):發送心跳包,每30秒發送一次,告訴Eureka Server自己還活著,
Cancel(服務下線):Eureka Client在程式關閉時向Eureka Server發送取消請求;發送請求后,該客戶端實體資訊將從Eureka Server的實體注冊表中洗掉,
Get Registry(獲取服務注冊串列):獲取注冊串列,
Replicate(集群中資料同步):Eureka Server集群中的資料復制與同步,
Make Remote Call(遠程呼叫):完成遠程服務呼叫,
2、Eureka常用配置引數
2.1、eureka.instance
| hostname | 與此實體相關聯的主機名,是其他實體可以用來進行請求的準確名稱 |
| lease-renewal-interval-in-seconds | Eureka Client發送心跳給Eureka Server的時間間隔,默認為30 秒 |
| lease-expiration-duration-in-seconds | Eureka Server在接收到實體的最后一次發出的心跳后,等待多久才可以將此實體洗掉,默認為90秒 |
2.2、eureka.server
| enable-self-preservation |
自我保護模式,當出現出現網路磁區、eureka在短時間內丟失過多客戶端時,會進入自我保護模式,即一個服務長時間沒有發送心跳,Eureka Server也不會將其洗掉,默認為true |
2.3、eureka.client
| register-with-eureka | Eureka Client是否在Eureka Server上注冊自己的資訊以供其他服務發現,默認為true |
| fetch-registry | Eureka Client是否獲取eureka服務器注冊表上的注冊資訊,默認為true |
3、Eureka Server搭建
3.1、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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.inspur</groupId> <artifactId>scdemo-eureka</artifactId> <version>1.0</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.5.RELEASE</version> <relativePath /> </parent> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR3</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.2、application.yml
spring:
application:
name: scdemo-eureka
server:
port: 9000
eureka:
instance:
prefer-ip-address: true
#hostname: localhost
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://localhost:${server.port}/eureka
server:
enable-self-preservation: false
3.3、啟動類
@SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
3.4、控制臺
啟動Eureka Server后,可以在控制臺查看注冊的服務及相關的狀態資訊;根據上面的配置,控制臺地址為:
http://localhost:9000/
3.5、Eureka Server集群搭建
3.5.1、application.yml
集群的搭建需要修改下application.yml,設定eureka.client.register-with-eureka、eureka.client.fetch-registry為true,eureka.client.service-url.defaultZone改為集群的地址;假設在10.49.196.10、10.49.196.11、10.49.196.12三臺機器上部署Eureka Server,則application.yml修改如下:
spring:
application:
name: scdemo-eureka
server:
port: 9000
eureka:
instance:
prefer-ip-address: true
#hostname: localhost
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://10.49.196.10:9000/eureka,http://10.49.196.11:9000/eureka,http://10.49.196.12:9000/eureka
server:
enable-self-preservation: false
3.5.2、部署
分別在三臺機器上啟動Eureka Server,查看各控制臺:
http://10.49.196.10:9000/

http://10.49.196.10:9001/
http://10.49.196.12:9001/

4、Eureka Client使用
4.1、引入Eureka Client依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
4.2、配置Eureka
eureka:
instance:
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:9000/eureka/
#defaultZone: http://10.49.196.10:9000/eureka,http://10.49.196.11:9000/eureka,http://10.49.196.12:9000/eureka
4.3、啟動類啟用Eureka Client
@SpringBootApplication @EnableEurekaClient public class ServerApplication { public static void main(String[] args) { SpringApplication.run(ServerApplication.class, args); } }
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/93411.html
標籤:Java
下一篇:LeetCode–替換空格
