目录
2.1 技术选型
2.2 模块设计
2.3 微服务调用
2.4 创建⽗⼯程
2.5 创建商品微服务
2.6 创建订单微服务
2.1 技术选型
持久层 : SpingData Jpa数据库 : MySQL5.7其他 : SpringCloud Alibaba 技术栈
2.2 模块设计
--- shop-parent ⽗⼯程--- shop-product-api 商品微服务 api 【存放商品实体】--- shop-product-server 商品微服务 【端⼝:8081 】--- shop-order-api 订单微服务 api 【存放订单实体】--- shop-order-server 订单微服务 【端⼝:8091 】
2.3 微服务调用
在微服务架构中,最常⻅的场景就是微服务之间的相互调⽤。我们以电商系统中常⻅的
⽤户下单
为
例来演示微服务的调⽤:客户向订单微服务发起⼀个下单的请求,在进⾏保存订单之前需要调⽤商品微服务查询商品的信息。
我们⼀般把服务的主动调⽤⽅称为
服务消费者
,把服务的被调⽤⽅称为
服务提供者
。
2.4 创建⽗⼯程
创建⼀个maven
⼯程,然后在
pom.xml
⽂件中添加下⾯内容
<?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>
<groupId>cn.wolfcode</groupId>
<artifactId>shop-parent</artifactId>
<version>1.0.0</version>
<packaging>pom</packaging>
<!--⽗⼯程-->
<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.3.RELEASE</spring-cloudalibaba.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>
</project>
2.5 创建商品微服务
1.创建
shop-product-api
项⽬,然后在
pom.xml
⽂件中添加下⾯内容
<?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>shop-parent</artifactId>
<groupId>cn.wolfcode</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-product-api</artifactId>
<!--依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2
创建实体类
//商品
@Entity(name = "t_shop_product")
@Data
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long pid;//主键
private String pname;//商品名称
private Double pprice;//商品价格
private Integer stock;//库存
}
3.
创建
shop-product-server
项⽬,然后在
pom.xml
⽂件中添加下⾯内容
<?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>shop-parent</artifactId>
<groupId>cn.wolfcode</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-product-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>cn.wolfcode</groupId>
<artifactId>shop-product-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
4.
编写启动类
ProductServer.java
@SpringBootApplication
public class ProductServer {
public static void main(String[] args) {
SpringApplication.run(ProductServer.class,args);
}
}
5.
编写配置⽂件
application.yml
server :port : 8081spring :application :name : product-servicedatasource :driver-class-name : com.mysql.jdbc.Driverurl : jdbc : mysql : ///shop-product?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername : rootpassword : adminjpa :properties :hibernate :hbm2ddl :auto : updatedialect : org.hibernate.dialect.MySQL5InnoDBDialect
6.
在数据库中创建
shop-product
的数据库
7.
创建
ProductDao
public interface ProductDao extends JpaRepository<Product, Long> {
}
8.
创建
ProductService
接⼝和实现类
@Service
public class ProductServiceImpl implements ProductService {
@Autowired
private ProductDao productDao;
@Override
public Product findByPid(Long pid) {
return productDao.findById(pid).get();
}
}
9.
创建
Controller
@RestController
@Slf4j
public class ProductController {
@Autowired
private ProductService productService;
//商品信息查询
@RequestMapping("/product/{pid}")
public Product findByPid(@PathVariable("pid") Long pid) {
log.info("接下来要进⾏{}号商品信息的查询", pid);
Product product = productService.findByPid(pid);
log.info("商品信息查询成功,内容为{}", JSON.toJSONString(product));
return product;
}
}
10.
启动⼯程,等到数据库表创建完毕之后,加⼊测试数据
INSERT INTO t_shop_product VALUE( NULL , ' ⼩⽶ ' , '1000' , '5000' );INSERT INTO t_shop_product VALUE( NULL , ' 华为 ' , '2000' , '5000' );INSERT INTO t_shop_product VALUE( NULL , ' 苹果 ' , '3000' , '5000' );INSERT INTO t_shop_product VALUE( NULL , 'OPPO' , '4000' , '5000' );
11.通过浏览器访问服务
2.6 创建订单微服务
1.
创建
shop-order-api
项⽬,然后在
pom.xml
⽂件中添加下⾯内容
<?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>shop-parent</artifactId>
<groupId>cn.wolfcode</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-order-api</artifactId>
<!--依赖-->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
2
创建实体类
//订单
@Entity(name = "t_shop_order")
@Data
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long oid;//订单id
//⽤户
private Long uid;//⽤户id
private String username;//⽤户名
//商品
private Long pid;//商品id
private String pname;//商品名称
private Double pprice;//商品单价
//数量
private Integer number;//购买数量
}
3.
创建
shop-order-server
项⽬,然后在
pom.xml
⽂件中添加下⾯内容
<?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>shop-parent</artifactId>
<groupId>cn.wolfcode</groupId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>shop-order-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
<dependency>
<groupId>cn.wolfcode</groupId>
<artifactId>shop-order-api</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
</project>
4.
编写启动类
OrderServer.java
@SpringBootApplication
public class OrderServer {
public static void main(String[] args) {
SpringApplication.run(OrderServer.class,args);
}
}
5.
编写配置⽂件
application.yml
server :port : 8091spring :application :name : order-servicedatasource :driver-class-name : com.mysql.jdbc.Driverurl : jdbc : mysql : ///shop-order? serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=trueusername : rootpassword : adminjpa :properties :hibernate :hbm2ddl :auto : updatedialect : org.hibernate.dialect.MySQL5InnoDBDialect
6.在数据库中创建shop-order的数据库
7.
创建
OrderDao
public interface OrderDao extends JpaRepository<Order, Long> {
}
8.
创建
OrderService
接⼝和实现类
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
@Autowired
private OrderDao orderDao;
@Override
public Order createOrder(Long productId,Long userId) {
log.info("接收到{}号商品的下单请求,接下来调⽤商品微服务查询此商品信息",
productId);
//远程调⽤商品微服务,查询商品信息
Product product = null;
log.info("查询到{}号商品的信息,内容是:{}", productId,
JSON.toJSONString(product));
//创建订单并保存
Order order = new Order();
order.setUid(userId);
order.setUsername("叩丁狼教育");
order.setPid(productId);
order.setPname(product.getPname());
order.setPprice(product.getPprice());
order.setNumber(1);
orderDao.save(order);
log.info("创建订单成功,订单信息为{}", JSON.toJSONString(order));
return order;
}
}
9.
创建
Controller
@RestController
@Slf4j
public class OrderController {
@Autowired
private OrderService orderService;
@RequestMapping("/save")
public Order order(Long pid,Long uid) {
return orderService.createOrder(pid,uid);
}
}
10.直接往数据库中添加几条数据,启动项目进行调用即可。