多模块
product-server 所有业务逻辑 controller、service……
product-client 对外暴露的接口、商品列表、扣库存。
product-common 公用的对象,既能被外部服务调用,也能被内部其他模块使用。
Ps:多模块最重要的难点就是 pom.xml 配置的问题。
Product 微服务项目
All 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>com.imooc</groupId> <artifactId>product</artifactId> <version>0.0.1-SNAPSHOT</version> <modules> <module>common</module> <module>server</module> <module>client</module> </modules> <packaging>pom</packaging> <name>product</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-common.version>0.0.1-SNAPSHOT</product-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-common</artifactId> <version>${product-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>product</artifactId> <groupId>com.imooc</groupId> <version>0.0.1-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>product-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> <!-- 为了获取 common 的类 --> <dependency> <groupId>com.imooc</groupId> <artifactId>product-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.product.controller; import com.imooc.product.VO.ProductInfoVO; import com.imooc.product.VO.ProductVO; import com.imooc.product.VO.ResultVO; import com.imooc.product.dataobject.ProductCategory; import com.imooc.product.dataobject.ProductInfo; import com.imooc.product.service.CategoryService; import com.imooc.product.service.ProductService; import com.imooc.product.utils.ResultVOUtil; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import com.imooc.product.common.DecreaseStockInput; import com.imooc.product.common.ProductInfoOutput; /** * 商品 */ @RestController @RequestMapping("/product") public class ProductController { @Autowired private ProductService productService; @Autowired private CategoryService categoryService; /** * 1. 查询所有在架的商品 * 2. 获取类目type列表 * 3. 查询类目 * 4. 构造数据 */ @GetMapping("/list") public ResultVO<ProductVO> list() { //1. 查询所有在架的商品 List<ProductInfo> productInfoList = productService.findUpAll(); //2. 获取类目type列表 List<Integer> categoryTypeList = productInfoList.stream() .map(ProductInfo::getCategoryType) .collect(Collectors.toList()); //3. 从数据库查询类目 List<ProductCategory> categoryList = categoryService.findByCategoryTypeIn(categoryTypeList); //4. 构造数据 List<ProductVO> productVOList = new ArrayList<>(); for (ProductCategory productCategory: categoryList) { ProductVO productVO = new ProductVO(); productVO.setCategoryName(productCategory.getCategoryName()); productVO.setCategoryType(productCategory.getCategoryType()); List<ProductInfoVO> productInfoVOList = new ArrayList<>(); for (ProductInfo productInfo: productInfoList) { if (productInfo.getCategoryType().equals(productCategory.getCategoryType())) { ProductInfoVO productInfoVO = new ProductInfoVO(); BeanUtils.copyProperties(productInfo, productInfoVO); productInfoVOList.add(productInfoVO); } } productVO.setProductInfoVOList(productInfoVOList); productVOList.add(productVO); } return ResultVOUtil.success(productVOList); } /** * 获取商品列表(给订单服务用的) * @param productIdList * @return */ @PostMapping("/listForOrder") public List<ProductInfoOutput> listForOrder(@RequestBody List<String> productIdList) { return productService.findList(productIdList); } @PostMapping("/decreaseStock") public void decreaseStock(@RequestBody List<DecreaseStockInput> decreaseStockInputList) { productService.decreaseStock(decreaseStockInputList); } }
- 这里能拿到 common 模块的类,是因为在 pom.xml 里引入了 common。
package com.imooc.product; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @SpringBootApplication @EnableDiscoveryClient public class ProductApplication { public static void main(String[] args) { SpringApplication.run(ProductApplication.class, args); } }