1. GRPC的使用

1.导入maven依赖

io.grpc grpc-netty ${grpc.version} provided io.grpc grpc-protobuf ${grpc.version} provided io.grpc grpc-stub ${grpc.version} provided com.google.protobuf protobuf-java ${protobuf.version}

版本号为:
3.5.0
1.16.0
导入相应的编译插件
kr.motd.maven os-maven-plugin 1.6.0 org.apache.maven.plugins maven-compiler-plugin 2.3.2 ${project.build.sourceEncoding} ${java.version} ${java.version} org.apache.maven.plugins maven-deploy-plugin false org.apache.maven.plugins maven-surefire-plugin 2.19 **/*Test.java org.xolstice.maven.plugins protobuf-maven-plugin 0.6.1 com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier} ${project.build.directory}/generated-sourcesfalse compile-protobufgenerate-sources compile compile-grpcgenerate-sources compile-custom grpc-javaio.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}

2.编写proto文件 ArDeviceService.proto
syntax = "proto3"; option java_multiple_files = true; package cn.cloudwalk.aros.device; message CwArDeviceQueryParam { /** * 设备类型 */ string deviceType = 1; /** * 设备名称 */ string deviceName = 2; /** * 来源平台名称 */ string platformName = 3; /** * 系列号 */ string sn = 4; /** * 国标编码 */ string gbCode = 5; /** * 区域ID */ string regionId = 6; string businessId = 7; }message CloudwalkArDeviceResult{ /** * 编码 */ string code = 1 ; /** * 是否成功 */ bool success = 2; /** * 消息 */ string message = 3; /** * 返回结果 */ repeated CwArDeviceResult data = https://www.it610.com/article/4; } message CwArDeviceResult { /** * 设备类型 */ string deviceType = 1; /** * 设备名称 */ string deviceName = 2; /** * 来源平台名称 */ string platformName = 3; /** * 系列号 */ string sn = 4; /** * IP地址 */ string ipAddress = 5; /** * 端口 */ int32 port = 6; /** * 用户名 */ string userName = 7; /** * 密码 */ string userPassword = 8; /** * 国标编码 */ string gbCode = 9; /** * 区域ID */ string regionId = 10; /** * 租户ID */ string businessId = 11; } service CwArDeviceService { rpc query (CwArDeviceQueryParam) returns (CloudwalkArDeviceResult); }

执行mvn clean compile
3.服务端实现grpc接口
@Lazy(true) @Service("arDeviceServiceImpl") public class ArDeviceServiceImpl extends CwArDeviceServiceGrpc.CwArDeviceServiceImplBase { @Autowired CwArDeviceManager cwArDeviceManager; @Override public void query(CwArDeviceQueryParam request, StreamObserver responseObserver) { CwArDeviceQueryDTO dto = newCwArDeviceQueryDTO(); BeanCopyUtils.copyProperties(request, dto); //cn.cloudwalk.aros.client.device.param.CwArDeviceQueryParam param = BeanCopyUtils.copyProperties(request,cn.cloudwalk.aros.client.device.param.CwArDeviceQueryParam.class); cn.cloudwalk.aros.device.CwArDeviceResult cwArDeviceResult = null; try { if(cwArDeviceManager==null) { cwArDeviceManager= (CwArDeviceManager)SpringContextHolder.getBean("cwArDeviceManager"); } List list =this.cwArDeviceManager.query(dto); CloudwalkArDeviceResult.Builder rpcResponseBuilder = CloudwalkArDeviceResult.newBuilder(); CloudwalkArDeviceResult rpcResponse = null ; for(int i = 0; i< list.size(); i++){ CwArDeviceResultDTO item = list.get(i); cwArDeviceResult = cn.cloudwalk.aros.device.CwArDeviceResult.newBuilder().setBusinessId(item.getBusinessId()).setDeviceName(item.getDeviceName()).build(); rpcResponseBuilder.addData(cwArDeviceResult); } rpcResponse = rpcResponseBuilder.setCode("00000000").setSuccess(true).build(); responseObserver.onNext( rpcResponse ); } catch (Exception e) { CloudwalkArDeviceResult rpcErrorResponse = CloudwalkArDeviceResult.newBuilder().setCode("10000000000").setMessage("查询失败").setSuccess(false).build(); responseObserver.onNext( rpcErrorResponse ); } responseObserver.onCompleted(); }}

【1. GRPC的使用】SpringContextHolder工具类
package cn.cloudwalk.service.common.util; import org.apache.commons.lang3.Validate; import org.springframework.beans.BeansException; import org.springframework.beans.factory.DisposableBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; /** * 以静态变量保存Spring ApplicationContext, 可在任何代码任何地方任何时候取出ApplicaitonContext. * */ @Service @Lazy(false) public class SpringContextHolder implements ApplicationContextAware, DisposableBean {private static ApplicationContext applicationContext = null; /** * 取得存储在静态变量中的ApplicationContext. */ public static ApplicationContext getApplicationContext() { assertContextInjected(); return applicationContext; }/** * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. */ @SuppressWarnings("unchecked") public static T cwArDeviceManager(String name) { assertContextInjected(); return (T) applicationContext.getBean(name); }/** * 从静态变量applicationContext中取得Bean, 自动转型为所赋值对象的类型. */ public static T getBean(String requiredType) { assertContextInjected(); return (T)applicationContext.getBean(requiredType); }/** * 清除SpringContextHolder中的ApplicationContext为Null. */ public static void clearHolder() { applicationContext = null; }/** * 实现ApplicationContextAware接口, 注入Context到静态变量中. */ @Override public void setApplicationContext(ApplicationContext appContext) { applicationContext = appContext; }/** * 实现DisposableBean接口, 在Context关闭时清理静态变量. */ @Override public void destroy() throws Exception { SpringContextHolder.clearHolder(); }/** * 检查ApplicationContext不为空. */ private static void assertContextInjected() { Validate.validState(applicationContext != null, "applicaitonContext属性未注入, 请在applicationContext.xml中定义SpringContextHolder."); } }

4.启动类中启动grpc服务器
//grpc启动 Server server = null ; try { server = ServerBuilder. forPort(port) .addService( new ArDeviceServiceImpl () ) .build().start(); System.out.println( "grpc服务端启动成功, 端口=" + port ); server.awaitTermination(); }catch (Exception e){ System.out.println( "grpc服务端异常关闭:"+e); server.shutdown(); }

启动客户端
public class GrpcClient { private static final String host = "localhost"; private static final int serverPort = 9999; public static void main( String[] args ) throws Exception { ManagedChannel managedChannel = ManagedChannelBuilder.forAddress( host, serverPort ).usePlaintext().build(); try { CwArDeviceServiceGrpc.CwArDeviceServiceBlockingStub rpcDateService = CwArDeviceServiceGrpc.newBlockingStub( managedChannel ); CwArDeviceQueryParamcwArDeviceQueryParam = CwArDeviceQueryParam .newBuilder() .setBusinessId("1") .build(); CloudwalkArDeviceResult rpcDateResponse = rpcDateService.query( cwArDeviceQueryParam ); System.out.println( rpcDateResponse.getDataList()); } finally { managedChannel.shutdown(); } } }

    推荐阅读