Eureka Client 服务消费者(调用API接口)(使用OpenFeign)

一、简介

1. 本文介绍

本文将指导你如何创建一个Spring Boot应用程序作为Eureka Client服务消费者,该服务消费者会从Eureka Server获取服务提供者的列表,并通过该列表与具体的服务提供者进行通信。

在本文中,服务消费者使用 OpenFeign 调用服务提供者的API接口。

2. Eureka Client 介绍

在微服务架构中,服务发现是确保各微服务能够相互通信的关键组件之一。Netflix Eureka是一个广泛使用的开源服务发现工具,它允许服务提供者注册其位置,并让服务消费者发现这些提供者以进行通信。

  • Eureka Client 是一个Java客户端,用于简化与Eureka Server的交互。它可以自动注册自身到Eureka Server上,并定期发送心跳来更新其状态。
  • 服务提供者是实际提供业务逻辑的服务,它们会将自己注册到Eureka Server上。
  • 服务消费者是从Eureka Server获取服务提供者的列表,并通过该列表与具体的服务提供者进行通信。

3. 依赖版本

Spring Boot:3.4.0
Spring Boot Cloud:2024.0.0

二、创建 Eureka Client 服务消费者

1. 创建一个新的Spring Boot项目

可以通过Spring Initializr (https://start.spring.io/) 来快速创建一个新的Spring Boot项目。在创建时,请确保选择以下依赖项:

  • Spring Web:为了能够使用Spring MVC来构建Web应用程序。
  • Eureka Client:这个依赖项会引入所有必要的库来创建Eureka Client
  • OpenFeign:这个依赖项会引入OpenFeign客户端来进行服务间的通信。

2. 依赖

如果你手动创建了项目,你需要确保你的构建文件中包含了 Eureka Client 的依赖。

(1)Spring Cloud BOM

使用 spring-cloud-dependencies BOM来确保所有Spring Cloud相关依赖的版本一致性。

    <properties>
        <spring-cloud.version>2024.0.0</spring-cloud.version>
    </properties>

    <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>

(2)Eureka Client 依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

(3)OpenFeign 依赖

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

(4)POM完整示例

<?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>3.4.0</version>
        <relativePath/>
    </parent>

    <groupId>com.example</groupId>
    <artifactId>hello-eureka-client-consumer</artifactId>
    <version>1.0.0</version>
    <name>hello-eureka-client-consumer</name>
    <description>Eureka Client 服务消费者(调用API接口)(使用OpenFeign)</description>

    <properties>
        <java.version>21</java.version>
        <spring-cloud.version>2024.0.0</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </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>

    <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.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

3. 默认配置启动(不可用于生产)

Spring Cloud Netflix Eureka Client有默认的行为。如果没有指定Eureka服务器的位置,它会尝试连接到http://localhost:8761/eureka/。这是Eureka Server的默认地址和端口。

注意:在实际生产中,必须配置正确的 Eureka Server 地址,以及其他相关配置。

(1)启动日志


(2)Eureka Server管理界面

4. 配置Eureka Server的基本设置(application.yml)

spring:
  application:
    name: hello-eureka-client-consumer

server:
  port: 8082 # 应用程序的端口

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/ # Eureka Server的地址
    register-with-eureka: false # 是否注册自身到Eureka Server
    fetch-registry: true # 是否从Eureka Server获取注册表信息
  instance:
    prefer-ip-address: true # 使用IP地址而非主机名进行注册
    instance-id: ${
   spring.application.name}:${
   random.value} # 唯一实例ID

5. 启动成功

6. 无需注册服务

根据上诉配置,不会将自身注册到 Eureka Server, 只会拉取服务提供者的列表。

可以通过访问Eureka Server的管理界面(默认是http://localhost:8761)来查看效果:

三、启动日志分析

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.4.0)

2024-12-14T22:11:48.389+08:00  INFO 13732 --- [hello-eureka-client-consumer] [           main] c.c.HelloEurekaClientConsumerApplication : Starting HelloEurekaClientConsumerApplication using Java 21.0.1 with PID 13732 (E:\eureka\hello-eureka-client-consumer\target\classes started by SongGuanxun in E:\eureka\hello-eureka-client-consumer)
2024-12-14T22:11:48.396+08:00  INFO 13732 --- [hello-eureka-client-consumer] [           main] c.c.HelloEurekaClientConsumerApplication : No active profile set, falling back to 1 default profile: "default"
2024-12-14T22:11:51.130+08:00  INFO 13732 --- [hello-eureka-client-consumer] [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=08b0b462-971a-34b2-9c92-b11b1c919e3f
2024-12-14T22:11:52.190+08:00  INFO 13732 --- [hello-eure
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宋冠巡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值