javascript
微服务系列-基于Spring Cloud Eureka进行服务的注册与消费
公眾號「架構成長指南」,專注于生產實踐、云原生、分布式系統、大數據技術分享。
在之前的幾個教程中,我們學了:
使用 RestTemplate 的 Spring Boot 微服務通信示例
使用 WebClient 的 Spring Boot 微服務通信示例
使用 Spring Cloud Open Feign 的 Spring Boot 微服務通信示例
在本教程中,我們將學習如何在Spring boot微服務項目中使用Spring Cloud Eureka進行服務注冊與消費
服務注冊和發現概述
在微服務項目中,我們一般會對一個項目,以業務的維度拆分至多個服務,比如用戶服務、賬務服務、訂單服務、倉儲服務等,這些服務在生產環境部署,
至少是2個服務實例,如果業務量大幾十個都是有可能的。
試想這樣一種場景
訂單服務實例部署了4個,倉庫服務部署了5個,倉庫服務要調用訂單服務,如果沒有注冊中心,他會怎么做,那只有把對應的ip和端口寫死在代碼中,如果新增了一個訂單服務怎么辦?或者下線了訂單服務怎么辦?
另外,在云環境中,服務實例隨時都有可能啟動和關閉,隨之IP也會發生變化,沒法把IP寫死在代碼中。
基于以上問題就有了服務注冊中心Eureka
Eureka能實現服務自動的注冊和發現,在每次服務調用的時候根據服務名稱會獲取到目標服務的IP和端口,在進行調用。
如果服務下線或者上線,對應的服務的地址信息也會進行更新,這樣就保證了,隨時可以調用到有效的服務。
同時為了提高性能,這個服務地址信息會在每個服務本地緩存一份地址信息表,定時更新,這樣每次請求服務時,不用每次去Eureka查詢來降低服務調用耗時。
在本教程中,我們將學習如何使用SpringCloud Eureka進行服務注冊和發現,并通過OpenFeign進行服務的調用。
我們將做什么
我們部署一個Eureka Server,并將我們的微服務(部門服務和用戶服務)作為 Eureka 客戶端,注冊到Eureka Server,同時使用用戶服務調用根據部門服務的Service ID 來調用部門服務相關接口。
創建Eureka Server
1. 在 IntelliJ IDEA 中創建并設置 Spring boot 項目
讓我們使用 springinitializr創建一個 Spring boot 項目。
請參閱下面的屏幕截圖,在使用 springinitializr創建 Spring Boot 應用程序時輸入詳細信息 :
單擊生成按鈕以 zip 文件形式下載 Spring boot 項目。解壓zip文件并在IntelliJ IDEA中導入Spring boot項目。
這是 pom.xml 文件供參考:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.17</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>io.wz</groupId>
<artifactId>eureka-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>eureka-server</name>
<description>eureka-server</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.8</spring-cloud.version>
</properties>
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</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>
<image>
<builder>paketobuildpacks/builder-jammy-base:latest</builder>
</image>
</configuration>
</plugin>
</plugins>
</build>
</project>
2.添加@EnableEurekaServer注解
我們需要添加@EnableEurekaServer注解,使我們應用程序成為服務注冊中心。
package io.wz.eurekaserver;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
3. 禁用Eureka Server作為Eureka Client
默認情況下,每個Eureka Server 也是一個Eureka客戶端。由于我們只想讓他做好服務注冊中心,不想讓他做客戶端,因此我們將通過在application.properties文件中配置以下屬性來禁用此客戶端行為。
spring.application.name=Eureka Server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
4.啟動Eureka服務器
Eureka Server 提供了 UI,我們可以在其中看到有關注冊服務的所有詳細信息。
現在運行EurekaServerApplication并訪問 http://localhost:8761,會顯示以下界面
將Department-Service注冊至Eureka Server
請參閱本教程創建 部門服務 和 用戶服務 微服務: 使用 Spring Cloud Open Feign 的 Spring Boot 微服務通信示例
讓我們將這個部門服務 作為 Eureka 客戶端并向 Eureka 服務器注冊。
將 Eureka client pom添加到部門服務中:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
另外,添加 Spring Cloud 依賴項:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
添加版本屬性:
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>2021.0.4</spring-cloud.version>
</properties>
在application.properties中配置eureka.client.service-url.defaultZone 屬性 即可自動注冊到 Eureka Server。
spring.application.name=DEPARTMENT-SERVICE
eureka.instance.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
運行部門服務Eureka客戶端
完成此配置后,啟動Department-service并訪問 http://localhost:8761。
看到部門服務已使用 SERVICE ID 注冊為 DEPARTMENT-SERVICE,注意到狀態為 UP(1),這意味著服務已啟動并正在運行,并且部門服務的一個實例正在運行。
將User-Service微服務注冊為Eureka客戶端
添加以下依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在application.properties中配置eureka.client.service-url.defaultZone 屬性 即可自動注冊到 Eureka Server。
spring.application.name=USER-SERVICE
eureka.instance.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
當服務注冊到 Eureka Server 時,它會在一定的時間間隔內不斷發送心跳。如果 Eureka 服務器沒有收到來自任何服務實例的心跳,它將假定該服務實例已關閉并將其從池中取出
修改user-service的ApiClient
在上一節中中,我們使用APIClient完成了進行服務調用,但是是寫了部門服務的url
@FeignClient(value = "DEPARTMENT-SERVICE", url = "http://localhost:8080")
這次我們修改如下,去除url屬性
@FeignClient(value = "DEPARTMENT-SERVICE")
完整api如下
package io.wz.userservice.service;
import io.wz.userservice.dto.DepartmentDto;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
@FeignClient(value = "DEPARTMENT-SERVICE")
public interface APIClient {
@GetMapping(value = "/api/departments/{id}")
DepartmentDto getDepartmentById(@PathVariable("id") String departmentId);
}
運行用戶服務Eureka客戶端
以上配置后,啟動 user-service 并訪問http://localhost:8761。看到user-service已使用 SERVICE ID 注冊為USER-SERVICE。
您還可以注意到狀態為 UP(1),這意味著服務已啟動并正在運行,并且用戶服務的一個實例正在運行。
測試
增加測試日志
在部門服務的DepartmentServiceImpl的getDepartmentById方法增加調試日志,如果調用到此接口,會打印getDepartment By Id
@Override
public Department getDepartmentById(Long departmentId) {
log.info("getDepartment By Id:{} ",departmentId);
return departmentRepository.findById(departmentId).get();
}
啟動2個Department-Service
1. 啟動一個8082的部門服務
在idea中復制DepartmentServiceApplication配置,同時在啟動參數指定應用端口 -Dserver.port=8082
2. 啟動默認的部門服務,端口為8080
以上2個部門服務啟動完成,會在eureka看到2個部門服務
點擊獲取用戶 REST API進行測試
多點擊幾次,會看到2個部門服務控制臺都會打印,getDepartment By Id:1,這里使用的是Spring Cloud LoadBalancer提供的輪訓算法
結論
在本教程中,我們學習了如何在 Spring boot 微服務項目中使用Eureka來進行服務的注冊與發現,同時基于Feign進行服務的調用,但是這里還有遺留問題,如
- 啟動服務以后需要多久才會被消費方查詢到地址?
- 如果要做服務更新,如何讓消費無感知,感受不到服務再重啟?
- 如何讓調用方知道調用的是提供方多個實例中具體哪一個服務實例?
以上問題后續文章解答,請關注此公眾號。
源碼下載:
github
gitee
總結
以上是生活随笔為你收集整理的微服务系列-基于Spring Cloud Eureka进行服务的注册与消费的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 搭建Samba服务器笔记全套
- 下一篇: 文心一言 VS 讯飞星火 VS chat