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

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

@EnableEurekaServer:項目啟動類上使用@EnableEurekaServer注解/項目就是SpringCloud的注冊中心。
YML配置
配置3個eureka-server的application.yml
server:
port: 7001
#Eureka
eureka:
instance:
hostname: eureka7001.com #Eureka服務(wù)端實例名字
client:
register-with-eureka: false #表示是否向Eureka注冊中心注冊自己(服務(wù)器端不需要)
fetch-registry: false #false表示自己就是注冊中心
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文件修改
需要配置三個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 注冊集群的三個端口分別為 7001/7002/7003
啟動服務(wù)測試
啟動三個eureka-server進行訪問測試

下面 這里表示這有2個注冊中心的集群節(jié)點、當前的注冊中心會從這兩個節(jié)點進行同步服務(wù)、可以通過我們配置的hostname來進行識別。

查看當前注冊中心的集群節(jié)點。

到此這篇關(guān)于微服務(wù)SpringCloud-eureka(server)集群搭建的文章就介紹到這了,更多相關(guān)SpringCloud eureka集群搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Cloud出現(xiàn)Options Forbidden 403問題解決方法
本篇文章主要介紹了Spring Cloud出現(xiàn)Options Forbidden 403問題解決方法,具有一定的參考價值,有興趣的可以了解一下2017-11-11
SpringDataJPA之Specification復雜查詢實戰(zhàn)
這篇文章主要介紹了SpringDataJPA之Specification復雜查詢實戰(zhàn),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11
Java實現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法
這篇文章主要介紹了Java實現(xiàn)將漢字轉(zhuǎn)化為漢語拼音的方法,實例演示了Java引用pinyin4j庫實現(xiàn)漢子轉(zhuǎn)化成拼音的使用技巧,需要的朋友可以參考下2015-12-12
SpringBoot中@RestControllerAdvice注解的使用
這篇文章主要介紹了SpringBoot中@RestControllerAdvice注解的使用,@RestControllerAdvice主要用精簡客戶端返回異常,它可以捕獲各種異常,需要的朋友可以參考下2024-01-01

