简单的微服务架构
父pom.xml
- "1.0" encoding="UTF-8"?>
-
"http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
-
org.springframework.boot -
spring-boot-starter-parent -
2.3.9.RELEASE -
-
-
4.0.0 -
-
com.xkj.org -
cloud-demo -
pom -
1.0-SNAPSHOT -
-
- <module>order-servicemodule>
- <module>user-servicemodule>
- <module>eureka-servermodule>
-
-
-
-
UTF-8 -
UTF-8 -
1.8 -
Hoxton.SR10 -
5.1.47 -
2.1.1 -
-
-
-
-
-
org.springframework.cloud -
spring-cloud-dependencies -
${spring-cloud.version} -
pom -
import -
-
-
mysql -
mysql-connector-java -
${mysql.version} -
-
-
org.mybatis.spring.boot -
mybatis-spring-boot-starter -
${mybatis.version} -
-
-
-
-
-
org.projectlombok -
lombok -
-
user-service的pom.xml文件
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
-
cloud-demo -
com.xkj.org -
1.0-SNAPSHOT -
-
4.0.0 -
-
user-service -
pom -
-
-
-
org.springframework.boot -
spring-boot-starter-web -
-
-
-
mysql -
mysql-connector-java -
-
-
-
org.mybatis.spring.boot -
mybatis-spring-boot-starter -
-
-
-
application.yml
- server:
- port: 8081
- spring:
- datasource:
- url: jdbc:mysql://localhost:3306/cloud_user?useSSL=false
- username: root
- password: 123456
- driver-class-name: com.mysql.jdbc.Driver
- application:
- name: user-service
- mybatis:
- type-aliases-package: com.xkj.org.pojo
- configuration:
- map-underscore-to-camel-case: true
-
- logging:
- level:
- com.xkj.org: debug
- pattern:
- dateformat: MM-dd HH:mm:ss:SSS
order-service的关键配置
- "1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
-
-
cloud-demo -
com.xkj.org -
1.0-SNAPSHOT -
-
4.0.0 -
-
order-service -
-
-
-
org.springframework.boot -
spring-boot-starter-web -
-
-
-
mysql -
mysql-connector-java -
-
-
-
org.mybatis.spring.boot -
mybatis-spring-boot-starter -
-
-
-
order-service的application.yml文件
- server:
- port: 8080
- spring:
- datasource:
- url: jdbc:mysql://localhost:3306/cloud_order?useSSL=false
- username: root
- password: 123456
- driver-class-name: com.mysql.jdbc.Driver
- application:
- name: order-service
- mybatis:
- type-aliases-package: com.xkj.org.pojo
- configuration:
- map-underscore-to-camel-case: true
-
- logging:
- level:
- com.xkj.org: debug
- pattern:
- dateformat: MM-dd HH:mm:ss:SSS
配置RestTemplate Bean,交给spring容器:
- @SpringBootApplication
- @MapperScan("com.xkj.org.mapper")
- public class OrderApplication {
-
- public static void main(String[] args) {
- SpringApplication.run(OrderApplication.class, args);
- }
-
- @Bean
- public RestTemplate restTemplate() {
- return new RestTemplate();
- }
- }
远程调用user-service的接口:
- @Service
- public class OrderServiceImpl implements OrderService {
-
- @Autowired
- private OrderMapper orderMapper;
-
- @Autowired
- private RestTemplate restTemplate;
-
- @Override
- public Order queryOrderById(Long orderId) {
- Order order = orderMapper.findById(orderId);
- String url = "http://localhost:8081/user/" + order.getUserId();
- User user = restTemplate.getForObject(url, User.class);
- order.setUser(user);
- return order;
- }
- }
- DROP TABLE IF EXISTS `tb_order`;
- CREATE TABLE `tb_order` (
- `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '订单id',
- `user_id` bigint(20) NOT NULL COMMENT '用户id',
- `name` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '商品名称',
- `price` bigint(20) NOT NULL COMMENT '商品价格',
- `num` int(10) NULL DEFAULT 0 COMMENT '商品数量',
- PRIMARY KEY (`id`) USING BTREE,
- UNIQUE INDEX `username`(`name`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 109 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-
-
- INSERT INTO `tb_order` VALUES (101, 1, 'Apple 苹果 iPhone 12 ', 699900, 1);
- INSERT INTO `tb_order` VALUES (102, 2, '雅迪 yadea 新国标电动车', 209900, 1);
- INSERT INTO `tb_order` VALUES (103, 3, '骆驼(CAMEL)休闲运动鞋女', 43900, 1);
- INSERT INTO `tb_order` VALUES (104, 4, '小米10 双模5G 骁龙865', 359900, 1);
- INSERT INTO `tb_order` VALUES (105, 5, 'OPPO Reno3 Pro 双模5G 视频双防抖', 299900, 1);
- INSERT INTO `tb_order` VALUES (106, 6, '美的(Midea) 新能效 冷静星II ', 544900, 1);
- INSERT INTO `tb_order` VALUES (107, 2, '西昊/SIHOO 人体工学电脑椅子', 79900, 1);
- INSERT INTO `tb_order` VALUES (108, 3, '梵班(FAMDBANN)休闲男鞋', 31900, 1);
-
- DROP TABLE IF EXISTS `tb_user`;
- CREATE TABLE `tb_user` (
- `id` bigint(20) NOT NULL AUTO_INCREMENT,
- `username` varchar(100) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '收件人',
- `address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '地址',
- PRIMARY KEY (`id`) USING BTREE,
- UNIQUE INDEX `username`(`username`) USING BTREE
- ) ENGINE = InnoDB AUTO_INCREMENT = 109 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
-
- INSERT INTO `tb_user` VALUES (1, '柳岩', '湖南省衡阳市');
- INSERT INTO `tb_user` VALUES (2, '文二狗', '陕西省西安市');
- INSERT INTO `tb_user` VALUES (3, '华沉鱼', '湖北省十堰市');
- INSERT INTO `tb_user` VALUES (4, '张必沉', '天津市');
- INSERT INTO `tb_user` VALUES (5, '郑爽爽', '辽宁省沈阳市大东区');
- INSERT INTO `tb_user` VALUES (6, '范兵兵', '山东省青岛市');
-