步骤一: 引入依赖jar包
步骤二:引入hmily
配置
步骤三:在具体的实现方法上(服务提供端),加上HmilyTCC
or HmilyTAC
注解
步骤四:在feignClient调用方法上(消费方),加上Hmily
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-springcloud</artifactId>
<version>{last.version}</version>
</dependency>
<!--配置扫码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"/>
<dependency>
<groupId>org.dromara</groupId>
<artifactId>hmily-spring-boot-starter-springcloud</artifactId>
<version>{last.version}</version>
</dependency>
在项目的 resource
添加文件名为:hmily.yml
的配置文件
具体的参数配置可以参考配置详解,本地配置模式, zookeeper配置模式, nacos配置模式,apollo配置模式
只需要在参与hmily分布式事务调用的具体实现方法上加@HmilyTCC(confirmMethod = "confirm", cancelMethod = "cancel")
confirmMethod
: 确认方法名称,该方法参数列表与返回类型应与标识方法一致。
cancelMethod
: 回滚方法名称,该方法参数列表与返回类型应与标识方法一致。
TCC
模式应该保证 confirm
和 cancel
方法的幂等性,用户需要自行去开发这个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");
}
}
@HmilyTAC
@FeignClient
接口方法上加上 @Hmily
注解。@FeignClient(value = "helle-service")
public interface HelloService {
@Hmily
@RequestMapping("/helle-service/sayHello")
void say(String hello);
}
在调用任何RPC调用之前,当你需要聚合rpc调用成为一次分布式事务的时候,需要在聚合RPC调用的方法上,先行添加 @HmilyTCC
或者 @HmilyTAC
注解,表示开启全局事务。
hmily
自带, 这样 try
, confirm
, cancel
调用会落在同一个节点
充分利用了缓存,提搞了效率。在你的yaml配置如下:
hmily.ribbon.rule.enabled = true
feign.hystrix.enabled = true
, 默认使用线程池模式, 将会开启 HmilyHystrixConcurrencyStrategy
它在hystrix使用线程池模式的时候,能够照样通过threadLoacl
进行RPC传参数。ribbon:
MaxAutoRetriesNextServer : 0
MaxAutoRetries: 0
try
, confirm
, cancel
方法的所有异常不要自行catch
任何异常都应该抛出给 Hmily
框架处理。