SpringCloud?eureka(server)微服務(wù)集群搭建過程
工作原理:
Spring Cloud框架下的服務(wù)發(fā)現(xiàn)Eureka包含兩個(gè)組件
分別是: Eureka Server與Eureka Client
Eureka Server,也稱為服務(wù)注冊(cè)中心。各個(gè)服務(wù)啟動(dòng)后,會(huì)在Eureka Server中進(jìn)行注冊(cè),這樣Eureka Server的服務(wù)注冊(cè)表中將會(huì)存儲(chǔ)所有可用服務(wù)節(jié)點(diǎn)的信息,服務(wù)節(jié)點(diǎn)的信息可以在界面中直觀的看到。
Eureka Client也稱為服務(wù)(服務(wù)實(shí)例)。作為一個(gè)Java客戶端,用于簡化與Eureka Server的交互。Eureka Client內(nèi)置一個(gè) 使用輪詢負(fù)載算法的負(fù)載均衡器。服務(wù)啟動(dòng)后,Eureka Client將會(huì)向Eureka Server發(fā)送心跳更新服務(wù),如果Eureka Server在多個(gè)心跳周期內(nèi)沒有接收到某個(gè)服務(wù)的心跳,Eureka Server將會(huì)從服務(wù)注冊(cè)表中把這個(gè)服務(wù)節(jié)點(diǎn)移除(默認(rèn)90秒)。
Eureka組件的工作原理和核心功能點(diǎn)

上面的圖有三臺(tái) Eureka Server 組成的集群,每一臺(tái) Eureka Server服務(wù)在不同的地方。這樣三臺(tái) Eureka Server 就組建成了一個(gè)高可用集群服務(wù),只要三個(gè)服務(wù)有一個(gè)能一直正常運(yùn)行,就不會(huì)影響整個(gè)架構(gòu)的穩(wěn)定性。
eureka 高可用集群
Eureka服務(wù)是一個(gè)單點(diǎn)服務(wù),在生產(chǎn)環(huán)境就會(huì)出現(xiàn)單點(diǎn)故障,為了確保Eureka服務(wù)的高可用,我需要搭建Eureka服務(wù)的集群。搭建Eureka集群非常簡單,只要啟動(dòng)多個(gè)Eureka Server服務(wù)并且讓這些Server端之間彼此進(jìn)行注冊(cè)即可實(shí)現(xiàn)
在我們實(shí)際的開發(fā)生產(chǎn)環(huán)境中,eureka 常常是以集群的方式提供服務(wù)的,目的就是要保證高可用性,同時(shí)它還保證了分區(qū)容錯(cuò)性。這也滿足了一個(gè)健壯的分布式微服務(wù)所要求的 CAP 理論原則,即 eureka 保證了高可用性,分區(qū)容錯(cuò)性。
項(xiàng)目創(chuàng)建:
項(xiàng)目搭建的主要步驟和配置就是創(chuàng)建項(xiàng)目和引入pom依賴。新建3個(gè)eureka注冊(cè)中心

@EnableEurekaServer:項(xiàng)目啟動(dòng)類上使用@EnableEurekaServer注解/項(xiàng)目就是SpringCloud的注冊(cè)中心。
YML配置
配置3個(gè)eureka-server的application.yml
server:
port: 7001
#Eureka
eureka:
instance:
hostname: eureka7001.com #Eureka服務(wù)端實(shí)例名字
client:
register-with-eureka: false #表示是否向Eureka注冊(cè)中心注冊(cè)自己(服務(wù)器端不需要)
fetch-registry: false #false表示自己就是注冊(cè)中心
service-url:
defaultZone: http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/Maven 依賴
<?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.liy</groupId>
<artifactId>eurekaserver-7001</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eurekaserver-7001</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.0.RELEASE</spring-boot.version>
<spring-cloud.version>Hoxton.SR4</spring-cloud.version>
</properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</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>
<dependency>
<groupId>com.liy</groupId>
<artifactId>eurekaserver-7001</artifactId>
<version>0.0.1-SNAPSHOT</version>
<scope>test</scope>
</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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>本地hosts文件修改
需要配置三個(gè)hostname、否則無法集群
在C:\Windows\System32\drivers\etc\hosts 文件類增加 127.0.0.1 eureka7001.com 127.0.0.1 eureka7002.com 127.0.0.1 eureka7003.com 注冊(cè)集群的三個(gè)端口分別為 7001/7002/7003
啟動(dòng)服務(wù)測試
啟動(dòng)三個(gè)eureka-server進(jìn)行訪問測試

下面 這里表示這有2個(gè)注冊(cè)中心的集群節(jié)點(diǎn)、當(dāng)前的注冊(cè)中心會(huì)從這兩個(gè)節(jié)點(diǎn)進(jìn)行同步服務(wù)、可以通過我們配置的hostname來進(jìn)行識(shí)別。

查看當(dāng)前注冊(cè)中心的集群節(jié)點(diǎn)。

到此這篇關(guān)于微服務(wù)SpringCloud-eureka(server)集群搭建的文章就介紹到這了,更多相關(guān)SpringCloud eureka集群搭建內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
為什么SpringMVC中請(qǐng)求的body不支持多次讀取
這篇文章主要介紹了為什么SpringMVC中請(qǐng)求的body不支持多次讀取,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
java命令調(diào)用虛擬機(jī)方法總結(jié)
在本篇文章里我們給大家整理了關(guān)于java中的java命令如何調(diào)用虛擬機(jī)的方法和具體步驟,需要的朋友們跟著操作下。2019-05-05
Spring Cloud出現(xiàn)Options Forbidden 403問題解決方法
本篇文章主要介紹了Spring Cloud出現(xiàn)Options Forbidden 403問題解決方法,具有一定的參考價(jià)值,有興趣的可以了解一下2017-11-11
SpringDataJPA之Specification復(fù)雜查詢實(shí)戰(zhàn)
這篇文章主要介紹了SpringDataJPA之Specification復(fù)雜查詢實(shí)戰(zhàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java實(shí)現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法
這篇文章主要介紹了Java實(shí)現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法,實(shí)例演示了Java引用pinyin4j庫實(shí)現(xiàn)漢子轉(zhuǎn)化成拼音的使用技巧,需要的朋友可以參考下2015-12-12
使用Filter實(shí)現(xiàn)登錄權(quán)限驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了使用Filter實(shí)現(xiàn)登錄權(quán)限驗(yàn)證,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-10-10
SpringBoot中@RestControllerAdvice注解的使用
這篇文章主要介紹了SpringBoot中@RestControllerAdvice注解的使用,@RestControllerAdvice主要用精簡客戶端返回異常,它可以捕獲各種異常,需要的朋友可以參考下2024-01-01

