上章节介绍了如何搭建订单服务,下面将继续搭建用户服务。
创建 cloud_user 数据库,执行如下 SQL:
SET NAMES utf8mb4; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure for t_user -- ---------------------------- DROP TABLE IF EXISTS `t_user`; CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID', `name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL COMMENT '姓名', `age` int(11) NULL DEFAULT NULL COMMENT '年龄', `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '电子邮件', PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 5 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic; -- ---------------------------- -- Records of t_user -- ---------------------------- INSERT INTO `t_user` VALUES (1, '张珊', 27, 'zhangs@gmail.com'); INSERT INTO `t_user` VALUES (2, '李斯', 32, 'mrli@163.com'); INSERT INTO `t_user` VALUES (3, '王武', 29, 'w2u@sina.com'); INSERT INTO `t_user` VALUES (4, '赵柳', 38, 'zhaoliu@sina.com'); SET FOREIGN_KEY_CHECKS = 1;
运行 SQL 后,数据库中仅存在 t_user 表,如下图:
该表的内容如下图:
项目依赖如下,从父项目继承依赖:
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.hxstrive.springcloud</groupId> <artifactId>springcloud_learn</artifactId> <version>0.0.1-SNAPSHOT</version> <relativePath/> <!-- lookup parent from repository --> </parent> <artifactId>service_user</artifactId> <name>service_user</name> <description>service_user</description> <dependencies> <!-- 从父项目继承版本信息 --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> </project>
下面 bootstrap.yml 配置文件内容(注意配置文件名称):
server: port: 8081 spring: application: name: service-order # datasource datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/cloud_order?useUnicode=true&characterEncoding=utf-8 username: root password: aaaaaa cloud: # nacos 配置 nacos: # nacos 服务发现 discovery: server-addr: localhost:8848 username: nacos password: nacos # nacos 服务配置 config: server-addr: localhost:8848 username: nacos password: nacos refresh-enabled: true file-extension: yaml group: DEFAULT_GROUP logging: level: root: warn
package com.hxstrive.springcloud.user.entity; /** * 用户实体 * @author hxstrive.com */ public class UserEntity { /** 用户ID */ private int id; /** 姓名 */ private String name; /** 年龄 */ private int age; /** 电子邮件地址 */ private String email; // 省略 setter 和 getter }
package com.hxstrive.springcloud.user.mapper; import com.hxstrive.springcloud.user.entity.UserEntity; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; /** * 用户 Mapper * @author hxstrive.com */ @Mapper public interface UserMapper { @Select("select * from t_user where id=#{userId}") UserEntity getUser(int userId); }
package com.hxstrive.springcloud.user.service; import com.hxstrive.springcloud.user.entity.UserEntity; import com.hxstrive.springcloud.user.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; /** * 用户服务 * @author hxstrive.com */ @Service public class UserService { @Autowired private UserMapper userMapper; public UserEntity getUser(int userId) { return userMapper.getUser(userId); } }
package com.hxstrive.springcloud.user.controller; import com.hxstrive.springcloud.user.entity.UserEntity; import com.hxstrive.springcloud.user.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * 用户 Controller * @author hxstrive.com */ @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/{id}") public UserEntity getUser(@PathVariable("id") int id) { System.out.println("## UserController.getUser() id=" + id); return userService.getUser(id); } }
package com.hxstrive.springcloud.user; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class UserApplication { public static void main(String[] args) { SpringApplication.run(UserApplication.class, args); } }
当我们完成上面用户和订单服务的创建后,分别启动服务,此时查看 nacos,服务信息如下图:
使用浏览器验证服务:
(1)访问 http://localhost:8081/order/1 地址,获取订单信息,如下:
{ "id": 1, "title": "海尔冰箱335升 法式多门四开门冰箱 一级能效 风冷无霜 双变频省电 节能净味 家用大容量超薄变温 多门推荐 BCD-335WLHFD9DS9", "num": 1, "price": 2598, "userId": 1, "user": { "id": 1, "name": "张珊", "age": 27, "email": "zhangs@gmail.com" } }
注意,上面的 user 对象是通过 RestTemplate 接口调用从用户服务获得。
(2)访问 http://localhost:8082/user/1 地址,获取用户信息,如下:
{ "id": 1, "name": "张珊", "age": 27, "email": "zhangs@gmail.com" }