Eureka服務註冊與發現
- Eureka基礎知識
- 服務治理
- 服務註冊
- Eureka兩組建件
註冊中心-Eureka
介紹
又稱服務中心,管理各種服務功能包括服務的註冊、發現、熔斷、負載、降級等。
==什麼是註冊與發現==
- 在服務註冊與發現中,有一個註冊中心。當服務器啟動的時候,會把當前自己服務器的信息,比如 服務地址通訊地址等以別名等方式註冊到註冊中心上,另一方(消費者),以該別名的方式去註冊中心上獲取到實際的服務通訊地址,然後在實現本地RPC調用RPC遠程調用框架核心設計思想:自阿雨註冊中心,因為使用註冊中心管理每個服務與服務之間的一個依賴關係(==服務治理概念==)。在任何rpc遠程框架中,都會有一個註冊中心(存放接口地址)
Spring Cloud 封裝了 Netflix 公司開發的 一個基於 REST 服務的,服務註冊與發現的組件。Eureka 採用了 C-S 的設計架構。Eureka Server 作為服務註冊功能的服務器,它是服務註冊中心。而系統中的其他微服務,使用 Eureka 的客戶端連接到 Eureka Server,並維持心跳連接。這樣系統的維護人員就可以通過 Eureka Server 來監控系統中各個微服務是否正常運行。Spring Cloud 的一些其他模塊(比如Zuul)就可以通過 Eureka Server 來發現系統中的其他微服務,並執行相關的邏輯。
它主要包括兩個組件:Eureka Server 和 Eureka Client
- Eureka Server:提供服務註冊和發現的能力,各個微服務節點通過配置啟動後,會在Eureka Server 中進行註冊,這樣EurekaServer中服務註冊表中將會存儲所有可用節點的信息,在ui界面可以看到(通常就是微服務中的註冊中心)
- Eureka Client:一個Java客戶端,用於簡化與 Eureka Server 的交互,客戶端同時也具備一個內置的、使用輪詢(round-robin)負載算法的負載均衡器。在啟動後,將會向Eureka Server發送心跳(默認週期30秒),如果Eureka Server在多個心跳週期內沒有接收到某個節點的心跳,Eureka Server將會從服務註冊表中把這個節點移除(默認90秒)(通常就是微服務中的客戶端和服務端)
單機版的eureka
1.新建Maven工程 POM 添加 eureka-server
<!-- eureka-server -->
<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>
<!--監控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 一般通用配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.編寫yml 文件
#微服務建議一定要寫服務端口號和微服務名稱
server:
#端口號
port: 7001
eureka:
instance:
hostname: localhost
client:
#false表示不向註冊中心註冊自己(想註冊也可以,不過沒必要)
register-with-eureka: false
#false表示自己端就是註冊中心,職責就是維護服務實例,並不需要去檢索服務
fetch-registry: false
service-url:
#設置與eurekaServer交互的地址查詢服務和註冊服務都需要依賴這個地址
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
3.編寫啟動類
package com.yxl.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer //表示此項目是eureka的服務註冊中心
@SpringBootApplication
public class EurekaMain7001 {
public static void main(String[] args) {
SpringApplication.run(EurekaMain7001.class, args);
}
}
4.測試
啟動項目,在瀏覽器輸入http://localhost:7001/
將EurekaClient端 註冊進EurekaServer成為服務提供者provider
1.新建Maven工程 引入POM
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2。新建yml 添加
eureka:
client:
#true表示向註冊中心註冊自己,默認為true
register-with-eureka: true
#是否從EurekaServer抓取已有的註冊信息,默認為true。單節點無所謂,集群必須設置為true才能配合ribbon使用負載均衡
fetch-registry: true
service-url:
defaultZone: http://localhost:7001/eureka
3.在主配置類上加上@EnableEurekaClient註解,表示這個項目是eureka的客戶端。
@EnableAsync
@SpringBootApplication
@EnableEurekaClient
public class PaymentMain8001 {
public static void main(String[] args) {
SpringApplication.run(PaymentMain8001.class, args);
}
}
4.啟動項目,然後刷新頁面,成功註冊進註冊中心。
- Springcloud 倉庫地址 : github鏈接.
- 個人博客: http://blog.yanxiaolong.cn/.