Spring Cloud序幕
- 特性
- Spring Cloud 上下文
Spring Cloud序幕
特性
- 分布式/版本配置
- 服务注册和发现
- 路由
- 服务调用
- 负载均衡
- 短路
- 分布式消息
云原声
Spring Cloud Context
Bootstrap Context作为父上下文,名称为bootstrap,用来加载bootstrap配置信息,默认为bootstrap.properties|yml,PropertySource名称为applicationConfig: [classpath:/bootstrap.yml],加载具有最高优先级,默认的配置子上下文可以进行覆盖,也就是默认的bootstrap配置是最低优先级,自定义bootstrap的配置具有优先级。应用上下文作为子上下文。
bootstrap配置文件是由BootstrapApplicationListener进行加载,优先级为第6位
application配置文件是由ConfigFileApplicationListener进行加载,优先级为第11位
Bootstrap 配置
需要在程序启动的上一层进行配置,比如程序的运行参数、JVM参数、系统的环境变量
spring.cloud.bootstrap.namespring.cloud.bootstrap.location
覆盖远程配置
org.springframework.cloud.bootstrap.config.PropertySourceBootstrapProperties
spring.cloud.config.allowOverride=true允许覆盖配置spring.cloud.config.overrideNone=false外部化配置处于最低优先级并且不可覆盖spring.cloud.config.overrideSystemProperties=true只有系统环境、命令行参数以及环境变量可以覆盖远程配置,其他的本地配置文件不可覆盖
自定义Bootstrap 配置
在META-INF/spring.factories中添加org.springframework.cloud.bootstrap.BootstrapConfiguration对应的配置类
自定义Bootstrap 配置资源
这个自定义的PropertySource具有最高优先级
实现
org.springframework.cloud.bootstrap.config.PropertySourceLocator#locate方法1
2
3
4
5
6
7
8
9
10
11
12
13
14/**
* 自定义实现
*
* @author FelixFly 2019/12/22
*/
@Configuration
public class CustomPropertySourceLocator implements PropertySourceLocator {
@Override
public PropertySource<?> locate(Environment environment) {
return new MapPropertySource("customProperty",
Collections.singletonMap("spring.application.name", "custom-bootstrap"));
}
}在META-INF/spring.factories中加入配置
1
2org.springframework.cloud.bootstrap.BootstrapConfiguration=\
top.felixfly.cloud.config.CustomPropertySourceLocator
日志配置
可以将日志配置配置到bootstrap配置中,但是不支持自定义的前缀
环境改变
发布
EnvironmentChangedEvent事件
@COnfigurationProperties@RefreshScope
加密和解密
需要添加org.springframework.security:spring-security-rsa
端点
- [
POST]/actuator/env更新环境配置,重新绑定@CongigurationProperties和日志级别 /actuator/refresh重新加载bootstrap上下文以及刷新@RefreshScope标注的Bean/actuator/restart关闭应用上下文并重新启动(默认是关闭的)/actuator/pause和/actuator/resume调用应用上下文Lifecycle的stop()和start()方法
Spring Cloud Commons
@EnableDiscoverClient 启用服务发现
默认会自动注册到远程发现服务,不注册的需要调整
autoRegister属性值为false
只要实现DiscoveryClient或ReactiveDiscoveryClient并且在META-INF/spring.factories中在org.springframework.cloud.client.discovery.EnableDiscoveryClient下申明的类都可以服务发现。
健康检查
健康检查的配置类
DiscoveryClientHealthIndicatorProperties
spring.cloud.discovery.client.composite-indicator.enabled=false关闭所有的健康检查spring.cloud.discovery.client.health-indicator.enabled=false关闭服务发现的健康检查spring.cloud.discovery.client.health-indicator.includedescription= false关闭服务发现的健康检查描述
服务发现的配置
spring.cloud.discovery.blocking.enabled=false阻塞的服务发现关闭spring.cloud.discovery.reactive.enabled=falseReactive的服务发现关闭spring.cloud.discovery.enabled=false所有的服务发现都关闭
服务发现的优先级
spring.cloud.{clientIdentifier}.discovery.order通过此配置进行调整
ServiceRegistry 服务注册
org.springframework.cloud.client.serviceregistry.ServiceRegistry
void register(R registration)注册服务void deregister(R registration)反注册服务(撤销服务)
Registration是个标记接口
服务的自动注册
@EnableDiscoveryClient(autoRegister=false)通过注解的方式spring.cloud.service-registry.auto-registration.enabled=false通过配置文件的方式
服务自动注册事件
spring.cloud.service-registry.auto-registration.enabled=false事件会关闭
InstancePreRegisteredEvent服务注册前置事件InstanceRegisteredEvent服务注册后置事件
健康检查
/service-registry检查检查端点,GET获取所有的注册状态,POST改变服务的状态
Euraka服务状态stauts:UP、DOWN、OUT_OF_SERVICE和UNKOWN
RestTemplate 负载均衡服务调用客户端
通过添加LoadBalanced标识来表明负载均衡客户端
url必须是虚拟的主机名称,就是服务的名称,不是主机名称
建议使用
BlockingLoadBalancerClient,RibbonLoadBalancerClient正在维护不推荐使用两者都在的时候默认是使用
RibbonLoadBalancerClient,通过配置spring.cloud.loadbalancer.ribbon.enabled=false进行关闭
1 | |
WebClient 负载均衡服务调用客户端
通过添加LoadBalanced标识来表明负载均衡客户端
url必须是虚拟的主机名称,就是服务的名称,不是主机名称
1 | |
尝试失败请求
默认是关闭的。添加Spring Retry来启用,配置参数
client.ribbon.MaxAutoRetriesclient.ribbon.MaxAutoRetriesNextServerclient.ribbon.OkToRetryOnAllOperationsspring.cloud.loadbalancer.retry.enabled=false关闭重试请求
自定义实现BackOffPolicy
创建一个类型为LoadBalancedRetryFactory的Bean并重写createBackOffPolicy方法
1 | |
自定义实现RetryListener
创建一个类型为LoadBalancedRetryListenerFactory的Bean并且返回值是服务的RetryListener数组
1 | |
多个RestTemplate或者WebClient对象
普通的直接用,要是使用负载均衡的话,加上@LoadBalanced注解进行申明
1 | |
若是存在异常
java.lang.IllegalArgumentException: Can not set org.springframework.web.client.RestTemplate field com.my.app.Foo.restTemplate to com.sun.proxy.$Proxy89,尝试注入RestOperations或者调整配置spring.aop.proxyTargetClass=true