SpringCloud_Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)基礎(chǔ)及構(gòu)建步驟
代碼鏈接
https://github.com/lidonglin-bit/cloud
一、Eureka基礎(chǔ)知識(shí)
1.什么是服務(wù)治理
SpringCloud封裝了Netflix公司開(kāi)發(fā)的Eureka模塊來(lái)實(shí)現(xiàn)服務(wù)治理。
在傳統(tǒng)的RPC遠(yuǎn)程調(diào)用框架中,管理每個(gè)服務(wù)與服務(wù)之間依賴關(guān)系比較復(fù)雜、所以需要進(jìn)行服務(wù)治理,管理服務(wù)與服務(wù)之間依賴關(guān)聯(lián),以實(shí)現(xiàn)服務(wù)調(diào)用,負(fù)載均衡、容錯(cuò)等,實(shí)現(xiàn)服務(wù)發(fā)現(xiàn)與注冊(cè)。
2.什么是服務(wù)注冊(cè)
Eureka采用了CS的設(shè)計(jì)架構(gòu),Eureka Server作為服務(wù)注冊(cè)功能的服務(wù)器,它是服務(wù)注冊(cè)中心。
而系統(tǒng)中的其他微服務(wù),使用Eureka的客戶端連接到Eureka Server并維持心跳連接。這樣系統(tǒng)的維護(hù)人員可以通過(guò)Eureka Server來(lái)監(jiān)控系統(tǒng)中各個(gè)微服務(wù)是否正常運(yùn)行。
在服務(wù)注冊(cè)與發(fā)現(xiàn)中,有一個(gè)注冊(cè)中心。當(dāng)服務(wù)器啟動(dòng)的時(shí)候,會(huì)把當(dāng)前自己服務(wù)器的信息,比如:服務(wù)通訊地址等以別名方式注冊(cè)到注冊(cè)中心上。
另一方(消費(fèi)者服務(wù)),以該別名的方式去注冊(cè)中心上獲取到實(shí)際的服務(wù)通訊地址,然后,再實(shí)現(xiàn)本地RPC遠(yuǎn)程調(diào)用。
RPC遠(yuǎn)程調(diào)用框架核心設(shè)計(jì)思想:在于注冊(cè)中心,因?yàn)槭褂米?cè)中心管理每個(gè)服務(wù)與服務(wù)之間的一個(gè)依賴關(guān)系(服務(wù)治理概念)。
在任何RPC遠(yuǎn)程框架中,都會(huì)有一個(gè)注冊(cè)中心(存放服務(wù)地址相關(guān)信息(接口地址))。

3.Eureka兩組件
Eureka Server提供服務(wù)注冊(cè)服務(wù)
各個(gè)微服務(wù)節(jié)點(diǎn)通過(guò)配置啟動(dòng)后,會(huì)在Eureka Server中進(jìn)行注冊(cè),這樣Eureka Server中的服務(wù)注冊(cè)表中將會(huì)存儲(chǔ)所有可用服務(wù)節(jié)點(diǎn)的信息,服務(wù)節(jié)點(diǎn)的信息可以在界面中直觀看到。
Eureka Client通過(guò)注冊(cè)中心進(jìn)行訪問(wèn)
是一個(gè)Java客戶端,用于簡(jiǎn)化Eureka Server的交互,客戶端同時(shí)也具備一個(gè)內(nèi)置的、使用輪詢(round-robin)負(fù)載算法的負(fù)載均衡器。在應(yīng)用啟動(dòng)后,將會(huì)在Eureka Server發(fā)送心跳(默認(rèn)周期30秒)。如果Eureka Server在多個(gè)心跳周期內(nèi)沒(méi)有收到某個(gè)節(jié)點(diǎn)的心跳,Eureka Server將會(huì)從服務(wù)注冊(cè)表中把這個(gè)服務(wù)節(jié)點(diǎn)移出(默認(rèn)90秒)
二、單機(jī)Eureka構(gòu)建步驟
整體結(jié)構(gòu)

父工程pom文件
<!-- 統(tǒng)一管理jar包版本 -->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!-- 子模塊繼承之后,提供作用:鎖定版本+子modlue不用寫(xiě)groupId和version -->
<dependencyManagement>
<dependencies>
<!--spring boot 2.2.2-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud Hoxton.SR1-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--spring cloud alibaba 2.1.0.RELEASE-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
</configuration>
</plugin>
</plugins>
</build>
1.IDEA生成eurekaServer端服務(wù)注冊(cè)中心
1.建Module:cloud-eureka-server7001
2.改POM
<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>
</project>
1.X和2.X的對(duì)比說(shuō)明
1.X版本 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> 2.X版本 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
3.寫(xiě)YML
server:
port: 7001
spring:
application:
name: cloud-eureka-server7001
eureka:
instance:
hostname: localhost
#因?yàn)榉?wù)端不需要注冊(cè),所有為false
client:
register-with-eureka: false
fetchRegistry: false
service-url:
defaultZone: http://localhost:7001/eureka
4.主啟動(dòng)
@SpringBootApplication
@EnableEurekaServer
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class,args);
}
}
5.測(cè)試
2.服務(wù)提供者
EurekaClient端cloud-provider-payment8001將注冊(cè)進(jìn)EurekaServer成為服務(wù)提供者provider
1.建Module:cloud-provider-payment8001
2.改POM
<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>
3.寫(xiě)YML
spring:
application:
name: cloud-provider-payment8001
server:
port: 8001
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
4.主啟動(dòng)
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class,args);
}
}
5.測(cè)試
先啟動(dòng)EurekaServer
3.服務(wù)消費(fèi)者
EurekaClient端cloud-consumer-order80將注冊(cè)進(jìn)EurekaServer成為服務(wù)消費(fèi)者consumer
1.建Module:cloud-consumer-order80
2.改POM
<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>
3.寫(xiě)YML
spring:
application:
name: cloud-consumer-order80
server:
port: 80
eureka:
client:
register-with-eureka: true
fetchRegistry: true
service-url:
defaultZone: http://localhost:7001/eureka
4.主啟動(dòng)
@SpringBootApplication
@EnableEurekaClient
public class OrderMain80 {
public static void main(String[] args) {
SpringApplication.run(OrderMain80.class,args);
}
}
5.測(cè)試
1)先要啟動(dòng)EurekaServer,7001服務(wù)
2)再要啟動(dòng)服務(wù)提供者8001服務(wù)和服務(wù)消費(fèi)者80服務(wù)
3)eureka服務(wù)器

4)測(cè)試查詢:http://localhost/consumer/payment/get/31
5)測(cè)試添加:postman測(cè)試添加
6)測(cè)試8001服務(wù)和80服務(wù)效果一樣

到此這篇關(guān)于SpringCloud_Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)基礎(chǔ)及構(gòu)建步驟的文章就介紹到這了,更多相關(guān)SpringCloud_Eureka內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java filter中的chain.doFilter使用詳解
這篇文章主要介紹了Java filter中的chain.doFilter使用詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
SpringBoot?Security從入門到實(shí)戰(zhàn)示例教程
Spring?Security是一個(gè)功能強(qiáng)大且高度可定制的身份驗(yàn)證和訪問(wèn)控制框架,接下來(lái)通過(guò)本文給大家介紹SpringBoot?Security從入門到實(shí)戰(zhàn)示例教程,感興趣的朋友一起看看吧2022-05-05
ActiveMQ基于zookeeper的主從(levelDB Master/Slave)搭建
這篇文章主要介紹了ActiveMQ基于zookeeper的主從levelDB Master/Slave搭建,以及Spring-boot下的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
mybatis連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)雙表查詢
本文主要介紹了mybatis連接數(shù)據(jù)庫(kù)實(shí)現(xiàn)雙表查詢,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09
Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析
這篇文章主要介紹了Java Arrays.sort和Collections.sort排序?qū)崿F(xiàn)原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
java:無(wú)法訪問(wèn)org.springframework.boot.SpringApplication問(wèn)題
這篇文章主要介紹了java:無(wú)法訪問(wèn)org.springframework.boot.SpringApplication問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08

