Spring Cloud 全家桶
2019-04-01 17:52:35 1 举报
AI智能生成
登录查看完整内容
Spring Cloud 笔记
作者其他创作
大纲/内容
Spring CloudGreenwich GA
Spring Cloud Commons
通用抽象,诸如服务发现,负载平衡和断路器之类的模式适用于所有Spring Cloud客户端可以独立于实现(例如通过Eureka或Consul发现)的消耗的共同抽象层
Spring Cloud Context: Application Context Services
The Bootstrap Application Context
Application Context Hierarchies
parent context
Bootstrap context
main context
main application context
Changing the Location of Bootstrap Properties
spring.cloud.bootstrap.name=bootstrapspring.cloud.bootstrap.location=(default empty)
Overriding the Values of Remote Properties
spring.cloud.config.allowOverride=true
(it does not work to set this locally)
spring.cloud.config.overrideNone=true (default false)
Any local property source should override the remote settings.
spring.cloud.config.overrideSystemProperties=false
Customizing the Bootstrap Configuration
CustomBootstrapConfiguration
Spring @Configuration class
注意不要加到 @ComponentScan 和 @SpringBootApplication 可以扫描的路径。否则会加到 main application context中,这通常是不需要的。
/META-INF/spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration==sample.custom.CustomBootstrapConfiguration
Customizing the Bootstrap Property Sources
PropertySourceLocator
CustomPropertySourceLocator
org.springframework.cloud.bootstrap.BootstrapConfiguration=sample.custom.CustomPropertySourceLocator
Logging Configuration
Environment Changes
Refresh Scope
@RefreshScope
spring.cloud.refresh.extra-refreshable
management.endpoints.web.exposure.include=refresh
Encryption and Decryption
Dependencies
org.springframework.security:spring-security-rsa
Install JCE
<JDK>/jre/lib/security
Oracle JDK 1.8-151 or higher version,无须额外安装
检查是否支持JCE
$JAVA_HOME/bin/jrunscript -e 'print (javax.crypto.Cipher.getMaxAllowedKeyLength(\"RC5\") >= 256);'
Endpoints
POST /actuator/env
/actuator/refresh
/actuator/restart
/actuator/pause
/actuator/resume
Spring Cloud Commons: Common Abstractions
DiscoveryClient
@EnableDiscoveryClient
META-INF/spring.factories
org.springframework.cloud.client.discovery.EnableDiscoveryClient
Health Indicator
DiscoveryHealthIndicator
DiscoveryClientHealthIndicator
spring.cloud.discovery.client.composite-indicator.enabled=false
禁用健康检查中的DiscoveryClient检查项
spring.cloud.discovery.client.health-indicator.enabled=falsespring.cloud.discovery.client.health-indicator.include-description=false
Ordering DiscoveryClient instances
ServiceRegistry
Interface
Registration
ServiceRegistry Auto-Registration
spring.cloud.service-registry.auto-registration.enabled=false
禁用自动注册
Events
InstancePreRegisteredEvent
InstanceRegisteredEvent
Service Registry Actuator Endpoint
/service-registry
GET
POST
Registration Status
Spring RestTemplate as a Load Balancer Client
Annotations
@Configuration + @LoadBalanced + @Bean + RestTemplate
PS
The Ribbon client is used to create a full physical address
Spring WebClient as a Load Balancer Client
@Configuration + @LoadBalanced + @Bean + WebClient.Builder
Retrying Failed Requests
Spring Retry
Properties
spring.cloud.loadbalancer.retry.enabled=false
Disable the retry logic with Spring Retry on the classpath
<clientName>.ribbon.MaxAutoRetries=<clientName>.ribbon.MaxAutoRetriesNextServer=<clientName>.ribbon.OkToRetryOnAllOperations=
BackOffPolicy
@Configuration + @Bean + LoadBalancedRetryFactory
RetryListener
@Configuration + @Bean + LoadBalancedRetryListenerFactory
Multiple RestTemplate objects
@Configuration
@Bean + @Primary + RestTemplate
@Bean + @LoadBalanced + RestTemplate
Spring WebFlux WebClient as a Load Balancer Client
LoadBalancerExchangeFilterFunction
Ignore Network Interfaces
When running in Docker container
Force the use of only specified network addresses
Force the use of only site-local addresses
HTTP Client Factories
Http Client
ApacheHttpClientFactory
OkHttpClientFactory
Be created only if the OK HTTP jar is on the classpath
Http Connection Manager
ApacheHttpClientConnectionManagerFactory
OkHttpClientConnectionPoolFactory
spring.cloud.httpclientfactories.apache.enabled=false
Disable Apache Http Client
spring.cloud.httpclientfactories.ok.enabled=false
Disable OK Http Client with OK HTTP jar on the classpath
Enabled Features
/features endpoint
Feature types
abstract feature
named feature
Declaring features
HasFeature
HasFeatures.abstractFeatures(DiscoveryClient.class)
HasFeatures.namedFeatures(new NamedFeature(\"Spring Cloud Bus\
HasFeatures.builder()...build()
Spring Cloud Compatibility Verification
Security
Spring Cloud Security
Config
Spring Cloud Config
配置中心,配置管理工具包,让你可以把配置放到远程服务器,集中化管理集群配置,目前支持本地存储、Git以及Subversion
References
spring-cloud-config
Http Resources
/{application}/{profile}[/{label}] (默认)/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties
Config Server
@EnableConfigServer
Git Backend
spring.cloud.config.server.git.*
git.uri=https://github.com/spring-cloud-samples/config-repogit.skipSslValidation=falsegit.timeout=4git.searchPaths=cloneOnStart=true
Pattern Matching and Multiple Repositories
git.repos.*
Authentication
git.uri=https://github.com/spring-cloud-samples/config-repogit.username=git.password=
Git SSH
file-based SSH config
store keys
default directories
~/.ssh
~/.ssh/known_hosts
ssh-rsa format
URI points to an SSH location
git@github.com:configuration/cloud-configuration
properties-based SSH config
git.uri=git@gitserver.com:team/repo1.git git.ignoreLocalSshSettings=true git.hostKey=someHostKey git.hostKeyAlgorithm=ssh-rsa git.privateKey=
HTTPS proxy settings
~/.git/config
or system properties (-Dhttps.proxyHost and -Dhttps.proxyPort)
Placeholders in Git URI
{application} and {profile} and {label}
git.uri=https://github.com/myorg/{application}
Force pull in Git Repositories
git.force-pull=false(default)
Deleting untracked branches in Git Repositories
git.deleteUntrackedBranches=false(default)
Default it will keep all branch forever or till the next server restart (which creates new local repo)
Git Refresh Rate
spring.cloud.config.server.git.refreshRate=0(default 0 second)
config server will fetch updated configuration from the Git repo every time it is requested
spring.cloud.config.server.accept-empty=true
File System Backend
spring.cloud.config.server.native.searchLocations
Vault Backend
spring.cloud.config.server.vault.*
Accessing Backends Through a Proxy
spring.cloud.config.server.git.proxy.http*
spring.cloud.config.server.git.proxy.https.*
Sharing Configuration With All Applications
application.propertiesapplication.ymlapplication-*.properties
Vault Server
secret/application
JDBC Backend
CredHub Backend
Property Overrides
spring.cloud.config.server.overrides.*
Extra map for a property source to be sent to all clients unconditionally.
The Config Server has an “overrides” feature that lets the operator provide configuration properties to all applications
remote properties
spring.cloud.config.allow-override=true
Flag to indicate that {@link #isOverrideSystemProperties() systemPropertiesOverride} can be used. Set to false to prevent users from changing the default accidentally.
spring.cloud.config.overrideNone=false (default)
Override from any local property source.
spring.cloud.config.override-system-properties=true (default)
Flag to indicate that the external properties should override system properties.
spring.cloud.config.server.health.enabled=false
spring.cloud.config.server.health.repositories.*
HTTP Basic security
Dependecies
org.springframework.boot:spring-boot-starter-security
spring.security.user.name=userspring.security.user.password=
Java Cryptography Extension (JCE)
Prefix
{cipher}*
xxx.password='{cipher}735ce8bb1672067fca3530fa7b1237fdc123305b1f4342aea15e8ec20d0ac3f7'
Install JCE into <JDK>/jre/lib/security
/encrypt/status
/encrypt
/decrypt
Encrypt/Decrypt
Use /encrypt endpoint
curl localhost:8888/encrypt -d mysecret
curl localhost:8888/decrypt -d 682bc583...
Use spring command line client
Dependency
Spring Cloud CLI extensions installed
spring encrypt mysecret --key foo
spring decrypt --key foo 682bc583f...
encrypt.*
spring.cloud.config.server.encrypt.enabled=false
常见错误
/encrypt/status {\"description\":\"The encryption algorithm is not strong enough\
在 bootstrap.yml 中添加 encrypt.key
/encrypt{\"timestamp\":\"2019-04-02T10:26:57.351+0000\
禁用CSRF
Key Management
symmetric (shared) key
encrypt.key
asymmetric one (RSA key pair)
encrypt.keyStore.*
location=Contains a Resource locationpassword=Holds the password that unlocks the keystorealias=Identifies which key in the store to usesecret=
Creating a Key Store for Testing
Using Multiple Keys and Key Rotation
Serving Alternative Formats
Serving Plain Text
/{name}/{profile}/{label}/{path}
Embedding the Config Server
spring.cloud.config.server.bootstrap=false(default)
Push Notifications and Spring Cloud Bus
webhook
spring-cloud-config-monitor
Spring Cloud Bus
/monitor
Config Client
dependencyManagement
org.springframework.boot:spring-boot-starter-parent:pom:import
org.springframework.cloud:spring-cloud-dependencies:pom:import
dependencies
org.springframework.cloud:spring-cloud-starter-config
spring.application.name: myapp
spring.cloud.config.uri=http://myconfigserver.comspring.cloud.config.label=master
spring.config.name=spring.config.location=
Config First Bootstrap
bootstrap.ymlor an environment variable
spring.cloud.config.uri=http://localhost:8888
Discovery First Bootstrap
eureka.client.serviceUrl.defaultZone=
eureka.instance.metadataMap.*
user=password=
Config server is secured with HTTP Basic
configPath=/config
if the Config Server has a context path
spring.cloud.config.discovery.enabled=true (default false)spring.cloud.config.discovery.serviceId=configserver(default)
Config Client Fail Fast
spring.cloud.config.fail-fast=true
Config Client Retry
org.springframework.retry:spring-retry
org.springframework.boot:spring-boot-starter-aop
spring.cloud.config.fail-fast=false
spring.cloud.config.retry.*
initial-interval=1000max-attempts=6max-interval=2000multiplier=1.1
The default behavior is to retry six times with an initial backoff interval of 1000ms and an exponential multiplier of 1.1 for subsequent backoffs
Locating Remote Configuration Resources
/{name}/{profile}/{label}
spring.cloud.config.*
name=${spring.application.name}profile=${spring.profiles.active}label=\"master\"
Label can also be provided as a comma-separated list
Specifying Multiple Urls for the Config Server
Config-First Bootstrap mode
Specify multiple URLs
As a comma-separated list under the spring.cloud.config.uri property
Support per-Config Server auth credentials in each URL
Discovery-First Bootstrap mode
All config server instances register in a Service Registry like Eureka
Not (currently) support per-Config Server authentication and authorization
Configuring Read Timeouts
spring.cloud.config.request-read-timeout=0 (default )
spring.cloud.config.username
spring.cloud.config.password
Cloud Foundry
spring.cloud.config.uri=${vcap.services.configserver.credentials.uri:http://user:password@localhost:8888}
health.config.enabled=false (default)
health.config.time-to-live=300000 (default)
Providing A Custom RestTemplate
Configuration Beans
ConfigServicePropertySourceLocator
resources/META-INF/spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration = com.my.config.client.CustomConfigServiceBootstrapConfiguration
Vault
spring.cloud.config.token=
bootstram.yml
Nested Keys In Vault
Spring Cloud Vault
Discovery
Spring Cloud Consul
Spring Cloud Zookeeper
一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
Routing
Spring Cloud OpenFeign
org.springframework.cloud:spring-cloud-starter-openfeign
@EnableFeignClients
@FeignClient
@FeignClient(name = \"service-name\
name
required
serviceId
serviceId attribute is now deprecated in favor of the name attribute
contextId
changing the name of the ApplicationContext
url
fallback
熔断或调用失败的回调方法实现类
fallbackFactory
熔断或调用失败的回调方法实现类的工厂累
回调方法支持异常信息参数
@RequestMapping
Configuration beans
BeanType beanName: ClassName
client.config
Default Property Format
feign.client.config.default.<property name>=
Instance Property Format
feign.client.config.<feignName>.<property name>=
Timeouts
connectTimeout: 5000readTimeout: 5000
logger
loggerLevel: full
others
errorDecoder: com.example.SimpleErrorDecoder requestInterceptors: - com.example.FooRequestInterceptor - com.example.BarRequestInterceptor decode404: false encoder: com.example.SimpleEncoder decoder: com.example.SimpleDecoder contract: com.example.SimpleContract
client
feign.client.default-to-properties=true
Default: configuration properties has high priority then @Configuration bean
feign.okhttp.enabledfeign.httpclient.enabled
hystrix
feign.hystrix.enabled=false
Feign Hystrix Support
org.springframework.cloud:spring-cloud-starter-netflix-hystrix
feign.hystrix.enabled=true
Feign Hystrix Fallbacks
@FeignClient set the fallback attribute to the class name that implements the fallback
You also need to declare your implementation as a Spring bean
@FeignClient(fallback)
@FeignClient(fallbackFactory)
Feign and @Primary
Feign Inheritance Support
Feign request/response compression
feign.compression.response.enabled=true
Feign logging
1. Set feign logger level
2. Set spring boot logging level
Feign @QueryMap support
@SpringQueryMap
GET method request paremeter
Spring Cloud Gateway
org.springframework.cloud:spring-cloud-starter-gateway
spring.cloud.gateway.enabled=true
Route Predicate Factories
GatewayFilter Factories
Global Filters
TLS / SSL
Configuration
Reactor Netty Access Logs
CORS Configuration
Actuator API
Developer Guide
Building a Simple Gateway Using Spring MVC or Webflux
Netflix
Spring Cloud Netflix
Service Discovery: Eureka Clients
Netflix/eureka
org.springframework.cloud:spring-cloud-starter-netflix-eureka-client
eureka.instance.*(it registers itself)
eureka.instance.actuatorPrefix=\"/actuator\"
eureka.instance.appname
eureka.instance.appGroupName
eureka.instance.nonSecurePort=80
eureka.instance.securePort=443
eureka.instance.nonSecurePortEnabled=true
eureka.instance.securePortEnabled
eureka.instance.leaseRenewalIntervalInSeconds=30
每30秒会向Eureka Server发起Renew(续约)操作
eureka.instance.leaseExpirationDurationInSeconds=90
最后一次心跳时间后90秒(默认)就认为是下线了
eureka.instance.virtualHostName=\"unknown\"
eureka.instance.instanceId
${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}
eureka.instance.secureVirtualHostName=\"unknown\"
eureka.instance.ipAddress
eureka.instance.statusPageUrlPath=${eureka.instance.actuatorPrefix}/info
eureka.instance.statusPageUrl=http://${eureka.hostname}:7001/${eureka.instance.statusPageUrlPath}
eureka.instance.homePageUrlPath=\"/\"
eureka.instance.homePageUrl=
eureka.instance.healthCheckUrlPath=${eureka.instance.actuatorPrefix}/health
eureka.instance.healthCheckUrl=
eureka.instance.hostName
eureka.instance.preferIpAddress=false
eureka.instance.environment
eureka.instance.metadataMap
eureka.instance.metadataMap.zone
eureka.instance.metadataMap.cluster
eureka.instance.metadataMap.management.port=${management.server.port}
eureka.client.*(it can query the registry to locate other services)
eureka.client.enabled=true
eureka.client.registryFetchIntervalSeconds=30
eureka.client.instanceInfoReplicationIntervalSeconds=30
eureka.client.initialInstanceInfoReplicationIntervalSeconds=40
eureka.client.eurekaServiceUrlPollIntervalSeconds=300
eureka.client.eurekaServerReadTimeoutSeconds=8
eureka.client.eurekaServerConnectTimeoutSeconds=5
eureka.client.eurekaServerTotalConnections=200
eureka.client.eurekaServerTotalConnectionsPerHost=50
eureka.client.region=\"us-east-1\"
eureka.client.availabilityZones
eureka.client.eurekaConnectionIdleTimeoutSeconds=30
eureka.client.heartbeatExecutorThreadPoolSize=2
eureka.client.serviceUrl.defaultZone
http://user:password@localhost:8761/eureka
eureka.client.registerWithEureka=true
eureka.client.preferSameZoneEureka=true
eureka.client.fetchRegistry=true
eureka.client.healthcheck.enable=true
EurekaClient without Jersey
Why Is It so Slow to Register a Service
Instance registers itself into server
cost 1 heartbeat
Server refresh instance cache
Client fetch instance info
Zones
Service 1 in Zone 1
eureka.instance.metadataMap.zone = zone1eureka.client.preferSameZoneEureka = true
Service 1 in Zone 2
eureka.instance.metadataMap.zone = zone2eureka.client.preferSameZoneEureka = true
Service Discovery: Eureka Server
服务中心,云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移。
org.springframework.cloud:spring-cloud-starter-netflix-eureka-server
@EnableEurekaServer
Standalone Mode
eureka.instance.hostname=localhosteureka.client.registerWithEureka=falseeureka.client.fetchRegistry=falseeureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
Peer Awareness
peer1
eureka.instance.hostname=peer1eureka.client.serviceUrl.defaultZone=http://peer2/eureka/
peer2
eureka.instance.hostname=peer2eureka.client.serviceUrl.defaultZone=http://peer1/eureka/
/etc/hosts
xxx.xxx.xxx.xxx peer1xxx.xxx.xxx.xxx peer2
When to Prefer IP Address
eureka.instance.preferIpAddress=true
eureka.instance.hostname=${HOST_NAME}
Securing The Eureka Server
CSRF
By default when Spring Security is on the classpath it will require that a valid CSRF token be sent with every request to the app
Eureka clients will not generally possess a valid cross site request forgery (CSRF) token you will need to disable this requirement for the /eureka/** endpoints.
@EnableWebSecurityclass WebSecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().ignoringAntMatchers(\"/eureka/**\"); super.configure(http); }}
JDK 11 Support
JAXB modules
javax.xml.bind:jaxb-api:2.3.0
com.sun.xml.bind:jaxb-core:2.3.0
com.sun.xml.bind:jaxb-impl:2.3.0
unavailable-replicas
确保以下两个参数保持一致(hostname必须相同)eureka.instance.hostname=eureka.client.service-url.defaultZone=
eureka.instance.prefer-ip-address=true的情况下,eureka.client.service-url.defaultZone必须使用IP
Circuit Breaker: Hystrix Clients
熔断器,容错管理工具,旨在通过熔断机制控制服务和第三方库的节点,阻止级联失败,从而对延迟和故障提供更强大的容错能力
Netflix/Hystrix
How it Works
Hystrix Configuration
Circuit breaker pattern
@EnableCircuitBreaker
@HystrixCommand
@HystrixProperty
The state of the connected circuit breakers are exposed in the /health endpoint
Hystrix Metrics Stream
org.springframework.boot:spring-boot-starter-actuator
management.endpoints.web.exposure.include=hystrix.streamor management.endpoints.web.exposure.include=*
/actuator/hystrix.stream
Default Property Prefix
\thystrix.command.default.*
Instance Property Prefix
\thystrix.command.<HystrixCommandKey>.*
Execution
execution.timeout.enabled=trueexecution.isolation.strategy=THREAD (执行隔离策略)execution.isolation.thread.timeoutInMilliseconds=1000execution.isolation.thread.interruptOnTimeout=trueexecution.isolation.thread.interruptOnCancel=falseexecution.isolation.semaphore.maxConcurrentRequests=10
Fallback
fallback.isolation.semaphore.maxConcurrentRequests=10fallback.enabled=true
Circuit Breaker
circuitBreaker.enabled=true (断路器开关)circuitBreaker.requestVolumeThreshold=20 (断路器请求阈值)circuitBreaker.errorThresholdPercentage=50(断路器错误请求百分比)circuitBreaker.sleepWindowInMilliseconds=5000(断路器休眠时间)circuitBreaker.forceOpen=false(断路器强制开启)circuitBreaker.forceClosed=false(断路器强制关闭)
Metrics
metrics.rollingStats.timeInMilliseconds=10000metrics.rollingStats.numBuckets=10
metrics.rollingPercentile.enabled=truemetrics.rollingPercentile.timeInMilliseconds=60000metrics.rollingPercentile.numBuckets=6metrics.rollingPercentile.bucketSize=100
metrics.healthSnapshot.intervalInMilliseconds=500
Request Context
requestCache.enabled=truerequestLog.enabled=true
Collapser Properties
maxRequestsInBatch=Integer.MAX_VALUEtimerDelayInMilliseconds=10
Thread Pool Properties
coreSize=10(线程池大小)maximumSize=10maxQueueSize=−1(最大队列数量)queueSizeRejectionThreshold=5 (队列大小拒绝阈值)keepAliveTimeMinutes=1allowMaximumSizeToDivergeFromCoreSize=false
Circuit Breaker: Hystrix Dashboard
Circuit Breaker: Turbine
Hystrix Timeouts And Ribbon Clients
How to Include the Hystrix Dashboard
org.springframework.cloud:spring-cloud-starter-netflix-hystrix-dashboard
@EnableHystrixDashboard
HomePageUrlPath
/hystrix
TurbinePull 模式
Netflix/Turbine
Configuration (1.x)
org.springframework.cloud:spring-cloud-starter-netflix-turbine
@EnableTurbine
/turbine.stream?cluster=CLUSTERNAME
/clusters
list clusters have been configured in Turbine
turbine.instanceInsertPort
upper-case
The url cluster parameter must match an entry in turbine.aggregator.clusterConfig
a list of Eureka serviceIds that turbine uses to lookup instances
turbine.clusterNameExpression=metadata['cluster']
SPEL expression
metadata['cluster']
\"'default'\"
turbine.combineHostPort=true
turbine.endpoints.clusters.enabled=true
Turbine StreamPush 模式
spring-cloud-netflix-hystrix-stream
spring-cloud-starter-stream-*
@EnableTurbineStream
server
spring-cloud-starter-netflix-turbine-stream
don't need properties
turbine.appConfig
turbine.clusterNameExpression
turbine.aggregator.clusterConfig
Client Side Load Balancer: Ribbon
Netflix/ribbon
CommonClientConfigKey
Constant Field Values
org.springframework.cloud:spring-cloud-starter-netflix-ribbon
Feign already uses Ribbon
@RibbonClient(configuration)
Without Feign
@RibbonClients(defaultConfiguration)
Default for All Ribbon Clients
Netflix APIs native Properties
<clientName>.ribbon.*
Customizing the Ribbon Client
Working with load balancers
Customizing the Default for All Ribbon Clients
Customizing the Ribbon Client by Setting Properties
<clientName>.ribbon.NFLoadBalancerClassName: Should implement ILoadBalancer<clientName>.ribbon.NFLoadBalancerRuleClassName: Should implement IRule<clientName>.ribbon.NFLoadBalancerPingClassName: Should implement IPing<clientName>.ribbon.NIWSServerListClassName: Should implement ServerList<clientName>.ribbon.NIWSServerListFilterClassName: Should implement ServerListFilter
Using Ribbon with Eureka
Eureka override configuration beans
DefaultNIWSServerListFilter DiscoveryEnabledNIWSServerList DiscoveryEnabledServer NIWSDiscoveryPing
Example: How to Use Ribbon Without Eureka
@RibbonClient(name = \"<clientName>\")
Example: Disable Eureka Use in Ribbon
ribbon.eureka.enabled=false
Using the Ribbon API Directly
Beans
LoadBalancerClient
Caching of Ribbon Configuration
How to Configure Hystrix Thread Pools
zuul.ribbonIsolationStrategy=THREAD
SEMAPHORE(default)
THREAD
zuul.threadpool.useSeparateThreadPools=truezuul.threadpool.threadPoolKeyPrefix=zuulgw
用网关zuul时,熔断hytrix里面的坑
How to Provide a Key to Ribbon’s IRule
ribbon.<propertyName>=<value>
<clientName>.ribbon.<propertyName>=<value>
Timeout
ConnectTimeout=2000ReadTimeout=5000
Connections
MaxTotalHttpConnections=200MaxConnectionsPerHost=50
Retry
MaxAutoRetries=0MaxAutoRetriesNextServer=1OkToRetryOnAllOperations=retryableStatusCodes=
zone
EnableZoneAffinity=falseEnableZoneExclusivity=false
With Eureka
NFLoadBalancerClassName: Should implement ILoadBalancerNFLoadBalancerRuleClassName: Should implement IRuleNFLoadBalancerPingClassName: Should implement IPingNIWSServerListClassName: Should implement ServerListNIWSServerListFilterClassName: Should implement ServerListFilter
ServerListRefreshInterval=30000DeploymentContextBasedVipAddresses=
Without Eureka
External Configuration: Archaius
配置管理API,包含一系列配置管理API,提供动态类型化属性、线程安全配置操作、轮询框架、回调机制等功能。
Netflix client-side configuration library
Spring applications should generally not use Archaius directly
Archaius is an extension of the Apache Commons Configuration Project
Router and Filter: Zuul
Netflix/zuul
org.springframework.cloud:spring-cloud-starter-netflix-zuul
@EnableZuulProxy
zuul.<property name>=
zuul.routes.<routename>.<property name>=
zuul.prefix=zuul.stripPrefix=truezuul.addProxyHeaders=true
zuul.host.maxTotalConnections=200zuul.host.maxPerRouteConnections=20
If configured Zuul routes by specifying URLs
zuul.ribbonIsolationStrategy=SEMAPHORE
zuul.ignored-services='*'
routes
zuul.routes.<routename>=/<service id>/**
or example \"/<service id>/101\" is forwarded to \"/101\"
zuul.routes.<routename>.path=/<service id>/**zuul.routes.<routename>.serviceId=<service id>zuul.routes.<routename>.stripPrefix=true
zuul.routes.<routename>.path=/<service id>/**zuul.routes.<routename>.url:=http://example.com/users_service
retry
zuul.retryable=zuul.routes.<routename>.retryable=
Default is Apache HTTP Client
Use Ribbon RestClient
ribbon.restclient.enabled=true
Use okhttp3.OkHttpClient
ribbon.okhttp.enabled=true
Cookies and Sensitive Headers
this is a blacklist
Ignored Headers
zuul.ignoredHeaders=
zuul.ignoreSecurityHeaders=
Query String Encoding
zuul.forceOriginalQueryStringEncoding=
Request URI Encoding
zuul.decodeUrl=
Debug Filter
开启DebugRouting和DebugRequest
zuul.debug.parameter=debug
http://ZUUL_HOST:ZUUL_PORT/path?debug=true
zuul.debug.request=false
Management Endpoints
/routes
/routes/details
/filters
Strangulation Patterns and Local Forwards
Plain Embedded Zuul
@EnableZuulServer
Disable Zuul Filters
Providing Hystrix Fallbacks For Routes
Zuul Timeouts
If use service discovery
ribbon.ReadTimeout=ribbon.SocketTimeout=
If configured Zuul routes by specifying URLs
zuul.host.connect-timeout-millis=zuul.host.socket-timeout-millis=
Rewriting the Location header
Enabling Cross Origin Requests
WebMvcConfigurer Bean
zuul 核心过滤器
pre 过滤器
-3
ServletDetectionFilter
标记处理Servlet的类型
-2
Servlet30WrapperFilter
包装HttpServletRequest请求
-1
FormBodyWrapperFilter
包装请求体
1
DebugFilter
标记调试标志
5
PreDecorationFilter
处理请求上下文供后续使用
route 过滤器
10
RibbonRoutingFilter
serviceId请求转发
100
SimpleHostRoutingFilter
url请求转发
500
SendForwardFilter
forward请求转发
post 过滤器
0
SendErrorFilter
处理有错误的请求响应
1000
SendResponseFilter
处理正常的请求响应
常见问题
如何实现IP白名单
Polyglot support with Sidecar
org.springframework.cloud:spring-cloud-netflix-sidecar
@EnableSidecar
sidecar.port
The sidecar.port property is the port on which the non-JVM application listens
sidecar.health-uri
BackOff Policies
<clientName>.ribbon.MaxAutoRetries
<clientName>.ribbon.MaxAutoRetriesNextServer
<clientName>.ribbon.OkToRetryOnAllOperations
<clientName>.ribbon.retryableStatusCodes
Zuul
zuul.retryable
zuul.routes.<routename>.retryable
HTTP Clients
Configuration Bean
ClosableHttpClient
For Apache Http Cient
OkHttpClient
For OK HTTP
Modules In Maintenance Mode维护模式,不再添加新特性,只维护BUG
spring-cloud-netflix-archaiusspring-cloud-netflix-hystrix-contractspring-cloud-netflix-hystrix-dashboardspring-cloud-netflix-hystrix-streamspring-cloud-netflix-hystrixspring-cloud-netflix-ribbonspring-cloud-netflix-turbine-streamspring-cloud-netflix-turbinespring-cloud-netflix-zuul
Tracing
Spring Cloud Sleuth
日志收集工具包,封装了Dapper和log-based追踪以及Zipkin和HTrace操作,为SpringCloud应用实现了一种分布式追踪解决方案。
Introduction
Terminology
Span
id
descriptions
timestamped events
key-value annotations (tags)
process IDs (normally IP addresses)
Trace
Annotation
cs: Client Sentsr: Server Receivedss: Server Sentcr: Client Received
Purpose
Distributed Tracing with Zipkin
Visualizing errors
Distributed Tracing with Brave
spring.sleuth.http.legacy.enabled=false(default)
Log correlation
JSON Logback with Logstash
Logback->kafka->Logback->ElasticSearch->Kibana
Propagating Span Context
spring.sleuth.baggage-keys=
spring.sleuth.propagation-keys=
Adding Sleuth to the Project
Only Sleuth (log correlation)
org.springframework.cloud:spring-cloud-starter-sleuth
Sleuth with Zipkin via HTTP
org.springframework.cloud:spring-cloud-starter-zipkin
Sleuth with Zipkin over RabbitMQ or Kafka
Kafka
org.springframework.kafka:spring-kafka
properties
spring.zipkin.sender.type: kafka
Kafka Topic
zipkin
RabbitMQ
org.springframework.amqp:spring-rabbit
Overriding the auto-configuration of Zipkin
Reporter<Span>
ZipkinAutoConfiguration.REPORTER_BEAN_NAME
Sender
ZipkinAutoConfiguration.SENDER_BEAN_NAME
Additional Resources
Features
Sampling
Propagation
Current Tracing Component
Current Span
Instrumentation
Span lifecycle
Naming spans
Managing Spans with Annotations
Customizations
Sending Spans to Zipkin
Zipkin Stream Span Consumer
Integrations
Running examples
Messaging
事件、消息总线,用于在集群(例如,配置变化事件)中传播状态变化,可与Spring Cloud Config联合实现热部署
Quick Start
spring-cloud-starter-bus-amqp
spring-cloud-starter-bus-kafka
/bus/env
/bus/refresh
/refresh
Bus Endpoints
/actuator/bus-refresh
clears the @RefreshScope cache and rebinds @ConfigurationProperties
management.endpoints.web.exposure.include=bus-refresh
/actuator/bus-env
updates each instances environment with the specified key/value pair across multiple instances
management.endpoints.web.exposure.include=bus-env
/actuator/env
Addressing an Instance
spring.cloud.bus.id
default value
app:index:id
PS: 'vcap' is short for VMware's Cloud Application Platform
Addressing All Instances of a Service
Service ID Must Be Unique
Customizing the Message Broker
Tracing Bus Events
Broadcasting Your Own Events
Spring Cloud Stream
创建消息驱动微服务应用的框架
Overview
Main Concepts
Application model
The Binder Abstraction
spring-cloud-stream-binder-kafka
KafkaBinderConfigurationProperties
spring.cloud.stream.kafka.binder.*
brokers=localhost
defaultBrokerPort=9092
configuration={}
consumerProperties={}
headers=empty
healthTimeout=10
seconds
autoCreateTopics=true
enableDlq=false
dlqName=error.<destination>.<group>
KafkaConsumerProperties
spring.cloud.stream.kafka.bindings.<channelName>.consumer.*
KafkaProducerProperties
spring.cloud.stream.kafka.bindings.<channelName>.producer.*
spring-cloud-stream-binder-rabbit
TestSupportBinder
Persistent publish-subscribe support
Consumer Groups
spring.cloud.stream.bindings.<channelName>.group=
Consumer Types
Message-driven (sometimes referred to as Asynchronous)
Polled (sometimes referred to as Synchronous)
Partitioning support
分区是有状态处理中的关键概念,对于确保所有相关数据都一起处理,分区是至关重要的(出于性能或一致性方面的考虑)。例如,在带时间窗的平均计算示例中,重要的是,来自任何给定传感器的所有测量都应由同一应用实例处理。
Partitioning
Programming Model
Destination Binders
基于MessageChanel的绑定器,负责与外部消息传递系统集成的组件。
Destination Bindings
外部消息系统和应用程序之间提供桥梁生产者和消费者的消息(由目标绑定器创建)。
Default Bingding Interface
Sink
Message Consummer
SubscribableChannel
Source
Message Producer
Message Chanel
Processor
Both Message Consummer and Producer
Pollable Destination Binding
Customizing Channel Names
Producing and Consuming Messages
MessageBuilder.withPayload(name).build()
Spring Integration Support
@InboundChannelAdapter
@Transformer或@ServiceActivator
订阅模式
pub-sub model
Spring Cloud Stream annotation
@StreamListener
有分组概念,
competing model
Spring Integration annotation
无分组概念
Using @StreamListener Annotation
@SendTo
example
@EnableBinding(Sink.class)public class VoteHandler { @Autowired VotingService votingService; @StreamListener(Sink.INPUT) public void handle(Vote vote) { votingService.record(vote); }}
@EnableBinding(Processor.class)public class TransformProcessor { @Autowired VotingService votingService; @StreamListener(Processor.INPUT) @SendTo(Processor.OUTPUT) public VoteResult handle(Vote vote) { return votingService.record(vote); }}
Using @StreamListener for Content-based routing基于内容路由
Header Condition
目前,@StreamListener仅支持基于通道的binder(不支持反应式编程)。
Spring Cloud Function support
java.util.function.[Supplier/Function/Consumer]
spring.cloud.stream.function.definition
编程模式
imperative functions命令式函数
reactive functions响应式函数
Reactive Functions support
Functional Composition
支持管道符连接多个function
spring.cloud.stream.function.definition=toUpperCase|wrapInQuotes
Using Polled Consumers
Error Handling
Application Error Handling
在每个绑定订阅中处理错误<destination>+<group>
spring.cloud.stream.bindings.input.group=myGroup
@ServiceActivator(inputChannel = Processor.INPUT + \".myGroup.errors\") //channel name 'input.myGroup.errors'public void error(Message message) {\tSystem.out.println(\"Handling ERROR: \" + message);}
<channelName>.<destination>.errors
全局处理程序,处理所有绑定订阅错误
@StreamListener(\"errorChannel\")public void error(Message message) {\tSystem.out.println(\"Handling ERROR: \" + message);}
System Error Handling
Drop Failed Messages
default
DLQ - Dead Letter Queue
Re-queue Failed Messages
Retry Template
@StreamRetryTemplatepublic RetryTemplate myRetryTemplate() { return new RetryTemplate();}
Binders
Producers and Consumers
生产者
生产者是将消息发送到通道的任何组件
绑定器
消费者
使用者是从通道接收消息的任何组件
Binder SPI
Binder Detection
Classpath Detection
Multiple Binders on the Classpath
META-INF/spring.binders
Connecting to Multiple Systems
Binding visualization and control
spring-boot-starter-web
spring-boot-starter-actuator
spring-boot-starter-webflux
如果使用了响应式编程
Binder Configuration Properties
Config class
BinderProperties
prefix
spring.cloud.stream.binders.
type
inheritEnvironment
environment
defaultCandidate
Configuration Options
Binding Service Properties
org.springframework.cloud.stream.config.BindingServiceProperties
format
spring.cloud.stream.<property>=<value>
instanceCount
instanceIndex
dynamicDestinations
defaultBinder
overrideCloudConnectors
bindingRetryInterval
Binding Properties
spring.cloud.stream.bindings.<channelName>.<property>=<value>
spring.cloud.stream.bindings.default.<producer|consumer>.<property>=<value>
For all channels
spring.cloud.stream.<binder-type>.default.<producer|consumer>.<property>=<value>
For extended binding properties
Common Binding Properties
class
org.springframework.cloud.stream.config.BindingProperties
spring.cloud.stream.bindings.default.<property>=<value>
destination
kafka: topic
rabbit: exchange
group
contentType=application/json
binder
Consumer Properties
org.springframework.cloud.stream.binder.ConsumerProperties
spring.cloud.stream.bindings.<channelName>.consumer.<property>=<value>
spring.cloud.stream.bindings.default.consumer.<property>=<value>
autoStartup=true
concurrency=1
partitioned=false
headerMode
maxAttempts=3
backOffInitialInterval=1_000
backOffMaxInterval=10_000
backOffMultiplier=2.0
defaultRetryable=true
instanceIndex=-1
instanceCount=-1
retryableExceptions
useNativeDecoding=false
Advanced Consumer Configuration
ListenerContainerCustomizer
MessageSourceCustomizer
Producer Properties
org.springframework.cloud.stream.binder.ProducerProperties
spring.cloud.stream.bindings.<channelName>.producer.<property>=<value>
spring.cloud.stream.bindings.default.producer.<property>=<value>
partitionKeyExpression=null
partitionKeyExtractorClass=null
partitionSelectorClass=null
partitionSelectorExpression=null
partitionCount=1
kafka使用topic自身的分区设置
requiredGroups
Depends on the binder implementation
useNativeEncoding=false
errorChannelEnabled=false
Using Dynamically Bound Destinations
根据消息内容,动态路由到不同 Destination
BinderAwareChannelResolver
Content Type Negotiation
Mechanics
define contentType (in order of precedence)
HEADER
header-provided content type
BINDING
per-binding content type
spring.cloud.stream.bindings.input.content-type
DEFAULT
default content type
application/json
Content Type versus Argument Type
HandlerMethodArgumentResolvers
MessageConverter
Message Converters
fromMessage
toMessage
Provided MessageConverters
ApplicationJsonMessageMarshallingConverter
ByteArrayMessageConverter
application/octet-stream
ObjectStringMessageConverter
text/plain
JsonUnmarshallingConverter
application/x-java-object;type=foo.bar.Cat
User-defined Message Converters
@Bean + @StreamMessageConverter
AbstractMessageConverter
Schema Evolution Support
Inter-Application Communication
Connecting Multiple Application Instances
Instance Index and Instance Count
spring.cloud.stream.instanceCount
spring.cloud.stream.instanceIndex
Configuring Output Bindings for Partitioning
spring.cloud.stream.bindings.<output>.producer.<property>
partitionKeyExpression
partitionKeyExtractorName
org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy
partitionCount
partitionSelectorName
org.springframework.cloud.stream.binder.PartitionSelectorStrategy
Configuring Input Bindings for Partitioning
spring.cloud.stream.bindings.input.consumer.partitioned=true
spring.cloud.stream.instanceIndex=3
Kafka not required
spring.cloud.stream.instanceCount=5
Testing
Metrics Emitter
Binder Implementations
spring.cloud.stream.bindings.applicationMetrics.destination
spring.cloud.stream.metrics.key
${spring.application.name:${vcap.application.name:${spring.config.name:application}}}
spring.cloud.stream.metrics.properties=null
spring.cloud.stream.metrics.meter-filter
spring.cloud.stream.metrics.schedule-interval=1 min
Rabbit MQ Binder
Apache Kafka Binder\t
Kafka Binder Properties
spring.cloud.stream.kafka.binder.<property>=<value>
headers
second
requiredAcks=1
minPartitionCount=1
producerProperties={}
replicationFactor=1
autoAddPartitions=false
transaction.transactionIdPrefix=null
transaction.producer.*
headerMapperBeanName
Kafka Consumer Properties
spring.cloud.stream.kafka.bindings.<channelName>.consumer.<property>=<value>
for a chenel
spring.cloud.stream.kafka.default.consumer.<property>=<value>
for a binder-type channels
spring.cloud.stream.default.consumer.<property>=<value>
for all channels
autoRebalanceEnabled=true
ackEachRecord=false
autoCommitOffset=true
autoCommitOnError
resetOffsets=false
startOffset=earliest
configuration=
dlqName
dlqProducerProperties
standardHeaders
converterBeanName
idleEventInterval=30_000
destinationIsPattern=false
topic.properties
topic.replicas-assignment
topic.replication-factor
Kafka Producer Properties
spring.cloud.stream.kafka.bindings.<channelName>.producer.<property>=<value>
spring.cloud.stream.kafka.default.producer.<property>=<value>
spring.cloud.stream.default.producer.<property>=<value>
bufferSize=16384
sync=false
batchTimeout=0
messageKeyExpression=none
headers['myKey']
headerPatterns=*
all headers - except the id and timestamp
configuration
compression=none
none
gzip
snappy
lz4
zstd
override kafka-clients jar to 2.1.0 (or later)
参考 kafka documents
Transactional Binder
spring.cloud.stream.kafka.binder.transaction.transaction
spring.cloud.stream.kafka.binder.transaction.producer.*
BinderFactory
KafkaMessageChannelBinder
ProducerFactory
KafkaTransactionManager
PlatformTransactionManager
TransactionTemplate
annotations
@Transactional
Error Channels
ErrorMessage
KafkaSendFailureException
Kafka Metrics
spring.cloud.stream.binder.kafka.offset
failedMessage
record
Tombstone Records (null record values)
@Payload(required = false)
Using a KafkaRebalanceListener
KafkaBindingRebalanceListener
Dead-Letter Topic Processing
Partitioning with the Kafka Binder
Apache Kafka Streams Binder\t
Additional Binders
Spring Cloud Stream Samples
Spring Cloud Stream App Starters
Platform
Spring Cloud Azure
Spring Cloud for Amazon Web Services
Spring Cloud for Cloud Foundry
Cloud Foundry是VMware推出的业界第一个开源PaaS云平台,它支持多种框架、语言、运行时环境、云平台及应用服务
Spring Cloud - Cloud Foundry Service Broker
Spring Cloud Open Service Broker
Spring Cloud Skipper
允许您发现Spring Boot应用程序并管理其在多个云平台上的生命周期
Spring Cloud App Broker
提供了一个基于Spring Boot的框架,实现Open Service Broker API,能够快速创建服务代理,在配置托管服务时将应用程序和服务部署到平台
Spring Cloud Kubernetes
Spring Cloud Pipelines
CICD
Spring Cloud GCP
用于提供使用 Spring 集成 Google Cloud 平台的 API
Spring Cloud Function
Spring Cloud Connectors
简化了连接到服务的过程和从云平台获取操作的过程,有很强的扩展性,可以利用Spring Cloud Connectors来构建你自己的云平台
Test
Spring Cloud Contract
消费者驱动的契约测试(Consumer-Driven Contracts,简称CDC),是指从消费者业务实现的角度出发,驱动出契约,再基于契约,对提供者验证的一种测试方式。
Data Flow
Spring Cloud Data Flow
Spring Cloud Task
主要解决短命微服务的任务管理,任务调度的工作。
一个微服务就是一个任务。
Spring Cloud项目簇是针对云平台的,然而对于大部分云平台,任何运行于之上的应用都是长实效的,如果退出,那么平台会自动重启它们已达到“恢复“的目的。但是有些需求并不是这样的,可能存在一个生命周期极短的应用,它是定时执行的,当结束之后我并希望平台自动重启它,可能的话也希望能够获得应用运行的细节,比如起始时间,退出值等等。这就是Spring Cloud Task希望解决的问题。
Getting started
dependency
spring-cloud-starter-task
spring-boot-starter-jdbc
mysql-connector-java
runtime
Task Auto Configuration
TaskRepository
TaskLifecycleListener
The lifecycle of a Spring Cloud Task
spring.cloud.task.closecontext_enabled=false
TaskExecution
executionid
exitCode
taskName
startTime
endTime
exitMessage
errorMessage
arguments
Mapping Exit Codes
ExitCodeExceptionMapper
如果任务以SIG-INT或SIG-TERM终止,则除非代码中另外指定,否则退出代码为零。
Default config
DefaultTaskConfigurer
SimpleTaskConfiguration
DataSource
Table Prefix
spring.cloud.task.tablePrefix=yourPrefix
Enable/Disable table initialization
spring.cloud.task.initialize.enable=true
Externally Generated Task ID
spring.cloud.task.executionid=yourtaskId
External Task Id
spring.cloud.task.external-execution-id=
Parent Task Id
spring.cloud.task.parent-execution-id=
TaskConfigurer
组件
SimpleTaskRepository
TaskExplorer
SimpleTaskExplorer
DataSourceTransactionManager
if use DataSource
ResourcelessTransactionManager
Without DataSource
Task Name
TaskNameResolver
SimpleTaskNameResolver
spring.cloud.task.name
Task Execution Listener
how
TaskExecutionListener
onTaskStartup
onTaskEnd
onTaskFailed
unhandled exception is thrown by the task
Bean metchod annotations
@BeforeTask
@AfterTask
@FailedTask
Exceptions Thrown by Task Execution Listener
如果其中一个监听器的事件处理器抛出异常,那么其它监听器的时间处理器不会被调用。
一个事件处理器发生异常,不影响其它类型的事件处理器调用。
Exit code returned按优先级
ExitCodeEvent
ExitCodeGenerator
如果onTaskStartup抛出异常,ExitCode=1
否则按上述优先级返回
PS:不能使用 ExitCodeExceptionMapper 配置 ExitCode
Exit Messages
在 TaskExecutionListener 的事件处理方法中,设置ExitMesssage
taskExecution.setExitMessage(\"AFTER EXIT MESSAGE\");
Order of precedence
Restricting Spring Cloud Task Instances
spring-integration-core
spring-integration-jdbc
spring.cloud.task.single-instance-enabled=false
如果其它task正在运行
ExitCode=1
ExitMessage
Task with name \"application\" is already running.
Disabling Spring Cloud Task Auto Configuration
@EnableAutoConfiguration(exclude={SimpleTaskAutoConfiguration.class})
spring.cloud.task.autoconfiguration.enabled=true
改为false
Spring Batch Intergration
Spring Cloud Task’s integration with Spring Batch
Spring Cloud Stream Integration
Launching a Task from a Spring Cloud Stream
Appendices
Task Repository Schema
TASK_EXECUTION
TASK_TASK_BATCH
TASK_EXECUTION_PARAMS
TASK_SEQ
Spring Cloud Task App Starters
Reference Guide
Tasks
Others
Spring Cloud CLI
以命令行方式快速建立云组件
Spring Cloud Cluster
Spring Cloud Cluster现在已经被Spring Integration取代。提供在分布式系统中的集群所需要的基础功能支持,如:选举、集群的状态一致性、全局锁、tokens等常见状态模式的抽象和实现
0 条评论
回复 删除
下一页