1. 程式人生 > >使用thrift實現了Java伺服器和nodejs客戶端之間的跨平臺通訊

使用thrift實現了Java伺服器和nodejs客戶端之間的跨平臺通訊

注意,我們要把exe和tar檔案都下載下來,exe用來編譯你的thrift中間語言,而tar解壓後,我們可以看到csharp,php,java,js等多種開發語言的例項程式碼,對我們很有幫助的,下載最新版,其中包含lib檔案庫,包含各種語言需要的類庫;

下載之後,我們把exe檔案可放在C盤,建個Thrift目錄,把它放入,然後可以配置一下環境變數;將thrift的資料夾路徑新增到path變數中;

3. 編寫thrift檔案

首先編寫:tutorial.thrift
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

# Thrift Tutorial
# Mark Slee (
[email protected]
) # # This file aims to teach you how to use Thrift, in a .thrift file. Neato. The # first thing to notice is that .thrift files support standard shell comments. # This lets you make your thrift file executable and include your Thrift build # step on the top line. And you can place comments like this anywhere you like. # # Before running this file, you will need to have installed the thrift compiler # into /usr/local/bin. /** * The first thing to know about are types. The available types in Thrift are: * * bool Boolean, one byte * byte Signed byte * i16 Signed 16-bit integer * i32 Signed 32-bit integer * i64 Signed 64-bit integer * double 64-bit floating point value * string String * binary Blob (byte array) * map<t1,t2> Map from one type to another * list<t1> Ordered list of one type * set<t1> Set of unique elements of one type * * Did you also notice that Thrift supports C style comments? */ // Just in case you were wondering... yes. We support simple C comments too. /** * Thrift files can reference other Thrift files to include common struct * and service definitions. These are found using the current path, or by * searching relative to any paths specified with the -I compiler flag. * * Included objects are accessed using the name of the .thrift file as a * prefix. i.e. shared.SharedObject */ include "shared.thrift" /** * Thrift files can namespace, package, or prefix their output in various * target languages. */ namespace cpp tutorial namespace d tutorial namespace java tutorial namespace php tutorial namespace perl tutorial namespace haxe tutorial /** * Thrift lets you do typedefs to get pretty names for your types. Standard * C style here. */ typedef i32 MyInteger /** * Thrift also lets you define constants for use across languages. Complex * types and structs are specified using JSON notation. */ const i32 INT32CONSTANT = 9853 const map<string,string> MAPCONSTANT = {'hello':'world', 'goodnight':'moon'} /** * You can define enums, which are just 32 bit integers. Values are optional * and start at 1 if not supplied, C style again. */ enum Operation { ADD = 1, SUBTRACT = 2, MULTIPLY = 3, DIVIDE = 4 } /** * Structs are the basic complex data structures. They are comprised of fields * which each have an integer identifier, a type, a symbolic name, and an * optional default value. * * Fields can be declared "optional", which ensures they will not be included * in the serialized output if they aren't set. Note that this requires some * manual management in some languages. */ struct Work { 1: i32 num1 = 0, 2: i32 num2, 3: Operation op, 4: optional string comment, } /** * Structs can also be exceptions, if they are nasty. */ exception InvalidOperation { 1: i32 whatOp, 2: string why } /** * Ahh, now onto the cool part, defining a service. Services just need a name * and can optionally inherit from another service using the extends keyword. */ service Calculator extends shared.SharedService { /** * A method definition looks like C code. It has a return type, arguments, * and optionally a list of exceptions that it may throw. Note that argument * lists and exception lists are specified using the exact same syntax as * field lists in struct or exception definitions. */ void ping(), i32 add(1:i32 num1, 2:i32 num2), i32 calculate(1:i32 logid, 2:Work w) throws (1:InvalidOperation ouch), /** * This method has a oneway modifier. That means the client only makes * a request and does not listen for any response at all. Oneway methods * must be void. */ oneway void zip() } /** * That just about covers the basics. Take a look in the test/ folder for more * detailed examples. After you run this file, your generated code shows up * in folders with names gen-<language>. The generated code isn't too scary * to look at. It even has pretty indentation. */

附上shared.thrift

相關推薦

使用thrift實現Java伺服器nodejs客戶之間跨平臺通訊

注意,我們要把exe和tar檔案都下載下來,exe用來編譯你的thrift中間語言,而tar解壓後,我們可以看到csharp,php,java,js等多種開發語言的例項程式碼,對我們很有幫助的,下載最新版,其中包含lib檔案庫,包含各種語言需要的類庫; 下載之後,我們把exe檔案可放在C盤,建個Thri

Java基於UDP實現伺服器客戶之間通訊

UDPServer.java package com.zh.socket; import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import

應用java多執行緒實現伺服器與多客戶之間通訊

package test.concurrent.socket; import java.io.*; import java.net.Socket; /** * Created by dong on 15-6-22. * 伺服器端執行緒處理類 */ public class ServerThread

對於socket網路程式設計,搗鼓一週多,終於實現電腦為服務,學習板為客戶之間通訊

最開始是網上找到相關的客戶端,和服務端 的程式,然後試著去理解,但是這個過程很難過,因為從來沒有接觸過的,但是沒辦法,只能硬著頭皮啃,問老師,問學長,網上看視訊。終於是吧他啃下來了。 服務端的程式碼: #include <unistd.h> #include

Centos7安裝搭建NTP伺服器NTP客戶同步時間

NTP簡介: NTP是網路時間協議(Network Time Protocol),它是用來同步網路中各個計算機的時間的協議。 在計算機的世界裡,時間非常地重要 例如:對於火箭發射這種科研活動,對時間的統一性和準確性要求就非常地高,是按照A這臺計算機的時間,還是按照B這臺計算機的時間?

java 服務實現服務客戶之間通訊以及客戶之間通訊

此服務端是利用socket進行通訊 ServerSocket serversocket = new ServerSocket(12333); 使用本機的12333埠進行通訊,可以自己選擇 一般選擇10000以後的埠不會與其他服務衝突 import j

Cloudera Manager安裝之時間伺服器時間客戶(Ubuntu14.04)(二)

   第二步:  找一臺機器作為時間伺服器   我這裡,放到ubuntucmbigdata1這臺機器!   注意,之前是已經做了叢集時間同步了。   在ubuntu系統裡,跟centos系統有點出入。   需要安裝 [email protected

Cloudera Manager安裝之時間伺服器時間客戶(二)

前期部落格 帶大家手把手讀cloudera官網  https://www.cloudera.com/documentation/enterprise/5-3-x.html    說明:這些是可以點選進入,就直接可以下載得到電子檔了。我這裡不多贅述。

centos6.5下如何搭建一個ftp伺服器使用客戶

一:搭建伺服器: 1.下載vsftpd軟體: 2.以root身份進入/etc/vsftpd目錄: 3.修改vsftpd.conf這個配置檔案(千萬記住不要在等號兩邊留空格) 4.重啟vsftpd服務: 至此伺服器端就搭建完畢了,就這麼簡單 二:客戶端使用: ce

linux下安裝vpn伺服器vpn客戶

最近公司要求在一臺內網的伺服器上安裝lnmp的環境,然後就想到了在伺服器上安裝vpn,等安裝完環境再刪除. 參考 嘗試過很多工具,後來發現GitHub下有一個指令碼可以一鍵安裝,而且還有中文教程 安裝vpn 伺服器 如果是預設安裝,在

Linux系統實現udp模式下兩個客戶間的通訊

需要用到的知識:Linux C網路程式設計相關知識以及簡單的多執行緒操作 兩個客戶端的程式碼是一樣的,只要把埠號換一下,一個客戶端中的本機埠MY_PORT相當於另一個客戶端的目的埠DEST_PORT #include<stdio.h> #include<unistd.h

tcp 多路複用實現 兩個客戶之間通訊

/******************************* 伺服器端  ****************************/ #include <stdio.h> #include <stdlib.h> #include <neti

Delphi服務PHP客戶通過Socket通訊

在開始之前看下效果 PHP頁面作為客戶端傳送請求給作為服務端的Delphi應用程式 PHP客戶端頁面開啟如下 Delphi服務端應用程式開啟如下 每次PHP頁面重新整理一下,Delphi的文字框都顯示"我上線啦!"做的很粗糙,但是似乎可以作為某種功能的demo PHP

多個客戶之間通訊

伺服器 #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <sys/socket.h> #include &

java實現伺服器客戶之間的檔案傳輸

實現思路 一、客戶端發文件:首先建立和伺服器的連線,然後我們通過IO流來實現資料的傳輸,步驟: 1、通過伺服器的IP地址和埠號實現和伺服器的連線(這裡不要忘記先開伺服器哦) 2、獲取本地的檔案的地址,建立java和檔案的連線。 3、獲取檔案輸入流和資料輸出流(注意:這裡的輸入和輸出都是指

C#實現伺服器客戶之間通訊

TCP  套接字程式設計 伺服器端實現步驟: 1、使用Socket類建立套接字。 2、利用Bind方法將建立的套接字繫結到指定的地址結構。 3、利用Listen方法設定套接字為監聽模式,使得伺服器進入被動開啟狀態。 4、接受客戶端的連線請求。 5、接收、應答客戶端的資料請求

在Android上實現SSL握手,實現伺服器客戶之間Socket互動

public class MySSLSocket extends Activity {      private static final int SERVER_PORT = 50030;//埠號      private static final String SERVER_IP = "218.206.17

linux下用多執行緒實現socket伺服器客戶的非同步通訊

前面介紹了用select函式來實現socket的非同步收發資料,但是select函式也有一些缺陷,要使socket能持續地通訊,select必須不停地檢測,這樣程序就會一直阻塞在這裡,限制了功能的擴充套件,這裡我們用多執行緒的方式,另建立兩個執行緒用來發送/接收

golang thrift 原始碼分析,伺服器客戶究竟是如何工作的

首先編寫thrift檔案(rpcserver.thrift),執行thrift --gen go rpcserver.thrift,生成程式碼 namespace go rpc service RpcService { string SayHi(1: s

Java review--NIO例項:實現服務客戶的簡單通訊

客戶端程式碼: package nio; import java.io.IOException; import java.net.InetSocketAddress; import java.net.ServerSocket; import java.nio.Byte