当前位置:网站首页>Microservice system design -- data model and system architecture design

Microservice system design -- data model and system architecture design

2022-06-24 18:37:00 Zhuangxiaoyan

Abstract

After sorting out the previous requirements , The demand for parking fees in shopping malls is very clear , This section is based on the previous output as input , Start system design , Including function module design 、 Storage design 、 Architecture design, etc , Provide a good basic guarantee for the following coding .

One 、 Design entity data

Based on the above business conditions , It is divided into seven small modules according to the field , Each module is divided into corresponding entities 、 event , Key data entities are sketched out by software - Contact diagram ( Not all entities are included ), As shown in the figure below :

  1. members , vehicle , Monthly card ( Bind cell phone number , Enter the vehicle , Monthly card )
  2. Parking lot , Gate ( The vehicle stops 、 Vehicle departure )
  3. integral ( Sign in 、 exchange )
  4. Billing rules ( admission 、 appearance )
  5. Transaction flow ( payment 、 Recharge )
  6. news ( push )
  7. Wash the car

Two 、 Business module design

According to the first demand analysis , We have identified key processes 、 Main business modules and main business entities in the modules 、 Entity related events . This case can be developed in the mode of single entity , But in order to simulate the scenario of microservice development , Therefore, the design method of microservices is followed here . Based on key business entity contacts and events , Integrate the business module into seven sub services .

  • Member services , Include member information 、 Vehicle information 、 Monthly membership card
  • Basic resource services , Including parking spaces 、 Gate , Vehicle parking records
  • Billing services , Vehicle entry and exit records , Billing rules
  • Points service , Redeem points , Membership score , Member sign in points
  • Financial services , Payment flow , Recharge flow , Financial statistics
  • Message service , Record the contents of the notice
  • Car wash service , The car wash coupon redeemed for points goes to the car wash shop

How fine is the granularity of service splitting , There is no uniform standard in the industry , It must be based on the company team 、 Personnel capability level and product usage , Don't be too careful , Too coarse also loses the meaning of microservices . Each microservice can be maintained by two or three developers , Avoid excessive maintenance , Distract oneself , At the same time, it can ensure rapid response to maintenance and upgrading .

3、 ... and 、 Storage system design

One benefit of the microservice architectural style , Is a persistent encapsulation . We can according to the needs of each service , Choose different persistence technologies . Select the data storage method according to the characteristics of each data type , That is, mixed persistence , Mix structured storage with unstructured storage . Different storage models are used between different services , Mixed storage can also be used in a single service . Since the microservice structure is to be adopted , From independent development 、 function 、 Deployment and operation and maintenance are separate small applications . The internal business logic processing of each small application , Access to database , And the database is independent .

According to the business scenario of this case , We split into seven sub repositories , Respectively :

  • park_member—— Member Services Library
  • park_resource—— Parking lot resource service Library
  • park_charging—— Billing service library
  • park_card—— Points service Library
  • park_finance—— Financial service library
  • park_message—— Message service library
  • park-carwash—— Car wash service shop

In fact, there are some teams that implement microservices , Split Services , But the repository is still a , In reality, there should be quite a few . You can't say wrong , It can only be said that it does not conform to the suggestions of microservice .

Structured storage uses the community version MySQL 5.7+ edition , Unstructured storage mainly involves caching , use Redis 4.0 + edition .( Here we need to select the appropriate data storage tool according to the specific data storage type , Whether transactions are required , The query efficiency …… etc. .) It is suggested to design some common fields in structured storage , It is mainly used for tracking data records , The common fields of the library table structure are as follows :

 `create_by` varchar(32) DEFAULT NULL COMMENT ' founder ',
  `create_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ' Date of creation ',
  `update_by` varchar(32) DEFAULT NULL COMMENT ' Updated by ',
  `update_date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT ' Updated date ',
  `remark` varchar(500) DEFAULT NULL COMMENT ' remarks ',
  `version` int(4) DEFAULT '0' COMMENT ' edition ',
  `state` int(4) DEFAULT '1' COMMENT ' state '

The creator of each data record 、 Creation time , Subsequent changes by 、 Change the time , Non business level remarks 、 edition 、 state , It is helpful for data maintenance personnel to identify , It is recommended to add... To each table . The database creation script is as follows

CREATE DATABASE `park_member` CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE DATABASE `park_resource` CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE DATABASE `park_charging` CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE DATABASE `park_card` CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE DATABASE `park_finance` CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE DATABASE `park_message` CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE DATABASE `park_carwash` CHARACTER SET utf8 COLLATE utf8_general_ci;

3.1 park_member Library initialization table structure

-- ----------------------------
-- Table structure for member
-- ----------------------------
DROP TABLE IF EXISTS `member`;
CREATE TABLE `member` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `phone` varchar(11) DEFAULT NULL COMMENT ' cell-phone number ',
  `birth` varchar(10) DEFAULT NULL COMMENT ' Birthday ',
  `full_name` varchar(20) DEFAULT NULL COMMENT ' full name ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Member information ';

-- ----------------------------
-- Table structure for month_card
-- ----------------------------
DROP TABLE IF EXISTS `month_card`;
CREATE TABLE `month_card` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `card_no` varchar(20) DEFAULT NULL COMMENT ' Membership card No ',
  `start` varchar(16) DEFAULT NULL COMMENT ' Start of validity period ',
  `ends` varchar(16) DEFAULT NULL COMMENT ' Expiry date: ',
  `member_id` varchar(32) DEFAULT NULL COMMENT ' Membership number ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Member monthly card information ';

-- ----------------------------
-- Table structure for vehicle
-- ----------------------------
DROP TABLE IF EXISTS `vehicle`;
CREATE TABLE `vehicle` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `member_id` varchar(32) DEFAULT NULL COMMENT ' Membership number ',
  `plate_no` varchar(10) DEFAULT NULL COMMENT ' license plate number ',
  `vehicle_inf` varchar(50) DEFAULT NULL COMMENT ' Vehicle model ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Member vehicles ';

3.2 park_resource Library initialization table structure

-- ----------------------------
-- Table structure for brake
-- ----------------------------
DROP TABLE IF EXISTS `brake`;
CREATE TABLE `brake` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `code` varchar(20) DEFAULT NULL COMMENT ' Number ',
  `desc` varchar(50) DEFAULT NULL COMMENT ' describe ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Parking lot gate ';

-- ----------------------------
-- Table structure for stall
-- ----------------------------
DROP TABLE IF EXISTS `stall`;
CREATE TABLE `stall` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `code` varchar(10) DEFAULT NULL COMMENT ' Number ',
  `is_parked` int(2) DEFAULT NULL COMMENT ' Is it occupied ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Parking space table ';

-- ----------------------------
-- Table structure for stall_parked
-- ----------------------------
DROP TABLE IF EXISTS `stall_parked`;
CREATE TABLE `stall_parked` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `stall_id` varchar(32) DEFAULT NULL COMMENT ' Parking space number ',
  `plate_no` varchar(30) DEFAULT NULL COMMENT ' License plate ',
  `mtype` int(2) DEFAULT NULL COMMENT '0  admission ,1  appearance ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Parking records ';

3.3 park_charging Library initialization table structure

-- ----------------------------
-- Table structure for charging_rule
-- ----------------------------
DROP TABLE IF EXISTS `charging_rule`;
CREATE TABLE `charging_rule` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `start` int(4) DEFAULT NULL COMMENT ' Start of parking time ',
  `end` int(4) DEFAULT NULL COMMENT ' The parking time is over ',
  `fee` float DEFAULT NULL COMMENT ' charge ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Billing rules ';

-- ----------------------------
-- Table structure for entrance
-- ----------------------------
DROP TABLE IF EXISTS `entrance`;
CREATE TABLE `entrance` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `member_id` varchar(32) DEFAULT NULL COMMENT ' Membership number ',
  `plate_no` varchar(10) DEFAULT NULL COMMENT ' License plate ',
  `brake_id` varchar(32) DEFAULT NULL COMMENT ' Gate number ',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `no_idx` (`plate_no`),
  KEY `member_idx` (`member_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Vehicles enter ';

-- ----------------------------
-- Table structure for vexists
-- ----------------------------
DROP TABLE IF EXISTS `vexists`;
CREATE TABLE `vexists` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `member_id` varchar(32) DEFAULT NULL COMMENT ' Membership number ',
  `brake_id` varchar(32) DEFAULT NULL COMMENT ' Gate number ',
  `plate_no` varchar(32) DEFAULT NULL COMMENT ' license plate number ',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `no_idx` (`plate_no`),
  KEY `member_idx` (`member_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' The vehicle drove out ';

3.4 park_card Library initialization table structure

-- ----------------------------
-- Table structure for exchange
-- ----------------------------
DROP TABLE IF EXISTS `exchange`;
CREATE TABLE `exchange` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `member_id` varchar(32) DEFAULT NULL COMMENT ' Membership number ',
  `card_qty` int(11) DEFAULT NULL COMMENT ' Number of points ',
  `ctype` int(4) DEFAULT NULL COMMENT '0  Coupon ,1  Car wash voucher ',
  `code` varchar(30) DEFAULT NULL COMMENT ' Coupon code ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Redeem points ';

-- ----------------------------
-- Table structure for member_card
-- ----------------------------
DROP TABLE IF EXISTS `member_card`;
CREATE TABLE `member_card` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `member_id` varchar(32) DEFAULT NULL COMMENT ' Membership number ',
  `cur_qty` varchar(20) DEFAULT NULL COMMENT ' Currently available points ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Membership score ';

-- ----------------------------
-- Table structure for member_sign
-- ----------------------------
DROP TABLE IF EXISTS `member_sign`;
CREATE TABLE `member_sign` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `member_id` varchar(32) DEFAULT NULL COMMENT ' Membership number ',
  `cnt` int(4) DEFAULT NULL COMMENT ' Number of points ',
  `ctype` int(4) DEFAULT NULL COMMENT '0  Sign in ,1  Shopping mall consumption ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Member sign in ';

3.5 park_finance Library initialization table structure

-- ----------------------------
-- Table structure for billing
-- ----------------------------
DROP TABLE IF EXISTS `billing`;
CREATE TABLE `billing` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `member_id` varchar(32) DEFAULT NULL COMMENT ' Membership number ',
  `fee` float DEFAULT '0' COMMENT ' Pay the amount ',
  `plate_no` varchar(10) DEFAULT NULL COMMENT ' license plate number ',
  `duration` float DEFAULT '0' COMMENT ' Parking Duration ',
  PRIMARY KEY (`id`) USING BTREE,
  KEY `no_idx` (`plate_no`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' The vehicle drives out of the payment flow ';

-- ----------------------------
-- Table structure for month_card_recharge
-- ----------------------------
DROP TABLE IF EXISTS `month_card_recharge`;
CREATE TABLE `month_card_recharge` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `card_no` varchar(20) DEFAULT NULL COMMENT ' Monthly card No ',
  `member_id` varchar(32) DEFAULT NULL COMMENT ' Membership number ',
  `amount` float DEFAULT NULL COMMENT ' Top up amount ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Member monthly card recharge ';

3.6 park_message Library initialization table structure

-- ----------------------------
-- Table structure for message
-- ----------------------------
DROP TABLE IF EXISTS `message`;
CREATE TABLE `message` (
  `id` varchar(32) NOT NULL DEFAULT '',
  `mtype` char(10) DEFAULT NULL COMMENT ' Message type ,PAY  Payment message ,BIND  The binding information ',
  `mcontent` varchar(500) DEFAULT NULL COMMENT ' The message content ',
  `member_id` varchar(32) DEFAULT NULL COMMENT ' members ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT=' Push message ';

3.7 park_carwash Library table structure

-- ----------------------------
-- Table structure for car_wash
-- ----------------------------
DROP TABLE IF EXISTS `car_wash`;
CREATE TABLE `car_wash` (
  `id` varchar(32) NOT NULL,
  `member_id` varchar(32) DEFAULT NULL COMMENT ' Membership number ',
  `plate_no` varchar(10) DEFAULT NULL COMMENT ' license plate number ',
  `ticket_code` varchar(20) DEFAULT NULL COMMENT ' Code of car wash coupon ',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

3.8 Initialize test data

With a preliminary database model , Some data needs to be initialized , For example, billing rules 、 Gate information , Parking information .

3.8.1 Gate data :

INSERT INTO `brake` VALUES ('4edb0820241041e5a0f08d01992de4c0', 'ct1', ' Entrance ', 'admin', '2019-12-27 11:37:22', NULL, '2019-12-27 11:37:22', NULL, 0, 1);
INSERT INTO `brake` VALUES ('989170c529a348b3b93bf2a4653e8ea9', 'ct2', ' Entrance ', 'admin', '2019-12-27 11:37:45', NULL, '2019-12-27 11:37:45', NULL, 0, 1);
INSERT INTO `brake` VALUES ('e489029055654bccb3cd601f0be71c41', 'ct3', ' Exit ', 'admin', '2019-12-27 11:37:36', NULL, '2019-12-27 11:37:36', NULL, 0, 1);
INSERT INTO `brake` VALUES ('f726873ed17441ea8dfbf78381bcde78', 'ct4', ' Exit ', 'admin', '2019-12-27 11:37:41', NULL, '2019-12-27 11:37:41', NULL, 0, 1);

3.8.2 Parking space data :

INSERT INTO `stall` VALUES ('004ac347b94e42bb8f0f6febd3265e35', 'P336', 0, 'admin', '2019-12-27 11:42:03', NULL, '2019-12-27 11:42:03', NULL, 0, 1);
INSERT INTO `stall` VALUES ('008773e089664ce49607c86b89dd8c0f', 'P250', 0, 'admin', '2019-12-27 11:42:03', NULL, '2019-12-27 11:42:03', NULL, 0, 1);
INSERT INTO `stall` VALUES ('0110ef02554f46ce91a3eeec6ecf2f95', 'P224', 0, 'admin', '2019-12-27 11:42:03', NULL, '2019-12-27 11:42:03', NULL, 0, 1);
INSERT INTO `stall` VALUES ('014f1f2b972e4e0092d749a7437f824d', 'P577', 0, 'admin', '2019-12-27 11:42:04', NULL, '2019-12-27 11:42:04', NULL, 0, 1);
INSERT INTO `stall` VALUES ('019f4aa0c22849e1a5758aaa33b855df', 'P229', 0, 'admin', '2019-12-27 11:42:03', NULL, '2019-12-27 11:42:03', NULL, 0, 1);

3.8.3  Billing rules :

INSERT INTO `charging_rule` VALUES ('41ed927623cf4a0bb5354b10100da992', 0, 30, 0, 'admin', '2019-12-27 11:26:08', NULL, '2019-12-27 11:26:08', '30  Free in minutes ', 0, 1);
INSERT INTO `charging_rule` VALUES ('41ed927623cf4a0bb5354b10100da993', 31, 120, 5, 'admin', '2019-12-27 11:26:12', NULL, '2019-12-27 11:26:12', '2  Within hours ,5  element ', 0, 1);
INSERT INTO `charging_rule` VALUES ('4edb0820241041e5a0f08d01992de4c0', 121, 720, 10, 'admin', '2019-12-27 11:34:06', NULL, '2019-12-27 11:34:06', '2  hours  12  Within hours ,10  element ', 0, 1);
INSERT INTO `charging_rule` VALUES ('7616fb412e824dcda41ed9367feadfda', 721, 1440, 20, 'admin', '2019-12-27 13:35:37', NULL, '2019-12-27 13:35:37', '12  these  24  when ,20  element ', 0, 1);

3.8.4  Unstructured storage

The main use of Redis ,MongoDB And other middleware to store the real-time information of available parking spaces , Billing rule information and other thermal data .

Four 、 System architecture design

There is no optimal architecture , Only the most appropriate architecture , All system design principles should take solving business problems as the ultimate goal , And with the development of business , Continuous iterative evolution . Through the above business module 、 Partition of storage model , The basic code architecture is already clearly visible . Integrated business module 、 Microservice architecture features , Output functional architecture design drawings .

Based on the overall functional architecture diagram , The corresponding functions can be realized by using specific functional components . It was also mentioned in the early stage ,Spring Cloud The whole family bucket contains many components , Open the box , This provides great convenience for quickly starting the development of microservices . meanwhile , Then, it should be integrated into the development and practice of some commonly used efficient tools to improve the coding efficiency, such as Lombok,MBG etc. .

Blog reference

原网站

版权声明
本文为[Zhuangxiaoyan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241324330330.html