文档

HTTP 协议的基本使用

更新时间:

在 SOFARPC(非 SOFABoot 环境)中使用 HTTP 作为服务端协议时,支持 JSON 以序列化方式作为一些基础的测试方式使用。

发布服务

// 只有1个线程执行。
ServerConfig serverConfig = new ServerConfig()
.setStopTimeout(60000)
.setPort(12300)
.setProtocol(RpcConstants.PROTOCOL_TYPE_HTTP)
.setDaemon(true);

// 发布一个服务,每个请求要执行 1 秒。
ProviderConfig<HttpService> providerConfig = new ProviderConfig<HttpService>()
.setInterfaceId(HttpService.class.getName())
.setRef(new HttpServiceImpl())
.setApplication(new ApplicationConfig().setAppName("serverApp"))
.setServer(serverConfig)
.setUniqueId("uuu")
.setRegister(false);
providerConfig.export();

引用服务

因为是 HTTP + JSON,所以引用方可以直接通过 HttpClient 进行调用,以下为一段测试代码。

private ObjectMapper mapper =new ObjectMapper();
HttpClient httpclient =HttpClientBuilder.create().build();
// POST 正常请求。
String url ="http://127.0.0.1:12300/com.alipay.sofa.rpc.server.http.HttpService:uuu/object";
HttpPost httpPost =new HttpPost(url);
httpPost.setHeader(RemotingConstants.HEAD_SERIALIZE_TYPE,"json");
ExampleObj obj =new ExampleObj();
obj.setId(1);
obj.setName("xxx");
byte[] bytes = mapper.writeValueAsBytes(obj);
ByteArrayEntity entity =new ByteArrayEntity(bytes,
ContentType.create("application/json"));

httpPost.setEntity(entity);

HttpResponse httpResponse = httpclient.execute(httpPost);
Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
byte[] data =EntityUtils.toByteArray(httpResponse.getEntity());

ExampleObj result = mapper.readValue(data,ExampleObj.class);

Assert.assertEquals("xxxxx", result.getName());
  • 本页导读 (0)
文档反馈