Skip to main content

This page is available in Simplified Chinese.

Hprose

Hprose 是支持多语言的 RPC 协议,使用自己的值编码传输参数和返回值。Meteor 提供 HTTP 接入。

  • 支持 HTTP 请求响应和 Hprose 值编码。
  • 支持通过函数名调用,并将对象、集合等值映射到客户端类型。
  • 不支持 Flow.Publisher<T> 流式方法。

引入依赖​

<dependency>
<groupId>net.hasor</groupId>
<artifactId>meteor-protocol-hprose</artifactId>
<version>5.0.0-SNAPSHOT</version>
</dependency>

启用协议​

提供者监听端口并提供服务,消费者通过出站协议调用服务。

提供者
configuration.connector("http")
.bind("127.0.0.1", 8080)
.protocol("hprose").contextPath("/hprose");
消费者
configuration.connector("http")
.protocol("hprose")
.contextPath("/hprose");

发起 RPC 调用​

在已启动的 MetContext 中发布 EchoService,客户端调用 echo,输出 hello。

服务定义
import net.hasor.meteor.annotation.MetParam;

public interface EchoService {
String echo(@MetParam(id = 1) String text);
}

public class EchoServiceImpl implements EchoService {
public String echo(String text) { return text; }
}
提供者
context.publisher().rsfService(EchoService.class, new EchoServiceImpl())
.name("meteor.example.EchoService")
.protocol("hprose")
.aliasName("Hprose", "echo")
.register();
消费者
import net.hasor.meteor.address.InterAddress;

// 订阅服务接口
var binding = context.publisher().rsfService(EchoService.class)
.name("meteor.example.EchoService")
.protocol("hprose")
.aliasName("Hprose", "echo")
.register();

// 通过直连地址获取代理
EchoService client = context.getRsfClient(new InterAddress("hprose", "127.0.0.1", 8080))
.getRemote(binding);

// 调用服务
System.out.println(client.echo("hello"));

协议配置​

  • HTTP 路径通过 contextPath 设置。
  • 服务别名通过 aliasName("Hprose", "echo") 设置;原生函数名例如 echo_echo。

注意事项​

  • Hprose 函数名和报文格式不能与 JSON-RPC 混用。
  • 对象、日期、字节和引用的映射需与客户端 SDK 一致。