java如何发送电报,gRPC实现跨语言的微服务间通信 -- 精通外语的电报员与煲电报粥的小怪兽...

通过Spring Boot创建Java项目,pom.xml中加入如下依赖

org.springframework.boot

spring-boot-starter

org.springframework.boot

spring-boot-starter-test

test

io.grpc

grpc-netty-shaded

1.21.0

io.grpc

grpc-protobuf

1.21.0

io.grpc

grpc-stub

1.21.0

kr.motd.maven

os-maven-plugin

1.5.0.Final

org.xolstice.maven.plugins

protobuf-maven-plugin

0.5.1

com.google.protobuf:protoc:3.7.1:exe:${os.detected.classifier}

grpc-java

io.grpc:protoc-gen-grpc-java:1.21.0:exe:${os.detected.classifier}

src/main/java

false

compile

compile-custom

org.springframework.boot

spring-boot-maven-plugin

定义IDL文件

syntax = "proto3";

option java_multiple_files = true;

option java_package = "net.changjinglu.proto";

option java_outer_classname = "TelegraphProto";

package telegraph;

// The greeting service definition.

service TelegraphService {

// Sends a greeting

rpc SayLove (LoveRequest) returns (LoveReply) {}

}

// The request message containing the user's name.

message LoveRequest {

string message = 1;

}

// The response message containing the greetings

message LoveReply {

string message = 1;

}

编译生成IDL定义的Java服务接口,相关代码会生成到配置对应的路径下

mvn clean install

bVbtm3C?w=680&h=426

实现IDL定义的Java服务接口

public class TelegraphGreeterImpl extends TelegraphServiceGrpc.TelegraphServiceImplBase {

@Override

public void sayLove(LoveRequest request, StreamObserver responseObserver) {

System.out.println("收到Node小怪兽的消息:"+request.getMessage());

responseObserver.onNext(LoveReply.newBuilder().setMessage("I Love U Too").build());

//结束

responseObserver.onCompleted();

}

}

编写并启动Java服务端

public class GrpcServer {

/** GRPC 服务端 */

private Server server;

public static void main(String[] args) throws IOException, InterruptedException {

GrpcServer grpcService = new GrpcServer();

grpcService.start();

System.out.println("GRPC 服务端启动成功");

//GRPC 服务端需要手动阻塞线程

grpcService.waitTermination();

}

private void start() throws IOException {

//绑定接口、启动服务

this.server = ServerBuilder.forPort(8899)

.addService(new TelegraphGreeterImpl())

.build()

.start();

System.out.println("server start!");

//这里是为了防止jvm关闭了,但是tcp还没有关闭的情况

Runtime.getRuntime().addShutdownHook(new Thread(()->{

System.out.println("关闭jvm");

GrpcServer.this.stop();

}));

}

private void stop() {

if (this.server != null) {

this.server.shutdown();

}

}

private void waitTermination() throws InterruptedException {

if (this.server != null) {

server.awaitTermination();

}

}

}

bVbtm5H?w=890&h=444

编写并启动Nodejs客户端,客户端使用相同的IDL

var PROTO_FILE_PATH = '/Users/wenyuan/Nodejs/grpc/proto/telegraph.proto';

var grpc = require('grpc');

var grpcService = grpc.load(PROTO_FILE_PATH).telegraph;

function main() {

var stub = new grpcService.TelegraphService('localhost:8899',grpc.credentials.createInsecure());

stub.sayLove({message:'今晚的月色真美'},function (error, result) {

console.log('收到Java小怪兽的消息: ' + result.message);

});

}

main();

Java服务端收到消息并回复

bVbtm6z?w=1378&h=472

Nodejs客户端收到Java服务端的回复

bVbtm8Q?w=1052&h=236

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值