什么是微服务?

"微服务架构是一种架构模式,它提倡将单一应用程序划分成一组小的服务,服务之间相互协调、互相配合,为用户提供最终价值。每个服务运行在其独立的进程中,服务和服务之间采用轻量级的通信机制相互沟通(通常是基于HTTP的Restful API).每个服务都围绕着具体的业务进行构建,并且能够被独立的部署到生产环境、类生产环境等。另外,应尽量避免统一的、集中的服务管理机制,对具体的一个服务而言,应根据业务上下文,选择合适的语言、工具对其进行构" ——Martin Fowler的博客

SpringCloud简介

SpringCloud用来干嘛?

学习新技术,官网会给出最权威的答案,不妨看看官网对SpringCloud的介绍:

https://spring.io/projects/spring-cloud

Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state).

Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具,如:配置管理【configuration management】,服务发现【service discovery】,熔断机制【circuit breakers】,智能路由【intelligent routing】,微代理【micro-proxy】,控制总线【control bus】,一次性令牌【one-time tokens】,全局锁【global locks】,leader投票选举【leadership election】,分布式会话【distributed sessions】,集群状态【cluster state】。

特性

Spring Cloud 侧重提供开箱即用的典型用例和允许自由扩展的机制。

  • Distributed/versioned configuration 分布式/版本控制配置
  • Service registration and discovery 服务注册与发现
  • Routing 路由
  • Service-to-service calls 服务到服务的调用
  • Load balancing 负载均衡
  • Circuit Breakers 熔断机制
  • Global locks 全局锁
  • Leadership election and cluster state 选举与集群状态管理
  • Distributed messaging 分布式消息

构建注意事项

从0到1构建新的SpringCloud工程

最简单的方法是访问start.spring.io网站,选择合适的SpringBoot和SpringCloud版本,进行构建。

在已有SpringBoot应用程序基础上构建SpringCloud

需要注意版本兼容性,确定你需要的SpringCloud版本,这由原本存在的SpringBoot版本决定。目前的版本依赖关系如下:

 

需要注意的是Dalston,Edgware,Finchley已经不再支持。

更多详细的版本对应关系,可以访问网址:https://start.spring.io/actuator/info

spring-cloud: {
	//...省略
    Greenwich.M1: "Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE",
    Greenwich.SR6: "Spring Boot >=2.1.0.RELEASE and <2.1.999.BUILD-SNAPSHOT",
    Greenwich.BUILD-SNAPSHOT: "Spring Boot >=2.1.999.BUILD-SNAPSHOT and <2.2.0.M4",
    Hoxton.SR9: "Spring Boot >=2.2.0.M4 and <2.3.7.BUILD-SNAPSHOT",
    Hoxton.BUILD-SNAPSHOT: "Spring Boot >=2.3.7.BUILD-SNAPSHOT and <2.4.0.M1",
    2020.0.0-M3: "Spring Boot >=2.4.0.M1 and <=2.4.0.M1",
    2020.0.0-M4: "Spring Boot >=2.4.0.M2 and <=2.4.0-M3",
    2020.0.0-SNAPSHOT: "Spring Boot >=2.4.0-M4"
}
spring-cloud-alibaba: {
	2.2.1.RELEASE: "Spring Boot >=2.2.0.RELEASE and <2.3.0.M1"
}

官方建议:官方通过service release【SR】,将Bug修复和向后兼容的特性添加到每个版本系列中,因此,一旦确定要使用哪个版本的SpringCloud,就应使用该版本系列的最新服务版本。

了解版本对应关系约束必要性之后,便可以引入适当的Spring Cloud BOM了。

<properties>
    <spring.cloud-version>Hoxton.SR8</spring.cloud-version>
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring.cloud-version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

和SpringBoot一样,SpringCloud工程也包含starters,你可以为其添加许多不同的特性,并将其作为依赖加入项目之中。大多数情况下,你只需要将starters加入到类路径下就可以启用功能。以下展示如何将Spring Cloud Config客户端和Spring Cloud Netflix Eureka客户端添加到应用程序:

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    ...
</dependencies>

微服务架构

下图为杨波老师于2018年5月7日在一个可供中小团队参考的微服务架构技术栈一文中,结合自身的实战落地经验,总结的一套贴近国内技术文化特色的轻量级微服务参考技术栈。

 

SpringCloud的主要项目:

 

这是SpringCloud官方在维护的项目集合,有些组件功能完全可以被更优秀的框架替代,这个视情况而定。

SpringCloud组件选型

 

 

原文链接:https://www.cnblogs.com/summerday152/p/14001421.html

如果觉得本文对你有帮助,可以转发关注支持一下