SpringCloud_Eureka服務注冊與發(fā)現基礎及構建步驟
代碼鏈接
https://github.com/lidonglin-bit/cloud
一、Eureka基礎知識
1.什么是服務治理
SpringCloud封裝了Netflix公司開發(fā)的Eureka模塊來實現服務治理。
在傳統的RPC遠程調用框架中,管理每個服務與服務之間依賴關系比較復雜、所以需要進行服務治理,管理服務與服務之間依賴關聯,以實現服務調用,負載均衡、容錯等,實現服務發(fā)現與注冊。
2.什么是服務注冊
Eureka采用了CS的設計架構,Eureka Server作為服務注冊功能的服務器,它是服務注冊中心。
而系統中的其他微服務,使用Eureka的客戶端連接到Eureka Server并維持心跳連接。這樣系統的維護人員可以通過Eureka Server來監(jiān)控系統中各個微服務是否正常運行。
在服務注冊與發(fā)現中,有一個注冊中心。當服務器啟動的時候,會把當前自己服務器的信息,比如:服務通訊地址等以別名方式注冊到注冊中心上。
另一方(消費者服務),以該別名的方式去注冊中心上獲取到實際的服務通訊地址,然后,再實現本地RPC遠程調用。
RPC遠程調用框架核心設計思想:在于注冊中心,因為使用注冊中心管理每個服務與服務之間的一個依賴關系(服務治理概念)。
在任何RPC遠程框架中,都會有一個注冊中心(存放服務地址相關信息(接口地址))。
3.Eureka兩組件
Eureka Server提供服務注冊服務
各個微服務節(jié)點通過配置啟動后,會在Eureka Server中進行注冊,這樣Eureka Server中的服務注冊表中將會存儲所有可用服務節(jié)點的信息,服務節(jié)點的信息可以在界面中直觀看到。
Eureka Client通過注冊中心進行訪問
是一個Java客戶端,用于簡化Eureka Server的交互,客戶端同時也具備一個內置的、使用輪詢(round-robin)負載算法的負載均衡器。在應用啟動后,將會在Eureka Server發(fā)送心跳(默認周期30秒)。如果Eureka Server在多個心跳周期內沒有收到某個節(jié)點的心跳,Eureka Server將會從服務注冊表中把這個服務節(jié)點移出(默認90秒)
二、單機Eureka構建步驟
整體結構
父工程pom文件
<!-- 統一管理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端服務注冊中心
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的對比說明
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
#因為服務端不需要注冊,所有為false
client:
register-with-eureka: false
fetchRegistry: false
service-url:
defaultZone: http://localhost:7001/eureka
4.主啟動
@SpringBootApplication @EnableEurekaServer public class EurekaMain7001 { public static void main(String[] args) { SpringApplication.run(EurekaMain7001.class,args); } }
5.測試
2.服務提供者
EurekaClient端cloud-provider-payment8001將注冊進EurekaServer成為服務提供者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.主啟動
@SpringBootApplication @EnableEurekaClient public class PaymentMain8001 { public static void main(String[] args) { SpringApplication.run(PaymentMain8001.class,args); } }
5.測試
先啟動EurekaServer
3.服務消費者
EurekaClient端cloud-consumer-order80將注冊進EurekaServer成為服務消費者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.主啟動
@SpringBootApplication @EnableEurekaClient public class OrderMain80 { public static void main(String[] args) { SpringApplication.run(OrderMain80.class,args); } }
5.測試
1)先要啟動EurekaServer,7001服務
2)再要啟動服務提供者8001服務和服務消費者80服務
3)eureka服務器
4)測試查詢:http://localhost/consumer/payment/get/31
5)測試添加:postman測試添加
6)測試8001服務和80服務效果一樣
到此這篇關于SpringCloud_Eureka服務注冊與發(fā)現基礎及構建步驟的文章就介紹到這了,更多相關SpringCloud_Eureka內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
spring中通過ApplicationContext getBean獲取注入對象的方法實例
今天小編就為大家分享一篇關于spring中通過ApplicationContext getBean獲取注入對象的方法實例,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-03-03使用restTemplate.postForEntity()的問題
這篇文章主要介紹了使用restTemplate.postForEntity()的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09