义乌购区域电商框架说明

   目前公司在做新的业务,同事搭建了一套完全独立的架构,可发挥的余地比较多,做了一下整理。

Svn 地址   http://10.6.6.232/svn/reb  (这个是公司内部的地址,对外是无法访问的)

采用 jdk8.0+   tomcat7.0+   各个文件编码UTF-8(preference中更改)

为了规范开发人员的开发,统一编码格式eclipse-format.xml 导入到eclipse java format 中。

 

整个工程的框架:


 

1.reb-api:

entity(不分bo po bean  service接口 utils 等一些每个模块都用到的地方

 

 

2.reb-client:

dubbo 客户端 存各个模块通用的bean配置(比如说发送邮件Service 消息发送service 等) 以及缓存service地方

这个工程中 如果和reb-service模块中的service有命名方式冲突请在前面加上Client关键字

如命名方式 ClientUserService(对应reb-service.UserService  Client+${etity}+Service 方式

需要使用dubbo 提供的service时 应在 reb-client-dubbo-consumer.xml中新增一条数据

 

    <dubbo:application name="reb-client" />

    <dubbo:registry address="zookeeper://10.6.104.77:2181" />


    <dubbo:reference id="fooService" interface="com.yiwugou.reb.api.service.demo.FooService" />
    <dubbo:reference id="barService" interface="com.yiwugou.reb.api.service.demo.BarService" />

 
 

缓存 请在service方法中加上 @CacheAnnotation注解

产生的缓存key  包名.类名.方法名(参数,参数,参数……)

"com.yiwugou.reb.client.user.ClientUserService.cacheSelect(50,username)"

 

清缓存  cacheDeleteService.delete(key);

 

 

3.reb-dao:

数据库访问模块 提供分表与非分表方式

分表方式:在sharding-jdbc.xml中增加分表策略

 

这就是把t_user 表分为 t_user_0 t_user_1 两张表

真实数据库中 并不存在t_user 表 只存在 t_user_0 t_user_1 两张表

mybatis.xml 中写法 还是select * from t_user; datasource会自动把t_user 分解成两张物理表(t_user_0 t_user_1) 分开查询

 

 

注意分表时候 xml insert写法 不要包含返回的自动生成的主键 应该用类似oracleseq自动生成主键

分表时 insert xml如下配置 不要包含selectkey

 

 

 

不分表时候 表id建议还是使用mysql的自增策略(你要使用seq也可以)

不分表情况下Insert时候 如果需要返回id

采用jdbcTemplate的方法 例如

BaseService.insertNoSharding(BaseEntity base); 返回自动生成的主键

如果不需要返回主键 则使用原先的 insert(T t)

其他操作照旧

 

使用insertNoSharding 时候 entity建议加上对应的数据库表名 @Table(name=”t_bar”) 以及不和数据库对应的列 @Transient

 

@Data
@Table(name = "t_bar")
public class Bar implements BaseEntity {
    private static final long serialVersionUID = 1L;

    private Long id;
    private String productName;
    private Long userId;
    private Long createTime;
    
    @Transient
    private String trans;

}

 

 

如果不加 默认的表名为 类名转下划线 如 WebProduct à web_product

 

注意 不分表情况下 建议只有insert需要返回主键的时候 采用jdbcTemplate 其他还是采用统一的mybatis

 

 

 

单库分表策略
由于macat不支持单库分表 转为采用 当当的开源项目 sharding-jdbc

单库分表有一些复杂的sql语句不支持 详细的请看sharding-jdbc 官网

https://github.com/dangdangdotcom/sharding-jdbc

例子中 t_user 为逻辑表  对应的物理表为 t_user_0t_user_1  (数据库中不存在 t_user 这个表)

分表中 主键不能使用自增长的方式 采用类似oracleseq生成策略
mysql
新增一表 REB_SEQ  mysql新增方法 fun_next_value 


 

4.reb-service:

Service端已经dubbo service

如果要使一个service注册到dobbo请在 reb-service-dubbo-provider.xml中新增

 

    <dubbo:application name="reb-service" />

    <dubbo:registry address="zookeeper://10.6.104.77:2181" />

    <dubbo:protocol name="dubbo" port="20880" />

    <dubbo:service interface="com.yiwugou.reb.api.service.demo.FooService" ref="fooService" />
    <dubbo:service interface="com.yiwugou.reb.api.service.demo.BarService" ref="barService" />

 

 

Dubbo 启动入口 DemoProvider (通过zookeeper  请去相关网址下载)

 

 

事务采用注解方式  请指定事务bean id 因为xml中配置了多个database事务

例如

 

    /**
     * 事务测试 一定要指定名称 因为配置了多个Transactional
     */
    @Override
    @Transactional("transactionManagerSharding")
    public void transactional() {
        System.err.println("before:" + this.count(null));
        Foo user1 = new Foo();
        long id = this.selectNextId("SEQ_T_USER");
        user1.setId(id);
        user1.setUsername("ppp1");
        user1.setCreateTime(DateUtils.toDatabaseTime());
        System.err.println(this.insert(user1));

        Foo user2 = new Foo();
        id = this.selectNextId("SEQ_T_USER");
        user2.setId(id);
        user2.setUsername("ppp2");
        user2.setCreateTime(DateUtils.toDatabaseTime());
        System.err.println(this.insert(user2));

        System.err.println(1 / 0);
    }

 

 

猜你喜欢

转载自5keit.iteye.com/blog/2311146