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不用寫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.寫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.寫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.寫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動(dòng)態(tài)代理靜態(tài)代理實(shí)例分析
這篇文章主要介紹了Java動(dòng)態(tài)代理靜態(tài)代理實(shí)例分析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03spring中通過(guò)ApplicationContext getBean獲取注入對(duì)象的方法實(shí)例
今天小編就為大家分享一篇關(guān)于spring中通過(guò)ApplicationContext getBean獲取注入對(duì)象的方法實(shí)例,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03使用restTemplate.postForEntity()的問(wèn)題
這篇文章主要介紹了使用restTemplate.postForEntity()的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Java信號(hào)量Semaphore原理及代碼實(shí)例
這篇文章主要介紹了Java信號(hào)量Semaphore原理及代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10Java IO流之原理分類與節(jié)點(diǎn)流文件操作詳解
流(Stream)是指一連串的數(shù)據(jù)(字符或字節(jié)),是以先進(jìn)先出的方式發(fā)送信息的通道,數(shù)據(jù)源發(fā)送的數(shù)據(jù)經(jīng)過(guò)這個(gè)通道到達(dá)目的地,按流向區(qū)分為輸入流和輸出流2021-10-10