微服务架构Spring Cloud

304 阅读4分钟

1. 微服务与微服务架构

微服务强调的是服务的大小,他关注的是具体解决某一个问题/提供落地对象服务的一个服务应用;

微服务架构是一种将单个应用程序开发为一组小服务的方法,每个小服务都在自己的进程中运行并与轻量级机制(通常是HTTP资源API)进行通信。这些服务围绕业务功能构建,并且可以由全自动部署机制独立部署。这些服务的集中管理几乎没有,可以用不同的编程语言编写并使用不同的数据存储技术。
微服务架构的概念源于2014年3月Martin Fowler所写的一篇文章“Microservices”。

2. 微服务架构与Spring Cloud的关系

微服务架构只是一种架构风格,Spring Cloud是对这种技术的实现。同样实现微服务架构的还有Dubbo。详情可参考DubboDubbo和Spring Cloud的区别

3. Spring Cloud核心组件

3.1. 注册中心-eureka/nacos

  • 什么是eureka
    eureka是Netflix的子模块之一,也是一个核心的模块,eureka里有2个组件,一个是EurekaServer(一个独立的项目),这个是用于定位服务以实现中间层服务器的负载平衡和故障转移;另一个便是EurekaClient(我们的微服务)它是用于与Server交互的。
    Spring Cloud Eureka 封装了 Netflix 公司开发的Eureka模块来实现服务注册和发现。

重要提醒:Eureka官方宣布:Eureka 2.0的开发停止,但是我们还可以继续使用1.X。同时阿里巴巴团队开源出nacos注册中心,功能比Eureka更强大,后续项目将会从Eureka切换到nacos

The existing open source work on eureka 2.0 is discontinued. The code base and artifacts that were released as part of the existing repository of work on the 2.x branch is considered use at your own risk.
Eureka 1.x is a core part of Netflix's service discovery system and is still an active project.
来自:github.com/Netflix/eur…

3.2. 客户端负载均衡-Ribbon

  • 什么是Ribbon

Ribbon is a Inter Process Communication (remote procedure calls) library with built in software load balancers. The primary usage model involves REST calls with various serialization scheme support. 来自:github.com/Netflix/rib…

Ribbon是Netflix发布的开源项目,是一个进程间通信(远程过程调用)库,具有内置的软件负载均衡器,属于客户端负载均衡器。
Spring Cloud Ribbon是基于Netflix Ribbon实现的一套客户端负载均衡的工具。

  • 客户端负载均衡和服务端负载均衡

3.3. 服务调用-feign

  • 什么是feign

Feign makes writing java http clients easier
Feign is a Java to HTTP client binder inspired by Retrofit, JAXRS-2.0, and WebSocket. Feign's first goal was reducing the complexity of binding Denominator uniformly to HTTP APIs regardless of ReSTfulness.
来自:github.com/OpenFeign/f…

Feign使编写Java HTTP客户端更加容易。Feign是一套HTTP客户端"绑定器"。这个"绑定"有点像ORM。ORM是把数据库字段和代码中的实体"绑定"起来;Feign提供的基本功能就是方便、简单地把HTTP的Request/Response和代码中的实体"绑定"起。

3.4. 断路器-hystrix

  • 什么是hystrix

Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable. 来自:github.com/Netflix/Hys…

Hystrix是Netflix发布的开源项目,是一个延迟和容错库,旨在隔离对远程系统,服务和第三方库的访问点,停止级联故障,并在不可避免发生故障的复杂分布式系统中实现弹性。

服务雪崩的处理方式

  • 降级
    降级是当我们的某个微服务响应时间过长,或者不可用了,不能把异常信息返回出来,或者让程序一直卡在那里,而是让程序直接返回,避免程序雪崩。
  • 熔断
    熔断就好像我们生活中的跳闸一样,比如说你的电路出故障了,为了防止出现大型事故这里直接切断了你的电源以免意外继续发生。
  • 限流
    限流就是限制你某个微服务的使用量(可用线程)。

3.5. 网关- zuul/gateway

  • 什么是zuul

Zuul is a gateway service that provides dynamic routing, monitoring, resiliency, security, and more. 来自:github.com/Netflix/zuu…

Zuul是Netflix发布的开源项目,是一个L7应用程序网关,可提供动态路由,监视,弹性,安全性等。其中路由功能负责将外部请求转发到具体的微服务实例上。

3.6. 配置中心-config/nacos

  • 什么是配置中心
    我们既然要做项目, 那么就少不了配置,传统的项目还好,但是我们微服务项目,每个微服务就要做独立的配置,这样难免有点复杂,所以,config项目出来了,它就是为了解决这个问题: 把你所有的微服务配置通过某个平台:比如github,gitlib或者其他的git仓库 进行集中化管理(当然,也可以放在本地)

Spring Cloud Config

3.7. 其它

断路器监控、分布式链路追踪