spring-cloud 源码解析

简介: 为什么要看源码1. 因为源码获得很容易, 代码本身是最准确的讲解2. 推测的结论不准3. "实验"周期长,代价大4. 通过查看历史版本差异, 了解功能演化5. 源码面前,了无秘密

为什么要看源码


  1. 因为源码获得很容易, 代码本身是最准确的讲解
  2. 推测的结论不准
  3. "实验"周期长,代价大
  4. 通过查看历史版本差异, 了解功能演化
  5. 源码面前,了无秘密


下载源码方法


直接点 CTRL + 类, 通常是编译的内容, 可以在不下载源码时简单看一下, 但有时编译效果不好, 也没有注释说明等. 这时最好是下载源码.

IDE 中都有相应功能, IDEA 和 eclipse 都有相应功能

IDEA 中下载源码 [Download Sources], 可以只下载某个 jar 包对应的源码.

G}3D`YFU71HUN4RXO[)_{FA.png

命令行方式可以一次下载全部源码:

mvn dependency:sources
...
Downloading from ssllrepo: http://dev.myrepo.com/artifactory/repo/com/fasterxml/classmate/1.3.4/classmate-1.3.4-sources.jar
Downloaded from ssllrepo: http://dev.myrepo.com/artifactory/repo/com/fasterxml/classmate/1.3.4/classmate-1.3.4-sources.jar (48 kB at 694 kB/s)
...


下载的代码会保存到本地 maven 库里, 即${user.home}/.m2/repository


查看源码先要了解模块全图, 了解原理


核心功能:

  1. Distributed/versioned configuration
  2. Service registration and discovery
  3. Routing
  4. Service-to-service calls
  5. Load balancing
  6. Circuit Breakers
  7. Global locks
  8. Leadership election and cluster state
  9. Distributed messaging


看源码要注意的问题


  1. 要从结构上把握,胸中要有全景图
  2. 要从需求出发, 知晓作者是在解决一个什么问题
  3. 重点是作者为什么是这么解决的,好处,坏处,如何取舍的
  4. 你的时间很宝贵, 不要过度纠缠, 通过源码能解决你的疑问


最好带着问题去看



问题1: 有哪些配置选项

https://docs.spring.io/spring...

配置举例:

三种方式

application.properties 或 application.yml 或命令行选项

配置项

# REMOTE DEVTOOLS (RemoteDevToolsProperties)
spring.devtools.remote.context-path=/.~~spring-boot!~ # Context path used to handle the remote connection.
spring.devtools.remote.proxy.host= # The host of the proxy to use to connect to the remote application.
spring.devtools.remote.proxy.port= # The port of the proxy to use to connect to the remote application.
spring.devtools.remote.restart.enabled=true # Whether to enable remote restart.
spring.devtools.remote.secret= # A shared secret required to establish a connection (required to enable remote support).
spring.devtools.remote.secret-header-name=X-AUTH-TOKEN # HTTP header used to transfer the shared secret.

对应的Java类

RemoteDevToolsProperties.java

官方的 Java 包会在通过META-INF目录下的

spring-autoconfigure-metadata.properties
spring-configuration-metadata.json


例如:

~/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/2.0.3.RELEASE/spring-boot-autoconfigure-2.0.3.RELEASE.jar!/META-INF/spring-configuration-metadata.json

{
  "hints": [
    ...,
    {
      "name": "spring.datasource.data",
      "providers": [
        {
          "name": "handle-as",
          "parameters": {
            "target": "java.util.List<org.springframework.core.io.Resource>"
          }
        }
      ]
    },
    {
      "name": "spring.datasource.driver-class-name",
      "providers": [
        {
          "name": "class-reference",
          "parameters": {
            "target": "java.sql.Driver"
          }
        }
      ]
    },
    ...
    }

JSON 结构比较简单, 容易看懂.

在这个文件里定义了相应的配置项,数值类型,以及可能的备选项进行描述, 从而使得 IDE 可以透过代码提示, 方便用户键入.


问题2: 最小原型是什么

未完待续(鉴于很多转载/抓取的从不注明出处,我先发个半截的)


问题3: 如何调试

未完待续


问题4: 如何模拟服务失败, 模拟容错

未完待续


问题5: 如何负载均衡

未完待续


问题6: 如何进行压力测试, 模拟熔断

未完待续


问题7: 如何自动布署

未完待续


用测试代码验证自己的想法


请 mark https://segmentfault.com/a/11... 等待更新。

相关文章
|
1天前
|
XML 人工智能 Java
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
Spring Bean名称生成规则(含源码解析、自定义Spring Bean名称方式)
|
1天前
|
监控 Java 微服务
第八章 Spring Cloud 之 Hystrix
第八章 Spring Cloud 之 Hystrix
|
1天前
|
监控 Java API
第七章 Spring Cloud 之 GateWay
第七章 Spring Cloud 之 GateWay
|
1天前
|
消息中间件 Java Nacos
第三章 Spring Cloud简介
第三章 Spring Cloud简介
|
2天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
2天前
|
Dubbo Java 应用服务中间件
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
|
8天前
|
Java 关系型数据库 MySQL
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
UWB (ULTRA WIDE BAND, UWB) 技术是一种无线载波通讯技术,它不采用正弦载波,而是利用纳秒级的非正弦波窄脉冲传输数据,因此其所占的频谱范围很宽。一套UWB精确定位系统,最高定位精度可达10cm,具有高精度,高动态,高容量,低功耗的应用。
一套java+ spring boot与vue+ mysql技术开发的UWB高精度工厂人员定位全套系统源码有应用案例
|
9天前
|
负载均衡 Java 开发者
细解微服务架构实践:如何使用Spring Cloud进行Java微服务治理
【4月更文挑战第17天】Spring Cloud是Java微服务治理的首选框架,整合了Eureka(服务发现)、Ribbon(客户端负载均衡)、Hystrix(熔断器)、Zuul(API网关)和Config Server(配置中心)。通过Eureka实现服务注册与发现,Ribbon提供负载均衡,Hystrix实现熔断保护,Zuul作为API网关,Config Server集中管理配置。理解并运用Spring Cloud进行微服务治理是现代Java开发者的关键技能。
|
9天前
yolo-world 源码解析(六)(2)
yolo-world 源码解析(六)
18 0
|
9天前
yolo-world 源码解析(六)(1)
yolo-world 源码解析(六)
12 0

推荐镜像

更多