Spring Cloud Square 0.4.0-M1 版发布 | Retrofit | OkHttp

原文地址: https://spring.io/blog/2021/04/13/introducing-spring-cloud-square (opens new window)

限量阿里云优惠券 (opens new window)

# 正文

我们很高兴地宣布, 我们已经发布了 Spring Cloud Square 孵化器项目的第一个公开里程碑版本. 该项目为 Spring Cloud LoadBalancer 提供了 OkHttpClientRetrofit 以及基于非阻塞式 WebClientRetrofit 客户端的集成. Retrofit 是来自 Square 的声明式 HTTP 客户端.

# 如何集成 OkHttpClient 到 Spring Cloud LoadBalancer

一个新的应用程序拦截器将通过自动配置被添加到 OkHttpClient 中. 它为 Spring Cloud LoadBalancer 提供解析协议, 主机和端口, 并重写 URL 功能.

想要使用 Spring Cloud 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 来发起请求. Spring Cloud LoadBalancer 将选择一个可用的服务实例来响应该请求.

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

# Retrofit, OkHttpClient, Spring Cloud LoadBalancer

我们还可以使用负载均衡过的 OkHttpClient 实例来执行 Retrofit 请求.

想要使用基于 OkHttpClientRetrofitSpring Cloud LoadBalancer , 请将 spring-cloud-square-retrofitspring-cloud-square-okhttp 依赖项添加到您的项目中:

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

使用 @EnableRetrofitClients 注解并创建一个带 @LoadBalanced 注解的 OkHttpClient.Builder bean 以便让 Spring 能够自动为您实例化并注入 Retrofit 客户端:

@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 客户端到其他组件中, 并使用它来进行负载均衡的调用了:

@Service
class AService {

    @Autowired
    HelloClient client;

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

您也可以查看 这个示例 (opens new window) 来充分的了解基于 OkHttpClientRetrofit 客户端如何完成负载均衡地请求.

# Retrofit, WebClient, Spring Cloud LoadBalancer

我们同样为 Retrofit 提供基于 WebClient 的支持.

想要使用基于 WebClientRetrofitSpring Cloud LoadBalancer, 请将 spring-cloud-square-retrofitspring-boot-starter-webflux 依赖项添加到您的项目中:

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

使用 @EnableRetrofitClients 注解并创建一个带 @LoadBalanced 注解的 WebClient.Builder bean 以便让 Spring 能够自动为您实例化并注入 Retrofit 客户端:

@Configuration
@EnableRetrofitClients
class OkHttpClientConfig {

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

然后创建一个 Retrofit 客户端, 并对其使用注解 @RetrofitClient, 然后将 serviceId 作为参数传递进去:

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

请确保使用的是 Retrofit 提供的方法注解, 例如 @GET("/"). 现在, 您可以注入 Retrofit 客户端到其他组件中, 并使用它来进行负载均衡地调用了:

@Service
class AService {

    @Autowired
    HelloClient client;

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

您也可以查看 这个示例 (opens new window) 来充分的了解基于 WebClientRetrofit 客户端如何完成负载均衡地请求.

# 备注

由于当前可用的发行版是一个里程碑版本, 因此您需要将 Spring Milestone Maven 仓库添加到您的项目中:

<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 Cloud 交流群: 617143034 (opens new window)

欢迎大家点击下方的图片领取限量 阿里云优惠券 (opens new window), 新购续费更优惠: 限量阿里云优惠券 (opens new window)