`

spring cloud 学习笔记

阅读更多
spring cloud 学习笔记

第一节

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



备注
默认情况下, 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

如图:




这是个潜在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







  • 大小: 71.6 KB
  • 大小: 53.2 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics