系列文章
Spring Cloud系列(一):Spirng Cloud變化
Spring Cloud系列(二):Eureka Server應(yīng)用
?
?
目錄
前言
注冊(cè)中心對(duì)比
????????Nacos
????????Zookeeper
????????Consul
搭建服務(wù)
????????準(zhǔn)備
????????搭建
????????????????搭建父模塊
????????????????搭建Server模塊
啟動(dòng)服務(wù)
測(cè)試
其他
前言
前面針對(duì)新版本的變化有了了解,接下來,對(duì) Spring Cloud 規(guī)范下的各大組件做一個(gè)介紹和應(yīng)用用,包括原理,首先就是微服務(wù)的核心——注冊(cè)中心 Eureka。
Eureka 是 Spring Cloud 提供的默認(rèn)的服務(wù)注冊(cè)中心,其提供了服務(wù)注冊(cè)與發(fā)現(xiàn)功能。
Eureka 包含?Eureka-Server 和 Eureka-Client 兩部分,?Eureka-Server 是服務(wù)注冊(cè)中心,用于管理注冊(cè)的所有服務(wù);Eureka-Client 是客戶端,用于服務(wù)提供者提供服務(wù)和服務(wù)消費(fèi)者調(diào)用服務(wù)。
其工作流程大體如下:
注冊(cè)中心對(duì)比
除了 Spring Cloud 默認(rèn)的注冊(cè)中心 Eureka 外,當(dāng)今主流的注冊(cè)中心還有 Nacos、Zookeeper、Consul 等。
Nacos
Nacos 是一個(gè)阿里開源的動(dòng)態(tài)服務(wù)發(fā)現(xiàn)、配置管理和服務(wù)管理平臺(tái),其不但提供了注冊(cè)中心的功能,還提供了配置中心、簡單的權(quán)限管理等功能,功能易用且強(qiáng)大。
Zookeeper
Zookeeper 是一個(gè)分布式服務(wù)框架,其采用存儲(chǔ)+通知的方式,解決分布式中的各種問題,其功能包括:發(fā)布/訂閱、分布式隊(duì)列、集群管理、分布式獨(dú)占鎖/讀寫鎖、集群Leader選舉、分布式ID生成等等。這些功能主要是利用其 Znode 節(jié)點(diǎn)的特性和其節(jié)點(diǎn)監(jiān)聽的功能,其采用 ZAB 協(xié)議保持?jǐn)?shù)據(jù)的一致性。
Consul
Consul 是由 HashiCorp 基于 Go 語言開發(fā)的支持多數(shù)據(jù)中心分布式高可用的服務(wù)發(fā)布和注冊(cè)服務(wù)軟件, 采用 Raft 算法保證服務(wù)的一致性,且支持健康檢查。
組件名 | 語言 | CAP | 主要功能 |
---|---|---|---|
Eureka | Java | AP | 注冊(cè)中心 |
Nacos | Java | AP/CP | 注冊(cè)中心、配置中心 |
Zookeeper | Java | CP | 注冊(cè)中心、配置中心、分布式隊(duì)列、分布式鎖、 分布式ID、集群管理等 |
Consul | Go | CP | 注冊(cè)中心、配置中心 |
搭建服務(wù)
準(zhǔn)備
名稱 | 版本 |
---|---|
Spring-Boot | 2.7.8 |
Spring Cloud | 2021.0.9 |
Spring Cloud Alibaba | 2021.0.5.0 |
JDK | 1.8 |
該系列的所有內(nèi)容相關(guān)環(huán)境,都已以上為主。
搭建
當(dāng)前項(xiàng)目分為兩個(gè)模塊,SpringCloudEurekaDemo2 和?SpringCloudServer。
SpringCloudEurekaDemo2 是父模塊,主要用于管理子模塊、管理依賴等等。
SpringCloudServer 就是注冊(cè)中心所在模塊,用于微服務(wù)中所有的服務(wù)注冊(cè)和管理。
搭建父模塊
- pom
<?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>
<modules>
<module>SpringCloudServer</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example.eureka</groupId>
<artifactId>SpringCloudEurekaDemo2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringCloudEurekaDemo2</name>
<description>SpringCloudEurekaDemo2</description>
<packaging>pom</packaging>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.9</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
</properties>
<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>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
搭建Server模塊
- pom
<?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">
<parent>
<artifactId>SpringCloudEurekaDemo2</artifactId>
<groupId>com.example.eureka</groupId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>SpringCloudServer</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
</dependencies>
</project>
- 啟動(dòng)類?
@EnableEurekaServer
@SpringBootApplication
public class ServerBootStrap {
public static void main(String[] args) {
SpringApplication.run(ServerBootStrap.class);
}
}
?說明:?@EnableEurekaServer 表示當(dāng)前服務(wù)是一個(gè) Eureka 注冊(cè)中心
- application.yml
spring:
application:
name: cloud-server
---
server:
port: 8081
spring:
config:
activate:
on-profile: serverA
eureka:
server:
#注冊(cè)中心多久檢查一次失效的實(shí)例
eviction-interval-timer-in-ms: 60
#自我保護(hù)
enable-self-preservation: true
instance:
hostname: SpringCloudServerA
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}:@project.version@
client:
service-url:
defaultZone: http://SpringCloudServerB:8082/eureka
register-with-eureka: true
fetch-registry: true
---
server:
port: 8082
spring:
config:
activate:
on-profile: serverB
eureka:
server:
#注冊(cè)中心多久檢查一次失效的實(shí)例
eviction-interval-timer-in-ms: 60
#自我保護(hù)
enable-self-preservation: true
instance:
hostname: SpringCloudServerB
prefer-ip-address: true
instance-id: ${spring.cloud.client.ip-address}:${server.port}:@project.version@
client:
service-url:
defaultZone: http://SpringCloudServerA:8081/eureka
register-with-eureka: true
fetch-registry: true
說明:1.spring.config.activate.on-profile:表示當(dāng)前環(huán)境名,這里采用不同的端口構(gòu)建偽集群
? ? ? ? ? ?2.eureka.server.eviction-interval-timer-in-ms:表示當(dāng)前注冊(cè)中心間隔多久檢查實(shí)例狀態(tài),單位秒,默認(rèn) 60秒
? ? ? ? ? ?3.eureka.server.enable-self-preservation:表示是否開啟自我保護(hù)(避免網(wǎng)絡(luò)故障導(dǎo)致服務(wù)不可用),默認(rèn) true
? ? ? ? ? ?4.eureka.instance.prefer-ip-address:表示猜測(cè)顯示主機(jī)名時(shí)是IP優(yōu)先,默認(rèn) false
? ? ? ? ? ?5.eureka.instance.instance-id:表示注冊(cè)在注冊(cè)中心Eureka上的唯一實(shí)例ID,默認(rèn) 主機(jī)名:應(yīng)用名:端口
? ? ? ? ? ?6.eureka.client.service-url.defaultZone:默認(rèn)注冊(cè)地址,默認(rèn)值 http://localhost:8761/eureka/
? ? ? ? ? ?7.eureka.client.register-with-eureka:表示是否注冊(cè)自己為服務(wù),默認(rèn) true
? ? ? ? ? ?8.eureka.client.fetch-registry:表示是否從注冊(cè)中心Eureka拉取服務(wù),默認(rèn) true
注意:1.這里?SpringCloudServerA 和?SpringCloudServerB 能生效的前提是已在C:\Windows\System32\drivers\etc\hosts?中配置映射
127.0.0.1 SpringCloudServerA
127.0.0.1 SpringCloudServerB
啟動(dòng)服務(wù)
以上配置完后,繼續(xù)。
1. 分別在 IDEA 中增加配置文件參數(shù) serverA 和 serverB(不是IDEA的自己加vm參數(shù))啟動(dòng)服務(wù)
2. 注冊(cè)中心有多個(gè)節(jié)點(diǎn)的,節(jié)點(diǎn)沒全部啟動(dòng)完,報(bào)錯(cuò)是正常的(相互注冊(cè)情況下),全部啟動(dòng)后再觀察日志
測(cè)試
服務(wù)啟動(dòng)好了,Eureka 自帶 DashBoard 可查看相應(yīng)的信息。
1. 瀏覽器訪問?http://localhost:8081/ 或者 http://localhost:8082/
其他
1. Eureka 全部節(jié)點(diǎn)都是平等的,不存在主從區(qū)分文章來源:http://www.zghlxwxcb.cn/news/detail-850468.html
2. Eureka 自我保護(hù)機(jī)制是非常有必要的,如果在15分鐘內(nèi)超過85%的客戶端節(jié)點(diǎn)都沒有正常的心跳,那么Eureka就認(rèn)為客戶端與注冊(cè)中間出現(xiàn)了網(wǎng)絡(luò)故障,Eureka Server自動(dòng)進(jìn)入我保護(hù)機(jī)制。進(jìn)入自我保護(hù)的注冊(cè)中心不會(huì)主動(dòng)剔除服務(wù),保證當(dāng)前注冊(cè)中心仍然可用,并且能進(jìn)行正常的注冊(cè)和調(diào)用,但不會(huì)主動(dòng)同步服務(wù)列表直到網(wǎng)絡(luò)正常。文章來源地址http://www.zghlxwxcb.cn/news/detail-850468.html
到了這里,關(guān)于Spring Cloud系列(二):Eureka Server應(yīng)用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!