SpringCloud最详细

本文详细介绍了SpringCloud作为微服务一站式解决方案的核心组件,包括Eureka、Ribbon、Feign、Hystrix、Zuul等,并与Dubbo进行对比。通过实例展示了如何搭建微服务环境,配置服务注册与发现、负载均衡、服务熔断和调用监控。最后,文章提到了Zuul作为服务网关的作用,实现了请求路由和过滤功能。
摘要由CSDN通过智能技术生成

SpringCloud是什么

  • SpringCloud, 基于SpringBoot提供了一套微服务解决方案,包括服务注册与发现,配置中心,全链路监控,服务网关,负载均衡,熔断器等组件,除了基于NetFlix的开源组件做高度抽象封装之外,还有一些选型中立的开源组件。

  • SpringCloud利用SpringBoot的开发便利性,巧妙地简化了分布式系统基础设施的开发,SpringCloud为开发人员提供了快速构建分布式系统的一些工具,包括配置管理,服务发现,断路器,路由,微代理,事件总线,全局锁,决策竞选,分布式会话等等,他们都可以用SpringBoot的开发风格做到一键启动和部署。

  • SpringBoot并没有重复造轮子,它只是将目前各家公司开发的比较成熟,经得起实际考研的服务框架组合起来,通过SpringBoot风格进行再封装,屏蔽掉了复杂的配置和实现原理,最终给开发者留出了一套 简单易懂,易部署和易维护的分布式系统开发工具包

  • SpringCloud 是 分布式微服务架构下的一站式解决方案,是各个微服务架构落地技术的集合体,俗称微服务全桶。

  • SpringBoot可以离开SpringClooud独立使用,开发项目,但是SpringCloud离不开SpringBoot,属于依赖关系

常用相关组件

  • 服务发现——Netflix Eureka

  • 客服端负载均衡 Netflix Ribbon

  • 服务端负载均衡:Feign

  • 断路器——Netflix Hystrix

  • 服务网关——Netflix Zuul

与dubbo的对比

Dubbo SpringCloud
服务注册中心 Zookeeper Spring Cloud Netfilx Eureka
服务调用方式 RPC REST API
服务监控 Dubbo-monitor Spring Boot Admin
断路器 不完善 Spring Cloud Netfilx Hystrix
服务网关 Spring Cloud Netfilx Zuul
分布式配置 Spring Cloud Config
服务跟踪 Spring Cloud Sleuth
消息总栈 Spring Cloud Bus
数据流 Spring Cloud Stream
批量任务 Spring Cloud Task

最大区别:SpringCloud抛弃了Dubbo的RPC通信,采用的是基于HTTP的REST方式。

严格来说,这两种方式各有优劣。虽然从一定程度上来说,后者牺牲了服务调用的性能,但也避免了上面提到的原生RPC带来的问题。而且REST相比RPC更为灵活,服务提供方和调用方的依赖只依靠一纸契约,不存在代码级别的强依赖,这在强调快速演化的微服务环境下,显得更加合适。dubbo是一套rpc框架但是springcloud确实微服的一站式解决方案

版本号

SpringCloud没有采用数字编号的方式命名版本号,而是采用了伦敦地铁站的名称,同时根据字母表的顺序来对应版本时间顺序,比如最早的Realse版本:Angel,第二个Realse版本:Brixton,然后是Camden、Dalston\Edgware,目前最新的是Hoxton SR4 CURRENT GA通用稳定版。

版本说明

BUILD-XXX      开发版    开发团队内部使用,不是很稳定

GA          稳定版    相比于开发版,基本上可以使用了

PRE(M1、M2)    里程碑版   主要是修复了一些BUG的版本,一个GA后通常有多个里程碑版

RC          候选发布版  该阶段的软件类似于最终版的一个发行观察期,基本只修复比较严重的BUG

SR          正式发布版  

搭建微服环境

准备工作

1.内存起码8g的电脑

2.idea

3.搭建父项目

springcloud-demo

建最简单的maven项目

建出来之后内部只有.idea和pom.xml

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>kj08</groupId>
    <artifactId>springcloud-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>common</module>
    </modules>
    <packaging>pom</packaging><!--必须是pom,使用maven分模块管理,都会有一个父级项目,pom文件一个重要的属性就是packaging(打包类型),一般来说所有的父级项目的packaging都为pom,packaging默认类型jar类型,如果不做配置,maven会将该项目打成jar包。-->
​
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencyManagement>
        <!--Maven中的dependencyManagement元素提供了一种管理依赖版本号的方式。
        在dependencyManagement元素中声明所依赖的jar包的版本号等信息,那么
        所有子项目再次引入此依赖jar包时则无需显式的列出版本号。Maven会沿着父子
        层级向上寻找拥有dependencyManagement 元素的项目,然后使用它指定的版本号。
        dependencyManagement中定义的只是依赖的声明,并不实现引入,因此子项目需要显式的声明需要用的依赖。-->
​
        <dependencies>
            <!--SpringCloud的依赖-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR1</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--Maven的继承和Java的继承一样,只能单继承,无法实现多继承,
            你是否想过我们如果要继承多个父模块的时候应该怎么做呢?或许你会
            想只往一个父模块中添加jar包依赖,只继承一个父模块就可以了,
            但是这样的话所有的jar包都混合在一起了,jar包分类就不在清晰了,
            其实我们可以用另外一种方式实现多模块继承,这个方法就是使用
            <type>pom</type><scope>import</scope>,解释一下:type
            标签的默认值是jar,代表我们依赖导入的是一个jar包,现在我们设置成了
            pom,说明导入的是一个父模块,后面的scope标签中的值import代表把父
            模块中的jar包导入进来,不过需要注意的是<type>pom</type><scope>
            import</scope>,这种方式只能用在<dependencyManagement>
            </dependencyManagement>-->
            <!--SpringBoot的依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.4.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--数据库-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
            <!--数据源-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>1.1.12</version>
            </dependency>
​
            <dependency>
                <groupId>org.mybatis.spring.boot</groupId>
                <artifactId>mybatis-spring-boot-starter</artifactId>
                <version>1.3.2</version>
            </dependency>
            <!--=========日志相关=============-->
            <!--junit-->
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>${junit.version}</version>
            </dependency>
            <!--logback-->
            <dependency>
                <groupId>ch.qos.logback</groupId>
                <artifactId>logback-core</artifactId>
                <version>1.2.3</version>
            </dependency>
            <!--log4j-->
            <dependency>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
                <version>${log4j.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-devtools</artifactId>
                <version>2.0.4.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>2.0.4.RELEASE</version>
            </dependency>
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.5.7</version>
                <scope>compile</scope>
            </dependency>
​
        </dependencies>
​
​
​
    </dependencyManagement>
​
</project>

4.创建一个公共调用模块 也就是一个子工程(父工程右键加模块) 取名common

用quickstart原型就行了

里面放公共的实体类

后面创建的服务或者是客户端如果用到这些类的话就不用去自己创建了,只需在pom要引用这个common模块就行了

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-demo</artifactId>
        <groupId>kj08</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
​
    <artifactId>common</artifactId>
​
    <name>common</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>
​
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
​
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
​
​
    </dependencies>
​
    <build>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一个基于Spring Boot实现的微服务框架。它提供了一系列开箱即用的工具和组件,帮助开发者快速构建和管理微服务应用。下面是Spring Cloud框架的详细介绍: 1. 服务发现 Spring Cloud提供了服务发现组件,可以自动注册和发现服务实例。它使用了Consul、Eureka、Zookeeper等注册中心,使得微服务之间的通信更加方便。 2. 负载均衡 Spring Cloud提供了负载均衡组件,可以自动将请求分配到多个实例上,从而实现负载均衡。它使用了Ribbon、LoadBalancer等组件,可以很好地处理高并发的请求。 3. 配置中心 Spring Cloud提供了配置中心组件,可以集中管理微服务的配置信息。它使用了Config Server、Vault等组件,可以实现动态的配置更新和管理。 4. 断路器 Spring Cloud提供了断路器组件,可以在服务发生故障时自动切换到备用服务上。它使用了Hystrix等组件,可以实现高可用性的服务架构。 5. API网关 Spring Cloud提供了API网关组件,可以将多个微服务聚合在一起,对外提供一个统一的接口。它使用了Zuul、Spring Cloud Gateway等组件,可以实现请求路由、过滤、转发等功能。 6. 分布式追踪 Spring Cloud提供了分布式追踪组件,可以跟踪微服务之间的调用链路和传递的数据。它使用了Zipkin、Sleuth等组件,可以实现请求的追踪和监控。 7. 消息总线 Spring Cloud提供了消息总线组件,可以实现微服务之间的消息传递和通信。它使用了Bus、Kafka等组件,可以实现消息的广播和订阅。 8. 安全管理 Spring Cloud提供了安全管理组件,可以实现微服务之间的安全通信和身份认证。它使用了OAuth2、Spring Security等组件,可以实现用户认证、授权和访问控制。 总之,Spring Cloud是一个非常强大的微服务框架,它提供了一系列开箱即用的组件和工具,可以帮助开发者快速构建和管理微服务应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值