qidao123.com技术社区-IT企服评测·应用市场

标题: Spring Boot集成Spring Cloud 2024(不利用Feign) [打印本页]

作者: 水军大提督    时间: 2025-5-1 11:10
标题: Spring Boot集成Spring Cloud 2024(不利用Feign)
本文先容Spring Boot集成Spring Cloud 2024,且不利用Feign,而是采用Spring 6自带的@HttpExchange方式举行服务调用的详细步骤:
环境准备


集成步骤

1. 创建项目并添加依赖

通过Spring Initializr(https://start.spring.io/)创建Spring Boot项目,在pom.xml文件中添加以下依赖:
  1. <properties>
  2.     <java.version>21</java.version>
  3.     <springcloud.version>2024.0.0</springcloud.version>
  4.     <springboot.version>3.4.1</springboot.version>
  5. </properties>
  6. <dependencyManagement>
  7.     <dependencies>
  8.         <dependency>
  9.             <groupId>org.springframework.cloud</groupId>
  10.             <artifactId>spring-cloud-dependencies</artifactId>
  11.             <version>${springcloud.version}</version>
  12.             <type>pom</type>
  13.             <scope>import</scope>
  14.         </dependency>
  15.     </dependencies>
  16. </dependencyManagement>
  17. <dependencies>
  18.     <!-- Spring Boot Web 依赖 -->
  19.     <dependency>
  20.         <groupId>org.springframework.boot</groupId>
  21.         <artifactId>spring-boot-starter-web</artifactId>
  22.     </dependency>
  23.     <!-- Spring Boot WebFlux 依赖,用于 @HttpExchange -->
  24.     <dependency>
  25.         <groupId>org.springframework.boot</groupId>
  26.         <artifactId>spring-boot-starter-webflux</artifactId>
  27.     </dependency>
  28.     <!-- Spring Cloud 服务发现依赖(以 Eureka 为例) -->
  29.     <dependency>
  30.         <groupId>org.springframework.cloud</groupId>
  31.         <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  32.     </dependency>
  33. </dependencies>
复制代码
2. 设置服务注册中央(以Eureka为例)

2.1 添加Eureka Server依赖

在服务注册中央项目的pom.xml中添加以下依赖:
  1. <dependency>
  2.     <groupId>org.springframework.cloud</groupId>
  3.     <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  4. </dependency>
复制代码
2.2 设置Eureka Server

在application.yml文件中添加如下设置:
  1. server:
  2.   port: 8761  # Eureka服务器端口
  3. spring:
  4.   application:
  5.     name: eureka-server  # 服务名称
  6. eureka:
  7.   client:
  8.     register-with-eureka: false  # 不将自己注册到Eureka
  9.     fetch-registry: false  # 不从Eureka获取服务列表
复制代码
2.3 启用Eureka Server

在Spring Boot主应用类上添加@EnableEurekaServer注解:
  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
  4. @SpringBootApplication
  5. @EnableEurekaServer
  6. public class EurekaServerApplication {
  7.     public static void main(String[] args) {
  8.         SpringApplication.run(EurekaServerApplication.class, args);
  9.     }
  10. }
复制代码
3. 设置服务提供者

3.1 添加Eureka客户端依赖

确保服务提供者项目的pom.xml中包含spring-cloud-starter-netflix-eureka-client依赖。
3.2 设置Eureka客户端

在application.yml中设置如下内容:
  1. spring:
  2.   application:
  3.     name: my-service-provider  # 服务名称
  4. eureka:
  5.   client:
  6.     service-url:
  7.       defaultZone: http://localhost:8761/eureka/  # Eureka服务器地址
  8.     register-with-eureka: true  # 注册到Eureka服务器
  9.     fetch-registry: true  # 从Eureka服务器获取服务列表
复制代码
3.3 创建服务接口和实现类

编写业务接口和实现类,提供具体的服务功能。比方:
  1. import org.springframework.web.bind.annotation.GetMapping;
  2. import org.springframework.web.bind.annotation.RestController;
  3. @RestController
  4. public class HelloController {
  5.     @GetMapping("/hello")
  6.     public String hello() {
  7.         return "Hello from service provider!";
  8.     }
  9. }
复制代码
3.4 启用Eureka客户端

在Spring Boot主应用类上添加@EnableEurekaClient注解:
  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.cloud.netflix.eureka.client.EnableEurekaClient;
  4. @SpringBootApplication
  5. @EnableEurekaClient
  6. public class ServiceProviderApplication {
  7.     public static void main(String[] args) {
  8.         SpringApplication.run(ServiceProviderApplication.class, args);
  9.     }
  10. }
复制代码
4. 设置服务消费者

4.1 添加依赖

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

与服务提供者类似,在application.yml中设置Eureka客户端信息,指定Eureka服务器地点等:
  1. spring:
  2.   application:
  3.     name: my-service-consumer  # 服务名称
  4. eureka:
  5.   client:
  6.     service-url:
  7.       defaultZone: http://localhost:8761/eureka/  # Eureka服务器地址
  8.     register-with-eureka: true  # 注册到Eureka服务器
  9.     fetch-registry: true  # 从Eureka服务器获取服务列表
复制代码
4.3 利用@HttpExchange举行服务调用

首先,创建一个设置类来设置WebClient和HttpServiceProxyFactory:
  1. import org.springframework.context.annotation.Bean;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.web.reactive.function.client.WebClient;
  4. import org.springframework.web.service.invoker.HttpServiceProxyFactory;
  5. @Configuration
  6. public class ClientConfig {
  7.     @Bean
  8.     public WebClient webClient() {
  9.         return WebClient.builder()
  10.                .build();
  11.     }
  12.     @Bean
  13.     public HttpServiceProxyFactory httpServiceProxyFactory(WebClient webClient) {
  14.         return HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient)).build();
  15.     }
  16. }
复制代码
然后,定义一个利用@HttpExchange的接口来调用服务提供者的接口:
  1. import org.springframework.web.service.annotation.GetExchange;
  2. import org.springframework.web.service.annotation.HttpExchange;
  3. @HttpExchange
  4. public interface HelloServiceClient {
  5.     @GetExchange("http://my-service-provider/hello")
  6.     String getHelloMessage();
  7. }
复制代码
最后,在服务消费者的控制器中利用该接口举行调用:
  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. @RestController
  5. public class ConsumerController {
  6.     @Autowired
  7.     private HelloServiceClient helloServiceClient;
  8.     @GetMapping("/call-provider")
  9.     public String callProvider() {
  10.         return helloServiceClient.getHelloMessage();
  11.     }
  12. }
复制代码
常见问题及办理办法

版本兼容性问题


服务注册与发现问题


@HttpExchange调用失败



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com技术社区-IT企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.4