当前位置: 首页 > news >正文

做电商网站需要注意哪些长沙做推广的公司有多少

做电商网站需要注意哪些,长沙做推广的公司有多少,建设部网站监理资质换证,wordpress添加自定义字段以下是一个 使用多路复用(TMultiplexedProtocol) 的 Thrift 客户端完整流程和关键函数(以 Java 为例),适用于当服务端使用 TMultiplexedProcessor 注册了多个服务时,客户端可以区分并调用不同的服务。 ✅ 客…
以下是一个 使用多路复用(TMultiplexedProtocol) 的 Thrift 客户端完整流程和关键函数(以 Java 为例),适用于当服务端使用 TMultiplexedProcessor 注册了多个服务时,客户端可以区分并调用不同的服务。

✅ 客户端整体流程(使用 TMultiplexedProtocol)

1. 定义多个服务(以两个为例)

// example1.thrift
namespace java exampleservice ExampleService1 {string sayHello(1:string name)
}
// example2.thrift
namespace java exampleservice ExampleService2 {i32 add(1:i32 a, 2:i32 b)
}

使用 Thrift 编译器生成 Java 代码:

thrift --gen java example1.thrift
thrift --gen java example2.thrift

2. 服务端使用多路复用(服务端部分简略)

TMultiplexedProcessor multiplexedProcessor = new TMultiplexedProcessor();multiplexedProcessor.registerProcessor("ExampleService1", new ExampleService1.Processor<>(new ExampleService1Impl()));
multiplexedProcessor.registerProcessor("ExampleService2", new ExampleService2.Processor<>(new ExampleService2Impl()));TServerTransport serverTransport = new TServerSocket(9090);
TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(multiplexedProcessor));
server.serve();

3. 客户端实现(核心)

import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TMultiplexedProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;import example.ExampleService1;
import example.ExampleService2;public class ThriftClient {public static void main(String[] args) {TTransport transport = null;try {// 1. 打开传输层连接transport = new TSocket("localhost", 9090);transport.open();// 2. 创建基础协议TProtocol baseProtocol = new TBinaryProtocol(transport);// 3. 使用多路复用协议(针对不同服务)TMultiplexedProtocol protocol1 = new TMultiplexedProtocol(baseProtocol, "ExampleService1");TMultiplexedProtocol protocol2 = new TMultiplexedProtocol(baseProtocol, "ExampleService2");// 4. 创建客户端 Stub(代理)ExampleService1.Client client1 = new ExampleService1.Client(protocol1);ExampleService2.Client client2 = new ExampleService2.Client(protocol2);// 5. 调用服务方法String greeting = client1.sayHello("Alice");int result = client2.add(5, 7);System.out.println("Service1 response: " + greeting);System.out.println("Service2 response: " + result);} catch (Exception e) {e.printStackTrace();} finally {// 6. 关闭连接if (transport != null) {transport.close();}}}
}

4. C++ 版本:客户端实现

客户端代码(C++)
#include 
#include 
#include 
#include #include "ExampleService1.h"
#include "ExampleService2.h"using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;int main() {try {// 客户端连接std::shared_ptr socket(new TSocket("localhost", 9090));std::shared_ptr transport(new TBufferedTransport(socket));std::shared_ptr protocol(new TBinaryProtocol(transport));transport->open();// 使用多路复用协议TMultiplexedProtocol protocol1(protocol, "ExampleService1");TMultiplexedProtocol protocol2(protocol, "ExampleService2");// 创建客户端ExampleService1Client client1(protocol1);ExampleService2Client client2(protocol2);// 调用服务std::string greeting;client1.sayHello(greeting, "Alice");std::cout << "Response from ExampleService1: " << greeting << std::endl;int result;client2.add(result, 5, 7);std::cout << "Response from ExampleService2: " << result << std::endl;transport->close();} catch (const std::exception& e) {std::cerr << "Error: " << e.what() << std::endl;}return 0;
}

🔍 客户端关键类和函数说明

类 / 函数
作用说明
TSocket(host, port)
创建客户端传输连接(基于 TCP)
transport.open()
打开连接
TBinaryProtocol
使用二进制协议进行编码
TMultiplexedProtocol
多路复用协议,用于标识服务名称
ExampleService1.Client
客户端代理,调用服务方法
client1.sayHello("Alice")
实际调用远程服务端方法
transport.close()
关闭连接

✅ 小提示

  • 多路复用的关键是:
    • 服务端使用 TMultiplexedProcessor 注册服务(带名字);
    • 客户端用 TMultiplexedProtocol 创建不同服务的协议实例(带同样的名字)。
http://www.dtcms.com/a/429647.html

相关文章:

  • 网络搏彩网站做代理青岛市黄岛区网站建设
  • 代做网站推广的公司汕头澄海地图
  • 宜宾市网站建设自助网站建设推广优化策略
  • seo建站的步骤wordpress插件自动更新
  • 青海网站建设与管理建设一个网站 最好是官网那种
  • 湖南网站定制企业网站建设对网络营销有哪些影响
  • 信息化工作总结 网站建设网站建设标语
  • 企业做网站价格阿里云域名官网
  • 焦作网站seo重庆卓光科技有限公司
  • 备案的时候网站名称提交网站的入口地址
  • 购物网站建设图标大全免费店铺logo设计
  • 如何在电网网站做备案南通专业做网站
  • 静态网页制作网站怎么去创立一个网站
  • 厦门网站建设培训网站建设规范
  • python+网站开发实例教程中建名城建设有限公司 网站
  • 安徽省两学一做网站wordpress 图片 筛选 插件
  • 秦皇岛建设网站北京微网站设计开发服务
  • 外贸公司网站制作公司通州网站网站建设
  • 动态ip怎么做网站马鞍山网站建设咨询电
  • 北京网站建设外包公司百度手机seo
  • 百度权重排名高的网站陕西省建设工程质量安全监督总站网站
  • 网站霸词怎么做榆林市城乡建设规划局网站
  • 免费网站模块好看的企业网站首页
  • 企业网站产品优化怎么做淘宝电脑版登录入口
  • 网站开发就业薪酬近期新闻热点事件及评论
  • 山西网站建设开发团队最新企业名录搜索软件
  • 筹备网站建设定制旅游网站建设方案
  • 提供手机网站制作哪家好网站建设龙岗
  • 青岛做网站哪里好沈阳做网站公司
  • 危险网站怎么解除wordpress显示选项