SpringCloud?eureka(server)微服務(wù)集群搭建過程
工作原理:
Spring Cloud框架下的服務(wù)發(fā)現(xiàn)Eureka包含兩個組件
分別是: Eureka Server與Eureka Client
Eureka Server,也稱為服務(wù)注冊中心。各個服務(wù)啟動后,會在Eureka Server中進(jìn)行注冊,這樣Eureka Server的服務(wù)注冊表中將會存儲所有可用服務(wù)節(jié)點的信息,服務(wù)節(jié)點的信息可以在界面中直觀的看到。
Eureka Client也稱為服務(wù)(服務(wù)實例)。作為一個Java客戶端,用于簡化與Eureka Server的交互。Eureka Client內(nèi)置一個 使用輪詢負(fù)載算法的負(fù)載均衡器。服務(wù)啟動后,Eureka Client將會向Eureka Server發(fā)送心跳更新服務(wù),如果Eureka Server在多個心跳周期內(nèi)沒有接收到某個服務(wù)的心跳,Eureka Server將會從服務(wù)注冊表中把這個服務(wù)節(jié)點移除(默認(rèn)90秒)。
Eureka組件的工作原理和核心功能點
上面的圖有三臺 Eureka Server 組成的集群,每一臺 Eureka Server服務(wù)在不同的地方。這樣三臺 Eureka Server 就組建成了一個高可用集群服務(wù),只要三個服務(wù)有一個能一直正常運(yùn)行,就不會影響整個架構(gòu)的穩(wěn)定性。
eureka 高可用集群
Eureka服務(wù)是一個單點服務(wù),在生產(chǎn)環(huán)境就會出現(xiàn)單點故障,為了確保Eureka服務(wù)的高可用,我需要搭建Eureka服務(wù)的集群。搭建Eureka集群非常簡單,只要啟動多個Eureka Server服務(wù)并且讓這些Server端之間彼此進(jìn)行注冊即可實現(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進(jìn)行訪問測試
下面 這里表示這有2個注冊中心的集群節(jié)點、當(dāng)前的注冊中心會從這兩個節(jié)點進(jìn)行同步服務(wù)、可以通過我們配置的hostname來進(jìn)行識別。
查看當(dāng)前注冊中心的集群節(jié)點。
到此這篇關(guān)于微服務(wù)SpringCloud-eureka(server)集群搭建的文章就介紹到這了,更多相關(guān)SpringCloud eureka集群搭建內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java 微信公眾號開發(fā)相關(guān)總結(jié)
公眾號作為主流的自媒體平臺,有著不少人使用。這次以文本回復(fù)作為案例來講解Java相關(guān)的微信公眾號開發(fā)2021-05-05IDEA(2022.2)搭建Servlet基本框架超詳細(xì)步驟
這篇文章主要給大家介紹了關(guān)于IDEA(2022.2)搭建Servlet基本框架超詳細(xì)步驟,Servlet容器負(fù)責(zé)Servlet和客戶的通信以及調(diào)用Servlet的方法,Servlet和客戶的通信采用"請求/響應(yīng)"的模式,需要的朋友可以參考下2023-10-10IDEA中啟動多個SpringBoot服務(wù)的實現(xiàn)示例
本文主要介紹了IDEA中啟動多個SpringBoot服務(wù)的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08Spring?Security自定義認(rèn)證邏輯實例詳解
這篇文章主要給大家介紹了關(guān)于Spring?Security自定義認(rèn)證邏輯的相關(guān)資料,文中通過實例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2022-01-01java數(shù)據(jù)結(jié)構(gòu)排序算法之歸并排序詳解
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)排序算法之歸并排序,結(jié)合具體實例形式詳細(xì)分析了歸并排序的原理、實現(xiàn)技巧與相關(guān)注意事項,需要的朋友可以參考下2017-05-05