SpringCloud2020微服务实战项目(一)

SpringCloud2020微服务实战项目(一)_第1张图片

约定>配置>编码

微服务cloud整体聚合父工程Project

搭建父工程步骤

  • New Project
  • 聚合总父工程名字
  • Maven 选版本
  • 工程名字
  • 字符编码
  • 注解生效激活
    SpringCloud2020微服务实战项目(一)_第2张图片
  • java编译版本选8
    SpringCloud2020微服务实战项目(一)_第3张图片
  • File Type过滤(自行选择)

父工程POM

maven架构选择org.apache.maven.archetypes:maven-archetype-site

  1. 删掉其中的src目录,因为我们这个项目是父工程,只提供版本控制
  2. Maven pom.xml中 与之间的区别?
    Maven使用元素来提供了一种管理依赖版本号的方式。
    通常会在一个组织或者项目的最顶层的父POM中看到元素。
    使用pom.xml中的dependencyManagement元素能让所有在子项目中引用一个依赖而不用 显式的列出版本号
    Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用这个dependencyManagement元素中指定的版本号。
    *dependencyManagement里只是声明依赖,并不实引入,因此子项目需要显示的声明需要用的依赖。
    *如果子项目中指定了版本号,那么会使用子项目中指定的jar版本。
    父工程Pom.xml 文件


<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.atguigu.springcloudgroupId>
    <artifactId>cloud2020artifactId>
    <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>
        <mybatis.spring.boot.version>1.3.0mybatis.spring.boot.version>
    properties>

    
    <dependencyManagement>
        <dependencies>
            
            <dependency>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-dependenciesartifactId>
                <version>2.2.2.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>Hoxton.SR1version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>2.1.0.RELEASEversion>
                <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.mybatis.spring.bootgroupId>
                <artifactId>mybatis-spring-boot-starterartifactId>
                <version>${mybatis.spring.boot.version}version>
            dependency>
            <dependency>
                <groupId>junitgroupId>
                <artifactId>junitartifactId>
                <version>${junit.version}version>
            dependency>
            <dependency>
                <groupId>org.projectlombokgroupId>
                <artifactId>lombokartifactId>
                <version>${lombok.version}version>
                <optional>trueoptional>
            dependency>
        dependencies>
    dependencyManagement>

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

project>



Rest微服务工程构建

项目结构
SpringCloud2020微服务实战项目(一)_第4张图片

构建步骤

1. cloud-provider-payment 8001:微服务提供者支付Module模块**

  • 建module- maven
  • 改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>cloud2020artifactId>
        <groupId>com.atguigu.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>
        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-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
project>
  • 写YML
server:
  port: 8001

spring:
  application:
    name: cloud-provider-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource      #当前数据源操作类型
    #driver-class-name: org.gjt.mm.mysql.Driver        #mysql驱动包
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2019?useUnicode=true&characterEncoding-utr-8&useSSL=false
    username: root
    password: root

mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.atguigu.springcloud.entities       #所有Entity别名类所在包

  • 主启动类
package com.atguigu.springcloud;

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

/**
 * @Classname PaymentMain8001
 * @Description 主启动类
 * @Date 2020/3/30 17:08
 * @Created by LD
 */
@SpringBootApplication
public class PaymentMain8001 {
    public static void main(String[] args) {
        SpringApplication.run(PaymentMain8001.class,args);
    }
}

  • 业务类
  1. 建表SQL
    数据库名 db2019
CREATE TABLE `payment`(
	`id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT 'ID',
	`serial` varchar(200) DEFAULT '',
	PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8
  1. entities

主实体Payment.class
Json封装体CommonResult.class

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Payment implements Serializable {
    private Long id;
    private String serial;
}

@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);
    }
}
  1. dao
    接口 interface PaymentDao
/**
 * @Classname PaymentDao
 * @Description 支付dao接口
 * @Date 2020/3/30 20:11
 * @Created by LD
 */
@Mapper
public interface PaymentDao {

    //读操作
    public int create(Payment payment);

    //写操作
    public Payment getPaymentById(@Param("id") Long id);

}
   mybatis的映射文件PaymentMapper.xml 


<mapper namespace="com.atguigu.springcloud.dao.PaymentDao">

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


    
    <resultMap id="BaseResultMap" type="com.atguigu.springcloud.entities.Payment">
        <id column="id" property="id" javaType="BIGINT"/>
        <id column="serial" property="serial" jdbcType="VARCHAR"/>
    resultMap>

    <select id="getPaymentById" parameterType="Long" resultMap="BaseResultMap">
        select * from payment where id=#{id}
    select>

mapper>
  1. services
    PaymentService接口
    PaymentServiceImp实现类

  2. controller

2. 热部署Devtools

  • 添加devtools到项目cloud-provider-payment8001

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
  • 添加plugin到父pom.xml
 
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
             
                <configuration>
                    <fork>truefork>
                    <addResources>trueaddResources>
                configuration>
            plugin>
  • 开启自动构建automatic build
    SpringCloud2020微服务实战项目(一)_第5张图片
  • Update the value of
    在父pom.xml中,使用快捷键 ctrl + shift + alt + /
    在这里插入图片描述SpringCloud2020微服务实战项目(一)_第6张图片
  • 重启IDEA
    亲测,不好用,IDEA2019.3.2

3. cloud-comsumer-order80 微服务消费者订单Module模块

SpringCloud2020微服务实战项目(一)_第7张图片

  • pom.xml
 <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.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>
project>
  • application.yml
server:
  port: 80
  • 主启动类
package com.aiguigu.springcloud;

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

@SpringBootApplication
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class,args);
    }
}
  • 业务类

RestTemplate(httpclient webservice rpc)
RestTemplate提供了多种便捷访问远程Http服务的方法,是一种简单便捷的访问restful服务模板类,是Spring提供的用于访问Rest服务的客户端模板工具集。
使用
使用restTemplate访问restful接口非常的简单粗暴无脑。
(url,requestMap, ResponseBean.class)这三个参数分别代表
REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。
SpringCloud2020微服务实战项目(一)_第8张图片

  • 测试
  • SpringCloud2020微服务实战项目(一)_第9张图片
  • 在这里插入图片描述

4. 工程重构

SpringCloud2020微服务实战项目(一)_第10张图片
SpringCloud2020微服务实战项目(一)_第11张图片

  • 新建 cloud-api-commons
    SpringCloud2020微服务实战项目(一)_第12张图片
  • Pom.xml

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

    <artifactId>cloud-api-commonsartifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-allartifactId>
            <version>5.1.0version>
        dependency>
    dependencies>
project>


  • 复制entities
    SpringCloud2020微服务实战项目(一)_第13张图片
  • maven命令 clean install,将模块添加到自己的maven仓库中
    SpringCloud2020微服务实战项目(一)_第14张图片
    SpringCloud2020微服务实战项目(一)_第15张图片
  • 订单80和支付8001分别改造
    1. 各自删除8001与80项目的实体包
    2. 各自导入打包完成的实体类依赖
      引入如下依赖到8001和80项目中
<dependency>
	<groupId>com.atguigu.springcloud</groupId>
    <artifactId>cloud-api-commons</artifactId>
    <version>${project.version}</version>
</dependency>

目前工程样图

你可能感兴趣的:(SpringCloud2020微服务实战项目(一))