如何学习spring cloud

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何学习spring cloud相关的知识,希望对你有一定的参考价值。

Spring Cloud 学习笔记(一)——入门、特征、配置

0 放在前面
0.1 参考文档
http://cloud.spring.io/spring-cloud-static/Brixton.SR7/
https://springcloud.cc/
http://projects.spring.io/spring-cloud/
0.2 maven配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>

0.3 简介
Spring Cloud为开发人员提供了快速构建分布式系统中的一些通用模式(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁,领导选举,分布式 会话,群集状态)。 分布式系统的协调引出样板模式(boiler plate patterns),并且使用Spring Cloud开发人员可以快速地实现这些模式来启动服务和应用程序。 它们可以在任何分布式环境中正常工作,包括开发人员自己的笔记本电脑,裸机数据中心和受管平台,如Cloud Foundry。
Version: Brixton.SR7
1 特征
Spring Cloud专注于为经典用例和扩展机制提供良好的开箱即用
分布式/版本配置
服务注册与发现
路由选择
服务调用
负载均衡
熔断机制
全局锁
领导人选举和集群状态
分布式消息
2 原生云应用程序
原生云是应用程序开发的一种风格,鼓励在持续交付和价值驱动领域的最佳实践。
Spring Cloud的很多特性是基于Spring Boot的。更多的是由两个库实现:Spring Cloud Context and Spring Cloud Commons。
2.1 Spring Cloud Context: 应用上下文服务
Spring Boot关于使用Spring构建应用有硬性规定:通用的配置文件在固定的位置,通用管理终端,监控任务。建立在这个基础上,Spring Cloud增加了一些额外的特性。
2.1.1 引导应用程序上下文
Spring Cloud会创建一个“bootstrap”的上下文,这是主应用程序的父上下文。对应的配置文件拥有最高优先级,并且,默认不能被本地配置文件覆盖。对应的文件名bootstrap.yml或bootstrap.properties。
可通过设置spring.cloud.bootstrap.enabled=false来禁止bootstrap进程。
2.1.2 应用上下文层级结构
当用SpringApplication或SpringApplicationBuilder创建应用程序上下文时,bootstrap上下文将作为父上下文被添加进去,子上下文将继承父上下文的属性。
子上下文的配置信息可覆盖父上下文的配置信息。
2.1.3 修改Bootstrap配置文件位置
spring.cloud.bootstrap.name(默认是bootstrap),或者spring.cloud.bootstrap.location(默认是空)
2.1.4 覆盖远程配置文件的值
spring.cloud.config.allowOverride=true
spring.cloud.config.overrideNone=true
spring.cloud.config.overrideSystemProperties=false
2.1.5 定制Bootstrap配置
在/META-INF/spring.factories的key为org.springframework.cloud.bootstrap.BootstrapConfiguration,定义了Bootstrap启动的组件。
在主应用程序启动之前,一开始Bootstrap上下文创建在spring.factories文件中的组件,然后是@Beans类型的bean。
2.1.6 定制Bootstrap属性来源
关键点:spring.factories、PropertySourceLocator
2.1.7 环境改变
应用程序可通过EnvironmentChangedEvent监听应用程序并做出响应。
2.1.8 Refresh Scope
Spring的bean被@RefreshScope将做特殊处理,可用于刷新bean的配置信息。
注意
需要添加依赖“org.springframework.boot.spring-boot-starter-actuator”
目前我只在@Controller测试成功
需要自己发送POST请求/refresh
修改配置文件即可
2.1.9 加密和解密
Spring Cloud可对配置文件的值进行加密。
如果有"Illegal key size"异常,那么需要安装JCE。
2.1.10 服务点
除了Spring Boot提供的服务点,Spring Cloud也提供了一些服务点用于管理,注意都是POST请求
/env:更新Environment、重新绑定@ConfigurationProperties跟日志级别
/refresh重新加载配置文件,刷新标记@RefreshScope的bean
/restart重启应用,默认不可用
生命周期方法:/pause、/resume
2.2 Spring Cloud Commons:通用抽象
服务发现、负载均衡、熔断机制这种模式为Spring Cloud客户端提供了一个通用的抽象层。
2.2.1 RestTemplate作为负载均衡客户端
通过@Bean跟@LoadBalanced指定RestTemplate。注意URI需要使用虚拟域名(如服务名,不能用域名)。
如下:
@Configuration
public class MyConfiguration

@LoadBalanced
@Bean
RestTemplate restTemplate()
return new RestTemplate();



public class MyClass
@Autowired
private RestTemplate restTemplate;

public String doOtherStuff()
String results = restTemplate.getForObject("http://stores/stores", String.class);
return results;



2.2.2 多个RestTemplate对象
注意@Primary注解的使用。
@Configuration
public class MyConfiguration

@LoadBalanced
@Bean
RestTemplate loadBalanced()
return new RestTemplate();


@Primary
@Bean
RestTemplate restTemplate()
return new RestTemplate();



public class MyClass
@Autowired
private RestTemplate restTemplate;

@Autowired
@LoadBalanced
private RestTemplate loadBalanced;

public String doOtherStuff()
return loadBalanced.getForObject("http://stores/stores", String.class);


public String doStuff()
return restTemplate.getForObject("http://example.com", String.class);



2.2.3 忽略网络接口
忽略确定名字的服务发现注册,支持正则表达式配置。
3 Spring Cloud Config
Spring Cloud Config提供服务端和客户端在分布式系统中扩展配置。支持不同环境的配置(开发、测试、生产)。使用Git做默认配置后端,可支持配置环境打版本标签。
3.1 快速开始
可通过IDE运行或maven运行。
默认加载property资源的策略是克隆一个git仓库(at spring.cloud.config.server.git.uri')。
HTTP服务资源的构成:
/application/profile[/label]
/application-profile.yml
/label/application-profile.yml
/application-profile.properties
/label/application-profile.properties

application是SpringApplication的spring.config.name,(一般来说'application'是一个常规的Spring Boot应用),profile是一个active的profile(或者逗号分隔的属性列表),label是一个可选的git标签(默认为"master")。
3.1.1 客户端示例
创建以Spring Boot应用即可,添加依赖“org.springframework.cloud:spring-cloud-starter-config”。
配置application.properties,注意URL为配置服务端的地址
spring.cloud.config.uri: http://myconfigserver.com

3.2 Spring Cloud Config 服务端
针对系统外的配置项(如name-value对或相同功能的YAML内容),该服务器提供了基于资源的HTTP接口。使用@EnableConfigServer注解,该服务器可以很容易的被嵌入到Spring Boot 系统中。使用该注解之后该应用系统就是一个配置服务器。
@SpringBootApplication
@EnableConfigServer
public class ConfigApplicion
public static void main(String[] args) throws Exception
SpringApplication.run(ConfigApplicion.class, args);



3.2.1 资源库环境
application 对应客户端的"spring.application.name"属性
profile 对应客户端的 "spring.profiles.active"属性(逗号分隔的列表)
label 对应服务端属性,这个属性能标示一组配置文件的版本
如果配置库是基于文件的,服务器将从application.yml和foo.yml中创建一个Environment对象。高优先级的配置优先转成Environment对象中的PropertySource。
3.2.1.1 Git后端
默认的EnvironmentRepository是用Git后端进行实现的,Git后端对于管理升级和物理环境是很方便的,对审计配置变更也很方便。也可以file:前缀从本地配置库中读取数据。
这个配置库的实现通过映射HTTP资源的label参数作为git label(提交id,分支名称或tag)。如果git分支或tag的名称包含一个斜杠 ("/"),此时HTTP URL中的label需要使用特殊字符串"(_)"来替代(为了避免与其他URL路径相互混淆)。如果使用了命令行客户端如 curl,请谨慎处理URL中的括号(例如:在shell下请使用引号''来转义它们)。
Git URI占位符
Spring Cloud Config Server支持git库URL中包含针对application和 profile的占位符(如果你需要,label也可包含占位符, 不过要牢记的是任何情况下label只指git的label)。所以,你可以很容易的支持“一个应用系统一个配置库”策略或“一个profile一个配置库”策略。
模式匹配和多资源库
spring:
cloud:
config:
server:
git:
uri: https://github.com/spring-cloud-samples/config-repo
repos:
simple: https://github.com/simple/config-repo
special:
pattern: special*/dev*,*special*/dev*
uri: https://github.com/special/config-repo
local:
pattern: local*
uri: file:/home/configsvc/config-repo

如果 application/profile不能匹配任何表达式,那么将使用“spring.cloud.config.server.git.uri”对应的值。在上例子中,对于 "simple" 配置库, 匹配模式是simple/* (也就说,无论profile是什么,它只匹配application名称为“simple”的应用系统)。“local”库匹配所有application名称以“local”开头任何应用系统,不管profiles是什么(来实现覆盖因没有配置对profile的匹配规则,“/*”后缀会被自动的增加到任何的匹配表达式中)。
Git搜索路径中的占位符
spring.cloud.config.server.git.searchPaths
3.2.1.2 版本控制后端文件系统使用
伴随着版本控制系统作为后端(git、svn),文件都会被check out或clone 到本地文件系统中。默认这些文件会被放置到以config-repo-为前缀的系统临时目录中。在Linux上,譬如应该是/tmp/config-repo-<randomid>目录。有些操作系统routinely clean out放到临时目录中,这会导致不可预知的问题出现。为了避免这个问题,通过设置spring.cloud.config.server.git.basedir或spring.cloud.config.server.svn.basedir参数值为非系统临时目录。
3.2.1.3 文件系统后端
使用本地加载配置文件。
需要配置:spring.cloud.config.server.native.searchLocations跟spring.profiles.active=native。
路径配置格式:classpath:/, classpath:/config,file:./, file:./config。
3.2.1.4 共享配置给所有应用
基于文件的资源库
在基于文件的资源库中(i.e. git, svn and native),这样的文件名application*命名的资源在所有的客户端都是共享的(如 application.properties, application.yml, application-*.properties,etc.)。
属性覆盖
“spring.cloud.config.server.overrides”添加一个Map类型的name-value对来实现覆盖。
例如
spring:
cloud:
config:
server:
overrides:
foo: bar

会使所有的配置客户端应用程序读取foo=bar到他们自己配置参数中。
3.2.2 健康指示器
通过这个指示器能够检查已经配置的EnvironmentRepository是否正常运行。
通过设置spring.cloud.config.server.health.enabled=false参数来禁用健康指示器。
3.2.3 安全
你可以自由选择任何你觉得合理的方式来保护你的Config Server(从物理网络安全到OAuth2 令牌),同时使用Spring Security和Spring Boot 能使你做更多其他有用的事情。
为了使用默认的Spring Boot HTTP Basic 安全,只需要把Spring Security 增加到classpath中(如org.springframework.boot.spring-boot-starter-security)。默认的用户名是“user”,对应的会生成一个随机密码,这种情况在实际使用中并没有意义,一般建议配置一个密码(通过 security.user.password属性进行配置)并对这个密码进行加密。
3.2.4 加密与解密
如果远程属性包含加密内容(以cipher开头),这些值将在通过HTTP传递到客户端之前被解密。
使用略
3.2.5 密钥管理
配置服务可以使用对称(共享)密钥或者非对称密钥(RSA密钥对)。
使用略
3.2.6 创建一个测试密钥库
3.2.7 使用多密钥和循环密钥
3.2.8 加密属性服务
3.3 可替换格式服务
配置文件可加后缀".yml"、".yaml"、".properties"
3.4 文本解释服务
/name/profile/label/path
3.5 嵌入配置服务器
一般配置服务运行在单独的应用里面,只要使用注解@EnableConfigServer即可嵌入到其他应用。
3.6 推送通知和总线
添加依赖spring-cloud-config-monitor,激活Spring Cloud 总线,/monitor端点即可用。
当webhook激活,针对应用程序可能已经变化了的,配置服务端将发送一个RefreshRemoteApplicationEvent。
3.7 客户端配置
3.7.1 配置第一次引导
通过spring.cloud.config.uri属性配置Config Server地址
3.7.2 发现第一次引导
如果用的是Netflix,则用eureka.client.serviceUrl.defaultZone进行配置。
3.7.3 配置客户端快速失败
在一些例子里面,可能希望在没有连接配置服务端时直接启动失败。可通过spring.cloud.config.failFast=true进行配置。
3.7.4 配置客户端重试
添加依赖spring-retry、spring-boot-starter-aop,设置spring.cloud.config.failFast=true。默认的是6次重试,初始补偿间隔是1000ms,后续补偿为1.1指数乘数,可通过spring.cloud.config.retry.*配置进行修改。
3.7.5 定位远程配置资源
路径:/name/profile/label
"name" = $spring.application.name
"profile" = $spring.profiles.active (actually Environment.getActiveProfiles())
"label" = "master"
label对于回滚到之前的版本很有用。
3.7.6 安全
通过spring.cloud.config.password、spring.cloud.config.username进行配置。
参考技术A

spring cloud学习可以去看官网

主要是学习他的组件,各种各样的组件

Spring Cloud Eureka、Spring Cloud Ribbon、Spring Cloud Feign、Spring Cloud Hystrix、Spring Cloud Config、Spring Cloud Zuul、Spring Cloud Bus

如果要更详细的学习课程可以去https://www.itkc8.com看看里面很多关于Springcloud的学习视频

以上是关于如何学习spring cloud的主要内容,如果未能解决你的问题,请参考以下文章

Spring cloud简单学习总结

Spring cloud应该如何入门,需要学习哪些基础才可以快速掌握?

Spring Cloud 学习 Spring Cloud Config

spring+cloud学习

Spring Cloud 学习 Spring Boot Admin

Spring Cloud 学习——7. Spring Cloud Config