Spring云带的开始非常简单,并且使用其余模板与后端服务器进行通信。
但在我们的项目中,我们更喜欢使用okhttp来执行http请求,是否有人可以提供帮助?
答案 0 :(得分:2)
您可以在Github上查看通过Spring Cloud Netflix与Square的OkHttpClient和Netflix Ribbon集成的spring-cloud-square项目。让我们在OkHttpRibbonInterceptorTests.java class
中查看测试方法@Test
@SneakyThrows
public void httpClientWorks() {
Request request = new Request.Builder()
// here you use a service id, or virtual hostname
// rather than an actual host:port, ribbon will
// resolve it
.url("http://" + SERVICE_ID + "/hello")
.build();
Response response = builder.build().newCall(request).execute();
Hello hello = new ObjectMapper().readValue(response.body().byteStream(), Hello.class);
assertThat("response was wrong", hello.getValue(), is(equalTo("hello okhttp")));
}