公眾號(hào)「架構(gòu)成長指南」,專注于生產(chǎn)實(shí)踐、云原生、分布式系統(tǒng)、大數(shù)據(jù)技術(shù)分享。
在之前的幾個(gè)教程中,我們學(xué)了:
使用 RestTemplate 的 Spring Boot 微服務(wù)通信示例
使用 WebClient 的 Spring Boot 微服務(wù)通信示例
使用 Spring Cloud Open Feign 的 Spring Boot 微服務(wù)通信示例
在本教程中,我們將學(xué)習(xí)如何在Spring boot微服務(wù)項(xiàng)目中使用Spring Cloud Eureka
進(jìn)行服務(wù)注冊(cè)與消費(fèi)
服務(wù)注冊(cè)和發(fā)現(xiàn)概述
在微服務(wù)項(xiàng)目中,我們一般會(huì)對(duì)一個(gè)項(xiàng)目,以業(yè)務(wù)的維度拆分至多個(gè)服務(wù),比如用戶服務(wù)、賬務(wù)服務(wù)、訂單服務(wù)、倉儲(chǔ)服務(wù)等,這些服務(wù)在生產(chǎn)環(huán)境部署,
至少是2個(gè)服務(wù)實(shí)例,如果業(yè)務(wù)量大幾十個(gè)都是有可能的。
試想這樣一種場景
訂單服務(wù)實(shí)例部署了4個(gè),倉庫服務(wù)部署了5個(gè),倉庫服務(wù)要調(diào)用訂單服務(wù),如果沒有注冊(cè)中心,他會(huì)怎么做,那只有把對(duì)應(yīng)的ip和端口寫死在代碼中,如果新增了一個(gè)訂單服務(wù)怎么辦?或者下線了訂單服務(wù)怎么辦?
另外,在云環(huán)境中,服務(wù)實(shí)例隨時(shí)都有可能啟動(dòng)和關(guān)閉,隨之IP也會(huì)發(fā)生變化,沒法把IP寫死在代碼中。
基于以上問題就有了服務(wù)注冊(cè)中心Eureka
Eureka
能實(shí)現(xiàn)服務(wù)自動(dòng)的注冊(cè)和發(fā)現(xiàn),在每次服務(wù)調(diào)用的時(shí)候根據(jù)服務(wù)名稱會(huì)獲取到目標(biāo)服務(wù)的IP和端口,在進(jìn)行調(diào)用。
如果服務(wù)下線或者上線,對(duì)應(yīng)的服務(wù)的地址信息也會(huì)進(jìn)行更新,這樣就保證了,隨時(shí)可以調(diào)用到有效的服務(wù)。
同時(shí)為了提高性能,這個(gè)服務(wù)地址信息會(huì)在每個(gè)服務(wù)本地緩存一份地址信息表,定時(shí)更新,這樣每次請(qǐng)求服務(wù)時(shí),不用每次去Eureka
查詢來降低服務(wù)調(diào)用耗時(shí)。
在本教程中,我們將學(xué)習(xí)如何使用SpringCloud Eureka進(jìn)行服務(wù)注冊(cè)和發(fā)現(xiàn),并通過OpenFeign
進(jìn)行服務(wù)的調(diào)用。
我們將做什么
我們部署一個(gè)Eureka Server
,并將我們的微服務(wù)(部門服務(wù)和用戶服務(wù))作為 Eureka 客戶端,注冊(cè)到Eureka Server
,同時(shí)使用用戶服務(wù)調(diào)用根據(jù)部門服務(wù)的Service ID
來調(diào)用部門服務(wù)相關(guān)接口。
創(chuàng)建Eureka Server
1. 在 IntelliJ IDEA 中創(chuàng)建并設(shè)置 Spring boot 項(xiàng)目
讓我們使用 springinitializr創(chuàng)建一個(gè) Spring boot 項(xiàng)目。
請(qǐng)參閱下面的屏幕截圖,在使用 springinitializr創(chuàng)建 Spring Boot 應(yīng)用程序時(shí)輸入詳細(xì)信息 :
單擊生成
按鈕以 zip 文件形式下載 Spring boot 項(xiàng)目。解壓zip文件并在IntelliJ IDEA中導(dǎo)入Spring boot項(xiàng)目。
這是 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
注解,使我們應(yīng)用程序成為服務(wù)注冊(cè)中心。
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
默認(rèn)情況下,每個(gè)Eureka Server
也是一個(gè)Eureka
客戶端。由于我們只想讓他做好服務(wù)注冊(cè)中心,不想讓他做客戶端,因此我們將通過在application.properties
文件中配置以下屬性來禁用此客戶端行為。
spring.application.name=Eureka Server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
4.啟動(dòng)Eureka服務(wù)器
Eureka Server 提供了 UI,我們可以在其中看到有關(guān)注冊(cè)服務(wù)的所有詳細(xì)信息。
現(xiàn)在運(yùn)行EurekaServerApplication
并訪問 http://localhost:8761
,會(huì)顯示以下界面
將Department-Service注冊(cè)至Eureka Server
請(qǐng)參閱本教程創(chuàng)建 部門服務(wù) 和 用戶服務(wù) 微服務(wù): 使用 Spring Cloud Open Feign 的 Spring Boot 微服務(wù)通信示例
讓我們將這個(gè)部門服務(wù) 作為 Eureka 客戶端并向 Eureka 服務(wù)器注冊(cè)。
將 Eureka client pom添加到部門服務(wù)中:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
另外,添加 Spring Cloud 依賴項(xiàng):
<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
屬性 即可自動(dòng)注冊(cè)到 Eureka Server。
spring.application.name=DEPARTMENT-SERVICE
eureka.instance.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
運(yùn)行部門服務(wù)Eureka客戶端
完成此配置后,啟動(dòng)Department-service并訪問 http://localhost:8761
。
看到部門服務(wù)已使用 SERVICE ID 注冊(cè)為 DEPARTMENT-SERVICE
,注意到狀態(tài)為 UP(1)
,這意味著服務(wù)已啟動(dòng)并正在運(yùn)行,并且部門服務(wù)的一個(gè)實(shí)例正在運(yùn)行。
將User-Service微服務(wù)注冊(cè)為Eureka客戶端
添加以下依賴
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
在application.properties中配置eureka.client.service-url.defaultZone
屬性 即可自動(dòng)注冊(cè)到 Eureka Server。
spring.application.name=USER-SERVICE
eureka.instance.client.serviceUrl.defaultZone=http://localhost:8761/eureka/
當(dāng)服務(wù)注冊(cè)到 Eureka Server 時(shí),它會(huì)在一定的時(shí)間間隔內(nèi)不斷發(fā)送心跳。如果 Eureka 服務(wù)器沒有收到來自任何服務(wù)實(shí)例的心跳,它將假定該服務(wù)實(shí)例已關(guān)閉并將其從池中取出
修改user-service的ApiClient
在上一節(jié)中中,我們使用APIClient
完成了進(jìn)行服務(wù)調(diào)用,但是是寫了部門服務(wù)的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);
}
運(yùn)行用戶服務(wù)Eureka客戶端
以上配置后,啟動(dòng) user-service 并訪問http://localhost:8761
??吹?code>user-service已使用 SERVICE ID 注冊(cè)為USER-SERVICE
。
您還可以注意到狀態(tài)為 UP(1),這意味著服務(wù)已啟動(dòng)并正在運(yùn)行,并且用戶服務(wù)的一個(gè)實(shí)例正在運(yùn)行。
測試
增加測試日志
在部門服務(wù)的DepartmentServiceImpl
的getDepartmentById
方法增加調(diào)試日志,如果調(diào)用到此接口,會(huì)打印getDepartment By Id
@Override
public Department getDepartmentById(Long departmentId) {
log.info("getDepartment By Id:{} ",departmentId);
return departmentRepository.findById(departmentId).get();
}
啟動(dòng)2個(gè)Department-Service
1. 啟動(dòng)一個(gè)8082的部門服務(wù)
在idea中復(fù)制DepartmentServiceApplication
配置,同時(shí)在啟動(dòng)參數(shù)指定應(yīng)用端口 -Dserver.port=8082
2. 啟動(dòng)默認(rèn)的部門服務(wù),端口為8080
以上2個(gè)部門服務(wù)啟動(dòng)完成,會(huì)在eureka看到2個(gè)部門服務(wù)
點(diǎn)擊獲取用戶 REST API進(jìn)行測試
多點(diǎn)擊幾次,會(huì)看到2個(gè)部門服務(wù)控制臺(tái)都會(huì)打印,getDepartment By Id:1
,這里使用的是Spring Cloud LoadBalancer
提供的輪訓(xùn)算法
結(jié)論
在本教程中,我們學(xué)習(xí)了如何在 Spring boot 微服務(wù)項(xiàng)目中使用Eureka來進(jìn)行服務(wù)的注冊(cè)與發(fā)現(xiàn),同時(shí)基于Feign進(jìn)行服務(wù)的調(diào)用,但是這里還有遺留問題,如
- 啟動(dòng)服務(wù)以后需要多久才會(huì)被消費(fèi)方查詢到地址?
- 如果要做服務(wù)更新,如何讓消費(fèi)無感知,感受不到服務(wù)再重啟?
- 如何讓調(diào)用方知道調(diào)用的是提供方多個(gè)實(shí)例中具體哪一個(gè)服務(wù)實(shí)例?
以上問題后續(xù)文章解答,請(qǐng)關(guān)注此公眾號(hào)。文章來源:http://www.zghlxwxcb.cn/news/detail-746392.html
源碼下載:
github
gitee文章來源地址http://www.zghlxwxcb.cn/news/detail-746392.html
到了這里,關(guān)于微服務(wù)系列-基于Spring Cloud Eureka進(jìn)行服務(wù)的注冊(cè)與消費(fèi)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!