• springboot 使用 GRPC


    说明:

    GRPC是什么?

    首先我们知道 RPC是远程过程调用。

    而GRPC是RPC的一种实现。

    那么为什么要用GRPC呢?

    因为它支持跨语言的开发,换句话说,大家都用过FeignRPC,尤其在spring cloud中。

    然而它只支持java语言,而作为微服务,可能有很多其他的服务不是java开发的。因此需要满足这个需求,就需要一个跨语言的RPC,所以就会考虑使用GRPC

    好了,下面进入正题

    直接上代码。

    我们做一个Service和一个Client 进行交互。

    一,Service端

    1,POM

    注意:

    grpc-spring-boot-starter

    os-maven-plugin

    protobuf-maven-plugin

    
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.5.6
             
        
        com.leadtrans
        report
        1.6.0
        report
        Demo project for Spring Boot
        
            11
            2020.0.4
            
            2.3.2
            1.6.0
            0.5.1
        
        
    
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
            
                org.springframework.boot
                spring-boot-starter-web
            
    
            
            
                com.alibaba
                fastjson
                1.2.78
            
    
            
            
                org.lognet
                grpc-spring-boot-starter
                ${grpc-spring-boot-starter.version}
            
    
        
    
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    ${spring-cloud.version}
                    pom
                    import
                
            
        
        
            
                
                
                    kr.motd.maven
                    os-maven-plugin
                    ${os-maven-plugin.version}
                
            
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                    
                        
                            
                                repackage
                            
                        
                    
                
                
                    org.apache.maven.plugins
                    maven-surefire-plugin
                    
                        true
                    
                
                
                
                    org.xolstice.maven.plugins
                    protobuf-maven-plugin
                    ${protobuf-maven-plugin.version}
                    
                        com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}
                        grpc-java
                        io.grpc:protoc-gen-grpc-java:1.11.0:exe:${os.detected.classifier}
                        ${project.build.sourceDirectory}
                        false
                    
                    
                        
                            
                                compile
                                compile-custom
                            
                        
                    
                
            
        
    
    
    
    • 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
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115

    2,创建Proto

    PS:Proto文件夹,必须和JAVA同级!

    HelloWorld.protp

    PS:请使用 option java_package=“xxxx”,

    而不是 package xxxx

    package xxxx 会导致你引用其他包时,会报错

    syntax = "proto3";
    
    option java_multiple_files = true;
    option java_package="com.example.test.helloworld";
    
    //请求
    message Request {
      double num1 = 1;
      double num2 = 2;
      OperateType opType = 3;
    }
    
    //操作类型
    enum OperateType {
      Addition = 0;
      Division = 1;
      Multiplication = 2;
      Subtraction = 3;
    }
    message Response {
      double result = 1;
    }
    
    //定义服务
    service Operate {
      rpc Calculate (Request) returns (Response);
    }
    
    • 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

    3,执行命令,生成文件

    mvn clean install

    或者点击

    可以看到生成了文件:

    4,实现接口

    为了简单演示,这里就直接设置Response.Result=2

    OperateImpl

    @GRpcService
    public class OperateImpl extends OperateGrpc.OperateImplBase {
        @Override
        public void calculate(Request request,
                              StreamObserver responseObserver) {
            Response response=Response.newBuilder().setResult(2).build();
            System.out.println(response);
            responseObserver.onNext(response);
            responseObserver.onCompleted();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    二,Client端

    1,POM(同Service端)

    
    
        4.0.0
        
            org.springframework.boot
            spring-boot-starter-parent
            2.5.6
             
        
        com.example
        test1
        1.0
        test1
        Demo project for Spring Boot
        
            11
            2021.0.0
            2.3.2
            1.6.0
            0.5.1
        
        
            
                org.springframework.boot
                spring-boot-starter
            
    
            
                org.springframework.boot
                spring-boot-starter-test
                test
            
            
            
                org.lognet
                grpc-spring-boot-starter
                ${grpc-spring-boot-starter.version}
            
        
    
        
            
                
                
                    kr.motd.maven
                    os-maven-plugin
                    ${os-maven-plugin.version}
                
            
            
                
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                
                
                
                    org.xolstice.maven.plugins
                    protobuf-maven-plugin
                    ${protobuf-maven-plugin.version}
                    
                        com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}
                        grpc-java
                        io.grpc:protoc-gen-grpc-java:1.11.0:exe:${os.detected.classifier}
                        ${project.build.sourceDirectory}
                        false
                    
                    
                        
                            
                                compile
                                compile-custom
                            
                        
                    
                
            
        
    
    
    
    • 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
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81

    2,拷贝Proto

    和java同级目录

    3,执行命令

    mvn clean install

    4,调用方法

    CalculateService

    其中 main方法,是测试方法。

    GRPC启动的默认端口是6565,在main中设置了。

    public class CalculateService {
        private final ManagedChannel channel;
        private final OperateGrpc.OperateBlockingStub blockingStub;
    
        private CalculateService(ManagedChannel channel) {
            this.channel = channel;
            blockingStub = OperateGrpc.newBlockingStub(channel);
        }
    
        public CalculateService(String host, int port) {
            this(ManagedChannelBuilder.forAddress(host, port)
                    .usePlaintext()
                    .build());
        }
    
        public void shutdown() throws InterruptedException {
            channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
        }
    
        public float operate(float num1, float num2, OperateType operateType) {
            Request request = Request.newBuilder().setNum1(num1).setNum2(num2).setOpType(operateType).build();
            Response response = blockingStub.calculate(request);
            return (float) response.getResult();
        }
    
        public static void main(String[] args) {
            try {
                CalculateService service = new CalculateService("localhost", 6565);
                System.out.println(service.operate(100, 0, OperateType.Division));
                service.shutdown();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    
    }
    
    • 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

    三,结果

    先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦

  • 相关阅读:
    Spring依赖注入的三种方式
    Monaco Editor编辑器
    百趣代谢组学资讯:4篇经典案例助力科研不费力,均IF>12
    混淆技术研究笔记(二)yGuard入门
    设计模式 - 原型模式
    数图可视化品类空间管理系统入编《零售门店数字化赋能专项报告(2024年)》
    南开大学漏洞报送证书【文尾有福利】
    DSP Trick:向量长度估算
    当你的公司突然开始大量的裁员,被留下的你,真的准备好面对以后了吗?
    CDC Schemes
  • 原文地址:https://blog.csdn.net/segegefe/article/details/126114242