SpringCloud實(shí)現(xiàn)Eureka服務(wù)注冊與發(fā)現(xiàn)
GitHub地址:https://github.com/yudiandemingzi/spring-cloud-study
一、Eureka概述
1、Eureka特點(diǎn)
(1) Eureka是一個(gè)基于REST的服務(wù),用于定位服務(wù),以實(shí)現(xiàn)云端中間層服務(wù)發(fā)現(xiàn)和故障轉(zhuǎn)移。
(2) Eureka 主管服務(wù)注冊與發(fā)現(xiàn),在微服務(wù)中,以后了這兩者,只需要使用服務(wù)的標(biāo)識(shí)符(==就是那個(gè)在每個(gè)服務(wù)的yml文件中取得服務(wù)名稱==),
就可以訪問到服務(wù),不需要修改服務(wù)調(diào)用的配置文件。
(3) Eureka遵循AP原則(高可用,分區(qū)容錯(cuò)性),因?yàn)槭褂昧俗晕冶Wo(hù)機(jī)制所以保證了高可用。
2、Eureka兩大組件
兩大組件:Eureka Server(提供注冊服務(wù))、 Eureka Client(JAVA客戶端,負(fù)責(zé)發(fā)送心跳)
系統(tǒng)中的其他微服務(wù)使用Eureka客戶端連接到Eureka服務(wù)端維持心跳連接(即注冊)。SpringCloud的其他模塊可以通過Eureka Server 來發(fā)現(xiàn)系統(tǒng)中的微服務(wù)并加以調(diào)用
3、Eureka三大角色
Eureka Server:提供服務(wù)注冊和發(fā)現(xiàn)
Service Provider:服務(wù)提供方,將自身服務(wù)注冊到Eureka,從而使服務(wù)消費(fèi)方能夠找到
Service Consumer:服務(wù)消費(fèi)方,從Eureka獲取注冊服務(wù)列表,從而能夠消費(fèi)服務(wù)。
二、Eureka Server服務(wù)注冊中心
1、pom.xml
<!--注冊服務(wù)中心的jar要多個(gè)-server--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency>
2、application.yml
server: port: 7001 eureka: instance: hostname: localhost client: #聲明自己是個(gè)服務(wù)端 registerWithEureka: false #false表示不向注冊中心注冊自己 fetchRegistry: false #false表示自己就是注冊中心,職責(zé)是維護(hù)實(shí)例,不參加檢索 serviceUrl: #設(shè)置eureka server的交互地址,即對外暴露的地址 defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3、啟動(dòng)類
//注意:要在類前加@EnableEurekaServer標(biāo)注 @SpringBootApplication @EnableEurekaServer public class Eureka7001_APP { public static void main(String[] args) { SpringApplication.run(Eureka7001_APP.class,args); } }
運(yùn)行結(jié)果:輸入:http://localhost:7001/
三、Service Provider服務(wù)提供方
假設(shè)這個(gè)商品微服務(wù)。
1、pom.xml
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2、application.yml
server: port: 8001 #指定注冊中心地址 eureka: client: serviceUrl: defaultZone: http://localhost:7001/eureka/ #服務(wù)的名稱 spring: application: name: product-service
3、啟動(dòng)類
@SpringBootApplication public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); } }
4、啟動(dòng)后查看服務(wù)注冊中心
發(fā)現(xiàn)在服務(wù)注冊中心已經(jīng)注冊了一個(gè)服務(wù)
5、換端口號(hào)再啟動(dòng)一個(gè)
6、在看服務(wù)中心
這就是搭建了商品微服務(wù)集群。
四、Service Consumer服務(wù)消費(fèi)方
其實(shí)服務(wù)方和消費(fèi)在配置時(shí)候沒有任何區(qū)別,它們都屬于Eureka Client組件。只是涉及服務(wù)間的調(diào)用,所以就把被調(diào)方稱為提供方,調(diào)用方稱為消費(fèi)方。就好比訂單微服務(wù),
訂單服務(wù)肯定需要去調(diào)商品微服務(wù),所以這個(gè)訂單微服務(wù)對于商品來講可以理解服務(wù)提供方。一個(gè)微服務(wù)即可以是服務(wù)方也同時(shí)是提供方。
1、pom.xml
<!--這個(gè)對于每個(gè)不是注冊中心的微服務(wù)都要添加--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency>
2、application.yml
server: port: 9001 #指定注冊中心地址 eureka: client: serviceUrl: defaultZone: http://localhost:7001/eureka/ #服務(wù)的名稱 spring: application: name: order-service
3、啟動(dòng)類
@SpringBootApplication public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }
4、查看注冊中心
發(fā)現(xiàn)訂單微服務(wù)也成功注冊到注冊中心
至于訂單微服務(wù)如何調(diào)商品微服務(wù)呢,下一遍博客在寫咯。
以上就是SpringCloud實(shí)現(xiàn)Eureka服務(wù)注冊與發(fā)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于SpringCloud Eureka服務(wù)注冊與發(fā)現(xiàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
- SpringCloud?Eureka服務(wù)治理之服務(wù)注冊服務(wù)發(fā)現(xiàn)
- SpringCloud?Eureka服務(wù)注冊中心應(yīng)用入門詳解
- SpringCloud如何使用Eureka實(shí)現(xiàn)服務(wù)之間的傳遞數(shù)據(jù)
- SpringCloud服務(wù)注冊和發(fā)現(xiàn)組件Eureka
- 5分鐘搭建SpringCloud Eureka服務(wù)注冊中心的實(shí)現(xiàn)
- SpringCloud Eureka的使用教程
- Springcloud Eureka配置及集群代碼實(shí)例
- SpringCloud?Eureka應(yīng)用全面介紹
相關(guān)文章
Java快速批量移動(dòng)文件的實(shí)現(xiàn)方法
這篇文章主要介紹了Java快速批量移動(dòng)文件的實(shí)現(xiàn)方法,需要的朋友可以參考下2014-03-03JAVA Swing實(shí)現(xiàn)窗口添加課程信息過程解析
這篇文章主要介紹了JAVA Swing實(shí)現(xiàn)窗口添加課程信息過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Kotlin?標(biāo)準(zhǔn)函數(shù)和靜態(tài)方法示例詳解
這篇文章主要為大家介紹了Kotlin?標(biāo)準(zhǔn)函數(shù)和靜態(tài)方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Spring 實(shí)現(xiàn)給Bean屬性注入null值
這篇文章主要介紹了Spring 實(shí)現(xiàn)給Bean屬性注入null值的操作,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08