1. 前置工作
先准备两个能独立跑起来的微服务(user-server 和 order-server)
将 所需 SQL 导入
/* Navicat Premium Data Transfer Source Server : CentOS7 Source Server Type : MySQL Source Schema : cloud Target Server Type : MySQL Date: 28/05/2023 10:19:17 */ SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for tb_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 DEFAULT NULL COMMENT '商品名称', `price` bigint(20) NOT NULL COMMENT '商品价格', `num` int(10) 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; -- ---------------------------- -- Records of tb_order -- ---------------------------- 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); -- ---------------------------- -- Table structure for tb_user -- ---------------------------- 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 DEFAULT NULL COMMENT '收件人', `address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci 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; -- ---------------------------- -- Records of tb_user -- ---------------------------- 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, '刘亦菲', '湖北省武汉市'); SET FOREIGN_KEY_CHECKS = 1;
1.1 搭建 user-server
1.1.1 pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.9.RELEASE</version> <relativePath/> </parent> <groupId>com.snow</groupId> <artifactId>cloud_user</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <servlet.version>3.1.0</servlet.version> <druid.version>1.1.20</druid.version> <mybatisplus.version>3.4.1</mybatisplus.version> <mysql.version>5.1.6</mysql.version> <commons.lang.version>3.7</commons.lang.version> <commons.codec.version>1.10</commons.codec.version> <fastjson.version>1.2.45</fastjson.version> <lombok.version>1.18.4</lombok.version> <!-- SpringCloud版本号 --> <spring-cloud.version>Hoxton.SR10</spring-cloud.version> </properties> <!-- 版本锁定: 并不是jar包导入 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <!-- 打包方式为pom: 说明我们导入的是一个父工程 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatisplus.version}</version> <exclusions> <exclusion> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> </exclusion> </exclusions> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <!-- 阿里JSON解析器 --> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.16</version> </dependency> </dependencies> </project>
1.1.2 po,mapper,controller
@Data @TableName("tb_user") public class User { @TableId(type = IdType.AUTO) private Long id; private String username; private String address; }
@Mapper public interface UserMapper extends BaseMapper<User> { }
@RestController @RequestMapping("/user") public class UserController { @Resource private UserMapper userMapper; @GetMapping("/get/{id}") public User get(@PathVariable("id") Long id){ return userMapper.selectById(id); } }
1.1.3 yml
server: port: 8081 spring: datasource: url: jdbc:mysql://192.168.0.108:3306/cloud?useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver
1.1.4 启动类
@SpringBootApplication public class UserServerApplication { public static void main(String[] args) { SpringApplication.run(UserServerApplication.class, args); } }
1.1.5 启动并访问
效果如下:
1.2 搭建 order-server
1.2.1 pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.9.RELEASE</version> <relativePath/> </parent> <groupId>com.snow</groupId> <artifactId>cloud_order</artifactId> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <servlet.version>3.1.0</servlet.version> <druid.version>1.1.20</druid.version> <mybatisplus.version>3.4.1</mybatisplus.version> <mysql.version>5.1.6</mysql.version> <commons.lang.version>3.7</commons.lang.version> <commons.codec.version>1.10</commons.codec.version> <fastjson.version>1.2.45</fastjson.version> <lombok.version>1.18.4</lombok.version> <!-- SpringCloud版本号 --> <spring-cloud.version>Hoxton.SR10</spring-cloud.version> </properties> <!-- 版本锁定: 并不是jar包导入 --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <!-- 打包方式为pom: 说明我们导入的是一个父工程 --> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <!-- druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>${druid.version}</version> </dependency> <!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>${mybatisplus.version}</version> <exclusions> <exclusion> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> </exclusion> </exclusions> </dependency> <!-- lombok --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>${lombok.version}</version> </dependency> <!-- 阿里JSON解析器 --> <dependency> <groupId>com.alibaba.fastjson2</groupId> <artifactId>fastjson2</artifactId> <version>2.0.16</version> </dependency> </dependencies> </project>
1.2.2 po mapper controller
@Data @TableName("tb_order") public class Order { @TableId(type = IdType.AUTO) private Long id; private Long price; private String name; private Integer num; private Long userId; @TableField(exist = false) private User user; }
@Data public class User { private Long id; private String username; private String address; }
@Mapper public interface OrderMapper extends BaseMapper<Order> { }
@RestController @RequestMapping("/order") public class OrderController { @Resource private OrderMapper orderMapper; @GetMapping("/get/{id}") public Order get(@PathVariable("id") Long id){ Order order = orderMapper.selectById(id); return order; } }
1.2.3 yml
server: port: 9091 spring: datasource: url: jdbc:mysql://192.168.0.108:3306/cloud?useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver
1.2.4 启动类
@SpringBootApplication public class OrderServerApplication { public static void main(String[] args) { SpringApplication.run(OrderServerApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate(){ return new RestTemplate(); } }
1.2.5 启动并访问
效果如下:
至此 两个独立的微服务就顺利的跑起来了。
1.3 两个服务通信
上面的两个独立服务已经跑起来了,因为上来看呢, 可以看到我们的 order服务是需要调用 user 服务的。如下图
可以看到我们的 order信息里的 user 是为null,因为我们还没有去调用 user 服务。
下来我们在 order服务里调用 user 服务。
改造 order 服务的 OrderController:
@RestController @RequestMapping("/order") public class OrderController { @Resource private OrderMapper orderMapper; @Resource private RestTemplate restTemplate; @GetMapping("/get/{id}") public Order get(@PathVariable("id") Long id){ String url = "http://localhost:8081/user/get/"; Order order = orderMapper.selectById(id); User user = restTemplate.getForObject(url + order.getUserId(), User.class); order.setUser(user); return order; } }
启动,访问
可以看到 order 里有 user 的数据了 !OK!