13-创建商品服务提供者
2025年9月13日大约 2 分钟SpringCloud Alibaba For Page项目
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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.page</groupId>
<artifactId>page-dependencies</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../page-dependencies/pom.xml</relativePath>
</parent>
<artifactId>page-service-provider-item</artifactId>
<packaging>jar</packaging>
<name>page-service-provider-item</name>
<inceptionYear>2019-Now</inceptionYear>
<dependencies>
<!-- Spring Boot Begin -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot End -->
<!-- Spring Cloud Begin -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<exclusions>
<exclusion>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<exclusion>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</exclusion>
<exclusion>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</exclusion>
<exclusion>
<groupId>org.hdrhistogram</groupId>
<artifactId>HdrHistogram</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
<!-- Spring Cloud End -->
<!-- Projects Begin -->
<dependency>
<groupId>com.page</groupId>
<artifactId>page-commons-service</artifactId>
<version>${project.parent.version}</version>
</dependency>
<!-- Projects End -->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.page.service.provider.item.PageServiceProviderItemApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
Application
package com.page.service.provider.item;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication(scanBasePackages = "com.page")
@EnableDiscoveryClient
@MapperScan(basePackages = "com.page.commons.mapper")
@EnableSwagger2
public class PageServiceProviderItemApplication {
public static void main(String[] args) {
SpringApplication.run(PageServiceProviderItemApplication.class, args);
}
}
Controller
package com.page.service.provider.item.controller;
import com.page.commons.domain.TbItem;
import com.page.commons.service.TbItemService;
import com.page.commons.web.AbstractBaseController;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping(value = "item")
public class TbItemController extends AbstractBaseController<TbItem> {
@Autowired
private TbItemService tbItemService;
/**
* 说明:
* 消费者传递对象参数,提供者接收参数的方式必须是 POST
* 并且对象参数前要加上 @RequestBody 的注解
*
* @param tbItem
* @param pageNum
* @param pageSize
* @return
*/
@ApiOperation(value = "商品分页查询")
@ApiImplicitParams({
@ApiImplicitParam(name = "pageNum", value = "页码", required = true, paramType = "path"),
@ApiImplicitParam(name = "pageSize", value = "笔数", required = true, paramType = "path")
})
@PostMapping(value = "page/{pageNum}/{pageSize}")
public PageInfo<TbItem> page(
@ApiParam(name = "商品信息", required = false) @RequestBody TbItem tbItem,
@PathVariable int pageNum,
@PathVariable int pageSize
) {
PageInfo<TbItem> page = tbItemService.page(tbItem, pageNum, pageSize);
return page;
}
}
properties
bootstrap.properties
spring.application.name=page-service-provider-item-config
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.server-addr=119.3.110.207:8848
logging.level.com.page=DEBUG
bootstrap-prod.properties
spring.profiles.active=prod
spring.application.name=page-service-provider-item-config
spring.cloud.nacos.config.file-extension=yaml
spring.cloud.nacos.config.server-addr=119.3.110.207:8848
YAML
spring:
application:
name: page-service-provider-item
datasource:
druid:
url: jdbc:mysql://47.107.89.207:3306/myshop?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: cc123456
initial-size: 1
min-idle: 1
max-active: 20
test-on-borrow: true
driver-class-name: com.mysql.cj.jdbc.Driver
cloud:
nacos:
discovery:
server-addr: 119.3.110.207:8848
sentinel:
transport:
port: 8719
dashboard: 119.3.110.207:8081
server:
port: 10105
mybatis:
type-aliases-package: com.page.commons.domain
mapper-locations: classpath:mapper/*.xml
management:
endpoints:
web:
exposure:
include: "*"