本篇内容主要讲解“dubbo接口调用造成的数据插入重复问题怎么解决”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“dubbo接口调用造成的数据插入重复问题怎么解决”吧!
简单搭建一下dubbo环境 dubbo-service dubbo-service-sdk dubbo-service-web 1、新建module dubbo-service-sdk 定义服务接口
/** * @author v_liuwen * @date 2019-07-24 */ public interface AsoService { void saveAso(AsoInfoDto asoInfoDto) throws InterruptedException; }
/** * @author v_liuwen * @date 2019-07-24 */ @Data public class AsoInfoDto implements Serializable { private static final long serialVersionUID = 1L; private Long asoId; private String asoNo; private String linkOrderNo; private Date createTime; }
2、新建module dubbo-service 实现sdk的接口 依赖
<dependency> <groupId>top.qrainly</groupId> <artifactId>dubbo-service-sdk</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.13</version> </dependency>
/** * @author v_liuwen * @date 2019-07-24 */ @Service(version = "1.0", interfaceClass = AsoService.class) @Component @Slf4j public class AsoServiceImpl implements AsoService { @Override public void saveAso(AsoInfoDto asoInfoDto) throws InterruptedException { //模拟阻塞超时 Thread.sleep(5000); log.info("插入售后单信息-->{}", JSONObject.toJSONString(asoInfoDto)); } }
3、新建module dubbo-service-web 依赖
<dependency> <groupId>top.qrainly</groupId> <artifactId>dubbo-service-sdk</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> <dependency> <groupId>com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.13</version> </dependency>
定义业务接口
/** * @author v_liuwen * @date 2019-07-24 */ public interface CreateAsoService { void saveAso(); }
定义业务实现
/** * @author v_liuwen * @date 2019-07-24 */ @Service @Slf4j public class CreateAsoServiceImpl implements CreateAsoService { @Reference(version = "1.0",check = false) private AsoService asoService; @Override public void saveAso() { AsoInfoDto mock = JMockData.mock(AsoInfoDto.class); try { asoService.saveAso(mock); } catch (InterruptedException e) { log.error("异常-->{}",e.getMessage()); } } }
测试类
@RunWith(SpringRunner.class) @SpringBootTest public class DubboWebApplicationTests { @Autowired private CreateAsoService createAsoService; @Test public void contextLoads() { createAsoService.saveAso(); } }
启动dubbo-service 运行测试方法 控制台输出
2019-07-25 21:03:24.655 INFO 6720 --- [:20880-thread-5] t.q.dubboservice.impl.AsoServiceImpl : 插入售后单信息-->{"asoId":8248,"asoNo":"aQg","createTime":2995060930132,"linkOrderNo":"zDv2"} 2019-07-25 21:03:27.603 INFO 6720 --- [:20880-thread-6] t.q.dubboservice.impl.AsoServiceImpl : 插入售后单信息-->{"asoId":8248,"asoNo":"aQg","createTime":2995060930132,"linkOrderNo":"zDv2"} 2019-07-25 21:03:30.616 INFO 6720 --- [:20880-thread-7] t.q.dubboservice.impl.AsoServiceImpl : 插入售后单信息-->{"asoId":8248,"asoNo":"aQg","createTime":2995060930132,"linkOrderNo":"zDv2"}
完美复现问题 刚开始就怀疑是不是重试配置导致的,但是看了一下@Reference的retries默认也是0,也就是说不配置的话默认重试0次,也就是不重试。 那为啥还调了三次。于是尝试配置了重试次数为-1
@Reference(version = "1.0",retries = -1,check = false)
再次操作 控制台输出
到此,相信大家对“dubbo接口调用造成的数据插入重复问题怎么解决”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。