SpringCloud-基础知识+模块构建


SpringCloud-基础知识+模块构建

微服务理论架构

1、微服务概述

微服务架构是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间互相协调、互相配合,为用户提供最终价值。每个服务运行在其独立的进程中,服务和服务间采用轻量级的通信机制互相协作(通常是基于HTTP协议的Restful API)。每个服务都围绕具体业务进行构建,并且能够被独立的部署到生产环境,类生产环境等。另外,应当尽量避免统一的、集中式的服务管理机制,对具体的一个服务而言,应根据业务上下文,选择合适的语言、工具对其进行构建。

2、微服务架构的技术纬度

  • 服务注册与发现
  • 配置中心管理
  • 服务调用
  • 服务网关
  • 服务熔断
  • 服务监控
  • 负载均衡
  • 全链路追踪
  • 服务降级
  • 自动化构建部署
  • 服务消息队列
  • 服务定时任务调度操作

3、SpringCloud 简介

分布式微服务架构的一站式解决方案,是多种微服务架构落地技术的集合体,俗称微服务全家桶

4、SpringCloud技术栈

  • 服务注册与发现:Eureka
  • 服务负载与调用:RibbonFeign
  • 服务熔断降级:Hystrix
  • 服务网关:Zuul
  • 服务分布式配置:Spring Cloud Config
  • 服务开发:Spring Boot

Boot和Cloud版本组合选型

1、SpringCloud版本命名规则

采用英国伦敦地铁站进行命名,并由地铁站名称由A-Z依次类推的方式发布版本。

命名采用名称,而非版本号。

2、cloud和Boot的版本选型对应

Release Train Boot Version
2020.0.x aka Ilford 2.4.x
Hoxton 2.2.x, 2.3.x (Starting with SR5)
Greenwich 2.1.x
Finchley 2.0.x
Edgware 1.5.x
Dalston 1.5.x

详细版本对应管理信息:https://start.spring.io/actuator/info

Cloud组件停更后的替代

技术组件的替代

springcloud组件替换方案

cloud整体聚合父工程项目创建

1、新建一个Maven项目

注意:Maven选择3.5版本以上

新建cloud父工程项目

2、父工程的pom文件

com.wt.springcloud
cloud2021
1.0-SNAPSHOT

pom



  UTF-8
  1.8
  1.8
  4.12
  1.2.17
  1.16.18
  8.0.18
  1.1.16
  1.3.0




  
    
    
      org.springframework.boot
      spring-boot-dependencies
      2.2.2.RELEASE
      pom
      import
    
    
    
      org.springframework.cloud
      spring-cloud-dependencies
      Hoxton.SR1
      pom
      import
    
    
    
      com.alibaba.cloud
      spring-cloud-alibaba-dependencies
      2.2.0.RELEASE
      pom
      import
    
    
    
      mysql
      mysql-connector-java
      ${mysql.version}
    
    
    
      com.alibaba
      druid-spring-boot-starter
      ${druid.verison}
    
    
    
      org.mybatis.spring.boot
      mybatis-spring-boot-starter
      ${mybatis.spring.boot.verison}
    
    
    
      org.projectlombok
      lombok
      ${lombok.version}
    
    
    
      junit
      junit
      ${junit.version}
    
    
    
      log4j
      log4j
      ${log4j.version}
    
  

  • 标签dependencyManagement和dependencies的区别:
    • dependencyManagement通常出现在父工程的pom文件中,用于表明引入依赖的版本号,子项目中引入依赖时,可以不写版本号,也可重新制定版本号。dependencyManagement只是声明要引入的依赖,并不会实际引入。
    • dependencies:引入具体的坐标依赖。

3、maven跳过单元测试

maven跳过单元测试

4、发布父工程项目到仓库

父工程创建后,可以使用 maven install 发布到仓库方便子工程继承。

发布父工程

cloud子模块的创建

1、新建Module模块

  • 选中父工程,右键new出module。

    创建子模块module

  • 父子模块层级结构

    子模块名称及路径

2、在pom.xml文件中,添加所需依赖(按需引入)


        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-starter-actuator
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
        
        
            com.alibaba
            druid-spring-boot-starter
            
        
        
        
            mysql
            mysql-connector-java
        
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
        
            org.springframework.boot
            spring-boot-devtools
            runtime
            true
        
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

3、编写application.yml配置文件

server:
  port: 8001
spring:
  application:
    name: cloud-payment-service #该微服务模块的名称
  datasource:
    # 当前数据源操作类型
    type: com.alibaba.druid.pool.DruidDataSource
    # mysql驱动类
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/cloud?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=GMT%2B8
    username: root
    password: root
mybatis:
  mapper-locations: classpath*:mapper/*.xml
  type-aliases-package: com.wt.springcloud.model

4、主启动类创建

通过SpringBoot模板创建的项目,不需要额外创建,已经存在主启动类。


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

5、根据实际业务需求编写业务代码

SpringBoot项目开启热部署

1、在pom.xml文件中添加maven插件


    项目名称
    
      
        org.springframework.boot
        spring-boot-maven-plugin
        
          true
          true
        
      
    
  

2、引入依赖



    org.springframework.boot
    spring-boot-devtools
    runtime
    true

3、在idea中设置自动编译

idea中开启自动编译

4、修改idea的注册表

  • 快捷键ctrl + shift + Alt + /
  • 选择registry选项
  • 勾选compiler.automake.allow.when.app.runningactionSystem.assertFocusAccessFromEdt

5、重启idea

通过RestTemplate跨模块调用接口

@RestController
public class OrderController {

    //调用的项目路径
    public static final String PAYMENT_URL = "http://localhost:8001";

    @Resource
    private RestTemplate restTemplate;

    @GetMapping("/consumer/creat")
    public CommonResult creat(Payment payment){

        //调用post请求接口,接口路径+参数对象+返回值类型
        return restTemplate.postForObject(PAYMENT_URL+"/payment/creat",
                                         payment
                                         ,CommonResult.class);
    }

    @GetMapping("/consumer/get/{id}")
    public CommonResult get(@PathVariable("id") Long id){

        //调用get请求接口,接口路径+返回值类型
        return restTemplate.getForObject(PAYMENT_URL+"/payment/get/"+id,CommonResult.class);
    }
}

提取公共类,作为单独模块,供其他模块引用

1、创建常用类模块

公共模块结构

2、通过maven将该模块发布到本地仓库

发布工程到maven本地仓库

3、所需模块,通过maven坐标依赖引入



    com.wt.springcloud
    cloud-api-commons
    ${project.version}