一 Eureka概述
- 服務(wù)啟動(dòng)時(shí)會(huì)生成服務(wù)的基本信息對(duì)象InstanceInfo,然后在啟動(dòng)時(shí)會(huì)register到服務(wù)治理中心。
- 注冊(cè)完成后會(huì)從服務(wù)治理中心拉取所有的服務(wù)信息,緩存在本地次绘。 之后服務(wù)會(huì)被30s(可配置)發(fā)送一個(gè)心跳信息窟却,續(xù)約服務(wù)。
- 如果服務(wù)治理中心在90s內(nèi)沒(méi)有收到一個(gè)服務(wù)的續(xù)約卵洗,就會(huì)認(rèn)為服務(wù)已經(jīng)掛了,會(huì)把服務(wù)注冊(cè)信息刪掉弥咪。
- 服務(wù)停止前过蹂,服務(wù)會(huì)主動(dòng)發(fā)送一個(gè)停止請(qǐng)求,服務(wù)治理中心會(huì)刪除這個(gè)服務(wù)的信息聚至。
- 如果Eureka Server收到的心跳包不足正常值的85%(可配置)就會(huì)進(jìn)入自我保護(hù)模式酷勺,在這種模式下,Eureka Server不會(huì)刪除任何服務(wù)信息扳躬。
二 Eureka架構(gòu)圖
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">在這里插入圖片描述</figcaption>
Registery:表示服務(wù)向注冊(cè)中心注冊(cè)脆诉。
Renew:表示服務(wù)向注冊(cè)中心發(fā)送心跳甚亭,表示該服務(wù)還活著,注冊(cè)中心不能刪除改服務(wù)击胜。
Cancel:表示注冊(cè)中心能刪除該服務(wù)
Get Registery:表示該服務(wù)重新注冊(cè)
Replicate:表示注冊(cè)中心之間相互注冊(cè)
Remote Call:表示遠(yuǎn)程調(diào)用
三 搭建Eureka環(huán)境
-
創(chuàng)建SpringBoot項(xiàng)目
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">在這里插入圖片描述</figcaption>
-
刪除多余的目錄(src等目錄亏狰,因?yàn)閯?chuàng)建maven的聚合工程需要)只剩下pom文件,名字叫做eureka-father作為聚合工程的父級(jí)工程偶摔,如圖
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">在這里插入圖片描述</figcaption>
pom文件的內(nèi)容如下:
<?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"> <!--基本信息--> <description>SpringBoot-Eureka環(huán)境搭建多模塊構(gòu)建示例</description> <modelVersion>4.0.0</modelVersion> <name>eureka-father</name> <packaging>pom</packaging> <!-- 項(xiàng)目說(shuō)明:這里作為聚合工程的父工程 --> <groupId>com.org.ldc</groupId> <artifactId>eureka-father</artifactId> <version>1.0.0.RELEASE</version> <!-- 繼承說(shuō)明:這里繼承SpringBoot提供的父工程 --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <!-- 表示子模塊 --> <modules> <module>eureka3000</module> </modules> <!-- 子模塊的依賴 --> <dependencyManagement> <dependencies> <dependency> <groupId>com.org.ldc</groupId> <artifactId>eureka3000</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies> </dependencyManagement></project>
-
創(chuàng)建子模塊eureka3000暇唾,方法如圖所示
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">在這里插入圖片描述</figcaption>
子模塊的pom文件內(nèi)容如下:
<?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> <groupId>com.org.ldc</groupId> <artifactId>eureka3000</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eureka3000</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <!-- 繼承本項(xiàng)目的父工程 --> <parent> <groupId>com.org.ldc</groupId> <artifactId>eureka-father</artifactId> <version>1.0.0.RELEASE</version> </parent> <!--引入eurekaserver 依賴--> <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-web</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</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>
- 子模塊的啟動(dòng)類如下:這里需要注意的就是要在類上加上@EnableEurekaServer注解
package com.org.ldc.eureka3000;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;@SpringBootApplication@EnableEurekaServerpublic class Eureka3000Application { public static void main(String[] args) { SpringApplication.run(Eureka3000Application.class, args); }}
- 子模塊的application.properties文件的內(nèi)容如下:
server: port: 3000eureka: server: enable-self-preservation: false #關(guān)閉自我保護(hù)機(jī)制 eviction-interval-timer-in-ms: 4000 #設(shè)置清理間隔(單位:毫秒 默認(rèn)是60*1000) instance: hostname: localhost client: registerWithEureka: false #不把自己作為一個(gè)客戶端注冊(cè)到自己身上 fetchRegistry: false #不需要從服務(wù)端獲取注冊(cè)信息(因?yàn)樵谶@里自己就是服務(wù)端,而且已經(jīng)禁用自己注冊(cè)了) serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka
-
以上創(chuàng)建的表示一個(gè)eureka節(jié)點(diǎn)辰斋,啟動(dòng)子模塊的啟動(dòng)類策州,然后瀏覽器訪問(wèn)http://localhost:3000,就會(huì)出現(xiàn)如下頁(yè)面,紅色框內(nèi)一開(kāi)始使沒(méi)有內(nèi)容的宫仗,因?yàn)槲疫@是搭建的有三個(gè)節(jié)點(diǎn)eureka和有注冊(cè)服務(wù)的服務(wù)工程够挂,所以紅色框內(nèi)會(huì)出現(xiàn)內(nèi)容,所以搭建不要糾結(jié)這個(gè)藕夫,只要出現(xiàn)這個(gè)界面下硕,就說(shuō)明eureka環(huán)境搭建好了,但是使單節(jié)點(diǎn)的汁胆,不是集群的
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">在這里插入圖片描述</figcaption>
接著創(chuàng)建服務(wù)模塊梭姓,創(chuàng)建過(guò)程和創(chuàng)建子模塊一樣,不再贅述嫩码,名字叫做user5000誉尖,如圖eureka3001、eureka3001是我創(chuàng)建的另外的兩個(gè)注冊(cè)中心節(jié)點(diǎn)
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">在這里插入圖片描述</figcaption>
服務(wù)模塊的pom文件內(nèi)容如下:
<?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> <groupId>com.org.ldc</groupId> <artifactId>user5000</artifactId> <version>0.0.1-SNAPSHOT</version> <name>user5000</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <!-- 繼承本項(xiàng)目的父工程 --> <parent> <groupId>com.org.ldc</groupId> <artifactId>eureka-father</artifactId> <version>1.0.0.RELEASE</version> </parent> <dependencies> <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> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Finchley.SR2</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.yml文件內(nèi)容如下:
server: port: 5000eureka: client: serviceUrl: defaultZone: http://localhost:3000/eureka/ #eureka服務(wù)端提供的注冊(cè)地址 參考服務(wù)端配置的這個(gè)路徑 instance: instance-id: user-1 #此實(shí)例注冊(cè)到eureka服務(wù)端的唯一的實(shí)例ID prefer-ip-address: true #是否顯示IP地址 leaseRenewalIntervalInSeconds: 10 #eureka客戶需要多長(zhǎng)時(shí)間發(fā)送心跳給eureka服務(wù)器铸题,表明它仍然活著,默認(rèn)為30 秒 (與下面配置的單位都是秒) leaseExpirationDurationInSeconds: 30 #Eureka服務(wù)器在接收到實(shí)例的最后一次發(fā)出的心跳后铡恕,需要等待多久才可以將此實(shí)例刪除,默認(rèn)為90秒spring: application: name: server-user #此實(shí)例注冊(cè)到eureka服務(wù)端的name
- 啟動(dòng)類如下:需要加上@EnableEurekaClient 注解
package com.org.ldc.user5000;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.eureka.EnableEurekaClient;@SpringBootApplication@EnableEurekaClient //開(kāi)啟eureka客戶端public class User5000Application { public static void main(String[] args) { SpringApplication.run(User5000Application.class, args); }}
-
最后測(cè)試先啟動(dòng)注冊(cè)中心的啟動(dòng)類丢间,然后啟動(dòng)服務(wù)模塊的啟動(dòng)類探熔,最后訪問(wèn)localhost:3000出現(xiàn)如下圖說(shuō)明搭建成功
<figcaption style="line-height: inherit; margin: 0px; padding: 0px; margin-top: 10px; text-align: center; color: rgb(153, 153, 153); font-size: 0.7em;">在這里插入圖片描述</figcaption>
更多的教程請(qǐng)關(guān)注公眾號(hào) 非科班的科班,烘挫。假如你覺(jué)得我寫(xiě)的好诀艰,路過(guò)的請(qǐng)點(diǎn)個(gè)贊,謝謝饮六。