protobuf简单测试应用

protobuf是google推出的一种数据交换协议,比较适合应用于底层服务交互,nodejs提供protobufjs包的实现,下面是一个简单的测试demo:

首先是.proto文件:

package desktop;
syntax = "proto3";
message helloworld
{
    required int32 id = 1;      // id
    required string str = 2;    // str
    optional int32 opt = 3;     // optional field
}

然后是一个测试的nodejs程序,主要是加载.proto文件->实例化message->message buffer化->将buffer保存进log文件:

var ProtoBuf = require("protobufjs");
var PORT = 33333;
var HOST = ‘127.0.0.1‘;

var fs = require(‘fs‘);

var root = ProtoBuf.loadSync("./desktop.helloworld.proto"),
    HelloWorld = root.lookupType("desktop.helloworld");

var hw = {
    ‘id‘: 101,
    ‘str‘: ‘helloworld!‘
}

var errMsg = HelloWorld.verify(hw)
console.log(errMsg)
if (errMsg) {
    throw errMsg
} else {
    var message = HelloWorld.create(hw)
    var buffer = HelloWorld.encode(message).finish()
    var message = HelloWorld.decode(buffer)
    console.log(message)
    fs.writeFile(‘./test.log‘, buffer, err => {
        if (!err) {
            console.log(‘Done!‘)
        } else {
            console.err(err)
        }
    })
}

具体效果:

protobuf相比传统的xml、json,数据传输更加紧凑,二进制协议也更加高效,非常适合于各种服务间的数据交换,目前各大主流语言基本都有具体实现。

时间: 2024-08-24 16:03:44

protobuf简单测试应用的相关文章

centos7搭建ELK Cluster集群日志分析平台(四):简单测试

续之前安装好的ELK集群 各主机:es-1 ~ es-3 :192.168.1.21/22/23 logstash: 192.168.1.24 kibana: 192.168.1.25 测试机:client: 192.168.1.26 在测试机上安装并启动filebeat 1. 下载filebeat  ~]# wget https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-5.4.0-linux-x86_64.tar.gz

Redis安装及简单测试

题目链接:11645 - Bits 题意:给定一个数字n,要求0-n的二进制形式下,连续11的个数. 思路:和 UVA 11038 这题类似,枚举中间,然后处理两边的情况. 不过本题最大的答案会超过longlong,要用高精度,不过借鉴http://www.cnblogs.com/TO-Asia/p/3214706.html这个人的方法,直接用两个数字来保存一个数字,这样能保存到2个longlong的长度,就足够存放这题的答案了. 代码: #include <stdio.h> #include

简单测试java - properties

import java.io.BufferedInputStream;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.util.Enumeration;import java.util.Properties;public class TestPorperty {    /**     * 写入-properties文件:     *     

基于MySQL元数据的Hive的安装和简单测试

引言: Hive是一种强大的数据仓库查询语言,类似SQL,本文将介绍如何搭建Hive的开发测试环境. 1. 什么是Hive? hive是基于Hadoop的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供简单的sql查询功能,可以将sql语句转换为MapReduce任务进行运行. 其优点是学习成本低,可以通过类SQL语句快速实现简单的MapReduce统计,不必开发专门的MapReduce应用,十分适合数据仓库的统计分析. 2.  按照Hive的准备条件 2.1  Hadoop集

Windows IO 性能简单测试

转自:http://bbs.csdn.net/topics/360111289, 有改动. #include <windows.h>#include <stdio.h>#include <process.h>#include<memory>#pragma comment(lib,"ws2_32.lib")ULONGLONG g_nReadCounts=0,g_nWriteCounts=0,g_nOtherCounts=0,g_nReads

构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(37)-文章发布系统④-百万级数据和千万级数据简单测试

我想测试EF在一百万条数据下的显示时间!这分数据应该有很多同学想要,看看EF的性能! 服务器 现在来向SQL2008R2插入1000000条数据吧 declare @i int; set @i=0; while @i<1000000 begin INSERT INTO [AppDB].[dbo].[MIS_Article] ([Id] ,[ChannelId] ,[CategoryId] ,[Title] ,[ImgUrl] ,[BodyContent] ,[Sort] ,[Click] ,[C

简单测试用控件.

props.push('{"prop":"x","min":-100,"max":100}'); props.push('{"prop":"y","min":-100,"max":100}'); props.push('{"prop":"scaleX","min":-1,"max

memcached window版 下载安装,简单测试

官网: http://www.memcached.org/ 只有tag格式的,搞了好久都没找到windows版的,还有很多uri找不开,google的都打不开,中国政府就是威武. 下载地址: http://jehiah.cz/projects/memcached-win32/files/memcached-1.2.1-win32.zip 这个用讯雷可以下载下来! 错误: 通过cmd命令行进入到D:\webEve\memcached(下载后的解压目录) 运行 memcached.exe -d in

Android Socket简单测试

这里是将pc作为server,设备作为客户端. Server端代码: public static final String SERVERIP = "192.168.0.2"; public static final int SERVERPORT = 51706; public void run() { try { System.out.println("S: Connecting..."); ServerSocket serverSocket = new Serve