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

做网站必须得ipc支部网站建设

做网站必须得ipc,支部网站建设,六安网新闻,政务公开与网站建设的矛盾初步了解 Protobuf 和 gRpc Protocol Buffers Protocol Buffers#xff08;又称protobuf#xff09;是谷歌的语言无关、平台无关、可扩展的机制#xff0c;用于序列化结构化数据。您可以在protobuf的文档中了解更多关于它的信息。 ProtoBuf 的定义 ProtoBuf是将类的定义…初步了解 Protobuf 和 gRpc Protocol Buffers Protocol Buffers又称protobuf是谷歌的语言无关、平台无关、可扩展的机制用于序列化结构化数据。您可以在protobuf的文档中了解更多关于它的信息。 ProtoBuf 的定义 ProtoBuf是将类的定义使用.protobuf文件进行描述。 //语言版本 syntax proto3; option java_multiple_files true; option java_package io.grpc.examples.helloworld; option java_outer_classname HelloWorldProto; package helloworld;//数据体User 属性username message User{string username 1; }通过protoc可以将.proto文件编译成需要的语言文件。本文以java语言示例。 protoc --proto_pathsrc --java_outbuild/gen src/foo.proto详细用法可以查看protoc的使用。 gRpc 在 gRPC 中客户端应用程序可以像本地对象一样直接调用不同机器上的服务器应用程序上的方法从而使您可以更轻松地创建分布式应用程序和服务。与许多 RPC 系统一样gRPC 基于定义服务的思想指定可以远程调用的方法及其参数和返回类型。在服务器端服务器实现这个接口并运行一个gRPC服务器来处理客户端调用。在客户端客户端有一个存根在某些语言中简称为客户端它提供与服务器相同的方法。(类似Android AIDL使用方式)。 gRPC 客户端和服务器可以在各种环境中运行并相互通信从 Google 内部的服务器到您自己的桌面并且可以用 gRPC 支持的任何语言编写。例如您可以使用 Java 轻松创建 gRPC 服务器并使用 Go、Python 或 Ruby 编写客户端。 默认情况下gRPC 使用Protocol BuffersGoogle 成熟的开源机制用于序列化结构化数据尽管它可以与 JSON 等其他数据格式一起使用。 【以上来自grpc简介】 Grpc 的定义 grpc 也定义在.proto文件如下 // The greeting service definition. service Greeter {// Sends a greetingrpc SayHello (HelloRequest) returns (HelloReply) {} }// The request message containing the name and user. message HelloRequest {string name 1;User user 2; }// The response message containing the greetings message HelloReply {string message 1; }grpc 的编译仍然需要编译.proto文件。所以离不开protoc, 还需要另外的插件配合编译。例如 protoc --pluginprotoc-gen-grpc-java \--grpc-java_out$OUTPUT_FILE --proto_path$DIR_OF_PROTO_FILE $PROTO_FILEProtobuf 插件 使用命令行对于大部分人来说是比较不方便的我们也可以使用Gradle插件来完成.proto的编译。下面分享一下完整的build.gradle文件。 plugins {// Provide convenience executables for trying out the examples.id applicationid com.google.protobuf version 0.9.4// Generate IntelliJ IDEAs .idea .iml project filesid idea }repositories {maven { // The google mirror is less flaky than mavenCentral()url https://maven-central.storage-download.googleapis.com/maven2/}mavenCentral()mavenLocal() }java {sourceCompatibility JavaVersion.VERSION_1_8targetCompatibility JavaVersion.VERSION_1_8 }// IMPORTANT: You probably want the non-SNAPSHOT version of gRPC. Make sure you // are looking at a tagged version of the example and not master!// Feel free to delete the comment at the next line. It is just for safely // updating the version in our release process. def grpcVersion 1.61.0 // CURRENT_GRPC_VERSION def protobufVersion 3.25.1 def protocVersion protobufVersion //https://github.com/protocolbuffers/protobuf //https://github.com/grpc/grpc-java dependencies {implementation io.grpc:grpc-protobuf:${grpcVersion}implementation io.grpc:grpc-services:${grpcVersion}implementation io.grpc:grpc-stub:${grpcVersion}compileOnly org.apache.tomcat:annotations-api:6.0.53// examples/advanced need this for JsonFormatimplementation com.google.protobuf:protobuf-java-util:${protobufVersion}runtimeOnly io.grpc:grpc-netty-shaded:${grpcVersion}testImplementation io.grpc:grpc-testing:${grpcVersion}testImplementation io.grpc:grpc-inprocess:${grpcVersion}testImplementation junit:junit:4.13.2testImplementation org.mockito:mockito-core:4.4.0 } //https://github.com/google/protobuf-gradle-plugin protobuf {protoc { artifact com.google.protobuf:protoc:${protocVersion} }plugins {grpc { artifact io.grpc:protoc-gen-grpc-java:${grpcVersion} }}generateProtoTasks {all()*.plugins { grpc {} }} }// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code. sourceSets {main {java {srcDirs build/generated/source/proto/main/grpcsrcDirs build/generated/source/proto/main/java}} } 通过build项目或者执行generateProto的task就可以生成java文件。 上面的.proto文件生成目录如下 下面提供java中的使用所有代码只有server和client两个文件。如下 /** Copyright 2015 The gRPC Authors** Licensed 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.*/package io.grpc.examples.helloworld;import io.grpc.Grpc; import io.grpc.InsecureServerCredentials; import io.grpc.Server; import io.grpc.stub.StreamObserver; import java.io.IOException; import java.util.concurrent.TimeUnit; import java.util.logging.Logger;/*** Server that manages startup/shutdown of a {code Greeter} server.*/ public class HelloWorldServer {private static final Logger logger Logger.getLogger(HelloWorldServer.class.getName());private Server server;private void start() throws IOException {/* The port on which the server should run */int port 50051;server Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create()).addService(new GreeterImpl()).build().start();logger.info(Server started, listening on port);Runtime.getRuntime().addShutdownHook(new Thread() {Overridepublic void run() {// Use stderr here since the logger may have been reset by its JVM shutdown hook.System.err.println(*** shutting down gRPC server since JVM is shutting down);try {HelloWorldServer.this.stop();} catch (InterruptedException e) {e.printStackTrace(System.err);}System.err.println(*** server shut down);}});}private void stop() throws InterruptedException {if (server ! null) {server.shutdown().awaitTermination(30, TimeUnit.SECONDS);}}/*** Await termination on the main thread since the grpc library uses daemon threads.*/private void blockUntilShutdown() throws InterruptedException {if (server ! null) {server.awaitTermination();}}/*** Main launches the server from the command line.*/public static void main(String[] args) throws IOException, InterruptedException {final HelloWorldServer server new HelloWorldServer();server.start();server.blockUntilShutdown();}static class GreeterImpl extends GreeterGrpc.GreeterImplBase {Overridepublic void sayHello(HelloRequest req, StreamObserverHelloReply responseObserver) {System.out.println(sayHello:req.getUser().getUsername()req.getName());HelloReply reply HelloReply.newBuilder().setMessage(Hello req.getUser().getUsername()req.getName()).build();responseObserver.onNext(reply);responseObserver.onCompleted();}} }/** Copyright 2015 The gRPC Authors** Licensed 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.*/package io.grpc.examples.helloworld;import io.grpc.Channel; import io.grpc.Grpc; import io.grpc.InsecureChannelCredentials; import io.grpc.ManagedChannel; import io.grpc.StatusRuntimeException;import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger;/*** A simple client that requests a greeting from the {link HelloWorldServer}.*/ public class HelloWorldClient {private static final Logger logger Logger.getLogger(HelloWorldClient.class.getName());private final GreeterGrpc.GreeterBlockingStub blockingStub;/*** Construct client for accessing HelloWorld server using the existing channel.*/public HelloWorldClient(Channel channel) {// channel here is a Channel, not a ManagedChannel, so it is not this codes responsibility to// shut it down.// Passing Channels to code makes code easier to test and makes it easier to reuse Channels.blockingStub GreeterGrpc.newBlockingStub(channel);}/*** Say hello to server.*/public void greet(String name) {logger.info(Will try to greet name ...);HelloRequest request HelloRequest.newBuilder().setName(name).setUser(User.newBuilder().setUsername(test ).build()).build();HelloReply response;try {response blockingStub.sayHello(request);} catch (StatusRuntimeException e) {logger.log(Level.WARNING, RPC failed: {0}, e.getStatus());return;}logger.info(Greeting: response.getMessage());}/*** Greet server. If provided, the first element of {code args} is the name to use in the* greeting. The second argument is the target server.*/public static void main(String[] args) throws Exception {String user world;// Access a service running on the local machine on port 50051String target localhost:50051;// Allow passing in the user and target strings as command line argumentsif (args.length 0) {if (--help.equals(args[0])) {System.err.println(Usage: [name [target]]);System.err.println();System.err.println( name The name you wish to be greeted by. Defaults to user);System.err.println( target The server to connect to. Defaults to target);System.exit(1);}user args[0];}if (args.length 1) {target args[1];}// Create a communication channel to the server, known as a Channel. Channels are thread-safe// and reusable. It is common to create channels at the beginning of your application and reuse// them until the application shuts down.//// For the example we use plaintext insecure credentials to avoid needing TLS certificates. To// use TLS, use TlsChannelCredentials instead.ManagedChannel channel Grpc.newChannelBuilder(target, InsecureChannelCredentials.create()).build();try {HelloWorldClient client new HelloWorldClient(channel);client.greet(user);} finally {// ManagedChannels use resources like threads and TCP connections. To prevent leaking these// resources the channel should be shut down when it will no longer be used. If it may be used// again leave it running.channel.shutdownNow().awaitTermination(5, TimeUnit.SECONDS);}} }
http://www.zqtcl.cn/news/636594/

相关文章:

  • 做杂志模板下载网站网站开发产品经理招聘
  • 深圳网站创建公司小程序代理怎么样
  • 所以免费爱做网站营销网站优化推广
  • 莆田网站制作设计东莞营销专业网站建设
  • joomla建站教程北京做网站ezhixi
  • 自己可以做拼单网站吗建设企业网站有哪些
  • 张掖北京网站建设新闻事件
  • 济南网站建设(力选聚搜网络)wordpress文章中写代码
  • 网站后台忘记密码买购网十大品牌网
  • 360免费建站网页链接石家庄建设局网站怎么打不开
  • 东莞网站建做俄罗斯外贸的网站
  • 基于vue.js旅游网站开发网络营销的主要形式有建设网站
  • 医院网站建设要素国外电商网站
  • 甘肃两学一做网站可以制作h5的网站
  • 微信公众号手机网站开发wordpress 集赞系统
  • 怎么做会员积分网站免费ppt模板下载有哪些
  • 六安网站开发如何用微信小程序做网站
  • 华为云自助建站好不好seo技巧分享
  • 做淘宝客的网站wordpress+附件丢失
  • 苏州市城乡建设局网站首页做动漫网站的素材
  • 济南网站建设系统介绍服务网站开发费属于研发支出吗
  • 网站建设方案进行工期安排Wix做的网站在国内打不开
  • 网站后台后缀名qq免费申请账号
  • seo网站优化代码静态网站可以做哪些
  • 网页素材及网站架构制作个人单页网站模板
  • 微小店网站建设价格建设网站设备预算
  • 电子商城网站开发公司泰州网络营销
  • 网站建设公司利润分配一些常用的网站
  • 鄂尔多斯做网站的公司北京企业网站设计报价
  • 南宁关键词网站排名wordpress付免签插件