[Spring Cloud] Spring Cloud 学习笔记一
1 下载JDK 1.8
2 下载JCE 1.8 解压到: JDK/jre/lib/security
3 下载Eclipse
4 下载Maven
5 property source: 属性配置文件 enviroment: spring.profiles.active - bootstrap context: bootstrap.properties/bootstrap.yml (默认配置,优先加载) - application context: application.yml.properties/application.yml
6 远程配置仓库:配置中心 GIT
配置服务器地址(默认端口:8080)
spring.cloud.config.uri=
强制拉取最新版本数据
spring.cloud.config.force-pull=true
spring.config.name=configserver server.port=8080
客户端连接默认端口: 8888
spring.cloud.config.failFast=true
启用本地配置覆盖配置中心配置(配置中心)
spring.cloud.config.allowOverride=true
禁用本地配置覆盖配置中心配置(配置中心)
spring.cloud.config.overrideNone=true
启用本地配置覆盖配置中心配置,但不覆盖本地配置
spring.cloud.config.overrideSystemProperties=false
支持跨环境管理配置文件(DEV/SIT/UAT/PRODUCION)
配置文件从GIT仓库获取
spring.cloud.config.server.git.uri=https://github.com/spring-cloud-samples/config-repo
认证(spring-boot-starter-security)
spring.cloud.config.server.git.username= spring.cloud.config.server.git.password=
spring: datasource: username: dbuser password: '{cipher}FKSAJDFGYOS8F7GLHAKERGFHLSAJ'
生成证书
keytool -genkeypair -alias mytestkey -keyalg RSA
-dname "CN=Web Server,OU=Unit,O=Organization,L=City,S=State,C=US"
-keypass changeme -keystore server.jks -storepass letmein
配置证书(bootstrap.yml)
encrypt: keyStore: location: classpath:/server.jks password: letmein alias: mytestkey secret: changeme
禁用临时目录
spring.cloud.config.server.git.basedir
配置文件命名规范
/{application}/{profile} /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
application: spring.application.name profile: spring.prifiles.active label: master/git 分支标签名
GIT添加配置文件
$ cd $HOME $ mkdir config-repo $ cd config-repo $ git init . $ echo info.foo: bar > application.properties $ git add -A . $ git commit -m "Add application.properties"
7 服务发现 Eureka/Consul/Zoookeeper
server: port: 8761
eureka: instance: hostname: localhost client: registerWithEureka: false fetchRegistry: false serviceUrl: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
eureka.instance.preferIpAddress=true eureka.instance.leaseRenewalIntervalInSeconds
客户端注册 通过META-INF/spring.factories的org.springframework.cloud.client.discovery.EnableDiscoveryClient=
eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/
禁用自动注册
@EnableDiscoveryClient(autoRegister=false)
禁用自动注册
spring.cloud.service-registry.auto-registration.enabled=false
eureka: instance: ... metadataMap: user: osufhalskjrtl password: lviuhlszvaorhvlo5847 configPath: /config
8 负载均衡 ribbon/zuul
zuul: threadPool: useSeparateThreadPools: true threadPoolKeyPrefix: zuulgw stores: ribbon: listOfServers: example.com,google.com
禁用失败重试(Spring Retry)
spring.cloud.loadbalancer.retry.enabled=false client.ribbon.MaxAutoRetries=3 client.ribbon.MaxAutoRetriesNextServer= client.ribbon.OkToRetryOnAllOperations
不同负载均衡策略共存
spring.aop.proxyTargetClass=true
指定网络接口
spring: cloud: inetutils: preferredNetworks: - 192.168 - 10.0
排除网络接口
spring: cloud: inetutils: ignoredInterfaces: - docker0 - veth.*
指定站点本地接口
spring: cloud: inetutils: useOnlySiteLocalInterfaces: true
9 HTTP Client/OK Http Client
spring.cloud.httpclientfactories.apache.enabled=true spring.cloud.httpclientfactories.ok.enabled=false
10 熔断 Hystrix/Turbine circuitBreaker.requestVolumeThreshold=20 circuitBreaker.errorThresholdPercentage=>50% metrics.rollingStats.timeInMilliseconds=10
@HystrixCommand
11 REST Feign @FeignClient
feign: client: config: feignName: connectTimeout: 5000 readTimeout: 5000 loggerLevel: full errorDecoder: com.example.SimpleErrorDecoder retryer: com.example.SimpleRetryer requestInterceptors: - com.example.FooRequestInterceptor - com.example.BarRequestInterceptor decode404: false
feign: hystrix: enabled: false
hystrix: command: default: execution: isolation: strategy: SEMAPHORE
导入配置
@Import
注入
@Autowired
配置
@Configuration @Bean @Scope
请求压缩
feign.compression.request.enabled=true feign.compression.response.enabled=true
12 日志 logging.level.cn.bisoft: DEBUG
13 服务端路由 zuul
zuul: routes: users: path: /myusers/** serviceId: users_service
uul: routes: echo: path: /myusers/** serviceId: myusers-service stripPrefix: true
hystrix: command: myusers-service: execution: isolation: thread: timeoutInMilliseconds: ...
myusers-service: ribbon: NIWSServerListClassName: com.netflix.loadbalancer.ConfigurationBasedServerList ListOfServers: http://example1.com,http://example2.com ConnectTimeout: 1000 ReadTimeout: 3000 MaxTotalHttpConnections: 500 MaxConnectionsPerHost: 100
客户端
zuul: routes: users: path: /myusers/** sensitiveHeaders: Cookie,Set-Cookie,Authorization url: https://downstream
endpoints.routes.enabled=false
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000 ribbon: ConnectTimeout: 3000 ReadTimeout: 60000
zuul: forceOriginalQueryStringEncoding: true
zuul.servlet-path
zuul.host.connect-timeout-millis= zuul.host.socket-timeout-millis=
14 RxJAVA
异步基于事件
15 Stream/Kafka/RabbitMQ
16 BUS
17 SSO OAuth2