1、创建父工程
首先创建一个maven项目作为父项目
然后在pom.xml文件中导入以下依赖
<!--引入父依赖-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.2.RELEASE</version>
</parent>
<!--定义版本-->
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF- 8</project.reporting.outputEncoding>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
<spring-cloud-alibaba.version>2.2.5.RELEASE</spring-cloud-alibaba.version>
</properties>
<!--依赖管理-->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>${spring-cloud-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
版本依赖关系对照图
2、创建基础模块
1)在创建的父项目下创建shop-common模块
模板创建完成后,idea会自动在父项目的pom文件中添加以下内容
<modules>
<module>shop-common</module>
</modules>
2)修改shop-common的pom文件
其他模块只需要在pom文件中,引入shop-common的依赖,就可以获取对应的jar包
不需要在额外引入依赖
<dependencies>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20 </version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3)创建application.properties文件
# 因为在pom中添加了mysql依赖,所以要配置上数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springcloud
spring.datasource.password=root
spring.datasource.username=root
4)创建实体类
shop-order订单表结构如下图所示:
shop-product产品表机构如下
根据数据库表,创建实体类
创建的实体类作为公共的,可以被其他模块共用
/**
* (ShopProduct)表实体类
*
* @since 2021-07-06 17:27:51
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ShopProduct implements Serializable {
private Integer pid;
private String pname;
private Double pprice;
private Integer stock;
}
/**
* (ShopOrder)表实体类
*
* @since 2021-07-06 17:25:30
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ShopOrder implements Serializable{
@TableId(type = IdType.AUTO)
private Integer oid;
private Integer uid;
private String username;
private Integer pid;
private String panme;
private Double pprice;
private Integer number;
}
3、创建产品微服务模块
1)创建shop-product模块
2)修改pom文件
<!--引入shop-common模块中的依赖-->
<dependencies>
<dependency>
<groupId>com.lyq</groupId>
<artifactId>shop-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>
3)创建主启动类
/**
* @program: springclouddemo076
* @description: 产品模块主启动类
* @create: 2021-07-06 18:52
**/
@SpringBootApplication
@MapperScan("com.lyq.shopproduct.dao")
public class ShopProductMain {
public static void main(String[] args) {
SpringApplication.run(ShopProductMain.class,args);
}
}
4)编写properties文件
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springcloud
spring.datasource.password=root
spring.datasource.username=root
# 端口号
server.port=8081
logging.level.com.lyq.shopproduct.dao=debug
5)编写业务
业务目标:根据产品id获取产品的所有信息
①controller层
@RestController
@RequestMapping("shopProduct")
public class ShopProductController{
/**
* 服务对象
*/
@Resource
private ShopProductService shopProductService;
@GetMapping("getProductById")
public ResultData getProductById(Integer pid) {
return shopProductService.getProductById(pid);
}
}
②service层
public interface ShopProductService extends IService<ShopProduct> {
ResultData getProductById(Integer pid);
}
@Service("shopProductService")
public class ShopProductServiceImpl extends ServiceImpl<ShopProductDao, ShopProduct> implements ShopProductService {
@Autowired
private ResultData resultData;
@Override
public ResultData getProductById(Integer pid) {
ShopProduct shopProduct = this.getById(pid);
resultData.setObject(shopProduct);
return resultData;
}
}
测试
4、创建订单微服务模块
1)创建shop-order模块
2)修改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">
<parent>
<artifactId>springcloud-demo076</artifactId>
<groupId>com.lyq</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-order</artifactId>
<dependencies>
<dependency>
<groupId>com.lyq</groupId>
<artifactId>shop-common</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--转换json格式的数据-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.61</version>
</dependency>
</dependencies>
</project>
3)properties配置文件
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springcloud
spring.datasource.password=root
spring.datasource.username=root
# 端口号
server.port=8091
logging.level.com.lyq.shopproduct.dao=debug
4)主启动类
@SpringBootApplication
@MapperScan("com.lyq.shoporder.dao")
public class ShopOrderMain {
public static void main(String[] args) {
SpringApplication.run(ShopOrderMain.class,args);
}
/**
* 将RestTemplate注入到spring容器中
* @return
*/
@Bean
public RestTemplate restTemplate() {
return new RestTemplate();
}
}
RestTemplate 简介
RestTemplate 是从 Spring3.0 开始支持的一个 HTTP 请求工具,它提供了常见的REST请求方案的模版,例如 GET 请求、POST 请求、PUT 请求、DELETE 请求以及一些通用的请求执行方法 exchange 以及 execute。RestTemplate 继承自 InterceptingHttpAccessor 并且实现了 RestOperations 接口,其中 RestOperations 接口定义了基本的 RESTful 操作,这些操作在 RestTemplate 中都得到了实现。
5)业务实现
往数据库shop-order表中添加一条数据
①controller
@RestController
@RequestMapping("shopOrder")
public class ShopOrderController {
/**
* 服务对象
*/
@Resource
private ShopOrderService shopOrderService;
@GetMapping("saveOrder")
public ResultData saveOrder(Integer pid ,Integer number) {
return shopOrderService.saveOrder(pid,number);
}
}
②service
public interface ShopOrderService extends IService<ShopOrder> {
ResultData saveOrder(Integer pid, Integer number);
}
@Service("shopOrderService")
public class ShopOrderServiceImpl extends ServiceImpl<ShopOrderDao, ShopOrder> implements ShopOrderService{
@Autowired
private RestTemplate restTemplate;
@Autowired
private ResultData resultData;
@Override
public ResultData saveOrder(Integer pid, Integer number) {
// 通过RestTemplate类中的方法,请求模块shop-product中的方法,
// 第一个参数为请求路径,第二个参数为请求方法的返回类型的字节码对象
ResultData forObject = restTemplate.getForObject("http://localhost:8081/shopProduct/getProductById?pid=" + pid,
ResultData.class);
// 将json格式的数据转换成string
String string = JSON.toJSONString(forObject.getObject());
// 将string类型的数据,转换成对象
ShopProduct shopProduct = JSON.parseObject(string, ShopProduct.class);
ShopOrder shopOrder = new ShopOrder();
shopOrder.setPid(pid);
shopOrder.setPanme(shopProduct.getPname());
shopOrder.setNumber(number);
shopOrder.setPprice(shopProduct.getPprice());
shopOrder.setUid(2);
shopOrder.setUsername("tom");
boolean save = this.save(shopOrder);
if (save == true) {
resultData.setCode(200);
resultData.setMsg("下单成功");
}
return resultData;
}
}
测试: