SpringCloud 基础案例搭建

案例

我们以电商系统中常见的用户下单为例,用户向订单微服务发起一个购买的请求。在进行保存订单之前 需要调用商品微服务查询当前商品库存,单价等信息。在这种场景下,订单微服务就是一个服务消费 者,商品微服务就是一个服务提供者

数据库表

案例所需要用到的数据库表,实体类可以自己生成

CREATE TABLE `tb_product` (
    `id` INT ( 11 ) NOT NULL AUTO_INCREMENT,
    `product_name` VARCHAR ( 40 ) DEFAULT NULL COMMENT '名称',
    `status` INT ( 2 ) DEFAULT NULL COMMENT '状态',
    `price` DECIMAL ( 10, 2 ) DEFAULT NULL COMMENT '单价',
    `product_desc` VARCHAR ( 255 ) DEFAULT NULL COMMENT '描述',
    `caption` VARCHAR ( 255 ) DEFAULT NULL COMMENT '标题',
    `inventory` INT ( 11 ) DEFAULT NULL COMMENT '库存',
PRIMARY KEY ( `id` ) 
) ENGINE = INNODB AUTO_INCREMENT = 2 DEFAULT CHARSET = utf8

DEMO环境搭建

1、创建父工程shop_parent,在IDEA中创建父工程 shop_parent 并引入坐标

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.13.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-logging</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.RELEASE</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>

2、创建微服务工程所需模块

  • 创建公共微服务模块 shop_service_common
  • 创建订单微服务模块 shop_service_order
  • 创建商品微服务模块 shop_service_product

3、设置工程依赖结构
shop_service_ordershop_service_product都引入shop_service_common

搭建商品微服务

导入依赖

shop_service_common中添加依赖,

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.32</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

编写实体类

在shop_service_common中创建 cn.nzblog.entity.Product 实体类,并配置

@Data
@Entity
@Table(name = "tb_product")
public class Product {
    @Id
    private Long id;
    private String productName;
    private Integer status;
    private BigDecimal price;
    private String productDesc;
    private String caption;
    private Integer inventory;
}

编写Dao接口

在shop_service_product中创建 cn.nzblog.product.dao.ProductDao 接口

public interface ProductDao extends JpaRepository<Product,Long>, JpaSpecificationExecutor<Product> {}

编写Service层

在shop_service_product中创建 cn.nzblog.product.service.ProductService

public interface ProductService {

    //根据ID查询
    Product findById(Long id);

}

在shop_service_product中创建 cn.nzblog.product.service.ProductServiceImpl 实现类

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Override
    public Product findById(Long id) {
        return productDao.findById(id).get();
    }
}

编写web层

在shop_service_product中创建 cn.nzblog.product.controller.ProductController

@RestController
@RequestMapping("/prodyct")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public Product findById(@PathVariable Long id) {
        Product product = productService.findById(id);
        return product;
    }

}

配置启动类

在shop_service_product中创建 cn.nzblog.product.ProductApplication 启动类

@SpringBootApplication
@EntityScan("cn.nzblog.entity")
public class ProductApplication {
    public static void main(String[] args) {
        SpringApplication.run(ProductApplication.class,args);
    }
}

配置YML文件

server:
  port: 9002
spring:
  application:
    name: shop-service-product
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8
    username: root
    password: 123456
  jpa:
    database: mysql
    show-sql: true
    open-in-view: true

RestTemplate调用微服务

我们这里使用Spring框架提供的RestTemplate类调用rest服务
1、在shop_service_order工程中ProductApplication启动类 中配置RestTemplate

@Bean
public RestTemplate getRestTemplate() {
    return new RestTemplate();
}

2、编写下Controller

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/buy/{id}",method = RequestMethod.GET)
    public Product findById(@PathVariable Long id) {
        Product product = null;
        product = restTemplate.getForObject("http://127.0.0.1:9001/product/"+id,Product.class);
        return product;
    }
}

添加新评论

评论列表