赞
踩
Service grpc 服务端的配置
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>proto-service</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>net.devh</groupId> <artifactId>grpc-server-spring-boot-starter</artifactId> <version>2.10.1.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.6.2</version> </extension> </extensions> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:3.5.1:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.11.0:exe:${os.detected.classifier}</pluginArtifact> <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot> <!--设置grpc生成代码到指定路径--> <outputDirectory>${project.basedir}/src/main/java</outputDirectory> <!--生成代码前是否清空目录--> <clearOutputDirectory>false</clearOutputDirectory> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
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<EventInfoResponse> 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(); } }
按照下面操作去点击:
生成的java 代码复制一份放到 客户端的代码包里。
代码结构:
proto : 放的是需要生成Java 文件的基础 .proto文件
客户端
proto-client 服务配置
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>org.example</groupId> <artifactId>proto-client</artifactId> <version>1.0-SNAPSHOT</version> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>net.devh</groupId> <artifactId>grpc-client-spring-boot-starter</artifactId> <version>2.10.1.RELEASE</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> <build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>1.6.2</version> </extension> </extensions> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:3.5.1:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.11.0:exe:${os.detected.classifier}</pluginArtifact> <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot> <!--设置grpc生成代码到指定路径--> <outputDirectory>${project.basedir}/src/main/java</outputDirectory> <!--生成代码前是否清空目录--> <clearOutputDirectory>false</clearOutputDirectory> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
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 项目结构:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。