git clone https://github.com/grpc/grpc-go.git $GOPATH/src/google.golang.org/grpc
git clone https://github.com/golang/net.git $GOPATH/src/golang.org/x/net
git clone https://github.com/golang/text.git $GOPATH/src/golang.org/x/text
go get -u github.com/golang/protobuf/{proto,protoc-gen-go}
git clone https://github.com/google/go-genproto.git $GOPATH/src/google.golang.org/genproto
经过以上步骤,已经将go需要的grpc和protobuf等支持下载到了GOPATH的src目录下边,接下来就是进入GOPATH目录的src目录下边,进行install
cd $GOPATH/src/
go install google.golang.org/grpc
在install之后会在GOPATH目录下边生成bin和pkg两个包,里边放的就是grpc和protobuf install之后的内容。
$ protoc --go_out=. ./helloworld.proto
$ protoc --go_out=plugins=grpc:. ./helloworld.proto
边两个命令是不一样的。第一个只是生成了proto序列化,反序列化代码的文件。 第二个则还增加服务器和客户端通讯、实现的公共库代码。因此如果是写客户端和服务端通信,要用第二个编译方式,如果只是作为序列化和反序列化的工具,用第一个就可以了。
Java在编译grpc的时候可以有两种方法
- 通过maven配置grpc,protobuf相关设置,
- 直接通过protoc.exe进行编译(文末链接)
使用maven配置的话,就可以通过maven编译proto文件,生成对应的代码,
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-nettyartifactId>
<version>1.6.1version>
dependency>
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-protobufartifactId>
<version>1.0.0version>
dependency>
<dependency>
<groupId>io.grpcgroupId>
<artifactId>grpc-stubartifactId>
<version>1.0.0version>
dependency>
<plugin>
<groupId>org.xolstice.maven.pluginsgroupId>
<artifactId>protobuf-maven-pluginartifactId>
<configuration>
<protocArtifact>com.google.protobuf:protoc:3.0.0:exe:${os.detected.classifier}protocArtifact>
<pluginId>grpc-javapluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.0:exe:${os.detected.classifier}pluginArtifact>
configuration>
<executions>
<execution>
<goals>
<goal>compilegoal>
<goal>compile-customgoal>
goals>
execution>
executions>
plugin>
本文中介绍的是GO和Java进行通信,其中Go是服务器端,Java是客户端。
.proto文件时不同端之间商定的交互协议,因此两端都要知晓协议内容。(关于protobuf应用,可以看官网,或网上看例子。)
syntax="proto3";
package msg;
message RouterInfo{
string ip=1;
string domain=2;
string applicationName=3;
string description=4;
}
message ResultToApp{
string userName=1;
string password=2;
}
service MessageChannel{
rpc registerServer(RouterInfo)returns(ResultToApp){}
}
package msg
import (
"net"
"log"
"fmt"
"google.golang.org/grpc"
"golang.org/x/net/context"
)
const (
port = "15000"
)
type server struct {
}
// 实现服务端的方法
func (s *server) RegisterServer(ctx context.Context, in *RouterInfo) (*ResultToApp, error) {
fmt.Print(*in)
name := "test"
pas := "123"
return &ResultToApp{UserName: name, Password: pas}, nil
}
func ListenFromJava() {
fmt.Println("接收到消息")
//监听接口
serverLis, err := net.Listen("tcp", "127.0.0.1:"+port)
if err != nil {
log.Fatal("failed to listen: %v", err)
}
s := grpc.NewServer()
//注册服务
RegisterMessageChannelServer(s, &server{})
if err := s.Serve(serverLis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
package com.photo.spider;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.concurrent.TimeUnit;
import msg.MessageChannelGrpc;
import msg.RegisterToServer;
/**
* Created by SunGuiyong on 2018/7/25.
*/
public class SendMsgToGo {
private final ManagedChannel channel;
private final MessageChannelGrpc.MessageChannelBlockingStub localStub;
public SendMsgToGo(String host, int port) {
channel = ManagedChannelBuilder.forAddress(host, port)
.usePlaintext(true)
.build();
//初始化
localStub = MessageChannelGrpc.newBlockingStub(channel);
}
public void shutdown() throws InterruptedException {
channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
}
public void greet(RegisterToServer.RouterInfo routerInfo) {
RegisterToServer.ResultToApp result = localStub.registerServer(routerInfo);
System.out.println("name :" + result.getUserName());
}
public static void main(String[] args) throws InterruptedException {
SendMsgToGo client = new SendMsgToGo("127.0.0.1", 15000);
RegisterToServer.RouterInfo p = RegisterToServer.RouterInfo.newBuilder()
.setIp("127.0.0.1")
.setDomain("www.test.cn")
.setApplicationName("这是一个测试")
.setDescription("这是一个Go与Java通过protobuf通信的测试").build();
client.greet(p);
client.shutdown();
}
}
protoc.exe工具链接:
https://download.csdn.net/download/believe__dream/10577322