<?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>com.imooc</groupId> <artifactId>order</artifactId> <version>0.0.1-SNAPSHOT</version> <modules> <module>client</module> <module>common</module> <module>server</module> </modules> <packaging>pom</packaging> <name>order</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.0.M3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Finchley.M2</spring-cloud.version> <product-client.version>0.0.1-SNAPSHOT</product-client.version> <order-common.version>0.0.1-SNAPSHOT</order-common.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.imooc</groupId> <artifactId>product-client</artifactId> <version>${product-client.version}</version> </dependency> <dependency> <groupId>com.imooc</groupId> <artifactId>order-common</artifactId> <version>${order-common.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> <version>2.0.0.M3</version> </dependency> </dependencies> </dependencyManagement> <repositories> <repository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </repository> <repository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-snapshots</id> <name>Spring Snapshots</name> <url>https://repo.spring.io/snapshot</url> <snapshots> <enabled>true</enabled> </snapshots> </pluginRepository> <pluginRepository> <id>spring-milestones</id> <name>Spring Milestones</name> <url>https://repo.spring.io/milestone</url> <snapshots> <enabled>false</enabled> </snapshots> </pluginRepository> </pluginRepositories> </project>
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>order</artifactId> <groupId>com.imooc</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>order-server</artifactId> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> </dependency> <dependency> <groupId>com.imooc</groupId> <artifactId>product-client</artifactId> </dependency> <dependency> <groupId>com.imooc</groupId> <artifactId>order-common</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
package com.imooc.order; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.feign.EnableFeignClients; @SpringBootApplication @EnableDiscoveryClient @EnableFeignClients(basePackages = "com.imooc.product.client") public class OrderApplication { public static void main(String[] args) { SpringApplication.run(OrderApplication.class, args); } }
订单服务如果要调用商品服务的话,启动类的时候要加一个参数 @EnableFeignClients(basePackages = "com.imooc.product.client"),不调用是不会扫描的,不扫描就调用不了商品的接口。 这是为啥:因为product的client并没有注册到服务中心啊。
package com.imooc.order.service.impl; import com.imooc.order.dataobject.OrderDetail; import com.imooc.order.dataobject.OrderMaster; import com.imooc.order.dto.OrderDTO; import com.imooc.order.enums.OrderStatusEnum; import com.imooc.order.enums.PayStatusEnum; import com.imooc.order.repository.OrderDetailRepository; import com.imooc.order.repository.OrderMasterRepository; import com.imooc.order.service.OrderService; import com.imooc.order.utils.KeyUtil; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.math.BigDecimal; import java.math.BigInteger; import java.util.List; import java.util.stream.Collectors; import com.imooc.product.client.ProductClient; import com.imooc.product.common.DecreaseStockInput; import com.imooc.product.common.ProductInfoOutput; @Service public class OrderServiceImpl implements OrderService { @Autowired private OrderDetailRepository orderDetailRepository; @Autowired private OrderMasterRepository orderMasterRepository; @Autowired private ProductClient productClient; @Override public OrderDTO create(OrderDTO orderDTO) { String orderId = KeyUtil.genUniqueKey(); //查询商品信息(调用商品服务) List<String> productIdList = orderDTO.getOrderDetailList().stream() .map(OrderDetail::getProductId) .collect(Collectors.toList()); List<ProductInfoOutput> productInfoList = productClient.listForOrder(productIdList); //计算总价 BigDecimal orderAmout = new BigDecimal(BigInteger.ZERO); for (OrderDetail orderDetail: orderDTO.getOrderDetailList()) { for (ProductInfoOutput productInfo: productInfoList) { if (productInfo.getProductId().equals(orderDetail.getProductId())) { //单价*数量 orderAmout = productInfo.getProductPrice() .multiply(new BigDecimal(orderDetail.getProductQuantity())) .add(orderAmout); BeanUtils.copyProperties(productInfo, orderDetail); orderDetail.setOrderId(orderId); orderDetail.setDetailId(KeyUtil.genUniqueKey()); //订单详情入库 orderDetailRepository.save(orderDetail); } } } //扣库存(调用商品服务) List<DecreaseStockInput> decreaseStockInputList = orderDTO.getOrderDetailList().stream() .map(e -> new DecreaseStockInput(e.getProductId(), e.getProductQuantity())) .collect(Collectors.toList()); productClient.decreaseStock(decreaseStockInputList); //订单入库 OrderMaster orderMaster = new OrderMaster(); orderDTO.setOrderId(orderId); BeanUtils.copyProperties(orderDTO, orderMaster); orderMaster.setOrderAmount(orderAmout); orderMaster.setOrderStatus(OrderStatusEnum.NEW.getCode()); orderMaster.setPayStatus(PayStatusEnum.WAIT.getCode()); orderMasterRepository.save(orderMaster); return orderDTO; } }
Common
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>order</artifactId> <groupId>com.imooc</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>order-common</artifactId> <dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> </dependencies> </project>
Client
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>order</artifactId> <groupId>com.imooc</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>order-client</artifactId> </project>