成都html5網(wǎng)站建設(shè)市場運營和市場營銷的區(qū)別
spring 和 grpc 的整合
首先我們要知道 grpc 中我們在使用的時候用到了 grpc 的那些東西
- dil 的編寫
- serverimpl
- serverbuilder
- addService
- 客戶端的 stub 編寫
這里面我們看一下我們那些地方可能需要 spring 幫我們管理,那些地方我們需要自己來管理呢?比如服務(wù)的構(gòu)建 spring
是可以幫我么做的,而且 addServer 可以通過注解的方式自動發(fā)布服務(wù),但是 server 的 imp 我們是需要自己來實現(xiàn)的,服務(wù)的端口可以通過 properties 的方式來做自動配置和默認(rèn)值的設(shè)置。注解的標(biāo)識 spring 使用 @GrpcService
服務(wù)端搭建開發(fā)環(huán)境
- spring的開發(fā)環(huán)境我們需要搭建,這個可以直接在 idea 創(chuàng)建,我就不記錄和展示
- 引入 grpc spring 相關(guān)的依賴
<dependency><groupId>net.devh</groupId><artifactId>grpc-server-spring-boot-starter</artifactId><version>2.15.0.RELEASE</version><exclusions><exclusion><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId></exclusion></exclusions>
</dependency><dependency><groupId>com.google.protobuf</groupId><artifactId>protobuf-java</artifactId><version>3.25.5</version>
</dependency>
因為這是一個 grpc 的 server 所以并不需要使用 java-web 相關(guān)的服務(wù),我們可以移除相關(guān)的依賴,并且在配置中禁用 web 相關(guān)的內(nèi)容
配置 & 移除 web 相關(guān)的內(nèi)容
grpc:server:port: 8888-- 禁用 web 的服務(wù)
spring:application:name: grpc-servermain:web-application-type: none
server:port: 9999
移除對應(yīng)的依賴在 pom 文件中
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId></exclusion><exclusion><artifactId>logback-classic</artifactId><groupId>ch.qos.logback</groupId></exclusion></exclusions>
</dependency>
這個時候我們就可以使用了,使用注解 @GrpcServer
使其在 springboot 中可以被注入 IOC 容器,然后就是很傳統(tǒng)的實現(xiàn) **ImplBase
并且她也會幫我們把這個服務(wù)發(fā)布到 GRPC 服務(wù)中 addServer
這樣 springboot 啟動就會自動提供服務(wù)接受 grpc client 發(fā)送來的請求。
@GrpcService
public class OnceMessageServiceImpl extends BiRpcServiceGrpc.BiRpcServiceImplBase {private final Logger log = LoggerFactory.getLogger(OnceMessageServiceImpl.class);@Overridepublic void getOnceMessage(final BiService.OnceMessageRequest request, final StreamObserver<BiService.OnceMessageResponse> responseObserver) {log.info("receive client request msg -> {}", request.getContent());responseObserver.onNext(BiService.OnceMessageResponse.newBuilder().setContent(UUID.randomUUID().toString()).build());responseObserver.onCompleted();}
}
syntax = "proto3";option java_package = "com.rpc.grpc.bi";
option java_multiple_files = false;
option java_outer_classname = "BiService";message OnceMessageRequest {string content = 1;
}message OnceMessageResponse {string content = 1;
}service BiRpcService {// 注意這里因為我們使用 future 的方式來通訊,這就不可以使用 stream,只能使用簡單的 rpcrpc getOnceMessage(OnceMessageRequest) returns (OnceMessageResponse) {}
}service NewBiRpcService {// 注意這里因為我們使用 future 的方式來通訊,這就不可以使用 stream,只能使用簡單的 rpcrpc newGetOnceMessage(OnceMessageRequest) returns (OnceMessageResponse) {}
}
客戶端搭建
引入依賴
<dependency><groupId>net.devh</groupId><artifactId>grpc-client-spring-boot-starter</artifactId><version>3.0.0.RELEASE</version>
</dependency>
增加配置
spring:application:name: grpc-rpc-boot-client
grpc:client:bi_server:address: static://localhost:8888negotiation-type: plaintextserver:port: 8989
編碼
由于 spring 以及對客戶端的服務(wù)創(chuàng)建,等配置做了封裝,所以我們并不需要關(guān)心 channel 的創(chuàng)建,我們直接注入對應(yīng)的 stub(這里有三種 stub,分別是 future,stub,block),直接使用注解 @GrpcClient 然后在里面放我們在配置文件中配置的服務(wù)名稱,直接就可以通過 stub 調(diào)用了,然后 channel 的關(guān)閉等操作我們也不用關(guān)心,這個已經(jīng)做好了相關(guān)的封裝,使用者直接關(guān)心業(yè)務(wù)層面的邏輯就可以了。
public class BiRpcServerBlockStub {@GrpcClient("bi_server")private BiRpcServiceGrpc.BiRpcServiceBlockingStub biRpcServiceBlockingStub;@GetMappingpublic Object getUserInfo(String msg) {final BiService.OnceMessageResponse onceMessage = biRpcServiceBlockingStub.getOnceMessage(BiService.OnceMessageRequest.newBuilder().setContent(msg).build());return onceMessage.getContent();}
}
大家可以關(guān)注我的WX