Spring Cloud 全家桶
2019-04-01 17:52:35 1 举报
AI智能生成
Spring Cloud 笔记
作者其他创作
大纲/内容
Spring Cloud Commons
通用抽象,诸如服务发现,负载平衡和断路器之类的模式适用于所有Spring Cloud客户端可以独立于实现(例如通过Eureka或Consul发现)的消耗的共同抽象层
Spring Cloud Context:
Application Context Services
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=bootstrap
spring.cloud.bootstrap.location=(default empty)
spring.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
Only system properties, command line arguments, and environment variables (but not the local config files) should override the remote settings.
Customizing the Bootstrap Configuration
CustomBootstrapConfiguration
Spring @Configuration class
注意不要加到 @ComponentScan 和 @SpringBootApplication 可以扫描的路径。
否则会加到 main application context中,这通常是不需要的。
否则会加到 main application context中,这通常是不需要的。
/META-INF/spring.factories
org.springframework.cloud.bootstrap.BootstrapConfiguration==sample.custom.CustomBootstrapConfiguration
Customizing the Bootstrap Property Sources
PropertySourceLocator
CustomPropertySourceLocator
Spring @Configuration class
/META-INF/spring.factories
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
Common Abstractions
DiscoveryClient
@EnableDiscoveryClient
META-INF/spring.factories
org.springframework.cloud.client.discovery.EnableDiscoveryClient
@EnableDiscoveryClient is no longer required,
only put a DiscoveryClient implementation on the classpath。
only put a DiscoveryClient implementation on the classpath。
Health Indicator
DiscoveryHealthIndicator
DiscoveryClientHealthIndicator
spring.cloud.discovery.client.composite-indicator.enabled=false
禁用健康检查中的DiscoveryClient检查项
spring.cloud.discovery.client.health-indicator.enabled=false
spring.cloud.discovery.client.health-indicator.include-description=false
spring.cloud.discovery.client.health-indicator.include-description=false
Ordering DiscoveryClient instances
ServiceRegistry
Interface
ServiceRegistry
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
UP,
DOWN
OUT_OF_SERVICE
UNKNOWN
DOWN
OUT_OF_SERVICE
UNKNOWN
Spring RestTemplate as a Load Balancer Client
Annotations
@Configuration + @LoadBalanced + @Bean + RestTemplate
PS
The URI needs to use a virtual host name (that is, a service name, not a host name)
The Ribbon client is used to create a full physical address
Spring WebClient as a Load Balancer Client
Annotations
@Configuration + @LoadBalanced + @Bean + WebClient.Builder
Retrying Failed Requests
Dependencies
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=
<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
Properties
spring.cloud.inetutils.ignoredIntegerfaces=docker0,veth.*
When running in Docker container
spring.cloud.inetutils.preferredNetworks=192.168,10.0
Force the use of only specified network addresses
spring.cloud.inetutils.useOnlySiteLocalInterfaces=192.168,10.0
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
Properties
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", ConsulBusAutoConfiguration.class))
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
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
{application}, which maps to spring.application.name on the client side
{profile}, which maps to spring.profiles.active on the client (comma-separated list)
{label}, which is a server side feature labelling a "versioned" set of config files (defaults to master.)
Config Server
Annotations
@EnableConfigServer
Git Backend
spring.cloud.config.server.git.*
git.uri=https://github.com/spring-cloud-samples/config-repo
git.skipSslValidation=false
git.timeout=4
git.searchPaths=
cloneOnStart=true
git.skipSslValidation=false
git.timeout=4
git.searchPaths=
cloneOnStart=true
Pattern Matching and Multiple Repositories
git.repos.*
Authentication
git.uri=https://github.com/spring-cloud-samples/config-repo
git.username=
git.password=
git.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=
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)
if set true, in case the local copy gets dirty
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
if false, server would return a HTTP 404 status
File System Backend
spring.cloud.config.server.native.searchLocations
Vault Backend
spring.cloud.config.server.vault.*
host=127.0.0.1
port=8200
scheme=http
backend=secret
defaultKey=application
profileSeparator=,
kvVersion=1
skipSslValidation=false
timeout=5
port=8200
scheme=http
backend=secret
defaultKey=application
profileSeparator=,
kvVersion=1
skipSslValidation=false
timeout=5
Accessing Backends Through a Proxy
spring.cloud.config.server.git.proxy.http*
spring.cloud.config.server.git.proxy.https.*
Sharing Configuration With All Applications
File Based Repositories
(git, svn, and native)
(git, svn, and native)
application.properties
application.yml
application-*.properties
application.yml
application-*.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)
Flag to indicate that when {@link #setAllowOverride(boolean) allowOverride} is true, external properties should take lowest priority and should not override any existing property sources (including local config files). Default false.
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.
Only system properties, command line arguments, and environment variables (but not the local config files) should override the remote settings.
Health Indicator
spring.cloud.config.server.health.enabled=false
spring.cloud.config.server.health.repositories.*
Security
HTTP Basic security
Dependecies
org.springframework.boot:spring-boot-starter-security
Properties
spring.security.user.name=user
spring.security.user.password=
spring.security.user.password=
Encryption and Decryption
Java Cryptography Extension (JCE)
Prefix
{cipher}*
xxx.password='{cipher}735ce8bb1672067fca3530fa7b1237fdc123305b1f4342aea15e8ec20d0ac3f7'
Dependecies
org.springframework.security:spring-security-rsa
Install JCE into <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
/encrypt/status
GET
/encrypt
POST
/decrypt
POST
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...
Properties
encrypt.*
spring.cloud.config.server.encrypt.enabled=false
常见错误
/encrypt/status
{"description":"The encryption algorithm is not strong enough","status":"INVALID"}
{"description":"The encryption algorithm is not strong enough","status":"INVALID"}
在 bootstrap.yml 中添加 encrypt.key
/encrypt
{"timestamp":"2019-04-02T10:26:57.351+0000","status":403,"error":"Forbidden","message":"Forbidden","path":"/encrypt"}
{"timestamp":"2019-04-02T10:26:57.351+0000","status":403,"error":"Forbidden","message":"Forbidden","path":"/encrypt"}
禁用CSRF
Key Management
symmetric (shared) key
encrypt.key
asymmetric one (RSA key pair)
encrypt.keyStore.*
location=Contains a Resource location
password=Holds the password that unlocks the keystore
alias=Identifies which key in the store to use
secret=
password=Holds the password that unlocks the keystore
alias=Identifies which key in the store to use
secret=
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)
By default, the flag is off, because it can delay startup
Push Notifications and Spring Cloud Bus
webhook
Github, Gitlab, Gitea, Gitee, Gogs or Bitbucket.
Dependencies
spring-cloud-config-monitor
Spring Cloud Bus
Endpoints
/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
Properties
spring.application.name: myapp
spring.cloud.config.uri=http://myconfigserver.com
spring.cloud.config.label=master
spring.cloud.config.label=master
spring.config.name=
spring.config.location=
spring.config.location=
spring.profiles.active=dev,test
if there are multiple profiles, the last one wins
Config First Bootstrap
bootstrap.yml
or an environment variable
or an environment variable
spring.cloud.config.uri=http://localhost:8888
Discovery First Bootstrap
eureka.client.serviceUrl.defaultZone=
eureka.instance.metadataMap.*
user=
password=
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)
spring.cloud.config.discovery.serviceId=configserver(default)
Config Client Fail Fast
spring.cloud.config.fail-fast=true
Config Client Retry
Dependencies
org.springframework.retry:spring-retry
org.springframework.boot:spring-boot-starter-aop
Properties
spring.cloud.config.fail-fast=false
spring.cloud.config.retry.*
initial-interval=1000
max-attempts=6
max-interval=2000
multiplier=1.1
max-attempts=6
max-interval=2000
multiplier=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}
Properties
spring.cloud.config.*
name=${spring.application.name}
profile=${spring.profiles.active}
label="master"
profile=${spring.profiles.active}
label="master"
PS
Label can be a git label, branch name, or commit ID
Label can also be provided as a comma-separated list
spring.cloud.config.label=myfeature,develop
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 )
Security
HTTP Basic security
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 Indicator
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
Consul 是一个支持多数据中心分布式高可用的服务发现和配置共享的服务软件,由 HashiCorp 公司用 Go 语言开发, 基于 Mozilla Public License 2.0 的协议进行开源. Consul 支持健康检查,并允许 HTTP 和 DNS 协议调用 API 存储键值对
Spring Cloud Zookeeper
一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
Routing
Spring Cloud OpenFeign
Dependencies
org.springframework.cloud:spring-cloud-starter-openfeign
Annotations
@EnableFeignClients
@FeignClient
@FeignClient(name = "service-name", fallback = XxxImpl.class)
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
Decoder feignDecoder: ResponseEntityDecoder (which wraps a SpringDecoder)
Encoder feignEncoder: SpringEncoder
Logger feignLogger: Slf4jLogger
Contract feignContract: SpringMvcContract
Feign.Builder feignBuilder: HystrixFeign.Builder
Client feignClient: if Ribbon is enabled it is a LoadBalancerFeignClient, otherwise the default feign client is used.
Encoder feignEncoder: SpringEncoder
Logger feignLogger: Slf4jLogger
Contract feignContract: SpringMvcContract
Feign.Builder feignBuilder: HystrixFeign.Builder
Client feignClient: if Ribbon is enabled it is a LoadBalancerFeignClient, otherwise the default feign client is used.
Properties
client.config
Default Property Format
feign.client.config.default.<property name>=
Instance Property Format
feign.client.config.<feignName>.<property name>=
Timeouts
connectTimeout: 5000
readTimeout: 5000
readTimeout: 5000
logger
loggerLevel: full
NONE, No logging (DEFAULT).
BASIC, Log only the request method and URL and the response status code and execution time.
HEADERS, Log the basic information along with request and response headers.
FULL, Log the headers, body, and metadata for both requests and responses.
BASIC, Log only the request method and URL and the response status code and execution time.
HEADERS, Log the basic information along with request and response headers.
FULL, Log the headers, body, and metadata for both requests and responses.
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
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
Http Client
feign.okhttp.enabled
feign.httpclient.enabled
feign.httpclient.enabled
hystrix
feign.hystrix.enabled=false
Feign Hystrix Support
Dependencies
org.springframework.cloud:spring-cloud-starter-netflix-hystrix
Properties
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.request.enabled=true
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048
feign.compression.request.mime-types=text/xml,application/xml,application/json
feign.compression.request.min-request-size=2048
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
Dependencies
org.springframework.cloud:spring-cloud-starter-gateway
Properties
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
References
Netflix/eureka
Dependencies
org.springframework.cloud:spring-cloud-starter-netflix-eureka-client
Properties
eureka.instance.*
(it registers itself)
(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)
(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
By default, EurekaClient uses Jersey for HTTP communication
After exclude Jersey Dependencies, EurekaClient uses Spring RestTemplate
Why Is It so Slow to Register a Service
A service is not available for discovery by clients until the instance, the server, and the client
all have the same metadata in their local cache (so it could take 3 heartbeats)
all have the same metadata in their local cache (so it could take 3 heartbeats)
Instance registers itself into server
cost 1 heartbeat
Server refresh instance cache
cost 1 heartbeat
Client fetch instance info
cost 1 heartbeat
Zones
Service 1 in Zone 1
eureka.instance.metadataMap.zone = zone1
eureka.client.preferSameZoneEureka = true
eureka.client.preferSameZoneEureka = true
Service 1 in Zone 2
eureka.instance.metadataMap.zone = zone2
eureka.client.preferSameZoneEureka = true
eureka.client.preferSameZoneEureka = true
Service Discovery: Eureka Server
服务中心,云端服务发现,一个基于 REST 的服务,用于定位服务,以实现云端中间层服务发现和故障转移。
Dependencies
org.springframework.cloud:spring-cloud-starter-netflix-eureka-server
Annotations
@EnableEurekaServer
High Availability, Zones and Regions
Standalone Mode
Properties
eureka.instance.hostname=localhost
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.client.registerWithEureka=false
eureka.client.fetchRegistry=false
eureka.client.serviceUrl.defaultZone=http://${eureka.instance.hostname}:${server.port}/eureka/
Peer Awareness
Properties
peer1
eureka.instance.hostname=peer1
eureka.client.serviceUrl.defaultZone=http://peer2/eureka/
eureka.client.serviceUrl.defaultZone=http://peer2/eureka/
peer2
eureka.instance.hostname=peer2
eureka.client.serviceUrl.defaultZone=http://peer1/eureka/
eureka.client.serviceUrl.defaultZone=http://peer1/eureka/
/etc/hosts
xxx.xxx.xxx.xxx peer1
xxx.xxx.xxx.xxx peer2
xxx.xxx.xxx.xxx peer2
When to Prefer IP Address
eureka.instance.preferIpAddress=true
eureka.instance.hostname=${HOST_NAME}
Securing The Eureka Server
Dependencies
org.springframework.boot:spring-boot-starter-security
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.
@EnableWebSecurity
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().ignoringAntMatchers("/eureka/**");
super.configure(http);
}
}
JDK 11 Support
Dependencies
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.hostname=
eureka.client.service-url.defaultZone=
eureka.instance.prefer-ip-address=true的情况下,
eureka.client.service-url.defaultZone必须使用IP
eureka.client.service-url.defaultZone必须使用IP
Circuit Breaker: Hystrix Clients
熔断器,容错管理工具,旨在通过熔断机制控制服务和第三方库的节点,阻止级联失败,从而对延迟和故障提供更强大的容错能力
References
Netflix/Hystrix
How it Works
Hystrix Configuration
Circuit breaker pattern
Dependencies
org.springframework.cloud:spring-cloud-starter-netflix-hystrix
Annotations
@EnableCircuitBreaker
@HystrixCommand
@HystrixProperty
Health Indicator
The state of the connected circuit breakers are exposed in the /health endpoint
Hystrix Metrics Stream
Dependencies
org.springframework.boot:spring-boot-starter-actuator
Properties
management.endpoints.web.exposure.include=hystrix.stream
or
management.endpoints.web.exposure.include=*
or
management.endpoints.web.exposure.include=*
Endpoints
/actuator/hystrix.stream
Properties
Default Property Prefix
hystrix.command.default.*
Instance Property Prefix
hystrix.command.<HystrixCommandKey>.*
Execution
execution.timeout.enabled=true
execution.isolation.strategy=THREAD (执行隔离策略)
execution.isolation.thread.timeoutInMilliseconds=1000
execution.isolation.thread.interruptOnTimeout=true
execution.isolation.thread.interruptOnCancel=false
execution.isolation.semaphore.maxConcurrentRequests=10
execution.isolation.strategy=THREAD (执行隔离策略)
execution.isolation.thread.timeoutInMilliseconds=1000
execution.isolation.thread.interruptOnTimeout=true
execution.isolation.thread.interruptOnCancel=false
execution.isolation.semaphore.maxConcurrentRequests=10
Fallback
fallback.isolation.semaphore.maxConcurrentRequests=10
fallback.enabled=true
fallback.enabled=true
Circuit Breaker
circuitBreaker.enabled=true (断路器开关)
circuitBreaker.requestVolumeThreshold=20 (断路器请求阈值)
circuitBreaker.errorThresholdPercentage=50(断路器错误请求百分比)
circuitBreaker.sleepWindowInMilliseconds=5000(断路器休眠时间)
circuitBreaker.forceOpen=false(断路器强制开启)
circuitBreaker.forceClosed=false(断路器强制关闭)
circuitBreaker.requestVolumeThreshold=20 (断路器请求阈值)
circuitBreaker.errorThresholdPercentage=50(断路器错误请求百分比)
circuitBreaker.sleepWindowInMilliseconds=5000(断路器休眠时间)
circuitBreaker.forceOpen=false(断路器强制开启)
circuitBreaker.forceClosed=false(断路器强制关闭)
Metrics
metrics.rollingStats.timeInMilliseconds=10000
metrics.rollingStats.numBuckets=10
metrics.rollingStats.numBuckets=10
metrics.rollingPercentile.enabled=true
metrics.rollingPercentile.timeInMilliseconds=60000
metrics.rollingPercentile.numBuckets=6
metrics.rollingPercentile.bucketSize=100
metrics.rollingPercentile.timeInMilliseconds=60000
metrics.rollingPercentile.numBuckets=6
metrics.rollingPercentile.bucketSize=100
metrics.healthSnapshot.intervalInMilliseconds=500
Request Context
requestCache.enabled=true
requestLog.enabled=true
requestLog.enabled=true
Collapser Properties
maxRequestsInBatch=Integer.MAX_VALUE
timerDelayInMilliseconds=10
timerDelayInMilliseconds=10
Thread Pool Properties
coreSize=10(线程池大小)
maximumSize=10
maxQueueSize=−1(最大队列数量)
queueSizeRejectionThreshold=5 (队列大小拒绝阈值)
keepAliveTimeMinutes=1
allowMaximumSizeToDivergeFromCoreSize=false
maximumSize=10
maxQueueSize=−1(最大队列数量)
queueSizeRejectionThreshold=5 (队列大小拒绝阈值)
keepAliveTimeMinutes=1
allowMaximumSizeToDivergeFromCoreSize=false
Circuit Breaker: Hystrix Dashboard
Circuit Breaker: Turbine
Hystrix Timeouts And Ribbon Clients
if your Ribbon connection timeout is one second and the Ribbon client might retry the request three times,
than your Hystrix timeout should be slightly more than three seconds.
than your Hystrix timeout should be slightly more than three seconds.
How to Include the Hystrix Dashboard
Dependencies
org.springframework.cloud:spring-cloud-starter-netflix-hystrix-dashboard
Annotations
@EnableHystrixDashboard
HomePageUrlPath
/hystrix
Turbine
Pull 模式
Pull 模式
References
Netflix/Turbine
Configuration (1.x)
Dependencies
org.springframework.cloud:spring-cloud-starter-netflix-turbine
Annotations
@EnableTurbine
Endpoints
/turbine.stream?cluster=CLUSTERNAME
/clusters
list clusters have been configured in Turbine
Properties
turbine.instanceInsertPort
turbine.aggregator.clusterConfig=SYSTEM,USER
upper-case
The url cluster parameter must match an entry in turbine.aggregator.clusterConfig
turbine.appconfig=customers,stores,ui,admin
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 Stream
Push 模式
Push 模式
client
Dependencies
spring-cloud-netflix-hystrix-stream
spring-cloud-starter-stream-*
Annotations
@EnableTurbineStream
server
Dependencies
spring-cloud-starter-netflix-turbine-stream
spring-cloud-starter-stream-*
Properties
don't need properties
turbine.appConfig
turbine.clusterNameExpression
turbine.aggregator.clusterConfig
Client Side Load Balancer: Ribbon
References
Netflix/ribbon
CommonClientConfigKey
Constant Field Values
Dependencies
org.springframework.cloud:spring-cloud-starter-netflix-ribbon
Annotations
@FeignClient
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
<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
DiscoveryEnabledNIWSServerList
DiscoveryEnabledServer
NIWSDiscoveryPing
Example: How to Use Ribbon Without Eureka
@RibbonClient(name = "<clientName>")
<clientName>.ribbon.listOfServers=example.com,google.com
Example: Disable Eureka Use in Ribbon
ribbon.eureka.enabled=false
Using the Ribbon API Directly
Beans
LoadBalancerClient
Caching of Ribbon Configuration
ribbon.eager-load.enabled=true (default is false)
ribbon.eager-load.clients=client1, client2, client3
ribbon.eager-load.clients=client1, client2, client3
How to Configure Hystrix Thread Pools
zuul.ribbonIsolationStrategy=THREAD
SEMAPHORE(default)
THREAD
zuul.threadpool.useSeparateThreadPools=true
zuul.threadpool.threadPoolKeyPrefix=zuulgw
zuul.threadpool.threadPoolKeyPrefix=zuulgw
References
用网关zuul时,熔断hytrix里面的坑
How to Provide a Key to Ribbon’s IRule
Properties
Default Property Format
ribbon.<propertyName>=<value>
Instance Property Format
<clientName>.ribbon.<propertyName>=<value>
Timeout
ConnectTimeout=2000
ReadTimeout=5000
ReadTimeout=5000
Connections
MaxTotalHttpConnections=200
MaxConnectionsPerHost=50
MaxConnectionsPerHost=50
Retry
MaxAutoRetries=0
MaxAutoRetriesNextServer=1
OkToRetryOnAllOperations=
retryableStatusCodes=
MaxAutoRetriesNextServer=1
OkToRetryOnAllOperations=
retryableStatusCodes=
zone
EnableZoneAffinity=false
EnableZoneExclusivity=false
EnableZoneExclusivity=false
With Eureka
NFLoadBalancerClassName: Should implement ILoadBalancer
NFLoadBalancerRuleClassName: Should implement IRule
NFLoadBalancerPingClassName: Should implement IPing
NIWSServerListClassName: Should implement ServerList
NIWSServerListFilterClassName: Should implement ServerListFilter
NFLoadBalancerRuleClassName: Should implement IRule
NFLoadBalancerPingClassName: Should implement IPing
NIWSServerListClassName: Should implement ServerList
NIWSServerListFilterClassName: Should implement ServerListFilter
ServerListRefreshInterval=30000
DeploymentContextBasedVipAddresses=
DeploymentContextBasedVipAddresses=
Without Eureka
listOfServers=example.com,google.com
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
References
Netflix/zuul
Dependecies
org.springframework.cloud:spring-cloud-starter-netflix-zuul
org.springframework.cloud:spring-cloud-starter-netflix-eureka-client
Annotations
@EnableZuulProxy
Properties
Default Property Format
zuul.<property name>=
Instance Property Format
zuul.routes.<routename>.<property name>=
zuul.prefix=
zuul.stripPrefix=true
zuul.addProxyHeaders=true
zuul.stripPrefix=true
zuul.addProxyHeaders=true
zuul.host.maxTotalConnections=200
zuul.host.maxPerRouteConnections=20
zuul.host.maxPerRouteConnections=20
If configured Zuul routes by specifying URLs
zuul.ribbonIsolationStrategy=SEMAPHORE
SEMAPHORE(default)
THREAD
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>.serviceId=<service id>
zuul.routes.<routename>.stripPrefix=true
or example "/<service id>/101" is forwarded to "/101"
zuul.routes.<routename>.path=/<service id>/**
zuul.routes.<routename>.url:=http://example.com/users_service
zuul.routes.<routename>.url:=http://example.com/users_service
retry
zuul.retryable=
zuul.routes.<routename>.retryable=
zuul.routes.<routename>.retryable=
Http Client
Default is Apache HTTP Client
Use Ribbon RestClient
ribbon.restclient.enabled=true
Use okhttp3.OkHttpClient
ribbon.okhttp.enabled=true
Cookies and Sensitive Headers
zuul.routes.<service id>.sensitiveHeaders=Cookie,Set-Cookie,Authorization
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=
ribbon.SocketTimeout=
If configured Zuul routes by specifying URLs
zuul.host.connect-timeout-millis=
zuul.host.socket-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 过滤器
5
PreDecorationFilter
处理请求上下文供后续使用
10
RibbonRoutingFilter
serviceId请求转发
100
SimpleHostRoutingFilter
url请求转发
500
SendForwardFilter
forward请求转发
post 过滤器
0
SendErrorFilter
处理有错误的请求响应
1000
SendResponseFilter
处理正常的请求响应
常见问题
X-Forwarded-For: client IP, proxy1 IP, proxy2 IP
X-Real-IP: client IP
X-Real-IP: client IP
如何实现IP白名单
Polyglot support with Sidecar
Dependencies
org.springframework.cloud:spring-cloud-netflix-sidecar
Annotations
@EnableSidecar
Properties
sidecar.port
The sidecar.port property is the port on which the non-JVM application listens
sidecar.health-uri
Retrying Failed Requests
BackOff Policies
By default, no backoff policy is used when retrying requests
Configuration
<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
维护模式,不再添加新特性,只维护BUG
spring-cloud-netflix-archaius
spring-cloud-netflix-hystrix-contract
spring-cloud-netflix-hystrix-dashboard
spring-cloud-netflix-hystrix-stream
spring-cloud-netflix-hystrix
spring-cloud-netflix-ribbon
spring-cloud-netflix-turbine-stream
spring-cloud-netflix-turbine
spring-cloud-netflix-zuul
spring-cloud-netflix-hystrix-contract
spring-cloud-netflix-hystrix-dashboard
spring-cloud-netflix-hystrix-stream
spring-cloud-netflix-hystrix
spring-cloud-netflix-ribbon
spring-cloud-netflix-turbine-stream
spring-cloud-netflix-turbine
spring-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 Sent
sr: Server Received
ss: Server Sent
cr: Client Received
sr: Server Received
ss: Server Sent
cr: 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)
dependencyManagement
org.springframework.cloud:spring-cloud-dependencies:pom:import
dependencies
org.springframework.cloud:spring-cloud-starter-sleuth
Sleuth with Zipkin via HTTP
dependencyManagement
org.springframework.cloud:spring-cloud-dependencies:pom:import
Dependencies
org.springframework.cloud:spring-cloud-starter-zipkin
Sleuth with Zipkin over RabbitMQ or Kafka
Kafka
dependencyManagement
org.springframework.cloud:spring-cloud-dependencies:pom:import
dependencies
org.springframework.cloud:spring-cloud-starter-zipkin
org.springframework.kafka:spring-kafka
properties
spring.zipkin.sender.type: kafka
Kafka Topic
zipkin
RabbitMQ
dependencyManagement
org.springframework.cloud:spring-cloud-dependencies:pom:import
dependencies
org.springframework.cloud:spring-cloud-starter-zipkin
org.springframework.amqp:spring-rabbit
properties
Overriding the auto-configuration of Zipkin
Configuration Beans
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 Bus
事件、消息总线,用于在集群(例如,配置变化事件)中传播状态变化,可与Spring Cloud Config联合实现热部署
Quick Start
Dependecies
spring-cloud-starter-bus-amqp
spring-cloud-starter-bus-kafka
Endpoints
/bus/env
/bus/refresh
/refresh
Bus Endpoints
Spring Cloud Bus
/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
Spring Cloud Commons
/actuator/refresh
/actuator/env
Addressing an Instance
spring.cloud.bus.id
default value
app:index:id
app is the vcap.application.name, if it exists, or spring.application.name
index is the vcap.application.instance_index, if it exists, spring.application.index, local.server.port, server.port, or 0 (in that order)
id is the vcap.application.instance_id, if it exists, or a random value
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
naturally partitioned (for example, Kafka)
not naturally partitioned (for example, RabbitMQ)
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
@EnableBinding(Source.class)
public class TimerSource {
@Bean
@InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "10", maxMessagesPerPoll = "1"))
public MessageSource timerMessageSource() {
return () -> new GenericMessage<>("Hello Spring Cloud Stream");
}
}
public class TimerSource {
@Bean
@InboundChannelAdapter(value = Source.OUTPUT, poller = @Poller(fixedDelay = "10", maxMessagesPerPoll = "1"))
public MessageSource
return () -> new GenericMessage<>("Hello Spring Cloud Stream");
}
}
@Transformer或@ServiceActivator
@EnableBinding(Processor.class)
public class TransformProcessor {
@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Object transform(String message) {
return message.toUpperCase();
}
}
public class TransformProcessor {
@Transformer(inputChannel = Processor.INPUT, outputChannel = Processor.OUTPUT)
public Object transform(String message) {
return message.toUpperCase();
}
}
订阅模式
pub-sub model
Spring Cloud Stream annotation
@StreamListener
有分组概念,
competing model
Spring Integration annotation
@Aggregator, @Transformer, or @ServiceActivator
无分组概念
Using @StreamListener Annotation
@StreamListener
@Payload, @Headers, and @Header
@SendTo
example
@EnableBinding(Sink.class)
public class VoteHandler {
@Autowired
VotingService votingService;
@StreamListener(Sink.INPUT)
public void handle(Vote vote) {
votingService.record(vote);
}
}
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);
}
}
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(target = Sink.INPUT, condition = "headers['type']=='bogey'")
目前,@StreamListener仅支持基于通道的binder(不支持反应式编程)。
Spring Cloud Function support
java.util.function.[Supplier/Function/Consumer]
spring.cloud.stream.function.definition
编程模式
imperative functions
命令式函数
命令式函数
@SpringBootApplication
@EnableBinding(Processor.class)
public static class ProcessorFromFunction {
public static void main(String[] args) {
SpringApplication.run(ProcessorFromFunction.class, "--spring.cloud.stream.function.definition=toUpperCase");
}
@Bean
public Function toUpperCase() {
return s -> s.toUpperCase();
}
}
@EnableBinding(Processor.class)
public static class ProcessorFromFunction {
public static void main(String[] args) {
SpringApplication.run(ProcessorFromFunction.class, "--spring.cloud.stream.function.definition=toUpperCase");
}
@Bean
public Function
return s -> s.toUpperCase();
}
}
reactive functions
响应式函数
响应式函数
@EnableAutoConfiguration
@EnableBinding(Processor.class)
public static class SinkFromConsumer {
public static void main(String[] args) {
SpringApplication.run(SinkFromConsumer.class, "--spring.cloud.stream.function.definition=reactiveUpperCase");
}
@Bean
public Function, Flux> reactiveUpperCase() {
return flux -> flux.map(val -> val.toUpperCase());
}
}
@EnableBinding(Processor.class)
public static class SinkFromConsumer {
public static void main(String[] args) {
SpringApplication.run(SinkFromConsumer.class, "--spring.cloud.stream.function.definition=reactiveUpperCase");
}
@Bean
public Function
return flux -> flux.map(val -> val.toUpperCase());
}
}
Reactive Functions support
Functional Composition
支持管道符连接多个function
spring.cloud.stream.function.definition=toUpperCase|wrapInQuotes
Using Polled Consumers
Error Handling
Application Error Handling
在每个绑定订阅中处理错误
<destination>+<group>
<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) {
System.out.println("Handling ERROR: " + message);
}
public void error(Message message) {
System.out.println("Handling ERROR: " + message);
}
<channelName>.<destination>.errors
全局处理程序,处理所有绑定订阅错误
@StreamListener("errorChannel")
public void error(Message message) {
System.out.println("Handling ERROR: " + message);
}
public void error(Message message) {
System.out.println("Handling ERROR: " + message);
}
System Error Handling
Drop Failed Messages
default
DLQ - Dead Letter Queue
Re-queue Failed Messages
Retry Template
@StreamRetryTemplate
public RetryTemplate myRetryTemplate() {
return new RetryTemplate();
}
public 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.
properties
type
inheritEnvironment
environment
defaultCandidate
Configuration Options
Binding Service Properties
org.springframework.cloud.stream.config.BindingServiceProperties
format
spring.cloud.stream.<property>=<value>
properties
instanceCount
instanceIndex
dynamicDestinations
defaultBinder
overrideCloudConnectors
bindingRetryInterval
Binding Properties
format
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
format
spring.cloud.stream.bindings.<channelName>.<property>=<value>
spring.cloud.stream.bindings.default.<property>=<value>
For all channels
properties
destination
kafka: topic
rabbit: exchange
group
contentType=application/json
binder
Consumer Properties
class
org.springframework.cloud.stream.binder.ConsumerProperties
format
spring.cloud.stream.bindings.<channelName>.consumer.<property>=<value>
spring.cloud.stream.bindings.default.consumer.<property>=<value>
For all channels
properties
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
class
org.springframework.cloud.stream.binder.ProducerProperties
format
spring.cloud.stream.bindings.<channelName>.producer.<property>=<value>
spring.cloud.stream.bindings.default.producer.<property>=<value>
For all channels
properties
autoStartup=true
partitionKeyExpression=null
partitionKeyExtractorClass=null
partitionSelectorClass=null
partitionSelectorExpression=null
partitionCount=1
kafka使用topic自身的分区设置
requiredGroups
headerMode
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
For all channels
Content Type versus Argument Type
HandlerMethodArgumentResolvers
MessageConverter
Message Converters
fromMessage
toMessage
Provided MessageConverters
ApplicationJsonMessageMarshallingConverter
application/json
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
Partitioning
Configuring Output Bindings for Partitioning
prefix
spring.cloud.stream.bindings.<output>.producer.<property>
properties
partitionKeyExpression
partitionKeyExtractorName
org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy
partitionCount
partitionSelectorName
org.springframework.cloud.stream.binder.PartitionSelectorStrategy
Configuring Input Bindings for Partitioning
properties
spring.cloud.stream.bindings.input.consumer.partitioned=true
spring.cloud.stream.instanceIndex=3
Kafka not required
spring.cloud.stream.instanceCount=5
Kafka not required
Testing
Health Indicator
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
Configuration Options
Kafka Binder Properties
format
spring.cloud.stream.kafka.binder.<property>=<value>
properties
brokers=localhost
defaultBrokerPort=9092
configuration={}
consumerProperties={}
headers
healthTimeout=10
second
requiredAcks=1
minPartitionCount=1
producerProperties={}
replicationFactor=1
autoCreateTopics=true
autoAddPartitions=false
transaction.transactionIdPrefix=null
transaction.producer.*
headerMapperBeanName
Kafka Consumer Properties
format
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
properties
autoRebalanceEnabled=true
ackEachRecord=false
autoCommitOffset=true
autoCommitOnError
resetOffsets=false
startOffset=earliest
enableDlq=false
configuration=
dlqName
dlqProducerProperties
standardHeaders
converterBeanName
idleEventInterval=30_000
destinationIsPattern=false
topic.properties
topic.replicas-assignment
topic.replication-factor
Kafka Producer Properties
format
spring.cloud.stream.kafka.bindings.<channelName>.producer.<property>=<value>
for a chenel
spring.cloud.stream.kafka.default.producer.<property>=<value>
for a binder-type channels
spring.cloud.stream.default.producer.<property>=<value>
for all channels
properties
bufferSize=16384
sync=false
batchTimeout=0
messageKeyExpression=none
example
headers['myKey']
headerPatterns=*
all headers - except the id and timestamp
example
!ask,as*
configuration
topic.properties
topic.replicas-assignment
topic.replication-factor
compression=none
example
none
gzip
snappy
lz4
zstd
override kafka-clients jar to 2.1.0 (or later)
参考 kafka documents
Transactional Binder
properties
spring.cloud.stream.kafka.binder.transaction.transaction
spring.cloud.stream.kafka.binder.transaction.producer.*
class
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
example
Partitioning with the Kafka Binder
Apache Kafka Streams Binder
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希望解决的问题。
但是有些需求并不是这样的,可能存在一个生命周期极短的应用,它是定时执行的,当结束之后我并希望平台自动重启它,可能的话也希望能够获得应用运行的细节,比如起始时间,退出值等等。这就是Spring Cloud Task希望解决的问题。
Getting started
dependency
spring-cloud-starter-task
spring-boot-starter-jdbc
mysql-connector-java
runtime
Task Auto Configuration
TaskRepository
TaskLifecycleListener
Features
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终止,则除非代码中另外指定,否则退出代码为零。
Configuration
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
DefaultTaskConfigurer
组件
TaskRepository
SimpleTaskRepository
TaskExplorer
SimpleTaskExplorer
PlatformTransactionManager
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
unhandled exception is thrown by the task
Exceptions Thrown by Task Execution Listener
如果其中一个监听器的事件处理器抛出异常,那么其它监听器的时间处理器不会被调用。
一个事件处理器发生异常,不影响其它类型的事件处理器调用。
Exit code returned
按优先级
按优先级
ExitCodeEvent
ExitCodeGenerator
1
如果onTaskStartup抛出异常,ExitCode=1
否则按上述优先级返回
PS:不能使用 ExitCodeExceptionMapper 配置 ExitCode
Exit Messages
在 TaskExecutionListener 的事件处理方法中,设置ExitMesssage
taskExecution.setExitMessage("AFTER EXIT MESSAGE");
Order of precedence
onTaskEnd
onTaskFailed
onTaskStartup
Restricting Spring Cloud Task Instances
dependency
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 条评论
下一页