背景:公司需要做一个小程序,从把服务器数据库里面的数据拷贝到本地

技术:spring-boot, XXL-RPC, JPA

问题 : 客户端查询数据,调用RPC服务,一直报错 StackOverflowError:null,

问题重现:

数据表test

  1. CREATE TABLE test (
  2. "rid" int(11) NOT NULL AUTO_INCREMENT,
  3. "name" varchar(45) DEFAULT NULL,
  4. "age" int(11) DEFAULT NULL,
  5. "create_time" datetime DEFAULT NULL,
  6. PRIMARY KEY ("rid")
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Test实体类

  1. import java.io.Serializable;
  2. import java.time.LocalDateTime;
  3. import javax.persistence.Column;
  4. import javax.persistence.Entity;
  5. import javax.persistence.GeneratedValue;
  6. import javax.persistence.GenerationType;
  7. import javax.persistence.Id;
  8. import javax.persistence.Table;
  9. import lombok.Getter;
  10. import lombok.Setter;
  11. @Getter
  12. @Setter
  13. @Entity
  14. @Table(name = "test")
  15. public class TestEntity implements Serializable{
  16. /**
  17. *
  18. */
  19. private static final long serialVersionUID = -7189192698006290220L;
  20. @Id
  21. @GeneratedValue(strategy = GenerationType.IDENTITY)
  22. @Column(name = "rid")
  23. private Integer id;
  24. private String name;
  25. private Integer age;
  26. private LocalDateTime createTime;
  27. }

Repository ,使用的是JPA,具体关于JPARepository的知识,自己补习吧

  1. import org.springframework.data.jpa.repository.JpaRepository;
  2. public interface TestRepository extends JpaRepository<TestEntity, Integer>{
  3. }

Service服务,直接与数据库交互的

  1. public interface TestService {
  2. TestEntity save(TestEntity entity);
  3. }
  1. @Service
  2. public class TestServiceImpl implements TestService{
  3. @Resource
  4. private TestRepository testRepository;
  5. @Override
  6. public TestEntity save(TestEntity entity) {
  7. return testRepository.save(entity);
  8. }
  9. }

服务端开放的RPC服务

  1. public interface WqqTestRpcService {
  2. TestEntity save(TestEntity entity);
  3. }
  1. import javax.annotation.Resource;
  2. import org.springframework.stereotype.Service;
  3. import com.wqq.test.TestEntity;
  4. import com.wqq.test.WqqTestRpcService;
  5. import com.wqq.test.TestService;
  6. import com.xxl.rpc.remoting.provider.annotation.XxlRpcService;
  7. @XxlRpcService
  8. @Service
  9. public class WqqTestRpcServiceImpl implements WqqTestRpcService{
  10. @Resource
  11. private TestService testService;
  12. @Override
  13. public TestEntity save(TestEntity entity) {
  14. return testService.save(entity);
  15. }
  16. }

客户端使用

  1. @XxlRpcReference
  2. private WqqTestRpcService wqqTestRpcService;
  3. public void save() {
  4. TestEntity entity = new TestEntity();
  5. entity.setAge(23);
  6. entity.setName("我是谁");
  7. entity.setId(3);
  8. entity.setCreateTime(LocalDateTime.now());
  9. wqqTestRpcService.save(entity);
  10. }

习惯写完之后测试

测试TestService, WqqTestRpcService

  1. @ContextConfiguration(classes = TestServerApplication.class)
  2. @RunWith(SpringRunner.class)
  3. @SpringBootTest
  4. //@Transactional
  5. //@Rollback
  6. public class Test{
  7. @Resource
  8. private TestService testService;
  9. @Resource
  10. private WqqTestRpcService wqqTestRpcService;
  11. @Test
  12. public void test1() {
  13. TestEntity entity = new TestEntity();
  14. entity.setAge(23);
  15. entity.setName("测试1");
  16. entity.setId(1);
  17. entity.setCreateTime(LocalDateTime.now());
  18. wqqTestRpcService.save(entity);
  19. }
  20. @Test
  21. public void test2() {
  22. TestEntity entity = new TestEntity();
  23. entity.setAge(23);
  24. entity.setName("测试2");
  25. entity.setId(2);
  26. entity.setCreateTime(LocalDateTime.now());
  27. testService.save(entity);
  28. }
  29. }

结果:数据库插入两条数据,说明方法是没有问题的

正式运行。开始报错 Caused by: java.lang.StackOverflowError: null

百思不得其解,查了网上很多基本都是说死循环或者是无线递归,可是我只是调用一个保存的方法,哪里来的死循环还有递归,还有说改变配置,但是我测试方法是行得通的,直觉告诉我不会说是改变配置(真的直觉,但是可能别人是有这样解决的,反正我没有试),后来又进行了很多次测试,结果完全想不到是参数类型的问题,把TestEntity的参数类型与LocalDateTime改成Date就可以,具体为啥这样,还没结论,只是知道这么不再报错,但是是不是真的最好的结局办法,依然不知, 希望有大神可以解惑吧

 

posted on 2019-07-11 14:09 背着核的桃子 阅读() 评论() 编辑 收藏

版权声明:本文为Cassie-wang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/Cassie-wang/p/11169628.html