grpc implements cross-language (go and java) service communication

Golang microservices in action: Using gRPC to achieve cross-language communication! 

With the development of microservice architecture, more and more companies are beginning to adopt microservice architecture to build distributed systems. In a microservices architecture, communication between services is very important. As an efficient, cross-platform, and cross-language RPC framework, gRPC has become the first choice of more and more enterprises. 

This article will introduce how to use gRPC to achieve cross-language communication. We will implement a service written in Golang, communicate with a client written in Python, and explore some core technical points of the gRPC framework.

Introduction

After multiple services are split, we often have to call each other between services to achieve a certain business function. We know that in the Java field, there are springCloud, Dubbo and other well-encapsulated communication components. But if the services are Cross-language, then these cannot be solved. Today there is a need. I want to realize communication between go service and java service, so I borrowed a lightweight and efficient communication framework from Google - grpc to implement it.

what is grpc

grpc official document

Project requirements

Implement a simple interface that uses Go services to remotely call Java services for integer addition.

Server design steps

  • The server is a maven project. After creating a new project, import the maven coordinates.
  • Create a proto folder in the main directory and the same directory as java to store communication protocols based on http2 and protobuf. Create a new justtest.proto
syntax = "proto3"; //协议版本号

option java_multiple_files = true;
option java_package = "io.grpc.add";
option java_outer_classname = "remote_add_service";

package Test; // 包名

//定义服务
service Greeter {
  //注意:这里是returns 不是return
  rpc RemoteAdd (AddRequest) returns (AddResponse) {}
}
//定义消息类型
message AddRequest {
  int32 num1 = 1;
  int32 num2 = 2;
}
message AddResponse {
  int32 answer = 1;
}
  • Create a new proto file and write the fields specified by the file protocol
  • Use maven install to generate a series of files reversely parsed by protoc
  • Start the server

client design

  • The client project is a golang project. We need to install the protobuf package in advance. There are many tutorials on the Internet. You can download it yourself.
  • It is worth noting that the go service requires some grpc packages. Some of these packages cannot be found on github, so you can download the warehouse of the libraries required by my go. I have put them all in the govendor of the project. In the directory, the packages inside are complete and can be used immediately.
  • Copy the protoc file in the maven project to the proto package in the golang project, then enter this folder directory and use the following instructions to generate a series of reversely parsed go files
 protoc --go-grpc_out=. 你的proto文件名.proto
 protoc --go_out=. 你的proto文件名.proto
  • Create a new client service to listen on a port and call the remote interface service at the same time

Demonstration example

Start the java service on the server side

Insert image description here

The client starts the golang service
client.go file

package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	pb "go_grpc/pb"
	"google.golang.org/grpc"
	"log"
	"net/http"
)

func main() {
	// Set up a connection to the server.
	conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())
	if err != nil {
		log.Fatalf("did not connect: %v", err)
	}
	defer conn.Close()
	client := pb.NewGreeterClient(conn)

	r := gin.Default()
	r.GET("/", func(c *gin.Context) {
		//todo 远程调用整数相加
		req := &pb.AddRequest{Num1: 10,Num2: 10}
		res, err := client.RemoteAdd(c, req)
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{
				"error": err.Error(),
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"result": fmt.Sprint(res.Answer),
		})
	})
	// Run http server
	if err := r.Run(":8052"); err != nil {
		log.Fatalf("could not run server: %v", err)
	}
}

Insert image description here

File code address

The file code is on my git, and the test can pass. You can fetch it and use it. The different branches in it are different services
https://github.com/zxhjames/grpc_demo

Guess you like

Origin blog.csdn.net/waysoflife/article/details/133930409