代码地址如下:
http://www.demodashi.com/demo/14110.html
Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。通过这种方式,Spring Boot致力于在蓬勃发展的快速应用开发领域(rapid application development)成为领导者。
Grpc 由 google 开发,是一款语言中立、平台中立、开源的远程过程调用(RPC)系统。
项目定位是电商平台和WMS系统中间的插件服务,wms采用.net语言开发,电商平台采用java开发,所以出现了多语言间的数据交换,开始考虑使用json协议,方便快捷,但是考虑到效率和系统发展的问题,考虑使用RPC框架,斟酌之后敲定为GRPC。
但是,我们拓展服务使用的SpringBoot,Grpc和SpringBoot集成的文章不是很多,大部分都是采用github上***-springboot.grpc.starter的项目,看了一下,并不是很感兴趣,所以自己根据官网的demo,尝试与SpringBoot集成,所以在这和大家分享下
Spring Boot 2.0.5.RELEASE
Grpc 1.15.0
Inteliji Idea 2018.3
org.springframework.boot
spring-boot-starter-parent
2.0.5.RELEASE
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
public static void main(String[] args) {
// 启动SpringBoot web
SpringApplication.run(Launcher.class, args);
}
io.grpc
grpc-netty-shaded
1.15.0
io.grpc
grpc-protobuf
1.15.0
io.grpc
grpc-stub
1.15.0
kr.motd.maven
os-maven-plugin
1.5.0.Final
org.xolstice.maven.plugins
protobuf-maven-plugin
0.5.1
com.google.protobuf:protoc:3.5.1-1:exe:${os.detected.classifier}
grpc-java
io.grpc:protoc-gen-grpc-java:1.15.0:exe:${os.detected.classifier}
compile
compile-custom
public class HelloWorldService extends HelloWorldServiceGrpc.HelloWorldServiceImplBase {
@Override
public void welcome(HelloWorld.NameRequest request, StreamObserver responseObserver) {
HelloWorld.EchoResponseOrBuilder echoResponseOrBuilder = HelloWorld.EchoResponse.newBuilder();
((HelloWorld.EchoResponse.Builder) echoResponseOrBuilder).setMergename("welcome " + request.getName());
responseObserver.onNext(((HelloWorld.EchoResponse.Builder) echoResponseOrBuilder).build());
responseObserver.onCompleted();
}
}
@Component("grpcLauncher")
public class GrpcLauncher {
private Logger logger = LoggerFactory.getLogger(Launcher.class);
/**
* 定义Grpc Server
*/
private Server server;
@Value("${grpc.server.port}")
private Integer grpcServerPort;
/**
* GRPC 服务启动方法
* @param grpcServiceBeanMap
*/
public void grpcStart(Map grpcServiceBeanMap) {
try{
ServerBuilder serverBuilder = ServerBuilder.forPort(grpcServerPort);
for (Object bean : grpcServiceBeanMap.values()){
serverBuilder.addService((BindableService) bean);
logger.info(bean.getClass().getSimpleName() + " is regist in Spring Boot");
}
server = serverBuilder.build().start();
logger.info("grpc server is started at " + grpcServerPort);
server.awaitTermination();
Runtime.getRuntime().addShutdownHook(new Thread(()->{grpcStop();}));
} catch (IOException e){
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
* GRPC 服务Stop方法
*/
private void grpcStop(){
if (server != null){
server.shutdownNow();
}
}
}
/**
* 自定义注解,用于获取Spring扫描到的类
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface GrpcService {
}
/**
* Spring Boot 启动器
*/
@SpringBootApplication
public class Launcher {
public static void main(String[] args) {
// 启动SpringBoot web
ConfigurableApplicationContext configurableApplicationContext = SpringApplication.run(Launcher.class, args);
Map grpcServiceBeanMap = configurableApplicationContext.getBeansWithAnnotation(GrpcService.class);
GrpcLauncher grpcLauncher = configurableApplicationContext.getBean("grpcLauncher",GrpcLauncher.class);
grpcLauncher.grpcStart(grpcServiceBeanMap);
}
}
至此Server端,集成完毕
@Component
public class GrpcClientMananer {
@Value("${grpc.client.host}")
private String host;
@Value("${grpc.client.port}")
private Integer port;
public ManagedChannel getChannel(){
ManagedChannel channel = ManagedChannelBuilder.forAddress(host,port)
.disableRetry()
.idleTimeout(2, TimeUnit.SECONDS)
.build();
return channel;
}
}
@Component
public class HelloWorldClient {
@Autowired
private GrpcClientMananer grpcClientMananer;
public void call(){
ManagedChannel channel = grpcClientMananer.getChannel();
HelloWorld.NameRequestOrBuilder nameRequestOrBuilder = HelloWorld.NameRequest.newBuilder();
((HelloWorld.NameRequest.Builder) nameRequestOrBuilder).setName("Geek");
HelloWorldServiceGrpc.HelloWorldServiceBlockingStub stub = HelloWorldServiceGrpc.newBlockingStub(channel);
HelloWorld.EchoResponse echoResponse = stub.welcome(((HelloWorld.NameRequest.Builder) nameRequestOrBuilder).build());
System.out.println(echoResponse.getMergename());
}
}
暂没
Spring Boot 集成 GRPC
代码地址如下:
http://www.demodashi.com/demo/14110.html
注:本文著作权归作者,由demo大师发表,拒绝转载,转载需要作者授权