• 微服务(二)——SpringCloud入门&项目搭建


    SpringCloud

    什么是微服务

    可以阅读以前做的笔记:https://qkongtao.cn/?p=549

    springcloud体系

    在这里插入图片描述

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

    springcloud简介

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

    Spring Cloud官方文档:https://cloud.spring.io/spring-cloud-static/Hoxton.SR1/reference/htmlsingle/
    Spring Cloud中文文档:https://www.bookstack.cn/read/spring-cloud-docs/docs-index.md
    Spring Boot官方文档:https://docs.spring.io/spring-boot/docs/2.2.2.RELEASE/reference/htmlsingle/

    案例:

    1. 京东促销架构示例
      在这里插入图片描述

    2. 阿里服务架构示例
      在这里插入图片描述

    3. 微服务基础服务示例
      在这里插入图片描述

    4. springcloud基础技术栈示例
      在这里插入图片描述

    Boot和Cloud版本选型

    版本

    Spring Boot 2.X 版
    源码地址:https://github.com/spring-projects/spring-boot/releases/
    Spring Boot 2 的新特性:https://github.com/spring-projects/spring-boot/wiki/spring-Boot-2.0-Release-Notes
    通过上面官网发现,Boot官方强烈建议你升级到2.X以上版本

    Spring Cloud H版

    源码地址:https://github.com/spring-projects/spring-cloud
    官网:https://spring.io/projects/spring-cloud

    选型

    SpringCloud和Springboot之间的依赖关系如何看:https://spring.io/projects/spring-cloud#overview
    更详细的版本对应查看方法(json):https://start.spring.io/actuator/info
    结果
    在这里插入图片描述

    接下来开发用到的组件版本

    • Cloud - Hoxton.SR1
    • Boot - 2.2.2.RELEASE
    • Cloud Alibaba - 2.1.0.RELEASE
    • Java - Java 8
    • Maven - 3.5及以上
    • MySQL - 5.7及以上

    Cloud组件停更和升级

    1. 停更引发的“升级惨案”

    • 停更不停用
    • 被动修复bugs
    • 不再接受合并请求
    • 不再发布新版本

    2. Cloud升级

    在这里插入图片描述

    • 服务注册中心
      × Eureka
      √ Zookeeper
      √ Consul
      √ Nacos
    • 服务调用
      √ Ribbon
      √ LoadBalancer
    • 服务调用2
      × Feign
      √ OpenFeign
    • 服务降级
      × Hystrix
      √ resilience4j
      √ sentienl
    • 服务网关
      × Zuul
      ! Zuul2
      √ gateway
    • 服务配置
      × Config
      √ Nacos
    • 服务总线
      × Bus
      √ Nacos

    项目:订单-支付微服务

    父工程pom文件

    <?xml version="1.0" encoding="UTF-8"?>
    
    <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.0</modelVersion>
    
        <groupId>cn.kt</groupId>
        <artifactId>springcloud</artifactId>
        <packaging>pom</packaging>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <junit.version>4.12</junit.version>
            <log4j.version>1.2.17</log4j.version>
            <lombok.version>1.16.18</lombok.version>
            <mysql.version>5.1.47</mysql.version>
            <druid.verison>1.1.9</druid.verison>
            <mybatis.spring.boot.verison>2.1.4</mybatis.spring.boot.verison>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <!--spring boot 2.2.2-->
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>2.2.2.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!--spring cloud Hoxton.SR1-->
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>Hoxton.SR1</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!--spring cloud alibaba 2.1.0.RELEASE-->
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>2.2.0.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <!-- MySql -->
                <dependency>
                    <groupId>mysql</groupId>
                    <artifactId>mysql-connector-java</artifactId>
                    <version>${mysql.version}</version>
                </dependency>
                <!-- Druid -->
                <dependency>
                    <groupId>com.alibaba</groupId>
                    <artifactId>druid-spring-boot-starter</artifactId>
                    <version>${druid.verison}</version>
                </dependency>
                <!-- mybatis-springboot整合 -->
                <dependency>
                    <groupId>org.mybatis.spring.boot</groupId>
                    <artifactId>mybatis-spring-boot-starter</artifactId>
                    <version>${mybatis.spring.boot.verison}</version>
                </dependency>
                <!--lombok-->
                <dependency>
                    <groupId>org.projectlombok</groupId>
                    <artifactId>lombok</artifactId>
                    <version>${lombok.version}</version>
                </dependency>
                <!--junit-->
                <dependency>
                    <groupId>junit</groupId>
                    <artifactId>junit</artifactId>
                    <version>${junit.version}</version>
                </dependency>
                <!-- log4j -->
                <dependency>
                    <groupId>log4j</groupId>
                    <artifactId>log4j</artifactId>
                    <version>${log4j.version}</version>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <finalName>cloud2020</finalName>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>2.2.2.RELEASE</version>
                    <configuration>
                        <fork>true</fork>
                        <addResources>true</addResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>
    
    </project>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105

    复习DependencyManagement和Dependencies
    Maven使用dependencyManagement元素来提供了一种管理依赖版本号的方式。

    通常会在一个组织或者项目的最顶层的父POM中看到dependencyManagement元素。

    使用pom.xml中的dependencyManagement元素能让所有在子项目中引用个依赖而不用显式的列出版本量。

    Maven会沿着父子层次向上走,直到找到一个拥有dependencyManagement元素的项目,然后它就会使用这个
    dependencyManagement元素中指定的版本号。

    例如:

    <dependencyManagement>
        <dependencies>
            <dependency>
            <groupId>mysq1</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.2</version>
            </dependency>
        <dependencies>
    </dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    然后在子项目里就可以添加mysql-connector时可以不指定版本号

    <dependencies>
        <dependency>
        <groupId>mysq1</groupId>
        <artifactId>mysql-connector-java</artifactId>
        </dependency>
    </dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

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

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

    IDEA右侧旁的Maven插件有Toggle ’ Skip Tests’ Mode按钮,这样maven可以跳过单元测试
    在这里插入图片描述

    父工程创建完成执行mvn : install将父工程发布到仓库方便子工程继承。

    支付模块构建

    创建微服务模块套路:建Module、改POM、写YML、主启动、业务类
    在这里插入图片描述

    1. 新建子模块子模块
      ​ 名字:cloud_pay_8001
    2. pom依赖
    <?xml version="1.0" encoding="UTF-8"?>
    <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>springcloud</artifactId>
            <groupId>cn.kt</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <artifactId>cloud-provider-payment8001</artifactId>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-actuator</artifactId>
            </dependency>
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid-spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-jdbc</artifactId>
            </dependency>
            <!--热部署-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
            </dependency>
            <!--eureka-client-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-zipkin</artifactId>
            </dependency>
    
        </dependencies>
    
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    1. 创建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/db2019?useUnicode=true&characterEncoding=
            UTF-8&useSSL=false&serverTimezone=GMT%2B8
        username: root
        password: root
    mybatis:
      mapper-locations: classpath*:mapper/*.xml
      type-aliases-package: com.eiletxie.springcloud.entities
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    1. 主启动类
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class PaymentMain001 {
    
        public static void main(String[] args) {
            SpringApplication.run(PaymentMain001.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    1. 业务类

    热部署Devtools

    开发时使用,生产环境关闭

    1. 添加坐标
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 添加plugin到pom.xml

    下段配置复制到聚合父类总工程的pom.xml

    <build>
        <!--
    	<finalName>你的工程名</finalName>(单一工程时添加)
        -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    1. 启动自动编译

    File -> Settings(New Project Settings->Settings for New Projects) ->Complier

    下面项勾选

    Automatically show first error in editor
    Display notification on build completion
    Build project automatically
    Compile independent modules in parallel
    在这里插入图片描述

    1. 修改允许热部署插件

    键入Ctrl + Shift + Alt + / ,打开Registry,勾选:

    compiler.automake.allow.when.app.running

    actionSystem.assertFocusAccessFromEdt

    1. 重启IDEA

    消费者订单模块

    1. 建Module

    创建名为cloud-consumer-order80的maven工程。

    1. 改POM

    2. 写YML

    3. 主启动

    4. 业务类

    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响应转换被转换成的对象类型。

    开启Run DashBoard

    如果多个微服务项目时,Run DashBoard没有自动启动,
    可以通过修改idea的workspace.xml的方式来快速打开Run Dashboard窗口

    1. 打开工程路径下的.idea文件夹的workspace.xml
    2. <component name="RunDashboard">中修改或添加以下代码:
    <option name="configurationTypes">
    	<set>
    		<option value="SpringBootApplicationConfigurationType"/>
        </set>
    </option>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    由于idea版本差异,可能需要关闭重启。

    启动后效果:
    在这里插入图片描述

    工程重构

    观察cloud-consumer-order80与cloud-provider-payment8001两工程有重复代码(entities包下的实体),建一个公共模块重构。

    1. 新建模块cloud-api-commons

    2. POM.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <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>springcloud</artifactId>
            <groupId>cn.kt</groupId>
            <version>1.0-SNAPSHOT</version>
        </parent>
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>cn.kt</groupId>
        <artifactId>cloud-api-commons</artifactId>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <scope>runtime</scope>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <optional>true</optional>
            </dependency>
            <dependency>
                <groupId>cn.hutool</groupId>
                <artifactId>hutool-all</artifactId>
                <version>5.1.0</version>
            </dependency>
        </dependencies>
    </project>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    1. 添加公共部分代码到公共模块,并删除其他模块中的该代码

    2. maven clean、install cloud-api-commons工程,以供给其他工程调用。

    3. 在其他使用到的公共代码的工程中的pom中添加自己的依赖

    <!--引入自己的公共api-->
    <dependency>
        <groupId>cn.kt</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    1. 进行测试
  • 相关阅读:
    计算机毕设源码网站springboot毕业设计管理系统
    SpringBatch 使用过程中遇到的问题
    618购物狂欢节有哪些数码好物值得抢购?年终必备神器清单大揭秘!
    如何正确的做增量加工
    java计算机毕业设计网上购物商城源码+系统+数据库+lw文档+mybatis+运行部署
    2.2 Basic Statistics
    【Eclipse】设置自动提示
    05-prometheus的联邦模式-分布式监控
    PyTorch入门教学——简介与环境配置
    《这里数据库指南》——如何用 LlamaIndex和Milvus Cloud 搭建聊天机器人的总结?
  • 原文地址:https://blog.csdn.net/qq_42038623/article/details/125441169