Spring Cloud Square 简介

发布 | Olga Maciaszek-Sharma | 2021 年 4 月 13 日 | ...

我们很高兴地宣布,我们已发布 Spring Cloud Square incubator 项目的第一个公开可用的里程碑版本。该项目为 Spring Cloud LoadBalancer 提供了 OkHttpClientRetrofit 的集成,以及非阻塞的 WebClient 支持的 Retrofit 客户端。 Retrofit 是 Square 提供的一个声明式 HTTP 客户端。

您可以在下面找到有关如何开始使用该项目的更多信息。您还可以查看项目存储库项目文档

OkHttpClient Spring Cloud LoadBalancer 集成

应用程序拦截器已添加到自动配置创建的 OkHttpClient 中。 它从 Spring Cloud LoadBalancer 解析 scheme、host 和 port 并重写 URL。

要使用 SC LoadBalancer 解析和选择实例来发送请求,请将 spring-cloud-square-okhttp 依赖项添加到您的项目中

<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-square-okhttp</artifactId>
		<version>0.4.0-M1</version>
</dependency>

然后创建一个使用 @LoadBalanced 注解的 OkHttpClient.Builder bean

@Configuration
class OkHttpClientConfig{
@Bean
@LoadBalanced
public OkHttpClient.Builder okHttpClientBuilder() {
    return new OkHttpClient.Builder();
    }
}

现在您可以使用 serviceId 或虚拟主机名,而不是请求中的实际 host:port。 SC LoadBalancer 通过选择一个可用的服务实例来解析它。

Request request = new Request.Builder()
                        .url("http://serviceId/hello").build();
Response response = builder.build().newCall(request).execute();

使用 OkHttpClient 和 Spring Cloud LoadBalancer 的 Retrofit

我们还使用负载均衡的 OkHttpClient 实例来运行 Retrofit 调用。

要将 Retrofit 与 Spring Cloud LoadBalancer 支持的 OkHttpClient 一起使用,请将 spring-cloud-square-retrofitspring-cloud-square-okhttp 依赖项添加到您的项目中

<dependencies>
    <dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-square-retrofit</artifactId>
		<version>0.4.0-M1</version>
    </dependency>
    <dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-square-okhttp</artifactId>
		<version>0.4.0-M1</version>
    </dependency>
</dependencies>

使用 @EnableRetrofitClients 注解让我们自动实例化并为您注入 Retrofit 客户端。 然后创建一个使用 @LoadBalanced 注解的 OkHttpClient.Builder bean 以在后台使用

@Configuration
@EnableRetrofitClients
class OkHttpClientConfig {

@Bean
@LoadBalanced
public OkHttpClient.Builder okHttpClientBuilder() {
    return new OkHttpClient.Builder();
    }
}

创建一个 Retrofit 客户端并使用 @RetrofitClient 对其进行注释,传递您的服务的 serviceId 作为参数(您也可以使用该注解传递包含用户创建的 Retrofit 客户端拦截器的自定义配置)

@RetrofitClient("serviceId")
interface HelloClient {
	@GET("/")
	Call<String> hello();
}

确保使用 Retrofit 方法注解,例如 @GET("/")。 现在您可以注入 Retrofit 客户端并使用它来运行负载均衡的调用(使用 serviceId 而不是实际的 host:port

class AService {

    @Autowired
    HelloClient client;

	public String hello() throws IOException {
		return client.hello().execute().body();
	}
}

我们创建了一个 基于 load-balanced-OkHttpClient 的 Retrofit 客户端的完整示例

使用 WebClient 和 Spring Cloud LoadBalancer 的 Retrofit

我们还使用适配器为 Retrofit 提供 WebClient 支持。

要将 Retrofit 与 Spring Cloud LoadBalancer 支持的 WebClient 一起使用,请将 spring-cloud-square-retrofitspring-boot-starter-webflux 启动器依赖项添加到您的项目中

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-square-retrofit-webclient</artifactId>
	<version>0.4.0-M1</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-loadbalancer</artifactId>
    </dependency>
</dependencies>

使用 @EnableRetrofitClients 注解让我们自动实例化并为您注入 Retrofit 客户端。 然后创建一个 使用 @LoadBalanced 注解的 WebClient.Builder bean 以在后台使用

@Configuration
@EnableRetrofitClients
class OkHttpClientConfig {

@Bean
@LoadBalanced
public WebClient.Builder webClientBuilder() {
    return WebClient.builder();
    }
}

创建一个 Retrofit 客户端并使用 @RetrofitClient 对其进行注释,将您的服务的 serviceId 作为参数传递

@RetrofitClient("serviceId")
interface HelloClient {
	@GET("/")
	Mono<String> hello();
}

确保使用 Retrofit 方法注解,例如 @GET("/")。 现在您可以注入 Retrofit 客户端并使用它来运行负载均衡的调用(使用 serviceId 而不是实际的 host:port

class AService {

    @Autowired
    HelloClient client;

	public Mono<String> hello() throws IOException {
		return client.hello();
	}
}

我们创建了一个 基于 load-balanced-WebClient 的 Retrofit 客户端的完整示例

注意

由于当前可用版本是一个里程碑版本,因此您需要将 Spring Milestone 存储库链接添加到您的项目,以用于本博客文章中提供的所有示例

<repositories>
    <repository>
        <id>spring-milestones</id>
        <url>https://repo.spring.io/milestone</url>
    </repository>
</repositories>

我们建议对其他 Spring Cloud 依赖项使用依赖项管理

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

获取 Spring 新闻通讯

随时了解 Spring 新闻通讯

订阅

领先一步

VMware 提供培训和认证,以加速您的进步。

了解更多

获取支持

Tanzu Spring 在一个简单的订阅中为 OpenJDK™、Spring 和 Apache Tomcat® 提供支持和二进制文件。

了解更多

即将举行的活动

查看 Spring 社区中所有即将举行的活动。

查看全部