数字化转型之微服务之 –Spring Cloud

背景:随着公司业务量的飞速发展,平台面临的挑战已经远远大于业务,需求量不断增加,技术人员数量增加,面临的复杂度也大大增加。在这个背景下,平台的技术架构也完成了从传统的单体应用到微服务化的演进。


目前大部分公司用的微服务架构比较多的是Spring-Cloud

中文文档:https://springcloud.cc/

官方文档:http://projects.spring.io/spring-cloud/#quick-start



Spring Cloud provides tools for developersto quickly build some of the common patterns in distributed systems (e.g.configuration management, service discovery, circuit breakers, intelligentrouting, micro-proxy, control bus, one-time tokens, global locks, leadershipelection, distributed sessions, cluster state). Coordination of distributedsystems leads to boiler plate patterns, and using Spring Cloud developers canquickly stand up services and applications that implement those patterns. Theywill work well in any distributed environment, including the developer's ownlaptop, bare metal data centres, and managed platforms such as Cloud Foundry.

https://spring.io/guides


 

Spring Cloud 基于 Spring Boot,为微服务体系开发中的架构问题,提供了一整套的解决方案——服务注册与发现,服务消费,服务保护与熔断,网关,分布式调用追踪,分布式配置管理等。


Spring Boot 是 Spring 的一套快速配置脚手架,使用默认大于配置的理念,用于快速开发单个微服务



那么Spring Boot和和Spring Cloud 功能区别是什么呢?

[if !supportLists]1.   [endif]Spring Boot 它内嵌Web服务器(tomcat/jetty)的可执行程序的框架。你开发的web应用不需要作为war包部署到web服务器中[例如:把war包扔到tomcat app下],Spring Boot 直接作为一个可启动程序,直接把Web启动起来加载起来。

[if !supportLists]2.   [endif]Spring Cloud是一套微服务开发和治理框架,来自Netflex的OSS,包含了微服务运行的功能,可以通过Spring-boot 项目集成很多功能比如 RabbitMQ,ES,Ribbon,负载均衡,限流等。



Spring Cloud 核心功能


核心功能:

[if !supportLists]·        [endif]分布式/版本化配置

[if !supportLists]·        [endif]服务注册和发现

[if !supportLists]·        [endif]路由

[if !supportLists]·        [endif]服务和服务之间的调用

[if !supportLists]·        [endif]负载均衡

[if !supportLists]·        [endif]断路器

[if !supportLists]·        [endif]分布式消息传递


使用 Spring Cloud作为微服务开发框架,Spring Boot 拥有嵌入式 Tomcat,可直接运行一个 jar 包来发布微服务,此外它还提供了一系列“开箱即用”的插件,例如:配置中心,服务注册与发现,熔断器,路由,代理,控制总线,一次性令牌,全局锁,leader选举,分布式 会话,集群状态等,可大量提高我们的开发效率。功能

Spring Cloud

路由与负载均衡

Ribbon

注册中心

Eureka

网关

Zuul

断路器

Hystrix

分布式配置

Config

服务调用跟踪

sleuth

日志输出

elk

认证集成

oauth2

消息总线

Bus

批量任务

Task


[if !supportLists]·        [endif] 

Spring Cloud 基本项目搭建步骤

Spring Cloud 服务注册发现实现原理 如图 





spring中配置文件官方文档http://docs.spring.io/spring-boot/docs/1.5.1.RELEASE/reference/htmlsingle/




流程:

[if !supportLists]·        [endif]请求统一通过 API 网关(Zuul)来访问内部服务。

[if !supportLists]·        [endif]网关接收到请求后,从注册中心(Eureka)获取可用服务。

[if !supportLists]·        [endif]由 Ribbon 进行均衡负载后,分发到后端具体实例。

[if !supportLists]·        [endif]微服务之间通过 Feign 进行通信处理业务。

[if !supportLists]·        [endif]Hystrix 负责处理服务超时熔断。

[if !supportLists]·        [endif]Turbine 监控服务间的调用和熔断相关指标。

Spring Cloud工具框架

[if !supportLists]·        [endif]Spring Cloud Config 配置中心,利用 Git 集中管理程序的配置。

[if !supportLists]·        [endif]Spring Cloud Netflix 集成众多Netflix的开源软件。

[if !supportLists]·        [endif]Spring Cloud Netflix Eureka 服务中心(类似于管家的概念,需要什么直接从这里取,就可以了),一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移。

[if !supportLists]·        [endif]Spring Cloud Netflix Hystrix 熔断器,容错管理工具,旨在通过熔断机制控制服务和第三方库的节点,从而对延迟和故障提供更强大的容错能力。

[if !supportLists]·        [endif]Spring Cloud Netflix Zuul 网关,是在云平台上提供动态路由,监控,弹性,安全等边缘服务的框架。Web 网站后端所有请求的前门。

[if !supportLists]·        [endif]Spring Cloud Netflix Archaius 配置管理 API,包含一系列配置管理API,提供动态类型化属性、线程安全配置操作、轮询框架、回调机制等功能。

[if !supportLists]·        [endif]Spring Cloud Netflix Ribbon 负载均衡

[if !supportLists]·        [endif]Spring Cloud Netflix Fegin REST客户端

[if !supportLists]·        [endif]Spring Cloud Bus 消息总线,利用分布式消息将服务和服务实例连接在一起,用于在一个集群中传播状态的变化。

[if !supportLists]·        [endif]Spring Cloud for Cloud Foundry 利用 Pivotal Cloudfoundry 集成你的应用程序。

[if !supportLists]·        [endif]Spring Cloud Cloud Foundry Service Broker 为建立管理云托管服务的服务代理提供了一个起点。

[if !supportLists]·        [endif]Spring Cloud Cluster 集群工具,基于 Zookeeper, Redis, Hazelcast, Consul 实现的领导选举和平民状态模式的抽象和实现。

[if !supportLists]·        [endif]Spring Cloud Consul 基于 Hashicorp Consul 实现的服务发现和配置管理。

[if !supportLists]·        [endif]Spring Cloud Security 安全控制,在 Zuul 代理中为 OAuth2 REST 客户端和认证头转发提供负载均衡。

[if !supportLists]·        [endif]Spring Cloud Sleuth 分布式链路监控,SpringCloud 应用的分布式追踪系统,和 Zipkin,HTrace,ELK 兼容。

[if !supportLists]·        [endif]Spring Cloud Data Flow 一个云本地程序和操作模型,组成数据微服务在一个结构化的平台上。

[if !supportLists]·        [endif]Spring Cloud Stream 消息组件,基于 Redis,Rabbit,Kafka 实现的消息微服务,简单声明模型用以在 Spring Cloud 应用中收发消息。

[if !supportLists]·        [endif]Spring Cloud Stream App Starters 基于 Spring Boot 为外部系统提供 Spring 的集成。

[if !supportLists]·        [endif]Spring Cloud Task 短生命周期的微服务,为 Spring Booot 应用简单声明添加功能和非功能特性。

[if !supportLists]·        [endif]Spring Cloud Task App Starters。

[if !supportLists]·        [endif]Spring Cloud Zookeeper 服务发现和配置管理基于 Apache Zookeeper。

[if !supportLists]·        [endif]Spring Cloud for Amazon Web Services 快速和亚马逊网络服务集成。

[if !supportLists]·        [endif]Spring Cloud Connectors 便于PaaS应用在各种平台上连接到后端像数据库和消息经纪服务。

[if !supportLists]·        [endif]Spring Cloud Starters (项目已经终止并且在 Angel.SR2 后的版本和其他项目合并)

[if !supportLists]·        [endif]Spring Cloud CLI 命令行工具,插件用 Groovy 快速的创建 Spring Cloud 组件应用。


Spring Cloud 的一些优点

[if !supportLists]·        [endif]有强大的 Spring 社区、Netflix 等公司支持,并且开源社区贡献非常活跃

[if !supportLists]·        [endif]标准化的将微服务的成熟产品和框架结合一起,Spring Cloud 提供整套的微服务解决方案,开发成本较低,且风险较小

[if !supportLists]·        [endif]基于 Spring Boot,具有简单配置、快速开发、轻松部署、方便测试的特点。

[if !supportLists]·        [endif]支持 REST 服务调用,相比于 RPC,更加轻量化和灵活(服务之间只依赖一纸契约,不存在代码级别的强依赖),有利于跨语言服务的实现,以及服务的发布部署。另外,结合 Swagger,也使得服务的文档一体化

[if !supportLists]·        [endif]提供了 Docker 及 Kubernetes 微服务编排支持。

[if !supportLists]·        [endif]国内外企业应用非常多,经受了大公司的应用考验(比如 Netfilx 公司),以及强大的开源社区支持。

Spring Cloud 的一些问题

[if !supportLists]·        [endif]支持 REST 服务调用,可能因为接口定义过轻,导致定义文档与实际实现不一致导致服务集成时的问题(可以使用统一文档和版本管理解决,比如 Swagger)。

[if !supportLists]·        [endif]另外,REST 服务调用性能会比 RPC 低一些(但也不是强绑定)

[if !supportLists]·        [endif]Spring Cloud 整合了大量组件,相关文档比较复杂,需要针对性的进行阅读。


©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容