本文介紹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
文件中添加以下依賴:
<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
中添加以下依賴:
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2.2 配置Eureka Server
在application.yml
文件中添加如下配置:
server:port: 8761 # Eureka服務器端口
spring:application:name: eureka-server # 服務名稱
eureka:client:register-with-eureka: false # 不將自己注冊到Eurekafetch-registry: false # 不從Eureka獲取服務列表
2.3 啟用Eureka Server
在Spring Boot主應用類上添加@EnableEurekaServer
注解:
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
中配置如下內容:
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 創建服務接口和實現類
編寫業務接口和實現類,提供具體的服務功能。例如:
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
注解:
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服務器地址等:
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
進行服務調用
首先,創建一個配置類來配置WebClient
和HttpServiceProxyFactory
:
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 {@Beanpublic WebClient webClient() {return WebClient.builder().build();}@Beanpublic HttpServiceProxyFactory httpServiceProxyFactory(WebClient webClient) {return HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient)).build();}
}
然后,定義一個使用@HttpExchange
的接口來調用服務提供者的接口:
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();
}
最后,在服務消費者的控制器中使用該接口進行調用:
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 {@Autowiredprivate HelloServiceClient helloServiceClient;@GetMapping("/call-provider")public String callProvider() {return helloServiceClient.getHelloMessage();}
}
常見問題及解決辦法
版本兼容性問題
- 原因:Spring Boot、Spring Cloud以及其他相關依賴版本不匹配。
- 解決辦法:參考官方文檔或社區推薦的版本組合,確保各組件版本兼容。
服務注冊與發現問題
- 原因:網絡問題、配置錯誤等導致服務無法注冊到Eureka或無法發現其他服務。
- 解決辦法:檢查網絡連接,確保Eureka服務器可訪問;仔細核對服務配置中的Eureka服務器地址、服務名稱等信息是否正確。
@HttpExchange
調用失敗
- 原因:服務名稱解析錯誤、接口路徑不匹配、網絡故障等。
- 解決辦法:檢查
@GetExchange
注解中的服務地址和接口路徑是否正確;排查網絡問題,確保服務消費者與提供者之間網絡暢通。