1.创建父工程(maven项目)
1.删除src(无用),只留下pom.xml
2.配置相关依赖
<!--打包方式: jar: java工程 war:web工程 pom:父工程-->
<packaging>pom</packaging>
<!--继承springboot的父工程-->
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<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:它只负责jar的管理 不负责jar的下载,如果想下载需要再子工程中引入依赖来下载。 -->
<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>
3.创建父工程的基础模块
4.配置pom文件
5.创建实体类
2.创建用户微服务
1.创建一个名为 shop_product2 的模块,并添加依赖
2.创建工程的mapper层
3.创建工程的主控制类,添加包扫描
4.创建 ProductService 接口
5.创建 ProductServiceImpl 实现类
6.创建 ProductController 类
7.书写配置类application.yml
8.在数据库创建好相对应的数据库表,并加入数据
9.在浏览器进行测试
3.创建订单微服务
1.创建一个名为 shop-order2 的模块,并添加依赖
<dependencies>
<!--shop-common2-->
<dependency>
<groupId>com.yjq</groupId>
<artifactId>shop-common2</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2.书写mapper层接口
3.创建主控制类,添加包扫描
4.创建service层
OrderService接口:
OrderService接口的实现:
5.创建controller层
package com.yjq.order.controller;
import com.yjq.entry.Order;
import com.yjq.entry.Product;
import com.yjq.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
@RestController
@RequestMapping("order")
public class OrderController {
@Autowired
private OrderService orderService;
@Resource
private RestTemplate restTemplate;
@GetMapping("saveOrder")
public String saveOrder(Integer pid,Integer num){
Order order=new Order();
order.setNumber(num);
order.setUid(2);
order.setUsername("张三");
order.setPid(pid);
//需要根据商品id查询商品对应的信息---->调用远程商品微服务。
//基于TCP协议------>RPC调用速度快非常耗资源 因为tcp连接属于常连接---jdbc----dubbo
//基FHttp协议----->httpClient调用速度慢 但是资源消耗少属于短连接。 请求头 相应体参数 微服 务都是基于http-些调用。
//可以自己封装http工具类来完成调用关系-----复杂
//springboot封装了一个工具可以完成服务之间的调用RestTemplate
Product product = restTemplate.getForObject("http://localhost:8081/product/findById/" + pid, Product.class);
order.setPname(product.getPname());
order.setPprice(product.getPprice());
orderService.saveOrder(order);
return "下单成功";
}
}
6.书写配置类application.yml
7.在主控制类创建RestTemplate并交于spring管理(不写运行时会显示找不到RestTemplate)
8.在浏览器运行测试
最后在数据库查看相应数据的变化