• SpringBoot + gRPC简单实现


    IDEA插件安装

    在这里插入图片描述

    protobuf协议编写

    在main文件夹下创建proto文件件,并编写protobuf协议文件,server和client端都要编写

    • file.proto
    syntax = "proto3";
    
    package protocol;
    
    option go_package = "protocol";
    option java_package = "com.redick.example.protocol";
    
    message File {
      string name = 1;
      int32 size = 2;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • message.proto
    syntax = "proto3";
    package protocol;
    
    import "file.proto";
    
    option go_package = "protocol";
    option java_multiple_files = true;
    option java_package = "com.redick.example.protocol";
    
    message User {
      reserved 6 to 7;
      reserved "userId2";
      int32 userId = 1;
      string username = 2;
      oneof msg {
        string error = 3;
        int32 code = 4;
      }
      string name = 8;
    
      UserType userType = 9;
      repeated int32 roles = 10;
    
      protocol.File file = 11;
      map hobbys = 12;
    }
    
    enum UserType {
      UNKNOW = 0;
      ADMIN = 1;
      BUSINESS_USER = 2;
    };
    
    service UserService {
      rpc getUser (User) returns (User) {}
      rpc getUsers (User) returns (stream User) {}
      rpc saveUsers (stream User) returns (User) {}
    }
    
    service FileService {
      rpc getFile(User) returns(File) {}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

    maven插件

    • maven依赖
        <dependency>
            <groupId>com.google.protobufgroupId>
            <artifactId>protobuf-javaartifactId>
            <version>3.19.4version>
        dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • maven中protobuf plugin
        <build>
            
            <extensions>
                <extension>
                    <groupId>kr.motd.mavengroupId>
                    <artifactId>os-maven-pluginartifactId>
                    <version>1.6.2version>
                extension>
            extensions>
            <plugins>
                <plugin>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-maven-pluginartifactId>
                    <version>${spring.boot.version}version>
                plugin>
                
                <plugin>
                    <groupId>org.xolstice.maven.pluginsgroupId>
                    <artifactId>protobuf-maven-pluginartifactId>
                    <version>0.6.1version>
                    
                    <configuration>
                        <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}protocArtifact>
                        <pluginId>grpc-javapluginId>
                        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.26.0:exe:${os.detected.classifier}pluginArtifact>
                        
                        
                        <protoSourceRoot>${project.basedir}/src/main/protoprotoSourceRoot>
                        
                        <outputDirectory>${project.basedir}/src/main/java/outputDirectory>
                        <clearOutputDirectory>falseclearOutputDirectory>
                        
                    configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compilegoal>
                                <goal>compile-customgoal>
                            goals>
                        execution>
                    executions>
                plugin>
            plugins>
        build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44

    代码生成

    命令行或idea插件,server和client端都要生成代码

    • 命令行
    mvn clean compile
    
    • 1
    • idea插件

    执行idea中的maven插件

    在这里插入图片描述

    • 生成代码文件如下

    在这里插入图片描述

    代码实现

    • maven依赖
        <properties>
            <maven.compiler.source>8maven.compiler.source>
            <maven.compiler.target>8maven.compiler.target>
            <protobuf.version>3.19.4protobuf.version>
            <grpc.version>1.46.0grpc.version>
            <spring.boot.version>2.6.3spring.boot.version>
            <grpc.starter.version>2.13.1.RELEASEgrpc.starter.version>
        properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starterartifactId>
                <version>${spring.boot.version}version>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-webartifactId>
                <version>${spring.boot.version}version>
            dependency>
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-starter-aopartifactId>
                <version>${spring.boot.version}version>
            dependency>
            <dependency>
                <groupId>net.devhgroupId>
                <artifactId>grpc-server-spring-boot-starterartifactId>
                <version>${grpc.starter.version}version>
            dependency>
            <dependency>
                <groupId>net.devhgroupId>
                <artifactId>grpc-client-spring-boot-starterartifactId>
                <version>2.13.1.RELEASEversion>
            dependency>
            <dependency>
                <groupId>io.grpcgroupId>
                <artifactId>grpc-stubartifactId>
                <version>1.46.0version>
            dependency>
            <dependency>
                <groupId>io.grpcgroupId>
                <artifactId>grpc-protobufartifactId>
                <version>1.46.0version>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
            dependency>
            <dependency>
                <groupId>com.google.protobufgroupId>
                <artifactId>protobuf-javaartifactId>
                <version>${protobuf.version}version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-databindartifactId>
                <version>2.11.4version>
            dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.coregroupId>
                <artifactId>jackson-coreartifactId>
                <version>2.11.4version>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    SpringBoot集成

    服务端代码开发
    • application.yml
    grpc:
      server:
        port: 9090
    
    server:
      port: 8080
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 服务端业务代码
    @GrpcService
    public class UserServiceImpl extends UserServiceGrpc.UserServiceImplBase {
    
        @Override
        public void getUser(User request, StreamObserver<User> responseObserver) {
            User user = User.newBuilder()
                    .setName("response name")
                    .build();
            responseObserver.onNext(user);
            responseObserver.onCompleted();
        }
    
        @Override
        public void getUsers(User request, StreamObserver<User> responseObserver) {
            User user = User.newBuilder()
                    .setName("user1")
                    .build();
            User user2 = User.newBuilder()
                    .setName("user2")
                    .build();
            responseObserver.onNext(user);
            responseObserver.onNext(user2);
    
            responseObserver.onCompleted();
        }
    
        @Override
        public StreamObserver<User> saveUsers(StreamObserver<User> responseObserver) {
            return new StreamObserver<User>() {
                @Override
                public void onNext(User user) {
                }
    
                @Override
                public void onError(Throwable throwable) {
                }
    
                @Override
                public void onCompleted() {
                    User user = User.newBuilder()
                            .setName("saveUsers user1")
                            .build();
                    responseObserver.onNext(user);
                    responseObserver.onCompleted();
                }
            };
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    客户端代码开发
    • application.yml
    server:
      port: 8081
      servlet:
        context-path: /grpc
    
    grpc:
      client:
        userClient:
          negotiationType: PLAINTEXT
          address: static://localhost:9090
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 客户端测试controller
    @RestController
    public class TestController {
    
        @GrpcClient("userClient")
        private UserServiceGrpc.UserServiceBlockingStub userService;
    
        @Autowired
        private GrpcInterceptor grpcInterceptor;
    
        @GetMapping("/getUser")
        public String getUser()     {
            User user = User.newBuilder()
                    .setUserId(100)
                    .putHobbys("pingpong", "play pingpong")
                    .setCode(200)
                    .build();
            Channel channel = ClientInterceptors.intercept(userService.getChannel(), grpcInterceptor);
            userService = UserServiceGrpc.newBlockingStub(channel);
            User u = userService.getUser(user);
            return u.getName();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
  • 相关阅读:
    Ingress:比Service更强大的服务暴露与负载均衡
    Redis持久化、主从与哨兵架构详解
    7.SpringEL使用正则表达式
    css常见布局
    简单好用的 SemVer: 如何命名你的应用版本
    【前端设计模式】之观察者模式
    Vue3 - 异步组件(写法及详细示例)
    关于医疗器械的算法、协议开发(五)
    CSS选择器大全48式
    集Oauth2+Jwt实现单点登录
  • 原文地址:https://blog.csdn.net/qq_31279701/article/details/127629771