gRPC 框架的 Spring Boot 启动器模块 gRPC Spring Boot Starter

您所在的位置:网站首页 grpcservice注解 gRPC 框架的 Spring Boot 启动器模块 gRPC Spring Boot Starter

gRPC 框架的 Spring Boot 启动器模块 gRPC Spring Boot Starter

#gRPC 框架的 Spring Boot 启动器模块 gRPC Spring Boot Starter | 来源: 网络整理| 查看: 265

授权协议: MIT 开发语言: Java 操作系统: 跨平台 软件首页: https://github.com/yidongnan/grpc-spring-boot-starter 软件文档: https://github.com/yidongnan/grpc-spring-boot-starter/blob/master/README-zh.md 软件介绍 特点

使用@ GrpcService自动创建并运行一个 gRPC 服务,内嵌在 spring-boot 应用中

使用@ GrpcClient自动创建和管理你的channel和stub

支持 Spring Cloud(向Consul或Eureka注册服务并获取gRPC服务器信息)

支持 Spring Sleuth 进行链路跟踪

支持对于 server、client 分别设置全局拦截器或单个的拦截器

支持 Spring-Security

支持 metric (micrometer / actuator)

可以使用 grpc-netty-shaded

版本

2.x.x.RELEASE 支持 Spring Boot 2 & Spring Cloud Finchley。

最新的版本:2.2.0.RELEASE

1.x.x.RELEASE 支持 Spring Boot 1 & Spring Cloud Edgware 、Dalston、Camden。

最新的版本:1.4.1.RELEASE

注意: 此项目也可以在没有Spring-Boot的情况下使用,但这需要一些手动bean配置。

使用方式 gRPC server + client

如果使用的是 Maven,添加如下依赖

net.devh grpc-spring-boot-starter 2.2.0.RELEASE

如果使用的 Gradle,添加如下依赖

dependencies { compile 'net.devh:grpc-spring-boot-starter:2.2.0.RELEASE' } gRPC 服务端

如果使用的是 Maven,添加如下依赖

net.devh grpc-server-spring-boot-starter 2.2.0.RELEASE

如果使用的 Gradle,添加如下依赖

dependencies { compile 'net.devh:grpc-server-spring-boot-starter:2.2.0.RELEASE' }

实现 gRPC server 的业务逻辑,并使用 @GrpcService 注解

@GrpcService public class GrpcServerService extends GreeterGrpc.GreeterImplBase { @Override public void sayHello(HelloRequest req, StreamObserver responseObserver) { HelloReply reply = HelloReply.newBuilder().setMessage("Hello ==> " + req.getName()).build(); responseObserver.onNext(reply); responseObserver.onCompleted(); } }

设置 gRPC 的 host 跟 port ,默认的监听的 host 是 0.0.0.0,默认的 port 是 9090。其他配置属性可以参考 settings。所有的配置文件在 server 中使用需增加 grpc.server. 的前缀

Properties示例 grpc.server.port=9090 grpc.server.address=0.0.0.0 Server-Security

支持使用 Spring-Security 加密你的 gRPC 应用。你只需要添加 Spring-Security(core 或者 config)依赖,然后根据需要再增加加密的配置

首先需要选择一个认证方案

BasicAuth(基础认证)

@Bean AuthenticationManager authenticationManager() { final List providers = new ArrayList(); providers.add(...); // Possibly DaoAuthenticationProvider return new ProviderManager(providers); } @Bean GrpcAuthenticationReader authenticationReader() { final List readers = new ArrayList(); readers.add(new BasicGrpcAuthenticationReader()); return new CompositeGrpcAuthenticationReader(readers); }

Certificate Authentication(证书认证)

@Bean AuthenticationManager authenticationManager() { final List providers = new ArrayList(); providers.add(new X509CertificateAuthenticationProvider(userDetailsService())); return new ProviderManager(providers); } @Bean GrpcAuthenticationReader authenticationReader() { final List readers = new ArrayList(); readers.add(new SSLContextGrpcAuthenticationReader()); return new CompositeGrpcAuthenticationReader(readers); }

相关的配置属性如下:

grpc.server.security.enabled=true grpc.server.security.certificateChainPath=certificates/server.crt grpc.server.security.privateKeyPath=certificates/server.key grpc.server.security.trustCertCollectionPath=certificates/trusted-clients-collection grpc.server.security.clientAuth=REQUIRE

使用 CompositeGrpcAuthenticationReader 类链式的调用多个认证方案

自定义认证方式(继承并实现 GrpcAuthenticationReader 类)

然后决定如果去保护你的服务

使用 Spring-Security 的注解

@Configuration @EnableGlobalMethodSecurity(proxyTargetClass = true, ...) public class SecurityConfiguration {

如果你想使用 Spring Security 相关的注解的话,proxyTargetClass 属性是必须的! 但是你会受到一条警告,提示 MyServiceImpl#bindService() 方式是用 final 进行修饰的。 这条警告目前无法避免,单他是安全的,可以忽略它。

手动配置

@Bean AccessDecisionManager accessDecisionManager() { final List


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3