Bootstrap

搭建微服务工程 【详细步骤】

在这里插入图片描述

一、准备阶段 🍉

本篇文章用到的技术栈

mysql+mybatis[mp]+springboot+springcloud alibaba

需要用到的数据库

订单数据库:

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for shop_order
-- ----------------------------
DROP TABLE IF EXISTS `shop_order`;
CREATE TABLE `shop_order`  (
  `oid` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '订单id',
  `uid` int(0) NULL DEFAULT NULL COMMENT '用户id',
  `username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户名',
  `pid` bigint(0) NULL DEFAULT NULL COMMENT '商品id',
  `pname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品名称',
  `pprice` decimal(10, 2) NULL DEFAULT NULL COMMENT '商品价格',
  `number` int(0) NULL DEFAULT NULL COMMENT '购买数量',
  PRIMARY KEY (`oid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 44956 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of shop_order
-- ----------------------------

SET FOREIGN_KEY_CHECKS = 1;

商品数据库

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for shop_product
-- ----------------------------
DROP TABLE IF EXISTS `shop_product`;
CREATE TABLE `shop_product`  (
  `pid` bigint(0) NOT NULL AUTO_INCREMENT COMMENT '商品id',
  `pname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品名',
  `pprice` decimal(10, 2) NULL DEFAULT NULL COMMENT '商品价格',
  `stock` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '商品库存',
  PRIMARY KEY (`pid`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of shop_product
-- ----------------------------
INSERT INTO `shop_product` VALUES (1, '华为手机', 1999.00, '100');
INSERT INTO `shop_product` VALUES (2, 'vivo手机', 2999.00, '100');
INSERT INTO `shop_product` VALUES (3, '小米', 2222.00, '1000');

SET FOREIGN_KEY_CHECKS = 1;

二、编码阶段🍉

1.搭建父工程🥝

在这里插入图片描述
根据自己的需要搭建父工程,我这里使用的是maven工程

引入依赖🍓

在这里插入图片描述

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.example</groupId>
  <artifactId>springcloud-pread</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <name>Archetype - springcloud-pread</name>
  <url>http://maven.apache.org</url>
  <modules>
    <module>qy165-lzq</module>
    <module>qy165-lzq01</module>
    <module>qy165-lzq02</module>
  </modules>
  <!--继承springboot父工程-->
  <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-cloud-alibaba.version>
  </properties>
  <!--dependencyManagement: 它只负责jar的管理 不负责jar的下载 子类使用该类型的jar包时,直接引用而无需写版本-->
  <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>

springboot 和 springcloud 以及springcloud alibaba他们的版本必须匹配

在这里插入图片描述

2. 创建公共模块🥝

在这里插入图片描述
公共模块中只需要创建两个实体类即可

引入依赖🍓

在这里插入图片描述

编写实体类🍓

在这里插入图片描述

package com.lzq;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.math.BigDecimal;

@Data
@TableName(value = "shop_order")
public class Order {
    @TableId(type = IdType.AUTO)
    private Integer oid;
    private Integer uid;
    private String username;
    private Integer pid;
    private String pname;
    private BigDecimal pprice;
    private Integer number;
}

在这里插入图片描述

package com.lzq;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

import java.math.BigDecimal;

@Data
@TableName(value = "shop_product")
public class Product1 {
    @TableId(type = IdType.AUTO)
    private Integer pid;
    private String pname;
    private BigDecimal pprice;
    private String stock;
}

3.商品系统🥝

在这里插入图片描述

引入依赖🍓

在这里插入图片描述

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>springcloud-pread</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>qy165-lzq01</artifactId>
    <name>Archetype - qy165-lzq01</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <!--公共模块引入-->
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>qy165-lzq</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>

        <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>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
    </dependencies>
</project>

配置文件🍓

在这里插入图片描述

#端口号8001~8008
server.port=8001
#数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=123456
spring.datasource.username=root
spring.datasource.url=jdbc:mysql:///wf1?serverTimezone=Asia/Shanghai

#sql日志打印控制台
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#mybatis映射文件
mybatis-plus.mapper-locations=classpath:/mapper/*.xml

主启动类🍓

在这里插入图片描述

package com.lzq;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class lzq {
    public static void main(String[] args) {
        SpringApplication.run(lzq.class,args);
    }
}

dao层🍓

在这里插入图片描述

package com.lzq.dao;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lzq.Product1;
import com.lzq.pojo.Product;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface ProductDao extends BaseMapper<Product1> {

}

service层🍓

在这里插入图片描述
在这里插入图片描述

package com.lzq.service.impl;

import com.lzq.Product1;
import com.lzq.dao.ProductDao;
import com.lzq.pojo.Product;
import com.lzq.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductDao productDao;


    @Override
    public Product1 findById(Integer id) {
        Product1 product = productDao.selectById(id);
        return product;
    }
}

controller层🍓

在这里插入图片描述

package com.lzq.controller;

import com.lzq.Product1;
import com.lzq.pojo.Product;
import com.lzq.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.websocket.server.PathParam;

@Controller
@ResponseBody
@RequestMapping("/aaa")
public class ProductController {
    @Autowired
    private ProductService productService;
    //{id} 这种方式表示在路径上以/的方式传输参数
    @GetMapping("/bbb/{id}")
    public Product1 getByid(@PathVariable Integer id){//@PathVariable该注解用来接收{id}的参数
        Product1 byId = productService.findById(id);
        return byId;
    }

}

4.订单系统🥝

在这里插入图片描述

引入依赖🍓

在这里插入图片描述

<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/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.example</groupId>
        <artifactId>springcloud-pread</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>qy165-lzq02</artifactId>
    <name>Archetype - qy165-lzq02</name>
    <url>http://maven.apache.org</url>
    <dependencies>
    <!--公共模块引入-->
    <dependency>
        <groupId>org.example</groupId>
        <artifactId>qy165-lzq</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

    <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>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
</dependencies>
</project>

配置文件🍓

在这里插入图片描述

#端口号9001~9008
server.port=9001
#数据源
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.password=123456
spring.datasource.username=root
spring.datasource.url=jdbc:mysql:///wf2?serverTimezone=Asia/Shanghai

#sql日志打印控制台
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

#mybatis映射文件
mybatis-plus.mapper-locations=classpath:/mapper/*.xml

主启动类🍓

在这里插入图片描述

package com.lzq;


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class Qy165Lzq03Application {

    public static void main(String[] args) {
        SpringApplication.run(Qy165Lzq03Application.class, args);

    }

    @Bean
    public RestTemplate aaa(){
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate;
    }

}

dao接口🍓

在这里插入图片描述

package com.lzq.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.lzq.Order;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface OrderDao extends BaseMapper<Order> {

}

service层🍓

在这里插入图片描述

在这里插入图片描述

package com.lzq.service.impl;

import com.lzq.Order;
import com.lzq.dao.OrderDao;
import com.lzq.service.OrderSercice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderSercice {
    @Autowired
    private OrderDao orderDao;
    @Override
    public Integer inset(Order order) {
        int insert = orderDao.insert(order);
        return insert;
    }
}

controller层🍓

在这里插入图片描述

package com.lzq.controller;

import com.lzq.Order;
import com.lzq.Product1;
import com.lzq.service.OrderSercice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping
public class OrderController {
    @Autowired
    private OrderSercice orderSercice;
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/aaa")
    public String insert(int pid,int num){
        System.out.println(pid);
        //构建一个订单对象
        Order order=new Order();
        order.setUid(1);//未来登录后一定能获取当前用户信息。
        order.setUsername("赵志强");
        order.setNumber(num);
        //商品信息---调用商品微服务的接口。---如何调用商品微服务的接口?--远程调用。[1]借助rabbitmq  [2]http远程调用【在java端模拟浏览器调用】。
        //spring封装了http远程调用 RestTemplate.默认该类没有交于spring容器管理. 创建一个配置类 写一个方法@Bean注解
        /**
         * String url, 远程调用的服务器的地址
         * Class<T> responseType, 远程调用的接口返回类型的反射类
         * Object... uriVariables: 远程接口需要传递的参数。
         */
        Product1 product = restTemplate.getForObject("http://localhost:8001/aaa/bbb/"+ pid, Product1.class);
        System.out.println(product);
        order.setPid(product.getPid());
        order.setPname(product.getPname());
        order.setPprice(product.getPprice());

        int i=orderSercice.inset(order);
        return i>0?"下单成功":"下单失败";
    }
}

悦读

道可道,非常道;名可名,非常名。 无名,天地之始,有名,万物之母。 故常无欲,以观其妙,常有欲,以观其徼。 此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。

;