公众号「架构成长指南」,专注于生产实践、云原生、分布式系统、大数据技术分享。

在之前的几个教程中,我们学了:

使用 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),这意味着服务已启动并正在运行,并且用户服务的一个实例正在运行。

测试

增加测试日志

在部门服务的DepartmentServiceImplgetDepartmentById方法增加调试日志,如果调用到此接口,会打印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进行服务的注册与消费的更多相关文章

  1. Spring Cloud微服务笔记(三)服务治理:Spring Cloud Eureka快速入门

    服务治理:Spring Cloud Eureka 一.服务治理 服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册与发现. 1.服务注册: 在服务治理框架中,通常会构 ...

  2. .NET Core微服务之基于Steeltoe使用Eureka实现服务注册与发现

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 =>  Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...

  3. Spring Cloud Eureka Server高可用注册服务中心的配置

    前言 Eureka 作为一个云端负载均衡,本身是一个基于REST的服务,在 Spring Cloud 中用于发现和注册服务. 那么当成千上万个微服务注册到Eureka Server中的时候,Eurek ...

  4. Spring Cloud(二):Spring Cloud Eureka Server高可用注册服务中心的配置

    前言 Eureka 作为一个云端负载均衡,本身是一个基于REST的服务,在 Spring Cloud 中用于发现和注册服务. 那么当成千上万个微服务注册到Eureka Server中的时候,Eurek ...

  5. 微服务架构之spring cloud eureka

    Spring Cloud Eureka是spring cloud的核心组件,负责服务治理功能,起到中心枢纽作用,其它组件都依赖eureka来获取服务,然后再根据项目需求实现自己的业务,eureka在整 ...

  6. 第三章 服务治理:Spring Cloud Eureka

    Spring Cloud Eureka是Spring Cloud Netflix 微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能.Spri ...

  7. 第三章 服务治理: Spring Cloud Eureka

    Spring Cloud Eureka是 Spring Cloud Netflix微服务套件中的一部分,它基于Netflix Eureka做了二次封装,主要负责完成微服务架构中的服务治理功能 服务治理 ...

  8. 服务治理:Spring Cloud Eureka

    Spring Cloud Eureka主要负责完成微服务架构中服务治理功能. 服务治理是微服务架构中最为核心和基础模块,主要用来实现各个微服务实例的自动注册和发现. 服务注册 微服务实例启动后向注册中 ...

  9. Spring Cloud Eureka Server使用(注册中心)

    一.Spring Cloud Eureka 基于Netflix Eureka做了二次封装 由两个组件组成 Eureka Server 注册中心, 供服务注册的服务器 Eureka Client 服务注 ...

  10. Spring Cloud Eureka 实现服务注册与发现

    微服务 是一种架构模式,跟具体的语言实现无关,微服务架构将业务逻辑分散到了各个服务当中,服务间通过网络层进行通信共同协作:这样一个应用就可以划分为多个服务单独来维护发布.构建一个可靠微服务系统是需要具 ...

随机推荐

  1. Linux chroot的关联操作 mount --bind

    假设新的根文件系统已经挂载到 /mnt mount --bind /dev /mnt/dev/ mount --bind /sys /mnt/sys/ mount --bind /proc /mnt/ ...

  2. RAT蓝队自动化测试框架

    RAT蓝队自动化测试框架 介绍 RAT 是根据 MITRE ATT&CK 战术矩阵测试蓝队检测能力的脚本框架,由 python2.7 编写,共有 50 多种不同 ATT&CK 技术点和 ...

  3. python移动文件

    #移动文件(目录) shutil.move("oldpos","newpos") shutil.move("D:/知乎日报/latest/一张优惠券, ...

  4. Linux中对管道命令中的任意子命令进行返回码校验

    ~~ linux return code with pipeline~~ ~~ linux 管道命令中的返回码~~ BASH SHELL中,通常使用 $? 来获取上一条命令的返回码. Shell Sc ...

  5. 《深入理解Java虚拟机》读书笔记:HotSpot虚拟机对象探秘

    基于实用优先的原则,以常用的虚拟机HotSpot和常用的内存区域Java堆为例,深入探讨HotSpot虚拟机在Java堆中对象分配.布局和访问的全过程.以下是本节内容的脑图. HotSpot虚拟机对象 ...

  6. 超详细的mysql总结(基本概念、DDL、DML)

    开发中存在着各种数据,比如用户的个人信息.商品详情.购买记录,这些数据都要以一定的方式储存,如果以文本的形式储存,每一次获取都要读取文件,如果信息有修改则需要直接修改文本,大量的数据会需要保存大量的文 ...

  7. Prism报错

    Rules.Default..WithoutFastExpressionCompiler()报错 说没有找到容器 1.查看Prism.Wpf源码 获取DryIoc容器规则 2.证明项目中出现了另外一个 ...

  8. 玄 - 利用DLL通知回调函数注入shellcode(上)

    序 偶然看到某国外大佬发布新技术-"Threadless"进程注入技术,据说可过EDR(确实可),总结该技术原理 - 在远程目标进程中利用DLL通知回调机制执行shellcode, ...

  9. AI绘画:StableDiffusion制作AI赛博机车图保姆级教程

    本教程收集于:AIGC从入门到精通教程汇总 如果你具备Stable Diffusion的基础知识,那制作AI赛博机车图会更为顺畅.即便没有这个基础,只要严格按照教程的步骤执行,也能成功制作出来. 本教 ...

  10. Pytorch语法——torch.autograd.grad

    The torch.autograd.grad function is a part of PyTorch's automatic differentiation package and is use ...