Streamlining HTTP client definition in Spring 6, CoApi provides zero boilerplate code auto-configuration for more convenient and efficient interface calls | 简化 Spring 6 中 HTTP 客户端定义,提供零样板代码自动配置,让接口调用更便捷高效
APACHE-2.0 License
Bot releases are hidden (Show)
Published by Ahoo-Wang 9 months ago
provider-service
& consumer-service
by @Ahoo-Wang in https://github.com/Ahoo-Wang/CoApi/pull/5
Full Changelog: https://github.com/Ahoo-Wang/CoApi/compare/v0.2.2...v0.5.0
Published by Ahoo-Wang 9 months ago
LoadBalancedWebClientFactoryBean
by @Ahoo-Wang in https://github.com/Ahoo-Wang/CoApi/pull/4
Full Changelog: https://github.com/Ahoo-Wang/CoApi/compare/v0.2.0...v0.2.2
Published by Ahoo-Wang 9 months ago
在 Spring Framework 6 中,引入了全新的 HTTP 客户端 - Spring6 HTTP Interface。
该接口允许开发者通过使用 @HttpExchange
注解将 HTTP 服务定义为 Java 接口。
然而,当前版本尚未提供自动配置的支持,需要开发者自己实现配置。
虽然 Spring 生态中已经存在 Spring Cloud OpenFeign ,但它并未支持响应式编程模型。
为解决这个问题,Spring Cloud OpenFeign 推荐了替代方案 feign-reactive。然而,这个替代方案目前已处于不积极维护状态,并且不支持 Spring Boot 3.2.x。
CoApi 应运而生,它提供了类似于 Spring Cloud OpenFeign 的零样板代码自动配置的支持。开发者只需定义接口,即可轻松使用。
使用 Gradle(Kotlin) 安装依赖
implementation("me.ahoo.coapi:spring-boot-starter")
使用 Gradle(Groovy) 安装依赖
implementation 'me.ahoo.coapi:spring-boot-starter'
使用 Maven 安装依赖
<dependency>
<groupId>me.ahoo.coapi</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${coapi.version}</version>
</dependency>
CoApi
- 第三方接口
baseUrl
: 定义请求的基础地址,该参数可以从配置文件中获取,如:baseUrl = "\${github.url}"
,github.url
是配置文件中的配置项
@CoApi(baseUrl = "\${github.url}")
interface GitHubApiClient {
@GetExchange("repos/{owner}/{repo}/issues")
fun getIssue(@PathVariable owner: String, @PathVariable repo: String): Flux<Issue>
}
配置文件:
github:
url: https://api.github.com
CoApi
- 客户端负载均衡@CoApi(serviceId = "github-service")
interface ServiceApiClient {
@GetExchange("repos/{owner}/{repo}/issues")
fun getIssue(@PathVariable owner: String, @PathVariable repo: String): Flux<Issue>
}
CoApi
@RestController
class GithubController(
private val gitHubApiClient: GitHubApiClient,
private val serviceApiClient: ServiceApiClient
) {
@GetMapping("/baseUrl")
fun baseUrl(): Flux<Issue> {
return gitHubApiClient.getIssue("Ahoo-Wang", "CoApi")
}
@GetMapping("/serviceId")
fun serviceId(): Flux<Issue> {
return serviceApiClient.getIssue("Ahoo-Wang", "CoApi")
}
}
In Spring Framework 6, a new HTTP client, Spring6 HTTP Interface, has been introduced. This interface allows developers to define HTTP services as Java interfaces using the @HttpExchange
annotation.
However, automatic configuration support is not yet provided in the current version, and developers need to implement configurations manually.
While the Spring ecosystem already has Spring Cloud OpenFeign, it lacks support for the reactive programming model. To address this, Spring Cloud OpenFeign recommends an alternative solution, feign-reactive. However, this alternative is currently not actively maintained and does not support Spring Boot 3.2.x.
CoApi comes into play to address these challenges. It offers support for zero boilerplate code auto-configuration, similar to Spring Cloud OpenFeign. Developers only need to define interfaces, making usage straightforward.
Use Gradle(Kotlin) to install dependencies
implementation("me.ahoo.coapi:spring-boot-starter")
Use Gradle(Groovy) to install dependencies
implementation 'me.ahoo.coapi:spring-boot-starter'
Use Maven to install dependencies
<dependency>
<groupId>me.ahoo.coapi</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>${coapi.version}</version>
</dependency>
CoApi
- a third-party interface
baseUrl
: Define the base address of the request, which can be obtained from the configuration file, for example:baseUrl = "\{github.url}"
,github.url
is the configuration item in the configuration file
@CoApi(baseUrl = "\${github.url}")
interface GitHubApiClient {
@GetExchange("repos/{owner}/{repo}/issues")
fun getIssue(@PathVariable owner: String, @PathVariable repo: String): Flux<Issue>
}
Configuration:
github:
url: https://api.github.com
CoApi
- Client Load Balancing@CoApi(serviceId = "github-service")
interface ServiceApiClient {
@GetExchange("repos/{owner}/{repo}/issues")
fun getIssue(@PathVariable owner: String, @PathVariable repo: String): Flux<Issue>
}
CoApi
@RestController
class GithubController(
private val gitHubApiClient: GitHubApiClient,
private val serviceApiClient: ServiceApiClient
) {
@GetMapping("/baseUrl")
fun baseUrl(): Flux<Issue> {
return gitHubApiClient.getIssue("Ahoo-Wang", "CoApi")
}
@GetMapping("/serviceId")
fun serviceId(): Flux<Issue> {
return serviceApiClient.getIssue("Ahoo-Wang", "CoApi")
}
}