4

Java实现gRPC服务

 2 years ago
source link: https://perkins4j2.github.io/posts/24127/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Java实现gRPC服务

发表于

2020-08-17

| 分类于 架构

| 阅读次数: 77

本文字数: 4.8k

|

阅读时长 ≈ 4 分钟

gRPC由GOOGLE开发,是一款语言中立、平台中立、开源的远程过程调用(RPC)系统。

syntax = "proto3";

package com.example.grpc;

option java_multiple_files = true;

enum Sentiment {
HAPPY = 0;
SLEEPY = 1;
ANGRY = 2;
}

message HelloRequest {
string name = 1;
int32 age = 2;
repeated string hobbies = 3;
map<string, string> bagOfTricks = 4;
Sentiment sentiment = 5;
}

message HelloResponse {
string greeting = 1;
}

// 4. service, unary request/response
service GreetingService {
rpc greeting(HelloRequest) returns (HelloResponse);
}

Maven

pb和gRPC包

    <dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.10.0</version>
</dependency>

<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty-shaded</artifactId>
<version>1.31.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>1.31.0</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>1.31.0</version>
</dependency>
</dependencies>
<build>
<defaultGoal>package</defaultGoal>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.6.2</version>
</extension>
</extensions>

<plugins>
<!-- protobuf 编译组件 -->
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.1</version>
<extensions>true</extensions>
<configuration>
<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>
<protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
<protocArtifact>com.google.protobuf:protoc:3.5.1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.20.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

Server

public class MyGrpcServer {
static public void main(String [] args) throws IOException, InterruptedException {
Server server = ServerBuilder.forPort(8080)
.addService(new GreetingServiceImpl()).build();

System.out.println("Starting server...");
server.start();
System.out.println("Server started!");
server.awaitTermination();
}

public static class GreetingServiceImpl extends GreetingServiceGrpc.GreetingServiceImplBase {
@Override
public void greeting(HelloRequest request, StreamObserver<HelloResponse> responseObserver) {
System.out.println(request);

String greeting = "Hello there, " + request.getName();

HelloResponse response = HelloResponse.newBuilder().setGreeting(greeting).build();

responseObserver.onNext(response);
responseObserver.onCompleted();
}
}
}

Client

public class MyGrpcClient {
public static void main(String[] args) throws InterruptedException {
ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 8080)
.usePlaintext(true)
.build();

GreetingServiceGrpc.GreetingServiceBlockingStub stub =
GreetingServiceGrpc.newBlockingStub(channel);

HelloResponse helloResponse = stub.greeting(
HelloRequest.newBuilder()
.setName("Ray")
.setAge(18)
.setSentiment(Sentiment.HAPPY)
.build());

System.out.println(helloResponse);

channel.shutdown();
}
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK