演进:单一应用架构->垂直应用架构->分布式服务架构->流动计算架构
国内微服务门派:
数据库表之间的关系
create table `product_info`( `product_id` varchar(32) not null, --企业级的用varchar,自己玩的项目可以用自增的但数量大了可能不够用 `product_name` varchar(64) not null comment '商品名称', `product_price` decimal(8,2) not null comment '单价', `product_stock` int not null comment '库存', `product_description` varchar(64) comment '描述', `product_icon` varchar(512) comment '小图', `category_type` int not null comment '类目编号', `create_time` timestamp not null default current_timestamp comment '创建时间', `update_time` timestamp not null default current_timestamp on update current_timestamp comment '更新时间', --MYSQL5.7才可以default current_timestamp primary key (`product_id`) ) comment '商品表';
create table `product_category`( `category_id` int not null auto_increment, -- 类目不太可能爆多,所以可以自增 `category_name` varchar(64) not null comment '类目名字', `category_type` int not null comment '类目编号', `create_time` timestamp not null default current_timestamp comment '创建时间', `update_time` timestamp not null default current_timestamp on update current_timestamp comment '更新时间', --MYSQL5.7才可以default current_timestamp primary key (`category_id`) unique key `uqe_category_type` (`category_type`) --类目是唯一的 ) comment '类目表'
create table `order_master`( `order_id` varchar(32) not null, `buyer_name` varchar(32) not null comment '买家名字', `buyer_phone` varchar(32) not null comment '买家电话', `buyer_address` varchar(128) not null comment '买家地址', `buyer_openid` varchar(64) not null comment '买家微信openid', `order_amount` decimal(8,2) not null comment '订单总金额', `order_status` tinyint(3) not null default '0' comment '订单状态,默认0新下单', `pay_status` tinyint(3) not null default '0' comment '支付状态,默认0未支付', `create_time` timestamp not null default current_timestamp comment '创建时间', `update_time` timestamp not null default current_timestamp on update current_timestamp comment '更新时间', --MYSQL5.7才可以default current_timestamp primary key (`order_id`), key `idx_buyer_openid` (`buyer_openid`) ) comment '订单表';
create table `order_detail`( `detail_id` varchar(32) not null, `order_id` varchar(32) not null, `product_id` varchar(32) not null, `product_name` varchar(64) not null comment '商品名字', `product_price` decimal(8,2) not null comment '商品价格', `product_quantity` int not null comment '商品数量', `product_icon` varchar(512) comment '商品小图', `create_time` timestamp not null default current_timestamp comment '创建时间', `update_time` timestamp not null default current_timestamp on update current_timestamp comment '更新时间', --MYSQL5.7才可以default current_timestamp primary key (`detail_id`), key `idx_order_id`(`order_id`) ) comment '订单详情表';
<?xml version="1.0" encoding="UTF-8" ?> <configuration> <appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern> %d - %msg%n </pattern> </layout> </appender> <appender name="fileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>DENY</onMatch> <onMismatch>ACCEPT</onMismatch> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>/Users/ztev/tomcat/sell/info.%d.log</fileNamePattern> </rollingPolicy> </appender> <appender name="fileErrorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> <encoder> <pattern> %msg%n </pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>/Users/ztev/tomcat/sell/error.%d.log</fileNamePattern> </rollingPolicy> </appender> <root level="info"> <appender-ref ref="consoleLog"/> <appender-ref ref="fileInfoLog"/> <appender-ref ref="fileErrorLog"/> </root> </configuration>
买家端类目模块的开发,按照dao->service->api的顺序开发。贯穿单元测试。
@Entity public class ProductCategory { @Id @GeneratedValue private Integer categoryId; private String categoryName; private Integer categoryType; ... }
@Transactional
就可以回滚插入的测试数据 <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency>
categoryType
可以定义一个枚举 @Getter public enum ProductStatusEnum { UP(0, "在架"), DOWN(1, "下架"); private Integer code; private String message; ProductStatusEnum(Integer code, String message) { this.code = code; this.message = message; } }
server: context-path: /sell
VO
,通过 @JsonProperty
可以起名不一致 @Data public class ProductVO { @JsonProperty("name") private String categoryName; private Integer type; }
OrderMaster
和 OrderDetail
之间有关联在创建订单的时候,用一个 OrderDTO
的类把他们连接起来 @Data public class OrderForm { @NotEmpty(message = "姓名必填") private String name; @NotEmpty(message = "手机号必填") private String phone; @NotEmpty(message = "地址必填") private String address; @NotEmpty(message = "openid必填") private String openid; @NotEmpty(message = "购物车不能为空") private String items; } .... public ResultVO<Map<String, String>> create(@Valid OrderForm orderForm, BindingResult bindingResult) {
JsonSerialize
public class Date2LongSerializer extends JsonSerializer<Date> { @Override public void serialize(Date date, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException { jsonGenerator.writeNumber(date.getTime() / 1000); } } //另一个类 @JsonSerialize(using = Date2LongSerializer.class) private Date updateTime;
application.yml
上用 jackson:default-property-inclusion:non_null
ab -n 100-c 100http://www.baidu.com
-n是请求,-c是并发,相当于100个人同时访问 ab -t 60-c 100http://www.baidu.com
-t是60秒,连续60秒并发100个 @Cacheable(cacheNames ="product",key ="123")
@CachePut(cacheNames ="product",key="123")
https://github.com/linxinzhe/java-springboot-sell