Grpc用户指南

编辑
更新时间: 2023-01-14

Grpc用户指南

  • 目前只支持grpc的一元同步调用

  • 引入jar包

  • 引入hmily配置

  • 在具体的实现方法上(服务提供端),加上@HmilyTCC or HmilyTAC 注解

引入依赖

Spring-Namespace

  • 引入依赖

        <dependency>
                  <groupId>org.dromara</groupId>
                  <artifactId>hmily-grpc</artifactId>
                  <version>{last.version}</version>
          </dependency>
  • 在xml中进行如下配置

    <!--配置扫码hmily框架的包-->
    <context:component-scan base-package="org.dromara.hmily.*"/>
    <!--设置开启aspectj-autoproxy-->
    <aop:aspectj-autoproxy expose-proxy="true"/>
    <!--配置Hmily启动的bean参数-->
    <bean id="hmilyApplicationContextAware" class="org.dromara.hmily.spring.HmilyApplicationContextAware"/>
     
    

    Spring-Boot

    • 引入依赖
      xml <dependency> <groupId>org.dromara</groupId> <artifactId>hmily-spring-boot-starter-grpc</artifactId> <version>{last.version}</version> </dependency>

引入Hmily配置

Grpc拦截器配置

  • grpc客户端中添加拦截器 GrpcHmilyTransactionFilter
  • grpc服务端中添加拦截器 GrpcHmilyServerFilter

Grpc客户端调用

  • 使用GrpcHmilyClient代替原来的调用方式来进行远程调用。
AccountResponse response = GrpcHmilyClient.syncInvoke(accountServiceBlockingStub, "payment", request, AccountResponse.class);

入参分别为被调用的AbstratcSub,方法名,具体的参数以及返回值类型

TCC模式
  • 对服务端事务方法的具体实现,加上@HmilyTCC(confirmMethod = "confirm", cancelMethod = "cancel")

  • confirmMethod : 确认方法名称,该方法参数列表与返回类型应与标识方法一致。

  • cancelMethod : 回滚方法名称,该方法参数列表与返回类型应与标识方法一致。

  • TCC模式应该保证 confirmcancel 方法的幂等性,用户需要自行去开发这个2个方法,所有的事务的确认与回滚,完全由用户决定。Hmily框架只是负责来进行调用


public class HelloServiceImpl implements HelloService  {

    @HmilyTCC(confirmMethod = "sayConfrim", cancelMethod = "sayCancel")
    public void say(String hello) {
         System.out.println("hello world");
    }
    
    public void sayConfrim(String hello) {
         System.out.println(" confirm hello world");
    }

    public void sayCancel(String hello) {
         System.out.println(" cancel hello world");
    }
}

重要注意事项

异常

  • try, confirm, cancel 方法的所有异常不要自行catch 任何异常都应该抛出给 Hmily框架处理。