Spring Boot集成Spring Cloud 2024(不使用Feign)

本文介绍Spring Boot集成Spring Cloud 2024,且不使用Feign,而是采用Spring 6自带的@HttpExchange方式进行服务调用的详细步骤:

环境准备

  • Spring Boot版本:推荐使用Spring Boot 3.4.1及以上版本,以更好地与Spring Cloud 2024适配。
  • Java版本:建议使用Java 21,以获得更好的性能和特性支持。

集成步骤

1. 创建项目并添加依赖

通过Spring Initializr(https://start.spring.io/)创建Spring Boot项目,在pom.xml文件中添加以下依赖:

xml 复制代码
<properties>
    <java.version>21</java.version>
    <springcloud.version>2024.0.0</springcloud.version>
    <springboot.version>3.4.1</springboot.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${springcloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <!-- Spring Boot Web 依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- Spring Boot WebFlux 依赖,用于 @HttpExchange -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <!-- Spring Cloud 服务发现依赖(以 Eureka 为例) -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
</dependencies>
2. 配置服务注册中心(以Eureka为例)
2.1 添加Eureka Server依赖

在服务注册中心项目的pom.xml中添加以下依赖:

xml 复制代码
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.2 配置Eureka Server

application.yml文件中添加如下配置:

yaml 复制代码
server:
  port: 8761  # Eureka服务器端口
spring:
  application:
    name: eureka-server  # 服务名称
eureka:
  client:
    register-with-eureka: false  # 不将自己注册到Eureka
    fetch-registry: false  # 不从Eureka获取服务列表
2.3 启用Eureka Server

在Spring Boot主应用类上添加@EnableEurekaServer注解:

java 复制代码
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. 配置服务提供者
3.1 添加Eureka客户端依赖

确保服务提供者项目的pom.xml中包含spring-cloud-starter-netflix-eureka-client依赖。

3.2 配置Eureka客户端

application.yml中配置如下内容:

yaml 复制代码
spring:
  application:
    name: my-service-provider  # 服务名称
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  # Eureka服务器地址
    register-with-eureka: true  # 注册到Eureka服务器
    fetch-registry: true  # 从Eureka服务器获取服务列表
3.3 创建服务接口和实现类

编写业务接口和实现类,提供具体的服务功能。例如:

java 复制代码
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello from service provider!";
    }
}
3.4 启用Eureka客户端

在Spring Boot主应用类上添加@EnableEurekaClient注解:

java 复制代码
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.client.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class ServiceProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceProviderApplication.class, args);
    }
}
4. 配置服务消费者
4.1 添加依赖

服务消费者项目的pom.xml中同样需要spring-cloud-starter-netflix-eureka-client以及spring-boot-starter-webflux依赖。

4.2 配置Eureka客户端

与服务提供者类似,在application.yml中配置Eureka客户端信息,指定Eureka服务器地址等:

yaml 复制代码
spring:
  application:
    name: my-service-consumer  # 服务名称
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/  # Eureka服务器地址
    register-with-eureka: true  # 注册到Eureka服务器
    fetch-registry: true  # 从Eureka服务器获取服务列表
4.3 使用@HttpExchange进行服务调用

首先,创建一个配置类来配置WebClientHttpServiceProxyFactory

java 复制代码
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

@Configuration
public class ClientConfig {
    @Bean
    public WebClient webClient() {
        return WebClient.builder()
               .build();
    }

    @Bean
    public HttpServiceProxyFactory httpServiceProxyFactory(WebClient webClient) {
        return HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient)).build();
    }
}

然后,定义一个使用@HttpExchange的接口来调用服务提供者的接口:

java 复制代码
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.annotation.HttpExchange;

@HttpExchange
public interface HelloServiceClient {
    @GetExchange("http://my-service-provider/hello")
    String getHelloMessage();
}

最后,在服务消费者的控制器中使用该接口进行调用:

java 复制代码
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConsumerController {
    @Autowired
    private HelloServiceClient helloServiceClient;

    @GetMapping("/call-provider")
    public String callProvider() {
        return helloServiceClient.getHelloMessage();
    }
}

常见问题及解决办法

版本兼容性问题
  • 原因:Spring Boot、Spring Cloud以及其他相关依赖版本不匹配。
  • 解决办法:参考官方文档或社区推荐的版本组合,确保各组件版本兼容。
服务注册与发现问题
  • 原因:网络问题、配置错误等导致服务无法注册到Eureka或无法发现其他服务。
  • 解决办法:检查网络连接,确保Eureka服务器可访问;仔细核对服务配置中的Eureka服务器地址、服务名称等信息是否正确。
@HttpExchange调用失败
  • 原因:服务名称解析错误、接口路径不匹配、网络故障等。
  • 解决办法 :检查@GetExchange注解中的服务地址和接口路径是否正确;排查网络问题,确保服务消费者与提供者之间网络畅通。
相关推荐
咩咩啃树皮6 小时前
第40篇:Vue3组件化开发精讲——组件拆分、复用、父子通信、工程化架构
java·前端·架构
鱟鲥鳚7 小时前
Spring Boot 集成 LangChain4j:从模型调用到 Tool Calling(Demo版)
java·spring boot
大模型码小白8 小时前
【Python零基础教程】继承、多态与魔法函数:面向对象编程三大核心特性详解
java·大数据·开发语言·人工智能·python·ai编程
腾渊信息科技公司9 小时前
Spring Boot对接MES实战:视觉检测数据自动同步方案
java·人工智能·spring boot·后端·计算机视觉·ai·软件需求
爱笑的源码基地10 小时前
高并发 Redis 缓存门诊HIS系统源码,含财务统计药房进销存
java·程序·门诊系统·诊所系统·云诊所源码
wuqingshun31415910 小时前
TCP超时重传机制是为了解决什么问题?
java
莫逸风12 小时前
【AgentScope 2.0】 0. 学习指南
java·llm·agent·agentscope
隔窗听雨眠13 小时前
Spring Boot在云原生时代的编程范式革新研究
spring boot·后端·云原生
z1234567898613 小时前
2026最新两款AI编程工具深度对比实测
java·数据库·ai编程
yaoxin52112314 小时前
470. Java 反射 - Member 接口与 AccessFlag
java·开发语言·python