領先一步
VMware 提供培訓和認證,以加速您的進度。
了解更多我們很高興地宣布,我們已發布 Spring Cloud Square 孵化器專案的第一個公開可用的里程碑版本。該專案為 Spring Cloud LoadBalancer 提供了 OkHttpClient 和 Retrofit 的整合,以及非阻塞的、由 WebClient 支援的 Retrofit 客戶端。Retrofit 是 Square 提供的一個宣告式 HTTP 客戶端。
您可以在下方找到更多關於如何開始使用該專案的資訊。您也可以查看專案儲存庫和專案文件。
一個應用程式攔截器被添加到自動配置建立的 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
實例來執行 Retrofit 呼叫。
要使用具有 Spring Cloud LoadBalancer 支援的 OkHttpClient
的 Retrofit,請將 spring-cloud-square-retrofit
和 spring-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 客戶端的完整範例。
我們也使用 adapters 來為 Retrofit 提供 WebClient 支援。
要使用具有 Spring Cloud LoadBalancer 支援的 WebClient
的 Retrofit,請將 spring-cloud-square-retrofit
和 spring-boot-starter-webflux
starter 依賴項新增到您的專案中
<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>