美文网首页
spring boot 微服务,单体共存的最后课题,不依赖第三方

spring boot 微服务,单体共存的最后课题,不依赖第三方

作者: 大继 | 来源:发表于2019-02-12 17:29 被阅读0次

    问题

    • 在未进行微服务化前的关联过度
    • 不想使用第三方组件。

    目前可实现方案大致有这几种

    • 同步数据到第三方组件例如elasticsearch这种库中重新聚合数据输出。
    • 使用第三服务 也就是 服务A,服务B , 使用服务C聚合输出。
    • 前端聚合,这种比较笨。
    • 等其他方案。

    不增加第三方组件,后期可拆分的方案

    • 在服务层加入关联定义

    实战

    • 在设计中避免一个服务引用其他服务超过多。
    • 在实际应用中多数都只有一个 类似user 的实体,懒关联。
    • 每个业务用户实体不同,都需要重新定义,但都必须有long userId.

    实例

    基础代码

    /**
     * 显示层由业务定义
     */
    @Entity
    @Table(name = "shelf")
    public class Shelf  {
    
        @Id
        @Column(length = 37)
        private String id;
    
        @Column
        private String name;
    
        @Column(length = 768)
        private String image;
    
        /**
         * 销售主体
         */
        @Column(length = 37)
        private String subjectClass;
    
        @Column(length = 37)
        private String subjectId;
    
        @Column
        private Double minPrice;
    
        @Column
        private Double maxPrice;
    
        @Column
        private Double originalMinPrice;
    
        @Column
        private Double originalMaxPrice;
    
        @Column
        private Long stock;
    
        /**
         * up down .
         */
        @Column(length = 16)
        private String status;
    
        /**
         * 供应商信息
         */
        @Column(length = 37)
        private String supplierSubjectClass;
    
        @Column(length = 37)
        private String supplierSubjectId;
    
        @Column
        private String supplierSubjectName;
    
        /**
         * 发起人
         */
        @Column
        private Long userId;
    
    //    @ManyToOne(fetch = FetchType.LAZY)
    //    @JoinColumn(name = "userId",foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT),updatable = false,insertable = false)
    //    private Class user;
    
        @Column
        private String description;
    
        @Column
        private Date updateTime;
    
        @Column
        private Date createTime;
        ......
    

    扩展用户信息代码

    @Entity
    @Table(name = "shelf")
    public class SiteShelf extends Shelf {
    
    
        @ManyToOne
        @JoinColumn(name = "userId",foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT),updatable = false,insertable = false)
        private User user;
    
        public User getUser() {
            return user;
        }
    
        public void setUser(User user) {
            this.user = user;
        }
    }
    

    测试

    @RunWith(SpringJUnit4ClassRunner.class)
    @SpringBootTest(classes = YmApplication.class)
    public class SiteShelfServiceImplTest {
    
    
        @Autowired
        private SiteShelfService siteShelfService;
    
    
        @Test
        public void test() throws JsonProcessingException {
    
    
            SiteShelf shelf = new SiteShelf();
    
            shelf.setId(UUID.randomUUID().toString());
            shelf.setUserId(1L);
    
            shelf = siteShelfService.save(shelf);
    
    
            ObjectMapper objectMapper = new ObjectMapper();
            System.out.println(objectMapper.writeValueAsString(shelf));
    
    
            shelf = siteShelfService.findById(shelf.getId());
            System.out.println(objectMapper.writeValueAsString(shelf));
    
    
        }
    
    }
    

    测试结果

    Hibernate: insert into shelf (create_time, description, image, max_price, min_price, name, original_max_price, original_min_price, status, stock, subject_class, subject_id, supplier_subject_class, supplier_subject_id, supplier_subject_name, update_time, user_id, dtype, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'SiteShelf', ?)
    {"id":"ff05d429-e494-460e-9fc0-7629cc324988","name":null,"image":null,"subjectClass":null,"subjectId":null,"minPrice":null,"maxPrice":null,"originalMinPrice":null,"originalMaxPrice":null,"stock":null,"status":null,"supplierSubjectClass":null,"supplierSubjectId":null,"supplierSubjectName":null,"userId":1,"description":null,"updateTime":null,"createTime":null,"user":null}
    Hibernate: select shelf0_.id as id2_10_0_, user1_.id as id1_13_1_, shelf0_.create_time as create_t3_10_0_, shelf0_.description as descript4_10_0_, shelf0_.image as image5_10_0_, shelf0_.max_price as max_pric6_10_0_, shelf0_.min_price as min_pric7_10_0_, shelf0_.name as name8_10_0_, shelf0_.original_max_price as original9_10_0_, shelf0_.original_min_price as origina10_10_0_, shelf0_.status as status11_10_0_, shelf0_.stock as stock12_10_0_, shelf0_.subject_class as subject13_10_0_, shelf0_.subject_id as subject14_10_0_, shelf0_.supplier_subject_class as supplie15_10_0_, shelf0_.supplier_subject_id as supplie16_10_0_, shelf0_.supplier_subject_name as supplie17_10_0_, shelf0_.update_time as update_18_10_0_, shelf0_.user_id as user_id19_10_0_, shelf0_.dtype as dtype1_10_0_, user1_.create_time as create_t2_13_1_, user1_.image as image3_13_1_, user1_.mobile as mobile4_13_1_, user1_.name as name5_13_1_, user1_.sex as sex6_13_1_, user1_.signature as signatur7_13_1_, user1_.update_time as update_t8_13_1_, user1_.user_detail_id as user_det9_13_1_, user1_.user_password_id as user_pa10_13_1_ from shelf shelf0_ inner join user user1_ on shelf0_.user_id=user1_.id where shelf0_.id=?
    Hibernate: select userdetail0_.id as id1_16_0_, userdetail0_.account_non_expired as account_2_16_0_, userdetail0_.account_non_locked as account_3_16_0_, userdetail0_.credentials_non_expired as credenti4_16_0_, userdetail0_.enabled as enabled5_16_0_, userdetail0_.user_id as user_id6_16_0_ from user_detail userdetail0_ where userdetail0_.id=?
    Hibernate: select userpasswo0_.id as id1_17_0_, userpasswo0_.create_time as create_t2_17_0_, userpasswo0_.non_expired as non_expi3_17_0_, userpasswo0_.password as password4_17_0_, userpasswo0_.salt as salt5_17_0_, userpasswo0_.update_time as update_t6_17_0_, userpasswo0_.user_id as user_id7_17_0_ from user_password userpasswo0_ where userpasswo0_.id=?
    Hibernate: select useraccoun0_.user_id as user_id6_14_0_, useraccoun0_.id as id1_14_0_, useraccoun0_.id as id1_14_1_, useraccoun0_.create_time as create_t2_14_1_, useraccoun0_.enable as enable3_14_1_, useraccoun0_.subject as subject4_14_1_, useraccoun0_.subject_id as subject_5_14_1_, useraccoun0_.user_id as user_id6_14_1_ from user_account useraccoun0_ where useraccoun0_.user_id=?
    Hibernate: select authoritie0_.user_id as user_id2_15_0_, authoritie0_.authority_id as authorit1_15_0_, authority1_.id as id1_2_1_, authority1_.create_time as create_t2_2_1_, authority1_.description as descript3_2_1_, authority1_.enable as enable4_2_1_, authority1_.name as name5_2_1_, authority1_.weight as weight6_2_1_ from user_authority authoritie0_ inner join authority authority1_ on authoritie0_.authority_id=authority1_.id where authoritie0_.user_id=?
    Hibernate: select authorityr0_.authority_id as authorit2_4_0_, authorityr0_.id as id1_4_0_, authorityr0_.id as id1_4_1_, authorityr0_.authority_id as authorit2_4_1_, authorityr0_.create_time as create_t3_4_1_, authorityr0_.resource_id as resource4_4_1_, resource1_.id as id1_9_2_, resource1_.create_time as create_t2_9_2_, resource1_.description as descript3_9_2_, resource1_.method as method4_9_2_, resource1_.name as name5_9_2_, resource1_.url as url6_9_2_ from authority_resource authorityr0_ left outer join resource resource1_ on authorityr0_.resource_id=resource1_.id where authorityr0_.authority_id=?
    {"id":"ff05d429-e494-460e-9fc0-7629cc324988","name":null,"image":null,"subjectClass":null,"subjectId":null,"minPrice":null,"maxPrice":null,"originalMinPrice":null,"originalMaxPrice":null,"stock":null,"status":null,"supplierSubjectClass":null,"supplierSubjectId":null,"supplierSubjectName":null,"userId":1,"description":null,"updateTime":null,"createTime":null,"user":{"id":1,"image":null,"name":"admin","mobile":null,"signature":null,"sex":null,"userPassword":{"id":"fa814b16-27c6-42b4-92c7-822619643e08","userId":1,"password":"$2a$10$oFmLe83KvkCf5Ht.8AMZ5uCjZqfiPxaAXrE2eBVQiC/TbFuy8X96m","salt":"salt","nonExpired":true,"updateTime":1549963230000,"createTime":1549963230000},"userDetail":{"id":"b5ebdda9-d78d-4fca-ad27-829d29928660","accountNonExpired":true,"accountNonLocked":true,"credentialsNonExpired":true,"userId":1,"enabled":true},"authorities":[{"id":"ROLE_admin","name":"系统管理员","enable":true,"description":"","weight":0,"authorityResources":[{"id":"1ce78559-fc78-42a7-b2ce-591eda7540c4","resource":{"id":"c97b91f9-e76b-4aa5-b45b-7ae218dc61af","name":"权限信息管理","url":"/authorities/**","method":"POST","description":"权限信息管理","createTime":1549963230000},"authorityId":"ROLE_admin","createTime":1549963230000},{"id":"24546cad-e454-4ccb-81da-76ba27f18778","resource":{"id":"3e613aa9-58af-4901-b6e6-a911176f3ad2","name":"用户信息维护","url":"/users/**","method":"POST","description":"用户信息修改,密码修改","createTime":1549963230000},"authorityId":"ROLE_admin","createTime":1549963230000},{"id":"350cbe5f-ee00-4732-b89b-156edc33b891","resource":{"id":"924e27f2-6e52-4454-a3c3-6f4382497c74","name":"用户查询","url":"/users/**","method":"GET","description":"用户信息列表,查询","createTime":1549963229000},"authorityId":"ROLE_admin","createTime":1549963230000},{"id":"4cfa0308-e821-468e-95b1-83d5915cad8c","resource":{"id":"322abb7a-e56c-4245-8ac1-1c1c77907230","name":"用户自定义分类","url":"/classifies/**","method":"POST","description":"用户自定义分类管理","createTime":1549963230000},"authorityId":"ROLE_admin","createTime":1549963230000},{"id":"946b2147-b205-499c-afd3-a72fb4ae5f3f","resource":{"id":"fcb4a4b4-b898-4ce9-a99e-ac228aa54d9f","name":"权限信息查询","url":"/articles/**","method":"GET","description":"文章查询","createTime":1549963230000},"authorityId":"ROLE_admin","createTime":1549963230000},{"id":"d04419d7-e6fc-498d-8b7b-c75f67efe6ee","resource":{"id":"eaa73a71-1e1a-4f66-bb44-b37f86b87a24","name":"用户自定义分类","url":"/classifies/**","method":"GET","description":"用户自定义分类","createTime":1549963230000},"authorityId":"ROLE_admin","createTime":1549963230000},{"id":"edc8753b-307c-4713-9636-246f388feb86","resource":{"id":"cb191853-8887-41a4-9f05-15510792dace","name":"资源信息管理","url":"/resources/**","method":"POST","description":"资源信息管理","createTime":1549963230000},"authorityId":"ROLE_admin","createTime":1549963230000},{"id":"f0fa686e-7aa7-451e-9bf4-b24fecf91f6c","resource":{"id":"a5d3265e-f0ca-4d7e-b37d-c7f2b10c2e65","name":"权限信息管理","url":"/articles/**","method":"POST","description":"文章管理","createTime":1549963230000},"authorityId":"ROLE_admin","createTime":1549963230000},{"id":"f1909477-f685-43a9-85f9-f855618abe42","resource":{"id":"3a25722f-43f7-44e9-a230-200ca0d6fdcf","name":"权限信息查询","url":"/authorities/**","method":"GET","description":"权限信息列表,查询","createTime":1549963230000},"authorityId":"ROLE_admin","createTime":1549963230000},{"id":"f3607ccd-a36a-4297-9719-79b2427e5ab6","resource":{"id":"3fae4573-c1f9-40ee-a839-66a91263c7a1","name":"资源信息查询","url":"/resources/**","method":"GET","description":"资源信息列表,查询","createTime":1549963230000},"authorityId":"ROLE_admin","createTime":1549963230000}],"createTime":1549963230000}],"userAccounts":[{"id":"befedb2a-773f-4a2f-91cd-397aa885c011","subject":"username","subjectId":"admin","userId":1,"enable":true,"createTime":1549963230000}],"updateTime":1549963230000,"createTime":1549963230000}}
    

    结束,希望对大家有所帮助

    相关文章

      网友评论

          本文标题:spring boot 微服务,单体共存的最后课题,不依赖第三方

          本文链接:https://www.haomeiwen.com/subject/rsfnjqtx.html