spring cloud 学习笔记

spring cloud 学习笔记

第一节

基础环境
一个注册心(eureka)
一个服务(hello world)
两个消费者(restTemplate+ribbon, feign)

[img]http://dl2.iteye.com/upload/attachment/0129/9428/f391dcb6-2a77-3df7-86db-c028df0487c0.jpg[/img]

备注
默认情况下, eureka会开户自我保护机制, 这时是不会移除服务的
背景是, 如果在eureka网络不稳定的情况, 移除注册的服务, 很可能存在其实这个服务是稳定的
这种设计, 本人感觉更像是实验室应用, 而不是真正的分布式高并发应用策略, 当然了, eureka可以关闭自我保护机制, 设定过期更新时间, 但目前没能按设计好的5秒更新

自我保护模式被激活的条件是:在 1 分钟后, Renews (last min) < Renews threshold



eureka:
server:
enable-self-preservation: false
eviction-interval-timer-in-ms: 5000
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/



第二节

熔断器
正常通过访问
httpclient -> ribbon -> eureka client 135ms
添加断路器hystrix后,1035ms

httpclient -> feign -> eureka client 142ms
添加断路器hystrix后,1029ms


zuul

zuul 使用ribbon作为http访问引擎

zuul - ribbon 150ms
zuul - feign 137ms

zuul - ribbon(熔断)1024ms
zuul - feign(熔断)1011

zuul - ribbon 之间的timeout

zuul - feign 之间的timeout


hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 10000


而不是

host:
connect-timeout-millis: 10000
socket-timeout-millis: 10000


备注
服务起来, 关掉eureka, zuul路由里仍然可以使用
SERVICE-RIBBON
SERVICE-HELLOWORLD
这种spring.application.name(serviceId)的方式访问


第三节
配置中心
文件名规则 xxx-dev.properties

xxx-xxx-dev.properties
已知 dev, product, test

例子:
服务端配置文件application.properties
#使用本地属性文件
spring.profiles.active=native

#属性文件地址,只要指定文件夹的路径
spring.cloud.config.server.native.searchLocations=classpath:/config/

配置文件1
config-client-dev.properties

url=dev-123
env.name=dev
env.password=dev123456


配置文件2
config-client-product.properties

url=product-123
env.name=product
env.password=product123456



客户端配置application.yml

spring.application.name:config-client
spring.cloud.config.profile:dev



进一步发现, 其实规则是xxx-xxx-xxx-xxxx.properties都可以,从后往前推

服务端
d-c-b-a.properties

对应客户端
spring.application.name:d-c-b
spring.cloud.config.profile:a

访问地址:
http://localhost:8888/d-c-b/a

http://localhost:8888/d-c-b-a.properties


进一步发现, 其实规则是xxx-xxx.properties对应的profile可以忽略,这时候profile可以是任意值

服务端
com-dataSource.properties

对应客户端
spring.application.name:com-dataSource
#其实这里写不写都可以
spring.cloud.config.profile:dev

访问地址:
http://localhost:8888/com-dataSource

http://localhost:8888/com-dataSource/dev

http://localhost:8888/com-dataSource/proc

http://localhost:8888/com-dataSource/test

http://localhost:8888/com/dataSource

http://localhost:8888/com-dataSource.properties

如图:

[img]http://dl2.iteye.com/upload/attachment/0129/9946/e077427c-4d9c-341e-af69-0b7bedd28aea.png[/img]


这是个潜在bug,所以在命名规则设计时,一定要文档约束,
推荐以下方式
config-client-dev.properties
config-client-test.properties
config-client-product.properties
然后以
http://localhost:8888/config-client-dev.properites

http://localhost:8888/com-dataSource/dev
http://localhost:8888/com-dataSource/test
方式来访问,以便区别dev,test,product环境


官方配置文件访问方式

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties



注意config-server与config-client之间的关系不是实时更新的
config-server启动后读取文件
config-client启动后,远程读取config-server自己工程的配置文件,然后自己保存副本,不会随着config-server更新而更新

想要config-client不重启更新的话,需要引入Spring Cloud Bus
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值