provider
和consumer
proto
文件介绍syntax | 指定语言版本 |
---|---|
syntax | 指定语言版本 |
option | 修改配置选项 |
service | 声明一个服务 |
rpc | 声明一个方法 |
resturns | 方法的返回值 |
message | 定义一个消息类型 |
repeated | 数组 |
stream | 用流来交互 |
一个栗子,helloworld.proto
:
syntax = "proto3"; //语法声明
// Greeter 微服务
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
// HelloRequest 请求数据格式
message HelloRequest {
string name = 1;
}
// HelloReply 响应数据格式
message HelloReply {
string message = 1;
}
fast-common-grpc-proto
,用来生成grpc相关接口。依赖如下,这个还配置了build,可以通过编译生成相关接口。<dependencies>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-netty</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-protobuf</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>${os-maven-plugin.version}</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf-maven-plugin.version}</version>
<configuration>
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
helloworld.proto
复制到 java/main/proto
下,注意一定要是proto
文件夹,点击compile
会成生成grpc接口文件GreeterGrpc
、Helloworld
复制到grpc包下面,创建GreeterServer
文件GreeterServer
的代码如下,继承 GreeterGrpc.GreeterImplBase
重写里面的sayHello
方法@Component
public class GreeterServer extends GreeterGrpc.GreeterImplBase implements InitializingBean {
@Value("${gRPC.port}")
private int port;
@Override
public void afterPropertiesSet() throws Exception {
ServerBuilder.forPort(port)
.addService(new GreeterServer())
.build()
.start();
}
@Override
public void sayHello(Helloworld.HelloRequest request,
io.grpc.stub.StreamObserver<Helloworld.HelloReply> responseObserver) {
Helloworld.HelloReply result = Helloworld.HelloReply.newBuilder().setMessage(request.getName()).build();
responseObserver.onNext(result);
responseObserver.onCompleted();
}
}
provider
和consumer
在根目录下运行命令
protoc -I helloworld/ helloworld/helloworld.proto --go_out=plugins=grpc:helloworld
service.go
,代码如下package main
import (
"context"
pb "go-grpc/helloworld"
"log"
"net"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"
)
const (
port = ":50051"
)
type server struct{} //服务对象
// SayHello 实现服务的接口 在proto中定义的所有服务都是接口
func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {
return &pb.HelloReply{Message: "Hello " + in.Name}, nil
}
func main() {
lis, err := net.Listen("tcp", port)
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer() //起一个服务
pb.RegisterGreeterServer(s, &server{})
// 注册反射服务 这个服务是CLI使用的 跟服务本身没有关系
reflection.Register(s)
if err := s.Serve(lis); err != nil {
log.Fatalf("failed to serve: %v", err)
}
}
client.go
,代码如下:package main
import (
"context"
"log"
"os"
"time"
pb "go-grpc/helloworld"
"google.golang.org/grpc"
)
const (
address = "localhost:50051"
defaultName = "world"
)
func main() {
//建立链接
conn, err := grpc.Dial(address, grpc.WithInsecure())
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.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
// 1秒的上下文
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not greet: %v", err)
}
log.Printf("Greeting: %s", r.Message)
}
接下来测试服务之间的调用
FastCommonGrpcExampleApplication
这个类,访问http://localhost:8080/hello
client.go
会看到输出GreeterServer
,启动FastCommonGrpcExampleApplication
,启动service.go
,访问http://localhost:8080/hello
client.go
会看到输出go地址:https://github.com/fafeidou/go-grpc
java地址:https://github.com/fafeidou/fast-cloud-nacos
java grpc: https://www.cnblogs.com/gutousu/p/9951956.html
入门及服务端创建和调用原理: https://www.cnblogs.com/wxlevel/p/9154246.html#auto_id_24
grpc(3):使用 golang 开发 grpc 服务端和客户端 : https://blog.csdn.net/freewebsys/article/details/59483427
gRPC基于Golang和Java的简单实现: https://www.jianshu.com/p/21d5d7624951