微服务系列-基于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进行服务的调用,但是这里还有遗留问题,如
- 启动服务以后需要多久才会被消费方查询到地址?
- 如果要做服务更新,如何让消费无感知,感受不到服务再重启?
- 如何让调用方知道调用的是提供方多个实例中具体哪一个服务实例?
以上问题后续文章解答,请关注此公众号。
微服务系列-基于Spring Cloud Eureka进行服务的注册与消费的更多相关文章
- Spring Cloud微服务笔记(三)服务治理:Spring Cloud Eureka快速入门
服务治理:Spring Cloud Eureka 一.服务治理 服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与发现. 1.服务注册: 在服务治理框架中,通常会构 ...
- .NET Core微服务之基于Steeltoe使用Eureka实现服务注册与发现
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
- Spring Cloud Eureka Server高可用注册服务中心的配置
前言 Eureka 作为一个云端负载均衡,本身是一个基于REST的服务,在 Spring Cloud 中用于发现和注册服务. 那么当成千上万个微服务注册到Eureka Server中的时候,Eurek ...
- Spring Cloud(二):Spring Cloud Eureka Server高可用注册服务中心的配置
前言 Eureka 作为一个云端负载均衡,本身是一个基于REST的服务,在 Spring Cloud 中用于发现和注册服务. 那么当成千上万个微服务注册到Eureka Server中的时候,Eurek ...
- 微服务架构之spring cloud eureka
Spring Cloud Eureka是spring cloud的核心组件,负责服务治理功能,起到中心枢纽作用,其它组件都依赖eureka来获取服务,然后再根据项目需求实现自己的业务,eureka在整 ...
- 第三章 服务治理:Spring Cloud Eureka
Spring Cloud Eureka是Spring Cloud Netflix 微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能.Spri ...
- 第三章 服务治理: Spring Cloud Eureka
Spring Cloud Eureka是 Spring Cloud Netflix微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能 服务治理 ...
- 服务治理:Spring Cloud Eureka
Spring Cloud Eureka主要负责完成微服务架构中服务治理功能. 服务治理是微服务架构中最为核心和基础模块,主要用来实现各个微服务实例的自动注册和发现. 服务注册 微服务实例启动后向注册中 ...
- Spring Cloud Eureka Server使用(注册中心)
一.Spring Cloud Eureka 基于Netflix Eureka做了二次封装 由两个组件组成 Eureka Server 注册中心, 供服务注册的服务器 Eureka Client 服务注 ...
- Spring Cloud Eureka 实现服务注册与发现
微服务 是一种架构模式,跟具体的语言实现无关,微服务架构将业务逻辑分散到了各个服务当中,服务间通过网络层进行通信共同协作:这样一个应用就可以划分为多个服务单独来维护发布.构建一个可靠微服务系统是需要具 ...
随机推荐
- SPI总线学习笔记
SPI是串行外设接口(Serial Peripheral Interface)的缩写,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为PCB的布局上节省空 ...
- 2021-7-11 Vue的计算属性和侦听器
计算属性是为了让页面显示更加简洁,基于data数据进行处理的方法,以下为实例 <!DOCTYPE html> <html> <head> <title> ...
- test20230304考试总结(2023春 · 字符串)
前言 赛时得分明细: A B C D Total Rank 100 100 0 70 270 2 C题如此说道:字符串没有学好的报应!! A. P4391 [BOI2009]Radio Transmi ...
- Python类与面向对象
Python类与面向对象 一.面向对象 1.1 面向对象概述 面向对象与面向过程? 面向过程编程的基本思想是:分析解决问题的步骤,使用函数实现每步对应的功能,按照步骤的先后顺序依次调用函数.面向过程只 ...
- 标题:在Godot中使用Node2D创建自定义的Label
在Godot游戏引擎中,我们经常需要在游戏中显示文本信息.通常,我们可以使用Label节点来实现这一点.但是,在某些情况下,你可能希望更灵活地控制文本的显示和样式.在本篇博客中,我们将学习如何通过使用 ...
- Hugging News #0821: Hugging Face 完成 2.35 亿美元 D 轮融资
每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...
- Vue【原创】基于elementui的【分组多选下拉框group-select】
效果图: 如图分为多选模式和单选模式. group-select: 1 <template> 2 <div> 3 <el-select 4 v-model="i ...
- RK3568开发笔记(六):开发板烧写ubuntu固件(支持mipi屏镜像+支持hdmi屏镜像)
前言 编译了uboot,kernel,buildroot后,可以单独输入固件,也可以整体打包成rootfs进行一次性输入,rootfs直接更新升级这个方式目前也是常用的. 烧写器软件:RKDe ...
- Ds100p -「数据结构百题」41~50
41.P3590 [POI2015]TRZ 给定一个长度为n的仅包含'B'.'C'.'S'三种字符的字符串,请找到最长的一段连续子串,使得这一段要么只有一种字符,要么有多种字符,但是没有任意两种字符出 ...
- SpringCloud-ZipKin搭建保姆级教程
服务链路追踪 一.服务追踪说明 微服务架构是通过业务来划分服务的,使⽤REST调⽤.对外暴露的⼀个接⼝,可能需要 很多个服务协同才能完成这个接⼝功能,如果链路上任何⼀个服务出现问题或者⽹络超 时,都会 ...