客户端接口使用 - Java
FolkMQ 是单连接“多路复用”架构(不需要连接池,单连接即可)。“生产者” 与 “消费者” 的能力是容纳在客户端接口里。客户端通过 url 的形式与服务端连接,通过 nameAs 为自己取名字(相当于默认的“消费者组”),且客户端要单例化。示例:
添加依赖包
<dependency>
<groupId>org.noear</groupId>
<artifactId>folkmq-transport-netty</artifactId>
<version>1.7.8</version>
</dependency>
连接
//tcp 端口(默认)
MqClient client = FolkMQ.createClient("folkmq://127.0.0.1:18602")
.nameAs("demoapp")
.connect();
//ws 端口(需要服务侧启用 websocket。参考"部署与运维/可选配置")
MqClient client = FolkMQ.createClient("folkmq:ws://127.0.0.1:18602")
.nameAs("demoapp")
.connect();
带签权的连接
//通过 url 签权
MqClient client = FolkMQ.createClient("folkmq://127.0.0.1:18602?ak=xxx&sk=yyyy")
.nameAs("demoapp")
.connect();
//通过 meta 元信息签权
MqClient client = FolkMQ.createClient("folkmq://127.0.0.1:18602")
.config(c->c.metaPut("ak", "xxx").metaPut("sk", "yyyy"))
.nameAs("demoapp")
.connect();
集群模式连接(多个服务地址用","号隔开)
//通过 url 签权
MqClient client = FolkMQ.createClient("folkmq://127.0.0.1:18602?ak=xxx&sk=yyyy",
"folkmq://127.0.0.1:18602?ak=xxx&sk=yyyy",
"folkmq:ws://127.0.0.1:18602?ak=xxx&sk=yyyy")
.nameAs("demoapp")
.connect();
//通过 meta 元信息签权
MqClient client = FolkMQ.createClient("folkmq://127.0.0.1:18602",
"folkmq://127.0.0.1:18602",
"folkmq:ws://127.0.0.1:18602")
.config(c->c.metaPut("ak", "xxx").metaPut("sk", "yyyy"))
.nameAs("demoapp")
.connect();