《SpringCloud专题03》-微服务架构编码构建-服务提供者

1.微服务cloud整体聚合工程

1.1.父工程步骤

New Project
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第1张图片
聚合总父工程名字
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第2张图片
Maven选版本
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第3张图片
字符编码
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第4张图片
注解生效激活
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第5张图片
java编译版本选8
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第6张图片
File Type过滤
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第7张图片

1.2.父工程POM



<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.0modelVersion>

    <groupId>com.itxiongmao.springcloudgroupId>
    <artifactId>springcloud2020artifactId>
    <version>1.0-SNAPSHOTversion>

    <modules>
        <module>cloud-provider-payment8001module>
    modules>

    <packaging>pompackaging>

    
    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <junit.version>4.12junit.version>
        <log4j.version>1.2.17log4j.version>
        <lombok.version>1.16.18lombok.version>
        <mysql.version>5.1.47mysql.version>
        <druid.version>1.1.16druid.version>
        <spring.boot.version>2.2.2.RELEASEspring.boot.version>
        <spring.cloud.version>Hoxton.SR1spring.cloud.version>
        <spring.cloud.alibaba.version>2.1.0.RELEASEspring.cloud.alibaba.version>
        <mybatis.spring.boot.version>1.3.0mybatis.spring.boot.version>
    properties>

    
    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>${spring.boot.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring.cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>${spring.cloud.alibaba.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>mysqlgroupId>
                <artifactId>mysql-connector-javaartifactId>
                <version>${mysql.version}version>
            dependency>
            <dependency>
                <groupId>com.alibabagroupId>
                <artifactId>druidartifactId>
                <version>${druid.version}version>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <configuration>
                    <fork>truefork>
                    <addResources>trueaddResources>
                configuration>
            plugin>
        plugins>
    build>

    
    <repositories>
        <repository>
            <id>nexus-aliyunid>
            <name>Nexus aliyunname>
            <url>http://maven.aliyun.com/nexus/content/groups/publicurl>
            <releases>
                <enabled>trueenabled>
            releases>
            <snapshots>
                <enabled>falseenabled>
            snapshots>
        repository>
    repositories>

project>

maven中跳过单元测试
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第8张图片

2.Rest微服务工程搭建

2.1.Cloud-provider-payment8001 微服务提供者Module模块

2.1.1.建module

创建完成后回到父工程查看pom文件变化
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第9张图片
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第10张图片

2.1.2.改POM

<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>springcloud2020artifactId>
        <groupId>com.itxiongmao.springcloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-provider-payment8001artifactId>


    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>2.0.0version>
        dependency>
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
            <version>1.1.10version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-jdbcartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

project>
2.1.3.主启动
package com.itxiongmao;

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

@SpringBootApplication
public class PaymentMain8001 {

    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class,args);
    }
}
2.1.4.写YML
server:
  port: 8001

spring:
  application:
    name: cloud-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: org.gjt.mm.mysql.Driver
    url: jdbc:mysql://127.0.0.1:3306/db2020_cloud?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 123456

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.itxiongmao.springcloud.entities
2.1.5.测试数据库
-- ----------------------------
-- Table structure for payment
-- ----------------------------
DROP TABLE IF EXISTS `payment`;
CREATE TABLE `payment` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `serial` varchar(200) DEFAULT NULL COMMENT '支付流水号',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='支付表';

-- ----------------------------
-- Records of payment
-- ----------------------------
INSERT INTO `payment` VALUES ('31', 'test001');
INSERT INTO `payment` VALUES ('32', 'test002');
INSERT INTO `payment` VALUES ('34', 'test003');
2.1.6.主实体Payment

《SpringCloud专题03》-微服务架构编码构建-服务提供者_第11张图片
新建一个module,专门把实体类做一个工程

package com.itxiongmao.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * @BelongsProject: springcloud2020
 * @BelongsPackage: com.itxiongmao.springcloud.entities
 * @CreateTime: 2020-07-07 23:32
 * @Description: TODO
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
    private Long id;
    private String serial;
}

<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>springcloud2020artifactId>
        <groupId>com.itxiongmao.springcloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-api-commonsartifactId>

    <dependencies>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <version>${lombok.version}version>
        dependency>
    dependencies>

project>
2.1.7.Json封装体CommonResult
package com.itxiongmao.springcloud.entities;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @BelongsProject: springcloud2020
 * @BelongsPackage: com.itxiongmao.springcloud.entities
 * @CreateTime: 2020-07-07 23:36
 * @Description: TODO
 */

@Data
@AllArgsConstructor
@NoArgsConstructor
public class CommonResult<T> {

    private Integer code;
    private String message;
    private T data;

    public CommonResult(Integer code, String message) {
        this(code, message, null);
    }
}
2.1.8.接口PaymentDao
package com.itxiongmao.dao;

import com.itxiongmao.springcloud.entities.Payment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @BelongsProject: springcloud2020
 * @BelongsPackage: com.itxiongmao.dao
 * @CreateTime: 2020-07-07 23:38
 * @Description: TODO
 */
@Mapper
public interface PaymentDao {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    Payment queryById(Long id);

    /**
     * 查询指定行数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    List<Payment> queryAllByLimit(@Param("offset") int offset, @Param("limit") int limit);


    /**
     * 通过实体作为筛选条件查询
     *
     * @param payment 实例对象
     * @return 对象列表
     */
    List<Payment> queryAll(Payment payment);

    /**
     * 新增数据
     *
     * @param payment 实例对象
     * @return 影响行数
     */
    int insert(Payment payment);

    /**
     * 修改数据
     *
     * @param payment 实例对象
     * @return 影响行数
     */
    int update(Payment payment);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    int deleteById(Long id);

}

2.1.9.mybatis的映射文件PaymentDao.xml


<mapper namespace="com.itxiongmao.dao.PaymentDao">

    <resultMap type="com.itxiongmao.springcloud.entities.Payment" id="PaymentMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="serial" column="serial" jdbcType="VARCHAR"/>
    resultMap>

    
    <select id="queryById" resultMap="PaymentMap" parameterType="Long">
        select
          id, serial
        from payment
        where id = #{id}
    select>

    
    <select id="queryAllByLimit" resultMap="PaymentMap">
        select
          id, serial
        from payment
        limit #{offset}, #{limit}
    select>

    
    <select id="queryAll" resultMap="PaymentMap">
        select
        id, serial
        from payment
        <where>
            <if test="id != null">
                and id = #{id}
            if>
            <if test="serial != null and serial != ''">
                and serial = #{serial}
            if>
        where>
    select>

    
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into payment(serial)
        values (#{serial})
    insert>

    
    <update id="update">
        update payment
        <set>
            <if test="serial != null and serial != ''">
                serial = #{serial},
            if>
        set>
        where id = #{id}
    update>

    
    <delete id="deleteById">
        delete from payment where id = #{id}
    delete>

mapper>
2.1.10.接口PaymentService
package com.itxiongmao.service;

import com.itxiongmao.springcloud.entities.Payment;

import java.util.List;

/**
 * @BelongsProject: springcloud2020
 * @BelongsPackage: com.itxiongmao.service
 * @CreateTime: 2020-07-07 23:48
 * @Description: TODO
 */
public interface PaymentService {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    Payment queryById(Long id);

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    List<Payment> queryAllByLimit(int offset, int limit);

    /**
     * 新增数据
     *
     * @param payment 实例对象
     * @return 实例对象
     */
    Payment insert(Payment payment);

    /**
     * 修改数据
     *
     * @param payment 实例对象
     * @return 实例对象
     */
    Payment update(Payment payment);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    boolean deleteById(Long id);

}

2.1.11.接口PaymentService实现类
package com.itxiongmao.service.impl;

import com.itxiongmao.dao.PaymentDao;
import com.itxiongmao.service.PaymentService;
import com.itxiongmao.springcloud.entities.Payment;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @BelongsProject: springcloud2020
 * @BelongsPackage: com.itxiongmao.service.impl
 * @CreateTime: 2020-07-07 23:49
 * @Description: TODO
 */
@Service("paymentService")
public class PaymentServiceImpl implements PaymentService {
    @Resource
    private PaymentDao paymentDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Payment queryById(Long id) {
        return this.paymentDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Payment> queryAllByLimit(int offset, int limit) {
        return this.paymentDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param payment 实例对象
     * @return 实例对象
     */
    @Override
    public Payment insert(Payment payment) {
        this.paymentDao.insert(payment);
        return payment;
    }

    /**
     * 修改数据
     *
     * @param payment 实例对象
     * @return 实例对象
     */
    @Override
    public Payment update(Payment payment) {
        this.paymentDao.update(payment);
        return this.queryById(payment.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        return this.paymentDao.deleteById(id) > 0;
    }
}

2.1.12.controller
package com.itxiongmao.controller;

import com.itxiongmao.service.PaymentService;
import com.itxiongmao.springcloud.entities.CommonResult;
import com.itxiongmao.springcloud.entities.Payment;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @BelongsProject: springcloud2020
 * @BelongsPackage: com.itxiongmao.controller
 * @CreateTime: 2020-07-07 23:50
 * @Description: TODO
 */
@RestController
@RequestMapping("payment")
public class PaymentController {
    /**
     * 服务对象
     */
    @Resource
    private PaymentService paymentService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("get/{id}")
    public CommonResult<Payment> selectOne(@PathVariable("id") Long id) {
        Payment payment = this.paymentService.queryById(id);
        return new CommonResult<Payment>(200,"select success 8001!",payment);
    }

    @PostMapping("create")
    public CommonResult create(Payment payment) {
        Payment insert = this.paymentService.insert(payment);
        System.out.println(insert);
        System.out.println("1234567890");
        return new CommonResult(200,"insert success" ,insert);
    }
}

3.启动测试

3.1.测试访问

http://127.0.0.1:8001/payment/get/31
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第12张图片

3.2.postman模拟post

《SpringCloud专题03》-微服务架构编码构建-服务提供者_第13张图片

3.3.开启Run DashBoard

通过修改idea的workspace.xml的方式快速打开Run Dashboard窗口,开启Run DashBoard

<component name="RunDashboard">
  <option name="configurationTypes">
    <set>
      <option value="SpringBootApplicationConfigurationType" />
    set>
  option>
  <option name="ruleStates">
    <list>
      <RuleState>
        <option name="name" value="ConfigurationTypeDashboardGroupingRule" />
      RuleState>
      <RuleState>
        <option name="name" value="StatusDashboardGroupingRule" />
      RuleState>
    list>
  option>
component>

《SpringCloud专题03》-微服务架构编码构建-服务提供者_第14张图片
部分同学可能由于idea版本不同,需要关闭重启

4.热部署Devtools

Adding devtools to your project(添加到你的子工程)

 <dependency>
     <groupId>org.springframework.bootgroupId>
     <artifactId>spring-boot-devtoolsartifactId>
     <scope>runtimescope>
     <optional>trueoptional>
 dependency>

Adding plugin to your pom.xml(添加到你的父工程)

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-maven-pluginartifactId>
            <configuration>
                <fork>truefork>
                <addResources>trueaddResources>
            configuration>
        plugin>
    plugins>
build>

Enabling automatic build
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第15张图片
Update the value of
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第16张图片
《SpringCloud专题03》-微服务架构编码构建-服务提供者_第17张图片
重启idea,测试热部署功能,修改Java代码,SpringBoot项目会重新自动重启!

你可能感兴趣的:(分布式微服务专题)