关于java:从零开始学gRPC二

5次阅读

共计 3448 个字符,预计需要花费 9 分钟才能阅读完成。

前言

gRPC 的大抵性能点置信大家肯定曾经有些理解。然而,在日常开发中咱们很少会应用原生的 gRPC,更多的是心愿 gRPC 能集成到现有利用的开发环境中。这篇文章会围绕如何以简略的形式将 gRPC 集成到 SpringBoot 开展解说。

疾速搭建

环境筹备

  • 操作系统:Windowns
  • JDK 版本:1.8
  • 编辑工具:IntelliJ IDEA Community Edition 2021.1.2 x64

框架选型

目前开源切比拟热门的 grpc 和 SpringBoot 整合框架有两种可选计划。别离是 LogNet 和 yidongnan。(这两者略微有些区别,在最初我会有个比照) 这里我应用后者作为 demo 演示。

创立我的项目

本人创立一个 maven 我的项目就好,没有别的要求(All in one,临时不须要分模块)。想要偷懒的同学也能够借鉴之前的我的项目,然而我 倡议本人入手尝试搭建比拟好。

MAVEN 依赖

留神:我只列出了要害依赖。而且注册核心选用的是 zookeeper,要留神 zookeeper 的版本,能够参考 这里。


<dependencies>
  <dependency>
    <groupId>net.devh</groupId>
    <artifactId>grpc-spring-boot-starter</artifactId>
    <version>2.13.1.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.6.8</version>
  </dependency>

  <!-- spring-cloud-zookeeper-starter-->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zookeeper-discovery</artifactId>
    <version>3.1.2</version>
  </dependency>
</dependencies>

配置


server:
  port: 8081

spring:
  application:
    name: demo-grpc
  cloud:
    zookeeper:
      connect-string: localhost:2181

grpc:
  client:
    greeterService1:
      address: discovery:/demo-grpc
      negotiationType: PLAINTEXT
  server:
    port: 9091

这个中央的 negotiationType 要留神点(不设置的话须要对客户端传输进行加密)

SLB 测试

  • 本地启动 zookeeper
  • 本地启动两次(记得改端口,tomcat 和 grpc 的都要改),测试代码如下。

  • 测试后果展现


    我前端都是通过一个地址拜访的,申请通过 gRPC 的负载配置被散发到不同的端口去了,由此可见 SLB 失效了。

LogNet 与 yidongnan

共性:

  1. 扩大了原生 gRPC 的服务注册、服务发现性能。
  2. 和 SpringCloud 深度整合,有很好的扩大能力。

差别:

  1. 服务发现:yidongnan 间接将服务发现和 gRPC 自身的 SLB 整合,LogNet 须要本人做 SLB,或者手动整合 gRPC 的软负载能力 (官网 demo 上没提到如何做 SLB,这里我暂且认为是这样子,晓得的敌人记得分割我更正)
  2. LogNet 多了对事务和全局异样的解决能力。(通常不倡议在 RPC 的过程中应用事务,所以我感觉这个性能有点鸡肋)
  3. LogNet 还整合了 spring-cloud-stream、spring-boot-validation。

总结

这里咱们很容易就发现 这两个框架次要做的是服务的主动注册和发现以及 gRPC 本身能力和 spring cloud 能力的整合(鉴权、服务可观测、链路追踪),服务治理能力弱的问题并没有失去解决(即没有提供一些适合的限流、降级、流量调度策略,也没有提供一个适合的控制台或者 API 去对服务进行治理。能够参考 Dubbo Admin,通过它能够对 Dubbo 服务进行治理)。

阐明:我这里没有演示 sleuth、actuator、security 这些的集成,有趣味的能够去看看 文档。

踩坑笔记

1.Client 端默认设置的是密文,必须要设置 TLS 证书。当然也能够应用文本传输,然而生产环境不倡议。客户端平安配置传送门

文本传输配置:grpc.client.[serviceName].negotiationType=PLAINTEXT

  1. 若不集成 SpringCloud-Discovery 则不能应用注册核心做服务发现,只能固定配置, 例如:
    grpc.client.[serviceName].address: static://localhost:9090,localhost:
    //9091。具体配置能够参考上面文档
 /**
     * Sets the target address uri for the channel. The target uri must be in the format:
     * {@code schema:[//[authority]][/path]}. If nothing is configured then the name of the client will be used along
     * with the default scheme. We recommend explicitly configuring the scheme used for the address resolutions such as
     * {@code dns:/}.
     *
     * <p>
     * <b>Examples</b>
     * </p>
     *
     * <ul>
     * <li>{@code static://localhost:9090} (refers to exactly one IPv4 or IPv6 address, dependent on the jre
     * configuration, it does not check whether there is actually someone listening on that network interface)</li>
     * <li>{@code static://10.0.0.10}</li>
     * <li>{@code static://10.0.0.10,10.11.12.11}</li>
     * <li>{@code static://10.0.0.10:9090,10.0.0.11:80,10.0.0.12:1234,[::1]:8080}</li>
     * <li>{@code dns:/localhost (might refer to the IPv4 or the IPv6 address or both, dependent on the system
     * configuration, it does not check whether there is actually someone listening on that network interface)}</li>
     * <li>{@code dns:/example.com}</li>
     * <li>{@code dns:/example.com:9090}</li>
     * <li>{@code dns:///example.com:9090}</li>
     * <li>{@code discovery:/foo-service}</li>
     * <li>{@code discovery:///foo-service}</li>
     * <li>{@code unix:<relative-path>} (Additional dependencies may be required)</li>
     * <li>{@code unix://</absolute-path>} (Additional dependencies may be required)</li>
     * </ul>
     *
     * @param address The string representation of an uri to use as target address or null to use a fallback.
     *
     * @see <a href="https://github.com/grpc/grpc/blob/master/doc/naming.md">gRPC Name Resolution</a>
     * @see NameResolverProvider
     */
正文完
 0