This is the 29th day of my participation in the More Text Challenge. For more details, see more Text Challenge

1, the preface

In microservice development, there are usually two types of invocation between services: Feign and RestTemplate, but in practice, especially Feign, there are various limitations and limitations, such as HTTP request mode, return type and so on, which can sometimes make you feel awkward. In microservice projects, invocation between services is very common and frequent, and performance is not very good.

In order to solve the above problems, repeated comparison, the final invocation between services to adopt the gRPC way, its remarkable characteristic is high performance (communication using Netty), through the proto file definition of the interface is very clear and flexible. This article mainly on the gRPC in the Spring Cloud project to use the actual combat.

GRPC related basic knowledge can refer to the previous article gRPC use.

2. Use of gRPC in Spring Cloud

As you can see from the previous article on gRPC usage, gRPC is a bit of a struggle to use directly, so it is necessary to use some open source frameworks. GRPC uses the open source project GRPC-Spring-boot-starter in the Spring Cloud to facilitate the development of applications in the Spring Cloud project.

(

Grpc-spring-boot-starter has some problems, but integrating with the Sping Cloud project is already quite high and is a good choice. If you have the time, energy, or need to develop on a source code basis.

)

The following uses an actual demo to illustrate the grPC-spring-boot-starter application.

2.1 the characteristics of

  • Use @grpcService to automatically create and run a gRPC service embedded in a Spring-boot application
  • Automatically create and manage your clients using @grpcclient
  • Support for Spring Cloud (register with Consul or Eureka and get gRPC server information)
  • Spring Sleuth supports link tracing
  • Global interceptors or single interceptors are supported for the server and client
  • Support Spring ws-security
  • Supporting metric (Micrometer/ACTUATOR)

(Look at the above features to see why you chose this open source project)

2.2 use the DEMO

2.2.1 Defining the gRPC interface

Declare the data model and RPC interface service based on Protobuf.

Create a common word module project spring-boot-grPC-common, used to define the GRPC interface (PROto), easy to use GRPC server and client. For example helloworld.proto(SRC \main\proto\helloworld.proto) :

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.xcbeyond.springboot.grpc.lib";
option java_outer_classname = "HelloWorldProto";

// The greeting service definition.
service Simple {
    // Sends a greeting
    rpc SayHello (HelloRequest) returns (HelloReply) {
    }
}

// The request message containing the user's name.
message HelloRequest {
    string name = 1;
}

// The response message containing the greetings
message HelloReply {
    string message = 1;
}
Copy the code

The proto command can be converted to the corresponding language code to generate Java code, or the Maven plug-in can be automatically generated at compile time. Using the Mavent plugin, you can add the following dependencies to pom.xml:

<build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>${os.plugin.version}</version> </extension> </extensions> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>${protobuf.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>Copy the code

When you compile a package in Maven, you will see Java classes automatically generated from Proto in the Target. (You can ignore errors that may occur during compilation.)

2.2.2 gRPC Server

Maven depends on:

<dependency> <groupId>net.devh</groupId> <artifactId>grpc-server-spring-boot-starter</artifactId> < version > 2.2.1. RELEASE < / version > < / dependency >Copy the code

To implement the gRPC Server business logic, use the annotation @grpcService to define the gRPC server, as shown below:

package com.xcbeyond.springboot.grpc.server.service;

import com.xcbeyond.springboot.grpc.lib.HelloReply;
import com.xcbeyond.springboot.grpc.lib.HelloRequest;
import com.xcbeyond.springboot.grpc.lib.SimpleGrpc;
import io.grpc.stub.StreamObserver;
import net.devh.boot.grpc.server.service.GrpcService;

/**
 * @Auther: xcbeyond
 * @Date: 2019/3/6 18:15
 */
@GrpcService
public class GrpcServerService extends SimpleGrpc.SimpleImplBase {
    @Override
    public void sayHello(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
        System.out.println("GrpcServerService...");
        HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + request.getName()).build();
        responseObserver.onNext(reply);
        responseObserver.onCompleted();
    }
}
Copy the code

Application. Yml configuration:

The default listening host is 0.0.0.0, and the default listening port is 9090. Setting it to 0 automatically allocates unused ports.

grpc:
  server:
    port: 0
Copy the code

2.2.3 gRPC client

Maven depends on:

<dependency> <groupId>net.devh</groupId> <artifactId>grpc-client-spring-boot-starter</artifactId> < version > 2.2.1. RELEASE < / version > < / dependency >Copy the code

Use the @grpcclient annotation to invoke the server interface

HelloReply response = simpleBlockingStub.sayHello(HelloRequest.newBuilder().setName(name).build());
Copy the code

Make a request directly to the server, just like invoking the local interface.

package com.xcbeyond.springboot.grpc.client.service; import com.xcbeyond.springboot.grpc.lib.HelloReply; import com.xcbeyond.springboot.grpc.lib.HelloRequest; import com.xcbeyond.springboot.grpc.lib.SimpleGrpc.SimpleBlockingStub; import io.grpc.StatusRuntimeException; import net.devh.boot.grpc.client.inject.GrpcClient; import org.springframework.stereotype.Service; /** * @Auther: xcbeyond * @Date: 2019/3/7 09:10 */ @Service public class GrpcClientService { @GrpcClient("spring-boot-grpc-server") private SimpleBlockingStub simpleBlockingStub; public String sendMessage(String name) { try { HelloReply response = simpleBlockingStub.sayHello(HelloRequest.newBuilder().setName(name).build()); return response.getMessage(); } catch (final StatusRuntimeException e) { return "FAILED with " + e.getStatus().getCode(); }}}Copy the code

Application. Yml configuration:

Spring-boot-grpc-server: indicates the application name of the server. In combination with the Spring Cloud Eureka registry, the IP address of the server is found through the name of the server, and the communication is actually netty communication. grpc: client: spring-boot-grpc-server: enableKeepAlive: true keepAliveWithoutCalls: true negotiationType: plaintextCopy the code

Please refer to the complete code of this demoGithub.com/xcbeyond/sp…