• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

grpc-spring-boot-starter: 特点 使用@ GrpcService自动创建并运行一个 gRPC 服务,内 ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

grpc-spring-boot-starter

开源软件地址:

https://gitee.com/mirrors/grpc-spring-boot-starter

开源软件介绍:

gRPC Spring Boot Starter

Build StatusMaven Central with version prefix filterMIT LicenseCrowdin

Client-JavadocServer-JavadocCommon-Javadoc

README: English | 中文

文档: English | 中文

QQ交流群:294712648

特性

  • 在 spring boot 应用中,通过 @GrpcService 自动配置并运行一个嵌入式的 gRPC 服务。

  • 使用 @GrpcClient 自动创建和管理您的 gRPC Channels 和 stubs

  • 支持Spring Cloud (向 ConsulEurekaNacos 注册服务并获取 gRPC 服务端信息)

  • 支持Spring Sleuth作为分布式链路跟踪解决方案(如果brave-instrument-grpc存在)

  • 支持全局和自定义的 gRPC 服务端/客户端拦截器

  • 支持 Spring-Security

  • 支持metric (基于micrometer/actuator )

  • 也适用于 (non-shaded) grpc-netty

版本

2.x.x.RELEASE 支持 Spring Boot 2.1.x/2.2.x 和 Spring Cloud Greenwich / Hoxton。

最新版本: 2.13.1.RELEASE

( 2.4.0.RELEASE 用于 Spring Boot 2.0.x & Spring Cloud Finchy).

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

最新版本: 1.4.2.RELEASE

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

用法

gRPC 服务端 + 客户端

使用一下命令添加 Maven 依赖项:

<dependency>  <groupId>net.devh</groupId>  <artifactId>grpc-spring-boot-starter</artifactId>  <version>2.13.1.RELEASE</version></dependency>

Gradle:

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

gRPC 服务端

使用一下命令添加 Maven 依赖项:

<dependency>  <groupId>net.devh</groupId>  <artifactId>grpc-server-spring-boot-starter</artifactId>  <version>2.13.1.RELEASE</version></dependency>

Gradle:

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

在服务端接口实现类上添加 @GrpcService 注解。

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

默认情况下,Grpc 服务器将监听端口 9090。 端口的配置和其他的 设置 可以通过 Spring 的属性机制进行更改。 服务端的配置使用 grpc.server. 前缀。

详情请参阅我们的文档

gRPC 客户端

使用一下命令添加 Maven 依赖项:

<dependency>  <groupId>net.devh</groupId>  <artifactId>grpc-client-spring-boot-starter</artifactId>  <version>2.13.1.RELEASE</version></dependency>

Gradle:

dependencies {  compile 'net.devh:grpc-client-spring-boot-starter:2.13.1.RELEASE'}

在 grpc 客户端的的 stub 字段上添加 @GrpcClient(serverName) 注解。

  • 请不要将 @GrpcClient 与 @Autowireed@Inject 一起使用。

    @GrpcClient("gRPC server name")private GreeterGrpc.GreeterBlockingStub greeterStub;

注意: 你可以将相同的 grpc 服务端名称用于多个 channel, 也可以使用不同的 stub (甚至使用不同的 stub 拦截器)

然后您可以向您的服务器发送查询,就像这样:

HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());

可以单独配置每个客户端的目标地址。 但在某些情况下,您可以仅依靠默认配置。 您可以通过 NameResolver.Factory Bean 类自定义默认的 url 映射。 如果您没有配置那个Bean,那么默认的 uri 将使用默认方案和名称(如:dns:<name>):

这些配置和其他的 设置 可以通过 Spring 的属性机制进行更改。 客户端使用grpc.client.(serverName)。 前缀。

详情请参阅我们的文档

使用 (non-shaded) grpc-netty 运行

这个库支持grpc-nettygrpc-nety-shaded。 后一种可能会防止与不兼容的 gRPC 版本冲突或不同 netty 版本之间的冲突。

注意: 如果在classpath 中存在 shaded netty, 则 shaded netty 将使用有线与 non-shaded grpc-netty。

您可以在 Maven 中这样使用。

<dependency>    <groupId>io.grpc</groupId>    <artifactId>grpc-netty</artifactId>    <version>${grpcVersion}</version></dependency><!-- For both --><dependency>    <groupId>net.devh</groupId>    <artifactId>grpc-spring-boot-starter</artifactId>    <version>...</version>    <exclusions>        <exclusion>            <groupId>io.grpc</groupId>            <artifactId>grpc-netty-shaded</artifactId>        </exclusion>    </exclusions></dependency><!-- For the server (only) --><dependency>    <groupId>net.devh</groupId>    <artifactId>grpc-server-spring-boot-starter</artifactId>    <version>...</version>    <exclusions>        <exclusion>            <groupId>io.grpc</groupId>            <artifactId>grpc-netty-shaded</artifactId>        </exclusion>    </exclusions></dependency><!-- For the client (only) --><dependency>    <groupId>net.devh</groupId>    <artifactId>grpc-client-spring-boot-starter</artifactId>    <version>...</version>    <exclusions>        <exclusion>            <groupId>io.grpc</groupId>            <artifactId>grpc-netty-shaded</artifactId>        </exclusion>    </exclusions></dependency>

Gradle:

compile "io.grpc:grpc-netty:${grpcVersion}"compile 'net.devh:grpc-spring-boot-starter:...' exclude group: 'io.grpc', module: 'grpc-netty-shaded' // For bothcompile 'net.devh:grpc-client-spring-boot-starter:...' exclude group: 'io.grpc', module: 'grpc-netty-shaded' // For the client (only)compile 'net.devh:grpc-server-spring-boot-starter:...' exclude group: 'io.grpc', module: 'grpc-netty-shaded' // For the server (only)

示例项目

这里可以阅读更多关于我们的示例项目。

排除故障

请参阅我们的文档寻求帮助。

参与贡献

我们始终欢迎您对项目作出任何贡献。 详见CONTRIBUTING.md


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap