文章目录
- 搭建springcloud微服务项目
- 1. 微服务父工程
- 2. 创建子模块-shop-common
- 3. 创建子模块--shop-product
- 4. 创建子模块--shop-order
搭建springcloud微服务项目
技术栈:
- springcloud-alibaba
- mybatis-plus 持久性框架
- mysql数据库5.7以上
- springboot来搭建每个微服务。
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 https://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.12.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.aaa</groupId>
<artifactId>qy156-shop-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>qy156-shop-parent</name>
<description>Demo project for Spring Boot</description>
<!--定义版本号-->
<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.3.RELEASE</spring-cloud-alibaba.version>
</properties>
<!--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>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. 创建子模块-shop-common
把其他模块公共得代码放入到该模块。- - -实体 工具类
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.0</version>
</dependency>
</dependencies>
定义相关得实体类
@Data
@TableName(value = "shop_order")
public class Order {
//订单id
@TableId
private Long oid;
//用户id
private Integer uid;
//用户名
private String username;
//商品id---购买时99---->活动结束后199
private Long pid;
//商品得名称
private String pname;
//商品得价格
private Double pprice;
//购买得数量
private Integer number;
}
@Data
@TableName("shop_product")
public class Product {
@TableId
private Long pid;
private String pname;
private Double pprice;
private Integer stock;
}
3. 创建子模块–shop-product
关于商品表操作的接口
<dependencies>
<!--引入公共模块-->
<dependency>
<groupId>com.aaa</groupId>
<artifactId>shop-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--引入springboot-starter-web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
配置文件
# 定义端口号 [8001~8009 未来方便搭建集群]
server:
port: 8001
#数据源得信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=Asia/Shanghai
username: root
password: root
# mybatis打印日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
dao接口
/**
* @program: qy156-shop-parent
* @description:
* @author: 闫克起
* @create: 2022-11-17 16:22
**/
public interface ProductDao extends BaseMapper<Product> {
}
service代码
@Service
public class ProductService implements IProductService {
@Autowired
private ProductDao productDao;
@Override
public Product findById(Long pid) {
return productDao.selectById(pid);
}
}
controller
@RestController
@RequestMapping("product")
public class ProductController {
@Autowired
private IProductService productService;
@GetMapping("/getById/{pid}")
public Product getById(@PathVariable Long pid){
return productService.findById(pid);
}
}
主启动类
@SpringBootApplication
@MapperScan(basePackages = "com.aaa.product.dao")
public class ProductApp {
public static void main(String[] args) {
SpringApplication.run(ProductApp.class,args);
}
}
4. 创建子模块–shop-order
关于订单表得所有操作接口
<dependencies>
<!--引入公共模块-->
<dependency>
<groupId>com.aaa</groupId>
<artifactId>shop-common</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<!--引入springboot-starter-web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--引入mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
配置
#端口号---[9001~9009]集群模式
server:
port: 9001
#数据源得信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springcloud?serverTimezone=Asia/Shanghai
username: root
password: root
# sql显示在控制台
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
dao接口
package com.aaa.order.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* @program: qy156-shop-parent
* @description:
* @author: 闫克起
* @create: 2022-11-18 14:34
**/
public interface OrderDao extends BaseMapper<OrderDao> {
}
service代码
@Service
public class OrderService implements IOrderService {
@Autowired
private OrderDao orderDao;
@Override
public int save(Order order) {
return orderDao.insert(order);
}
}
controller代码
package com.aaa.order.controller;
import com.aaa.entity.Order;
import com.aaa.entity.Product;
import com.aaa.order.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
/**
* @program: qy156-shop-parent
* @description:
* @author: 闫克起2
* @create: 2022-11-17 16:33
**/
@RestController
@RequestMapping("/order")
public class OrderController {
//必须创建并交于spring容器管理。这样才能被注入到相应的类属性上
@Autowired
private RestTemplate restTemplate;
@Autowired
private IOrderService orderService;
@GetMapping("/saveOrder")
public String saveOrder(Long pid,Integer num){
Order order=new Order();
//用户得信息可以从登录后获取--Session redis jwt
order.setUid(1);
order.setUsername("张成");
//为订单对象设置商品得信息
order.setPid(pid);
//需要远程调用商品微服务中的指定接口[注意:凡是关于商品的操作都有商品微服务来执行。]
//远程调用的方式:第一种基于TCP协议的RPC远程调用 第二种基于http协议Restful风格的调用。
//分布式架构:TCP协议的
//微服务架构:http协议的。---在spring框架中封装了一个工具RestTemplate。 如果不是使用的spring框架。你需要自己封装HttpClient工具
Product product = restTemplate.getForObject("http://localhost:8001/product/getById/"+pid, Product.class);
order.setPname(product.getPname());
order.setPprice(product.getPprice());
order.setNumber(num);
orderService.save(order);
return "下单成功";
}
}