中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁(yè) > news >正文

合肥高端網(wǎng)站開(kāi)發(fā)做一個(gè)企業(yè)網(wǎng)站大概需要多少錢

合肥高端網(wǎng)站開(kāi)發(fā),做一個(gè)企業(yè)網(wǎng)站大概需要多少錢,自己可以做招聘的網(wǎng)站嗎,網(wǎng)頁(yè)制作教程第三版趙豐年pdf文章目錄 1、Protocol Buffers定義接口1.1、編寫接口服務(wù)1.2、Protobuf基礎(chǔ)數(shù)據(jù)類型 2、服務(wù)器端實(shí)現(xiàn)2.1、生成gRPC服務(wù)類2.2、Java服務(wù)器端實(shí)現(xiàn) 3、java、go、php客戶端實(shí)現(xiàn)3.1、Java客戶端實(shí)現(xiàn)3.2、Go客戶端實(shí)現(xiàn)3.3、PHP客戶端實(shí)現(xiàn) 本文例子是在Window平臺(tái)測(cè)試,Ja…

文章目錄

    • 1、Protocol Buffers定義接口
      • 1.1、編寫接口服務(wù)
      • 1.2、Protobuf基礎(chǔ)數(shù)據(jù)類型
    • 2、服務(wù)器端實(shí)現(xiàn)
      • 2.1、生成gRPC服務(wù)類
      • 2.2、Java服務(wù)器端實(shí)現(xiàn)
    • 3、java、go、php客戶端實(shí)現(xiàn)
      • 3.1、Java客戶端實(shí)現(xiàn)
      • 3.2、Go客戶端實(shí)現(xiàn)
      • 3.3、PHP客戶端實(shí)現(xiàn)

本文例子是在Window平臺(tái)測(cè)試,Java編寫的gRPC服務(wù)器端,同時(shí)使用Java、Go、PHP編寫客戶端調(diào)用。

1、Protocol Buffers定義接口

1.1、編寫接口服務(wù)

// 定義protocol buffers版本(proto3)
syntax = "proto3";// 定義包名,避免協(xié)議消息類型之間的命名沖突。
package helloworld;// 定義java格式
option java_multiple_files = true;
option java_package = "com.penngo.grpc";
option java_outer_classname = "HelloWorldProto";
option objc_class_prefix = "HLW";// 服務(wù)接口定義
service Greeter { // 方法1定義rpc SayHello (HelloRequest) returns (HelloReply) {}// 方法2定義rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}// HelloRequest消息類型格式定義,
message HelloRequest {// name為定義字段名,1為消息傳輸中的字段編號(hào),使用后,不應(yīng)該改編號(hào)。string name = 1;
}// HelloReply消息類型格式定義,
message HelloReply {// message為定義字段名,1為消息傳輸中的字段編號(hào),使用后,不應(yīng)該改編號(hào)。string message = 1;
}

1.2、Protobuf基礎(chǔ)數(shù)據(jù)類型

    <th>C++</th><th>Java/Kotlin</th><th>Python</th><th>Go</th><th>Ruby</th><th>C#</th><th>PHP</th><th>Dart</th>
</tr>
<tr><td>double</td><td>double</td><td>double</td><td>float</td><td>float64</td><td>Float</td><td>double</td><td>float</td><td>double</td>
</tr>
<tr><td>float</td><td>float</td><td>float</td><td>float</td><td>float32</td><td>Float</td><td>float</td><td>float</td><td>double</td>
</tr>
<tr><td>int32</td><td>int32</td><td>int</td><td>int</td><td>int32</td><td>Fixnum or Bignum (as required)</td><td>int</td><td>integer</td><td>int</td>
</tr>
<tr><td>int64</td><td>int64</td><td>long</td><td>int/long<sup>[4]</sup></td><td>int64</td><td>Bignum</td><td>long</td><td>integer/string<sup>[6]</sup></td><td>Int64</td>
</tr>
<tr><td>uint32</td><td>uint32</td><td>int<sup>[2]</sup></td><td>int/long<sup>[4]</sup></td><td>uint32</td><td>Fixnum or Bignum (as required)</td><td>uint</td><td>integer</td><td>int</td>
</tr>
<tr><td>uint64</td><td>uint64</td><td>long<sup>[2]</sup></td><td>int/long<sup>[4]</sup></td><td>uint64</td><td>Bignum</td><td>ulong</td><td>integer/string<sup>[6]</sup></td><td>Int64</td>
</tr>
<tr><td>sint32</td><td>int32</td><td>int</td><td>int</td><td>int32</td><td>Fixnum or Bignum (as required)</td><td>int</td><td>integer</td><td>int</td>
</tr>
<tr><td>sint64</td><td>int64</td><td>long</td><td>int/long<sup>[4]</sup></td><td>int64</td><td>Bignum</td><td>long</td><td>integer/string<sup>[6]</sup></td><td>Int64</td>
</tr>
<tr><td>fixed32</td><td>uint32</td><td>int<sup>[2]</sup></td><td>int/long<sup>[4]</sup></td><td>uint32</td><td>Fixnum or Bignum (as required)</td><td>uint</td><td>integer</td><td>int</td>
</tr>
<tr><td>fixed64</td><td>uint64</td><td>long<sup>[2]</sup></td><td>int/long<sup>[4]</sup></td><td>uint64</td><td>Bignum</td><td>ulong</td><td>integer/string<sup>[6]</sup></td><td>Int64</td>
</tr>
<tr><td>sfixed32</td><td>int32</td><td>int</td><td>int</td><td>int32</td><td>Fixnum or Bignum (as required)</td><td>int</td><td>integer</td><td>int</td>
</tr>
<tr><td>sfixed64</td><td>int64</td><td>long</td><td>int/long<sup>[4]</sup></td><td>int64</td><td>Bignum</td><td>long</td><td>integer/string<sup>[6]</sup></td><td>Int64</td>
</tr>
<tr><td>bool</td><td>bool</td><td>boolean</td><td>bool</td><td>bool</td><td>TrueClass/FalseClass</td><td>bool</td><td>boolean</td><td>bool</td>
</tr>
<tr><td>string</td><td>string</td><td>String</td><td>str/unicode<sup>[5]</sup></td><td>string</td><td>String (UTF-8)</td><td>string</td><td>string</td><td>String</td>
</tr>
<tr><td>bytes</td><td>string</td><td>ByteString</td><td>str (Python 2)<br>bytes (Python 3)</td><td>[]byte</td><td>String (ASCII-8BIT)</td><td>ByteString</td><td>string</td><td>List<int></int></td>
</tr>
</tbody>
.proto

https://protobuf.dev/programming-guides/proto3/

2、服務(wù)器端實(shí)現(xiàn)

服務(wù)器端實(shí)例使用java編寫

2.1、生成gRPC服務(wù)類

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.penngo</groupId><artifactId>grpc-helloworld</artifactId><version>1.0</version><properties><maven.compiler.source>11</maven.compiler.source><maven.compiler.target>11</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>io.grpc</groupId><artifactId>grpc-netty-shaded</artifactId><version>1.59.0</version><scope>runtime</scope></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-protobuf</artifactId><version>1.59.0</version></dependency><dependency><groupId>io.grpc</groupId><artifactId>grpc-stub</artifactId><version>1.59.0</version></dependency><dependency> <!-- necessary for Java 9+ --><groupId>org.apache.tomcat</groupId><artifactId>annotations-api</artifactId><version>6.0.53</version><scope>provided</scope></dependency></dependencies><build><extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.7.1</version></extension></extensions><plugins><!-- grpc代碼生成插件 --><plugin><groupId>org.xolstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>0.6.1</version><configuration><protocArtifact>com.google.protobuf:protoc:3.24.0:exe:${os.detected.classifier}</protocArtifact><pluginId>grpc-java</pluginId><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.59.0:exe:${os.detected.classifier}</pluginArtifact></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin></plugins></build><repositories><repository><id>alimaven</id><name>Maven Aliyun Mirror</name><url>https://maven.aliyun.com/repository/central</url></repository></repositories><pluginRepositories><pluginRepository><id>public</id><name>aliyun nexus</name><url>https://maven.aliyun.com/nexus/content/groups/public/</url><releases><enabled>true</enabled></releases><snapshots><enabled>false</enabled></snapshots></pluginRepository></pluginRepositories>
</project>

命令行下執(zhí)行g(shù)RPC的java代碼

mvn compile

自動(dòng)掃描代碼目中src/main/proto/helloworld.proto下的proto文件,自動(dòng)生成gRPC相關(guān)代碼到target/generated-sources/protobuf目錄下。
在這里插入圖片描述

2.2、Java服務(wù)器端實(shí)現(xiàn)

HelloServer.java

package com.penngo;import com.penngo.grpc.GreeterGrpc;
import com.penngo.grpc.HelloReply;
import com.penngo.grpc.HelloRequest;
import io.grpc.Grpc;
import io.grpc.InsecureServerCredentials;
import io.grpc.Server;
import io.grpc.stub.StreamObserver;
import java.io.IOException;public class HelloServer {public static void main(String[] args) throws IOException, InterruptedException {int port = 50051;Server server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create()).addService(new GreeterImpl()).build().start();Runtime.getRuntime().addShutdownHook(new Thread(()->{System.err.println("*** shutting down gRPC server since JVM is shutting down");stopServer(server);System.err.println("*** server shut down");}));server.awaitTermination();}private static void stopServer(Server server) {if (server != null) {server.shutdown();}}static class GreeterImpl extends GreeterGrpc.GreeterImplBase {@Overridepublic void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {System.out.println("收到客戶端消息:" + req.getName());String msg = "我是Java Server";System.out.println("回復(fù)客戶端消息:" + msg);HelloReply reply = HelloReply.newBuilder().setMessage("你好!" + msg).build();responseObserver.onNext(reply);responseObserver.onCompleted();}@Overridepublic void sayHelloAgain(HelloRequest req, StreamObserver<HelloReply> responseObserver) {System.out.println("再次收到客戶端消息:" + req.getName());String msg = "我是Java Server2";System.out.println("再次回復(fù)客戶端消息:" + msg);HelloReply reply = HelloReply.newBuilder().setMessage(msg).build();responseObserver.onNext(reply);responseObserver.onCompleted();}}
}

https://github.com/protocolbuffers/protobuf/releases

3、java、go、php客戶端實(shí)現(xiàn)

3.1、Java客戶端實(shí)現(xiàn)

HelloClient.java

package com.penngo;import com.penngo.grpc.GreeterGrpc;
import com.penngo.grpc.HelloReply;
import com.penngo.grpc.HelloRequest;
import io.grpc.Grpc;
import io.grpc.InsecureChannelCredentials;
import io.grpc.ManagedChannel;import java.util.concurrent.TimeUnit;public class HelloClient {public static void main(String[] args) throws InterruptedException {String host = "localhost";int port = 50051;ManagedChannel managedChannel = Grpc.newChannelBuilderForAddress(host, port, InsecureChannelCredentials.create()).build();GreeterGrpc.GreeterBlockingStub blockingStub = GreeterGrpc.newBlockingStub(managedChannel);String msg1 = "我是java client";System.out.println("向服務(wù)器端發(fā)送消息:" + msg1);HelloRequest helloRequest1 = HelloRequest.newBuilder().setName(msg1).build();HelloReply reply1 = blockingStub.sayHello(helloRequest1);System.out.println("收到服務(wù)器端消息:" + reply1.getMessage());String msg2 = "我是java client2";System.out.println("再次向服務(wù)器端發(fā)送消息:" + msg2);HelloRequest helloRequest2 = HelloRequest.newBuilder().setName(msg2).build();HelloReply reply2 = blockingStub.sayHelloAgain(helloRequest2);System.out.println("再次收到服務(wù)器端消息:" + reply2.getMessage());managedChannel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);}}

3.2、Go客戶端實(shí)現(xiàn)

Go的代碼生成需要使用protoc.exe來(lái)編譯helloworld.proto服務(wù)文件,生成對(duì)應(yīng)的服務(wù)調(diào)用代碼
下載地址:https://github.com/protocolbuffers/protobuf/releases,當(dāng)前最新版本為protoc-25.1-win64.zip
解壓到目錄D:\Program Files\protoc-25.1-win64\bin,需要把這個(gè)目錄添加到環(huán)境變量PATH當(dāng)中。

安裝protocol編譯器的Go插件

$ go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
$ go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2

執(zhí)行下邊命令生成Go的gRPC代碼

protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative helloworld/helloworld.proto

在這里插入圖片描述

編寫客戶端實(shí)現(xiàn)代碼

package mainimport ("context""flag""google.golang.org/grpc""google.golang.org/grpc/credentials/insecure"pb "grpctest/helloworld""log""time"
)var (addr = flag.String("addr", "localhost:50051", "the address to connect to")
)func main() {flag.Parse()// Set up a connection to the server.conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()))if err != nil {log.Fatalf("did not connect: %v", err)}defer conn.Close()c := pb.NewGreeterClient(conn)// Contact the server and print out its response.ctx, cancel := context.WithTimeout(context.Background(), time.Second)defer cancel()msg1 := "我是go client"log.Printf("向服務(wù)器端發(fā)送消息: %s", msg1)r1, err1 := c.SayHello(ctx, &pb.HelloRequest{Name: msg1})if err1 != nil {log.Fatalf("could not greet: %v", err1)}log.Printf("收到服務(wù)器端消息:%s", r1.GetMessage())msg2 := "我是go client2"log.Printf("再次向服務(wù)器端發(fā)送消息: %s", msg1)r2, err2 := c.SayHelloAgain(ctx, &pb.HelloRequest{Name: msg2})if err2 != nil {log.Fatalf("could not greet: %v", err2)}log.Printf("再次收到服務(wù)器端消息: %s", r2.GetMessage())
}

3.3、PHP客戶端實(shí)現(xiàn)

安裝gRPC的PHP擴(kuò)展https://pecl.php.net/package/gRPC
當(dāng)前測(cè)試php版本7.3,下載php_grpc-1.42.0-7.3-nts-vc15-x64.zip
php.ini這個(gè)文件加入

extension=php_grpc.dll

新建composer.json

{"name": "xxs/grpc","require": {"grpc/grpc": "^v1.4.0","google/protobuf": "^v3.3.0"},"autoload":{"psr-4":{"GPBMetadata\\":"GPBMetadata/","Helloworld\\":"Helloworld/"}}
}

在composer.json相同目錄下執(zhí)行命令下載依賴庫(kù)

composer install

安裝grpc的php插件,https://github.com/lifenglsf/grpc_for_windows
解壓復(fù)制到項(xiàng)目下grpc_for_windows/x64/grpc_php_plugin.exe

執(zhí)行命令生成gRPC代碼

protoc --proto_path=. --php_out=. --grpc_out=. --plugin=protoc-gen-grpc=grpc_for_windows/x64/grpc_php_plugin.exe ./helloworld.proto

在這里插入圖片描述

client.php實(shí)現(xiàn)

<?php
require dirname(__FILE__) . '/vendor/autoload.php';
//echo dirname(__FILE__) . '/vendor/autoload.php';
function greet($hostname)
{$client = new Helloworld\GreeterClient($hostname, ['credentials' => Grpc\ChannelCredentials::createInsecure(),]);$request = new Helloworld\HelloRequest();$msg1 = "我是PHP client";$request->setName($msg1);echo "向服務(wù)器端發(fā)送消息: $msg1". PHP_EOL;list($response, $status) = $client->SayHello($request)->wait();if ($status->code !== Grpc\STATUS_OK) {echo "ERROR: " . $status->code . ", " . $status->details . PHP_EOL;exit(1);}echo "收到服務(wù)器端消息:".$response->getMessage() . PHP_EOL;$msg2 = "我是PHP client2";$request->setName($msg2);echo "再次向服務(wù)器端發(fā)送消息: $msg2". PHP_EOL;list($response, $status) = $client->SayHelloAgain($request)->wait();if ($status->code !== Grpc\STATUS_OK) {echo "ERROR: " . $status->code . ", " . $status->details . PHP_EOL;exit(1);}echo "再次收到服務(wù)器端發(fā)送消息:".$response->getMessage() . PHP_EOL;$client->close();
}$hostname = !empty($argv[2]) ? $argv[2] : 'localhost:50051';
greet($hostname);

附件源碼

http://www.risenshineclean.com/news/45140.html

相關(guān)文章:

  • thinkphp 網(wǎng)站根目錄地址免費(fèi)網(wǎng)絡(luò)推廣網(wǎng)站
  • o2o網(wǎng)站建設(shè)流程理發(fā)培訓(xùn)專業(yè)學(xué)校
  • 網(wǎng)站建設(shè)價(jià)值長(zhǎng)沙seo優(yōu)化推廣
  • 沒(méi)有備案的網(wǎng)站 推廣牛排seo
  • 什么網(wǎng)站可以免費(fèi)做視頻的軟件有哪些免費(fèi)學(xué)生網(wǎng)頁(yè)制作成品代碼
  • 網(wǎng)站建設(shè)的目的分析國(guó)內(nèi)新聞最新消息今天簡(jiǎn)短
  • 自己做網(wǎng)站賣閥門網(wǎng)絡(luò)營(yíng)銷方案的制定
  • 找人建個(gè)網(wǎng)站多少錢semantic ui
  • 如何減少網(wǎng)站建設(shè)中的錯(cuò)誤廣東東莞疫情最新情況
  • wdcp裝wordpress502小時(shí)seo
  • 國(guó)內(nèi)b2b電商平臺(tái)seo專業(yè)課程
  • 醫(yī)療網(wǎng)站做藥品是干嘛搜索引擎營(yíng)銷的案例有哪些
  • java網(wǎng)站開(kāi)發(fā)的參考文獻(xiàn)東莞網(wǎng)站優(yōu)化
  • php程序員網(wǎng)站開(kāi)發(fā)免費(fèi)培訓(xùn)課程
  • 專業(yè)seo站長(zhǎng)工具全面查詢網(wǎng)站市場(chǎng)調(diào)研報(bào)告1500字
  • 聊城網(wǎng)站營(yíng)銷簡(jiǎn)述提升關(guān)鍵詞排名的方法
  • 網(wǎng)站建設(shè)模式有哪些方面網(wǎng)絡(luò)推廣怎么找客戶
  • 建設(shè)網(wǎng)站思路青島seo排名公司
  • php網(wǎng)站集成支付寶接口2022百度seo優(yōu)化工具
  • 如何在天氣預(yù)報(bào)網(wǎng)站做引流網(wǎng)站關(guān)鍵詞優(yōu)化排名外包
  • 云主機(jī) 網(wǎng)站嗎網(wǎng)站友鏈查詢?cè)创a
  • 網(wǎng)站閉站保護(hù)網(wǎng)站運(yùn)營(yíng)一個(gè)月多少錢
  • 向國(guó)旗敬禮做時(shí)代新人網(wǎng)站上海自動(dòng)seo
  • 國(guó)外做btc的網(wǎng)站軟文推廣文案
  • 自己做家具展示網(wǎng)站優(yōu)化seo深圳
  • 昆明網(wǎng)站建設(shè)價(jià)目表最新軍事消息
  • 網(wǎng)站建設(shè)師百度百科網(wǎng)絡(luò)輿情監(jiān)測(cè)
  • 石家莊建站網(wǎng)頁(yè)模板百度人工服務(wù)
  • 源碼建站之網(wǎng)站建設(shè)搜索引擎平臺(tái)有哪些軟件
  • 做菠菜網(wǎng)站有沒(méi)有被騙的百度瀏覽器官方網(wǎng)站