SpringCloud Day01---微服务架构编码构建

1.微服务架构编码

约定 > 配置 > 编码

1.1 IDEA新建project工作空间

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

  • New Project

SpringCloud Day01---微服务架构编码构建_第1张图片

  • 聚合总父工程名字+项目名称

SpringCloud Day01---微服务架构编码构建_第2张图片

  • Maven选版本

SpringCloud Day01---微服务架构编码构建_第3张图片

  • 字符编码

SpringCloud Day01---微服务架构编码构建_第4张图片

  • 注解生效激活

SpringCloud Day01---微服务架构编码构建_第5张图片

  • java编译版本选8

SpringCloud Day01---微服务架构编码构建_第6张图片

  • File Type过滤

过滤掉无用的文件(处理后只是没有显示,但实际存在.)

SpringCloud Day01---微服务架构编码构建_第7张图片

1.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.rg.springcloudgroupId>
  <artifactId>cloud2021artifactId>
  <version>1.0-SNAPSHOTversion>
  <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.13junit.version>
    <log4j.version>1.2.17log4j.version>
    <lombok.version>1.18.20lombok.version>
    <mysql.version>5.1.47mysql.version>
    <druid.version>1.1.17druid.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>log4jgroupId>
        <artifactId>log4jartifactId>
        <version>${log4j.version}version>
      dependency>
      <dependency>
        <groupId>org.projectlombokgroupId>
        <artifactId>lombokartifactId>
        <version>1.18.20version>
        <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>

1.1.3 Maven工程落地细节复习

  1. Maven中的DependencyManagement和Dependencies区别

DependencyManagement

Maven 使用dependencyManagement 元素来提供了一种管理依赖版本号的方式。
通常会在一个组织或者项目的最顶层的父POM 中看到dependencyManagement 元素

使用pom.xml 中的dependencyManagement 元素能让所有在子项目中引用一个依赖而不用显式的列出版本号
Maven 会沿着父子层次向上走,直到找到一个拥有dependencyManagement 元素的项目,然后它就会使用这个dependencyManagement 元素中指定的版本号。

SpringCloud Day01---微服务架构编码构建_第8张图片

这样做的好处就是:如果有多个子项目都引用同一样依赖,则可以避免在每个使用的子项目里都声明一个版本号,这样当想升级或切换到另一个版本时,只需要在顶层父容器里更新,而不需要一个一个子项目的修改 ;另外如果某个子项目需要另外的一个版本,只需要声明version就可

  • **dependencyManagement里只是声明依赖,并不实现引入**,因此子项目需要显示的声明需要用的依赖。
    
  • 如果不在子项目中声明依赖,是不会从父项目中继承下来的;只有在子项目中写了该依赖项,并且没有指定具体版本, 才会从父项目中继承该项,并且version和scope都读取自父pom;
    
  • 如果**子项目中指定了版本号,那么会使用子项目中指定的jar版本**。
    
  1. maven中跳过单元测试

1.配置

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.pluginsgroupId>
            <artifactId>maven-surefire-pluginartifactId>
            <configuration>
                <skip>trueskip>
            configuration>
        plugin>
    plugins>
build>

2 IDEA工具支持(推荐)

SpringCloud Day01---微服务架构编码构建_第9张图片

1.2 Rest微服务工程构建

1.2.1 cloud-provider-payment8001微服务生产者支付Module模块

  • 建cloud-provider-payment8001

创建完成后请回到父工程查看pom文件变化:

SpringCloud Day01---微服务架构编码构建_第10张图片

  • 改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>cloud2021artifactId>
        <groupId>com.rg.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.17version>
        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-payment-service
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/db2021?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: 186259

mybatis:
  mapperLocations: classpath:mapper/*.xml               # 配置Mapper所有接口配置文件的位置.
  type-aliases-package: com.rg.springcloud.entities    # 所有Entity别名类所在包

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

    • 建表
    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
    
    • pojo实体类

    主实体Payment

    /**
     * Description 支付类
     * @author lxy
     * @version 1.0
     * @date 2021/12/23 9:50
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class Payment implements Serializable {
        private Long id;
        private String serial;
    }
    

    Json封装体CommonResult

    /**
     * @author lxy
     * @version 1.0
     * @Description 前后端进行交互的结果类
     * @date 2021/12/23 11:01
     */
    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public class CommonResult<T> {
        private Integer code;//状态码
        private String message;//状态信息
        private T data;//返回的json数据
    
        public CommonResult(Integer code,String message){//失败时调用的方法.
            this(code,message,null);
        }
    }
    
    • DAO层

    PaymentDao

    /**
     * @author lxy
     * @version 1.0
     * @Description 支付dao类
     * @date 2021/12/23 11:06
     */
    @Mapper
    @Repository
    public interface PaymentDao {
        public int create(Payment payment);
    
        public Payment getPaymentById(@Param("id") Long id);
    }
    

    PaymentMapper.xml

    
    DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
    
    <mapper namespace="com.rg.springcloud.dao.PaymentDao">
        
        <insert id="create" parameterType="com.rg.springcloud.pojo.Payment" useGeneratedKeys="true" keyProperty="id">
            INSERT INTO payment(serial) VALUES(#{serial})
        insert>
        <resultMap id="BaseResultMap" type="com.rg.springcloud.pojo.Payment">
            
            <id column="id" property="id" jdbcType="BIGINT"/>
            <result column="serial" property="serial" jdbcType="VARCHAR"/>
        resultMap>
        <select id="getPaymentById" resultMap="BaseResultMap" parameterType="long">
            SELECT * FROM payment WHERE id = #{id}
        select>
    
    
    mapper>
    
    • service层
    public interface PaymentService {
        public int create(Payment payment);
    
        public Payment getPaymentById(@Param("id") Long id);
    }
    
    @Service
    public class PaymentServiceImpl implements PaymentService {
        @Autowired
        private PaymentDao paymentDao;
        @Override
        public int create(Payment payment) {
            return paymentDao.create(payment);
        }
    
        @Override
        public Payment getPaymentById(Long id) {
            return paymentDao.getPaymentById(id);
        }
    }
    
    • controller层
    /**
     * @author lxy
     * @version 1.0
     * @Description PaymentController
     * @date 2021/12/23 11:27
     */
    
    @Slf4j
    @RestController
    public class PaymentController {
        @Resource
        private PaymentService paymentService;
    
        @PostMapping(value = "/payment/create")
        public CommonResult create(Payment payment){ // ??? 为啥这里不需要@RequestBody呢??
            int result = paymentService.create(payment);
            log.info("*****插入操作返回结果*:"+result);
            if(result>0) {
                return new CommonResult(200, "插入数据成功", result);
            }else
            {
                return new CommonResult(444, "插入数据失败");
            }
        }
    
        @GetMapping(value = "/payment/get/{id}")
        public CommonResult getPaymentById(@PathVariable("id") Long id) {
            Payment payment = paymentService.getPaymentById(id);
            log.info("*****查询结构:{}", payment);
            if(payment!=null){
                return new CommonResult(200, "查询成功", payment);
            }else{
                return new CommonResult(444, "没有对应记录,查询ID:" + id);
            }
        }
    }
    
  • 测试

SpringCloud Day01---微服务架构编码构建_第11张图片

SpringCloud Day01---微服务架构编码构建_第12张图片

SpringCloud Day01---微服务架构编码构建_第13张图片

  • 小结

SpringBoot创建模块的五个步骤:

1.建module

2.改POM

3.写YML

4.主启动

5.业务类

1.2.2 热部署Devtools

1.Adding devtools to your project

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

2.Adding plugin to your pom.xml

下段配置我们粘贴进聚合父类总工程的pom.xml里

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

3.Enabling automatic build

SpringCloud Day01---微服务架构编码构建_第14张图片

4.Update the value of

SpringCloud Day01---微服务架构编码构建_第15张图片

SpringCloud Day01---微服务架构编码构建_第16张图片

5.重启IDEA

6.进行测试

SpringCloud Day01---微服务架构编码构建_第17张图片

SpringCloud Day01---微服务架构编码构建_第18张图片

1.2.3 cloud-consumer-order80:微服务消费者订单Module模块

  • 建cloud-consumer-order80
  • 改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>cloud2021artifactId>
        <groupId>com.rg.springcloudgroupId>
        <version>1.0-SNAPSHOTversion>
    parent>
    <modelVersion>4.0.0modelVersion>

    <artifactId>cloud-consumer-order80artifactId>

    <dependencies>
        
        <dependency>
            <groupId>com.rg.springcloudgroupId>
            <artifactId>cloud-api-commonsartifactId>
            <version>${project.version}version>
        dependency>
        <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>
  • 写YML
server:
  port: 80
  • 主启动
@SpringBootApplication
public class OrderMain80 {
    public static void main(String[] args) {
        SpringApplication.run(OrderMain80.class,args);
    }
}
  • 业务类

    1. pojo

    同cloud-provider-payment8001模块的pojo

    1. 首说RestTemplate

    RestTemplate提供了多种便捷访问远程Http服务的方法,
    是一种简单便捷的访问restful服务模板类,是Spring提供的==用于访问Rest服务==的客户端模板工具集

    官网:https://docs.spring.io/spring-framework/docs/5.2.2.RELEASE/javadoc-api/org/springframework/web/client/RestTemplate.html

    使用:
    使用restTemplate访问restful接口非常的简单粗暴无脑。
    (url, requestMap, ResponseBean.class)这三个参数分别代表 REST请求地址、请求参数、HTTP响应转换被转换成的对象类型。

    1. config配置类
    @Configuration
    public class ApplicationContextConfig {
        //向SpringBoot容器中加入该组件
        @Bean
        public RestTemplate restTemplate(){
            return new RestTemplate();
        }
    }
    

    4.controller

@RestController
@Slf4j
public class OrderController {
    public static final String PAYMENT_URL = "http://localhost:8001";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/payment/create")客户端用浏览器是get请求,但是底层实质发送post调用服务端8001
    public CommonResult<Payment> create(Payment payment){
        return restTemplate.postForObject(PAYMENT_URL+"/payment/create",payment,CommonResult.class);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }
}
  • 测试

SpringCloud Day01---微服务架构编码构建_第19张图片

SpringCloud Day01---微服务架构编码构建_第20张图片

备注:

使用postman或者rest client测试报出Required request body is missing 错误(而在其他场合发送post请求时根据需要进行添加该注解.)
解决:把 @RequestBody去掉

1.2.4 工程重构

  • 观察问题

SpringCloud Day01---微服务架构编码构建_第21张图片

系统中有重复部分,所以应该重构

  • 新建 cloud-api-commons
  • 改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>cloud2021artifactId>
        <groupId>com.rg.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>
  • pojo

添加Payment实体和CommonResult通用封装类

  • maven命令clean install
  • 订单80和支付8001分别改造

删除各自的原先有过的entities文件夹==>各自粘贴POM内容

 
<dependency>
    <groupId>com.rg.springcloudgroupId>
    <artifactId>cloud-api-commonsartifactId>
    <version>${project.version}version>
dependency>

1.2.5 目前工程样图

SpringCloud Day01---微服务架构编码构建_第22张图片

你可能感兴趣的:(SpringCloud,微服务,架构,java,spring,cloud)