sofa协议服务器,HTTP 协议的基本使用

发布服务

// 只有1个线程执行。

ServerConfig serverConfig = new ServerConfig()

.setStopTimeout(60000)

.setPort(12300)

.setProtocol(RpcConstants.PROTOCOL_TYPE_HTTP)

.setDaemon(true);

// 发布一个服务,每个请求要执行 1 秒。

ProviderConfig providerConfig = new ProviderConfig()

.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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值