SpringCloud第一天

单机架构

  • 优点: 易于测试 便于集成 ⼩型项⽬友好
  • 缺点: 开发速度慢 启动时间⻓ 依赖庞⼤

分布式架构

SOA :Service Oriented Architecture ⾯向服务的架构

其中包含多个服务, 服务之间通过相互依赖最终提供⼀
系列的功能, ⼀个服务 通常以独⽴的形式存在与操作系
统进程中, 各个服务之间 通过⽹络调⽤。

微服务:将⼀个⼤的单体应⽤进⾏细粒度的服务化拆

分,每个拆分出来的服务各⾃独⽴打包部署,各个服务
之间 通过⽹络调⽤。

  • 优点
    • 易开发、理解和维护
    • 独⽴的部署和启动
  • 缺点
    • 分布式系统-》分布式事务问题
    • 需要管理多个服务-》服务治理
      常见组件和名字解释
  1. 网关:进行路由转发,过滤器,
  2. 服务发现注册(调用和被调用方的信息维护,当服务启动的时候,都会向他注册)
  3. 配置中心(动态更新application.properties),将每一个微服务的端口号啥的都记录到这里
  4. 链路追踪:分析调⽤链路耗时 例⼦:下单-》查询商品服务获取
    商品价格-》查询⽤户信息-》保存数据库,这个例子都是调用的不同的微服务,可以通过可视化的页面追踪
  5. 负载均衡器: 分发流量到多个节点,降低压⼒
  6. 熔断:保护⾃⼰和被调⽤⽅,当有一个服务不可用的时候,进行降级,也就是不用这个必要的服务

整体架构选用的是AlibabaCloud
SpringCloud很多组件是基于第三⽅整合,⽬前多个已
经不更新了,⽐如zuul、eureka、hystrix等。
AlibabaCloud 提供⼀站式微服务解决⽅法,已经和
SpringCloud进⾏了整合,组件互相⽀持
AlibabaCloud全家桶介绍

开始搭建项目初始结构

首先是数据库建表
user表

video表

video_user表

然后创建一个初始化的maven项目,一开始什么也没有,这时候我们也不需要src了,可以直接删掉

然后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>hzwq.wpb</groupId>
    <artifactId>spring-cloud</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <!--
           https://mvnrepository.com/artifact/org.spring
           framework.boot/spring-boot-dependencies/2.3.3.RELEASE-->
            <dependency>

                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>

                <version>2.3.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--
           https://mvnrepository.com/artifact/org.spring
           framework.cloud/spring-cloud�dependencies/Hoxton.SR8-->
            <dependency>

                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Hoxton.SR8</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--
           https://mvnrepository.com/artifact/com.alibab
           a.cloud/spring-cloud-alibaba-dependencies/2.2.1.RELEASE-->
            <dependency>

                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>

                <version>2.2.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <build>
        <plugins>
            <plugin>

                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>

                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

然后在这个项目中创建4个子模块

然后在user,video,order这三个模块中依赖于common模块

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>hzwq.wpb</groupId>
            <artifactId>wpb-common</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>

然后再common包下创建三个实体类

public class User {
 private Integer id;
 private String name;
 private String pwd;
 private String headImg;
 private String phone;
 private Date createTime;
 private String wechat;
}
public class Video {
 private Integer id;
 private String title;
 private String summary;
 private String coverImg;
 private Integer price;
 private Date createTime;
 private Double point;
}
public class VideoOrder {
 private Integer id;
 private String outTradeNo;
 private Integer state;
 private Date createTime;
 private Integer totalFee;
 private Integer videoId;
 private String videoTitle;
 private String videoImg;
 private Integer userId;
}

然后再在主pom文件中添加

<properties>
 <java.version>1.8</java.version>
 
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
 </properties>
 
 
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.2</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
</dependency>

然后在三个字模块中application.yml添加mybatis的配置

server:
 port: 9000
spring:
 application:
 name: wpb-video-service
 datasource:
 driver-class-name:
com.mysql.cj.jdbc.Driver
 url:
jdbc:mysql://127.0.0.1:3306/wpb?useUnicode=true&characterEncoding=utf-8&useSSL=false
 username: root
 password: 123456
# 控制台输出sql、下划线转驼峰
mybatis:
 configuration:
 log-impl:
org.apache.ibatis.logging.stdout.StdOutImpl
 map-underscore-to-camel-case: true

然后每个模块剩下的就和springboot基本上一致了。


个人qq:835493858 有事联系我
原文地址:https://www.cnblogs.com/wpbing/p/14322825.html