SpringBoot integrates high-performance microservice framework gRPC

As a mainstream microservice framework, Spring Boot has a mature community ecosystem. The market is widely used. For your convenience, we have compiled a series of introductory manuals for quick integration of common middleware based on spring boot, involving common open source components such as RPC, cache, message queue, sub-library and sub-table, registration center, distributed configuration, etc. There are about Dozens of articles will be released one after another. Interested students can follow & collect

1. Introduction

In gRPC, client applications can directly call methods of server applications on a different machine just like calling local objects, making it easier for us to create distributed applications and services.

gRPC is designed based on the HTTP/2 standard and brings features such as bidirectional streaming, flow control, header compression, and multiplexing requests on a single TCP connection. These features make it perform better on mobile devices and save power and space.

There are currently many excellent open source projects that use gRPC as a communication method, such as Kubernetes, SkyWalking, istio, etc. It is even said that Dubbo has begun to provide support for the gRPC protocol since version 2.7.5.

gRPC mainly provides two new RPC calling methods:

  • Ordinary RPC calling method, that is, request-response mode.

  • Based on HTTP/2.0 streaming calling method.

gRPC service calls support synchronous and asynchronous methods, as well as ordinary RPC and streaming modes, which can meet business needs to the greatest extent.

The streaming mode can make full use of the multiplexing function of the HTTP/2.0 protocol to realize parallel bidirectional data transmission on an HTTP link, effectively solving the one-way data transmission problem of HTTP/1.X and greatly reducing the number of HTTP connections. In this case, the performance of a single link can be fully utilized, which is comparable to the traditional RPC private long connection protocol: fewer links, higher performance.

Picture

The network I/O communication of gRPC is built based on Netty. The bottom layer of service calls is uniformly asynchronous. The synchronous calls are encapsulated by the upper layer on the basis of asynchronous. Therefore, gRPC’s asynchronousization is relatively thorough, which is very helpful in improving the throughput and reliability of I/O-intensive services.

Netty adopts the multiplexed Reactor thread model: based on Linux’s epoll and Selector, one I/O thread can process hundreds or thousands of links in parallel, solving the problem of traditional synchronous I/O communication thread expansion. NIO solves the asynchronous problem at the communication level, which is not necessarily related to the asynchronous problem of service calls.

Application scenarios:

In the early days of the company, in order to meet the rapid iteration of business, technology selection was random, and we often encountered development in multiple languages, such as java, python, php, .net, etc. to build different business systems. Now considering the upgrade of platform technology, some basic functions need to be unified and a number of microservice centers (such as user center and authority center) need to be built. Based on this background, we can consider using gRPC when making technology selection.

gRPC implementation steps:

  • Define a service and specify its methods that can be called remotely (including parameters and return types)

  • Implement this interface on the server side and run a gRPC server to handle client requests

  • Implement a stub Stub on the client to initiate remote method calls

    Picture

gRPC clients and servers can run and interact in multiple languages and environments! We can easily create a gRPC server in Java and a gRPC client using Java, Go, Python, and Ruby to access it.

2. proto interface specification

Add the following dependencies in pom.xml:

<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>
  • Introduce grpc-protobuf dependency and use Protobuf as the serialization library.

  • Introduce grpc-stub dependency and use gRPC Stub as the client.

Add maven dependencies

<build>
    <extensions>
        <extension>
            <groupId>kr.motd.maven</groupId>
            <artifactId>os-maven-plugin</artifactId>
            <version>1.5.0.Final</version>
        </extension>
    </extensions>
    <plugins>
        <plugin>
            <groupId>org.xolstice.maven.plugins</groupId>
            <artifactId>protobuf-maven-plugin</artifactId>
            <version>0.5.1</version>
            <configuration>
                <protocArtifact>com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}</protocArtifact>
                <pluginId>grpc-java</pluginId>
                <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.15.0:exe:${os.detected.classifier}</pluginArtifact>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>compile</goal>
                        <goal>compile-custom</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>
  • Introduce the os-maven-plugin plug-in to obtain parameters from the OS system. Because it is needed to obtain the os.detected.classifier parameter from the OS system.

  • Introduce the protobuf-maven-plugin plug-in to generate Service and from the protobuf file in the proto directory >Message class.

Define proto interface specification

service UserService {
    rpc query (UserRequest) returns (UserResponse);
}

message UserRequest {
    string name = 1;

}
message UserResponse {
    string name = 1;
    int32 age = 2;
    string address = 3;
}

Click the “compile” button of IDEA to compile the spring-boot-bulking-grpc-proto project and execute the protobuf-maven-plugin plug-in to generate it. The result is shown below:

Picture

3. Server-side implementation

Define annotation classes for scanning Grpc related interface services

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface GrpcService {
}

Interface implementation class

@GrpcService
public class UserService extends UserServiceGrpc.UserServiceImplBase {

    @Override
    public void query(UserRequest request, StreamObserver<UserResponse> responseObserver) {
        System.out.println("Parameters received by UserService, name: " + request.getName());

        UserResponse response = UserResponse.newBuilder().setName("Micro Technology").setAge(30).setAddress("Shanghai").build();
        responseObserver.onNext(response);
        responseObserver.onCompleted();
    }

}

Start the grpc server, listen to port 9091, and add the interface implementation class defined by proto

@Component
public class ServiceManager {
    private Server server;
    private int grpcServerPort = 9091;
    public void loadService(Map<String, Object> grpcServiceBeanMap) throws IOException, InterruptedException {
        ServerBuilder serverBuilder = ServerBuilder.forPort(grpcServerPort);
        // Use annotation scanning to add services
        for (Object bean : grpcServiceBeanMap.values()) {
            serverBuilder.addService((BindableService) bean);
            System.out.println(bean.getClass().getSimpleName() + " is regist in Spring Boot!");

        }
        server = serverBuilder.build().start();

        System.out.println("grpc server is started at " + grpcServerPort);

        //Add a hook to shut down the Server when the JVM process exits
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.err.println("*** shutting down gRPC server since JVM is shutting down");
                if (server != null) {
                    server.shutdown();
                }
                System.err.println("*** server shut down!!!");
            }
        });
        server.awaitTermination();
    }
}

Server side started successfully

Picture

4. Client call

Define the Stub instance of the interface, used to initiate remote service calls

@Configuration
public class GrpcServiceConfig {
    @Bean
    public ManagedChannel getChannel() {
        ManagedChannel channel = ManagedChannelBuilder.forAddress("localhost", 9091)
                .usePlaintext()
                .build();
        return channel;
    }
    @Bean
    public HelloServiceGrpc.HelloServiceBlockingStub getStub1(ManagedChannel channel) {
        return HelloServiceGrpc.newBlockingStub(channel);
    }
    @Bean
    public UserServiceGrpc.UserServiceBlockingStub getStub2(ManagedChannel channel) {
        return UserServiceGrpc.newBlockingStub(channel);
    }
}

Restful interface call, visit: http://localhost:8098/query

@RequestMapping("/query")
public String query() {
    UserRequest request = UserRequest.newBuilder()
            .setName("Microtechnology")
            .build();
    UserResponse userResponse = userServiceBlockingStub.query(request);
    String result = String.format("name:%s , age:%s , address:%s ", userResponse.getName(), userResponse.getAge(), userResponse.getAddress());
    System.out.println(result);
    return result;
}

5. Starter component available out of the box

The gRPC community currently does not provide a Spring Boot Starter library to simplify our configuration of gRPC. However, some masters in China have already open sourced one.

Address: https://github.com/yidongnan/grpc-spring-boot-starter

Features:

  • In the spring boot application, automatically configure and run an embedded gRPC service through @GrpcService

  • Use @GrpcClient to automatically create and manage your gRPC Channels and stubs

  • Support Spring Cloud (register services with Consul or Eureka or Nacos and obtain gRPC service information)

  • Support Spring Sleuth for link tracking (brave-instrumentation-grpc needs to be introduced separately)

  • Supports setting global interceptors or individual interceptors for server and client respectively.

  • Support Spring-Security

  • Support metric (based on micrometer / actuator)

  • Also works with (non-shaded) grpc-netty

6. Project source code address

https://github.com/aalansehaiyang/spring-boot-bulking

Three modules:
spring-boot-bulking-grpc-proto
spring-boot-bulking-grpc-client
spring-boot-bulking-grpc-server

Previous recommendations

  • Still using Mybatis? Spring Data JPA can increase your development efficiency several times!

  • Spring Boot integrates ElasticSearch to achieve high-performance search

  • Framework extension: Annotation RPC Consumer attribute dynamic injection

  • N ways to implement Taobao order automatic confirmation of receipt, killing interviewers instantly

  • In-depth analysis of the core architecture design of coupons

  • Inventory deduction plan for a fresh food e-commerce platform

  • I was shocked and calculated the profit account of the coupon!

  • How to design a high-performance flash sale system

  • How to achieve data synchronization between different systems through Binlog

  • How to design e-commerce coupons?

  • A single MySQL cannot support so many concurrent requests, what should we do?

  • Let’s talk about e-commerce promotion business

  • How does DDD solve complex business expansion problems?

  • To troubleshoot FGC issues in online services, just read this article!

  • Best practices for springboot + aop + Lua distributed current limiting

We are keen on collecting high concurrency, system architecture, microservices, message middleware, RPC framework, high-performance cache, search, distributed data framework, distributed collaborative services, distributed configuration center, middle-end architecture, domain-driven design, system Technical knowledge such as monitoring and system stability.

Picture

You are welcome to scan the ↑↑↑ QR code to join us. In the group, have in-depth exchanges, grow together, and make progress together! There are roads in the mountain of books, and diligence is the path, and there is no limit to the sea of learning, and hard work is the boat.

Follow the official account, reply “中台” in the background, and download PDF learning materials

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 138154 people are learning the system