Springboot集成grpc 简单实现

Service grpc 服务端的配置

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.2.RELEASE
         
    

    org.example
    proto-service
    1.0-SNAPSHOT

    
        8
        8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            net.devh
            grpc-server-spring-boot-starter
            2.10.1.RELEASE
        
        
            org.projectlombok
            lombok
        
    
    
        
            
                kr.motd.maven
                os-maven-plugin
                1.6.2
            
        
        
            
                org.xolstice.maven.plugins
                protobuf-maven-plugin
                0.6.1
                
                    com.google.protobuf:protoc:3.5.1:exe:${os.detected.classifier}
                    grpc-java
                    io.grpc:protoc-gen-grpc-java:1.11.0:exe:${os.detected.classifier}
                    ${project.basedir}/src/main/proto
                    
                    ${project.basedir}/src/main/java
                    
                    false
                
                
                    
                        
                            compile
                            compile-custom
                        
                    
                
            
        
    

application.yml

grpc:
  server:
    port: 7778
spring:
  application:
    name: grpc-server
server:
  port: 8890

文件:eventInfo.proto

syntax = "proto3";

option java_multiple_files = true;
option java_package = "com.event.proto";//生成代码的位置

service EventInfoService {
  rpc sendMessageEvent(EventInfoMessage) returns (EventInfoResponse) {}
}

message EventInfoMessage {
  string msg = 1;
}


message EventInfoResponse {
  int32 code = 1;
  string msg = 2;
  bool success = 3;
}

GrpcService

@GrpcService
public class EventInfoServiceGrpcImpl extends EventInfoServiceGrpc.EventInfoServiceImplBase {

    @Override
    public void sendMessageEvent(EventInfoMessage request, StreamObserver responseObserver) {
        EventInfoResponse.Builder eventInfo = EventInfoResponse.newBuilder();
        //业务处理
        String msg = request.getMsg();
        //
        if ("success".equals(msg)){
            eventInfo.setCode(200).setMsg("success"+100000).setSuccess(true);
        }else{
            eventInfo.setCode(500).setMsg("error"+100000).setSuccess(false);

        }
        responseObserver.onNext(eventInfo.build());
        responseObserver.onCompleted();
    }

}

按照下面操作去点击:

Springboot集成grpc 简单实现_第1张图片

 生成的java 代码复制一份放到 客户端的代码包里。

代码结构:

proto : 放的是需要生成Java 文件的基础  .proto文件

Springboot集成grpc 简单实现_第2张图片

客户端

 proto-client 服务配置

pom.xml



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.3.2.RELEASE
         
    

    org.example
    proto-client
    1.0-SNAPSHOT

    
        8
        8
    
    
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            net.devh
            grpc-client-spring-boot-starter
            2.10.1.RELEASE
        
        
            org.projectlombok
            lombok
        
    
    
        
            
                kr.motd.maven
                os-maven-plugin
                1.6.2
            
        
        
            
                org.xolstice.maven.plugins
                protobuf-maven-plugin
                0.6.1
                
                    com.google.protobuf:protoc:3.5.1:exe:${os.detected.classifier}
                    grpc-java
                    io.grpc:protoc-gen-grpc-java:1.11.0:exe:${os.detected.classifier}
                    ${project.basedir}/src/main/proto
                    
                    ${project.basedir}/src/main/java
                    
                    false
                
                
                    
                        
                            compile
                            compile-custom
                        
                    
                
            
        
    

Appilcation.yml

grpc:
  client:
    grpc-server:
      address: static://localhost:8888
      enableKeepAlive: true
      keepAliveWithoutCalls: true
      negotiationType: plaintext
spring:
  application:
    name: grpc-client
server:
  port: 8891

ClientController.java

@RestController
public class ClientController {

    @GrpcClient("local-grpc-server")
    private UserInfoServiceGrpc.UserInfoServiceFutureStub userInfoServiceStub;

    // http://localhost:8891/userInfo/query/11?str=test
    @RequestMapping(value = "/query/{id}")
    public String queryUser(@PathVariable Integer id, String str) throws ExecutionException, InterruptedException {

        System.out.println("-------sever-----");
        return  userInfoServiceStub.queryUserInfo3(UserStr.newBuilder().setStr(str).build()).get().getStr();
    }
}

client 项目结构:

Springboot集成grpc 简单实现_第3张图片

你可能感兴趣的:(spring,boot,java,rpc)