跳至主要內容

Spring Cloud

zzz大约 2 分钟后端SpringSpring Cloud

介绍

SpringCloud的学习笔记

2020-10-22

版本选择

Spring Boot

githubopen in new window

官网open in new window

Boot官方推荐使用2.X以上的版本

Spring Cloud

githubopen in new window

官网open in new window

查找Spring Boot和Spring Cloud的版本依赖关系

  • 从cloud的官网上查询
版本依赖信息
版本依赖信息
  • 更详细的查看方法

通过查看信息地址open in new window获取json,将json格式化之后可以看到spring-cloud的版本信息,如下图:

cloud版本
cloud版本
 "spring-cloud": {
      "Finchley.M2": "Spring Boot >=2.0.0.M3 and <2.0.0.M5",
      "Finchley.M3": "Spring Boot >=2.0.0.M5 and <=2.0.0.M5",
      "Finchley.M4": "Spring Boot >=2.0.0.M6 and <=2.0.0.M6",
      "Finchley.M5": "Spring Boot >=2.0.0.M7 and <=2.0.0.M7",
      "Finchley.M6": "Spring Boot >=2.0.0.RC1 and <=2.0.0.RC1",
      "Finchley.M7": "Spring Boot >=2.0.0.RC2 and <=2.0.0.RC2",
      "Finchley.M9": "Spring Boot >=2.0.0.RELEASE and <=2.0.0.RELEASE",
      "Finchley.RC1": "Spring Boot >=2.0.1.RELEASE and <2.0.2.RELEASE",
      "Finchley.RC2": "Spring Boot >=2.0.2.RELEASE and <2.0.3.RELEASE",
      "Finchley.SR4": "Spring Boot >=2.0.3.RELEASE and <2.0.999.BUILD-SNAPSHOT",
      "Finchley.BUILD-SNAPSHOT": "Spring Boot >=2.0.999.BUILD-SNAPSHOT and <2.1.0.M3",
      "Greenwich.M1": "Spring Boot >=2.1.0.M3 and <2.1.0.RELEASE",
      "Greenwich.SR6": "Spring Boot >=2.1.0.RELEASE and <2.1.18.BUILD-SNAPSHOT",
      "Greenwich.BUILD-SNAPSHOT": "Spring Boot >=2.1.18.BUILD-SNAPSHOT and <2.2.0.M4",
      "Hoxton.SR8": "Spring Boot >=2.2.0.M4 and <2.3.5.BUILD-SNAPSHOT",
      "Hoxton.BUILD-SNAPSHOT": "Spring Boot >=2.3.5.BUILD-SNAPSHOT and <2.4.0.M1",
      "2020.0.0-M3": "Spring Boot >=2.4.0.M1 and <=2.4.0.M1",
      "2020.0.0-M4": "Spring Boot >=2.4.0.M2 and <=2.4.0-M3",
      "2020.0.0-SNAPSHOT": "Spring Boot >=2.4.0-M4"
    }
  • 更加精确的方法

查看对应cloud版本文档,看官方推荐

入口
入口
boot版本
boot版本

学习文档

Spring Cloud

Hoxton.SR8版本官方资料open in new window

国人翻译中文文档open in new window

Spring Boot

官方资料open in new window

服务发现

nacos、eureka

nacos

集成了ribbon

spring-cloud-starter-alibaba-nacos-discovery集成ribbon

ribbon : 客户端需要给RestTemplate实例添加@LoadBalanced注解,开启@LoadBalancedRibbon的集成

@SpringBootApplication
@EnableDiscoveryClient
public class NacosConsumerApplication {

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

    public static void main(String[] args) {
        SpringApplication.run(NacosConsumerApplication.class, args);
    }

    @RestController
    public class TestController {

        private final RestTemplate restTemplate;

        @Autowired
        public TestController(RestTemplate restTemplate) {this.restTemplate = restTemplate;}

        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        public String echo(@PathVariable String str) {
            return restTemplate.getForObject("http://service-provider/echo/" + str, String.class);
        }
    }
}

网关

gateway 、Consul

GateWay

集成了hystrix

负载均衡

ribbon Fegin OpenFegin

ribbon需要配合@LoadBalanced注解和RestTemplate实例

Fegin在ribbon上进行了改造,直接在接口上添加注解,无需RestTemplate实例

OpenFegin在Fegin上进行了改造,增加了spring mvc等的支持

熔断

hystrix 和 Sentinel

hystrix

SpringCloud 对熔断集成

  1. 启动类增加@EnableCircuitBreaker注解

  2. 根据spring.cloud.circuit.breaker.enabled配置判断是否开启,默认为true

  3. "META-INF/spring.factories"中寻找org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker的实现,这里只能是一个实现

    比如:org.springframework.cloud.netflix.hystrix.HystrixCircuitBreakerConfiguration

    HystrixCircuitBreakerConfiguration中比较重要的一点就是创建了一个切面

    @Bean
    public HystrixCommandAspect hystrixCommandAspect() {
    	return new HystrixCommandAspect();
    }
    

    从HystrixCommandAspect中可以发现,其会监听@HystrixCommand@HystrixCollapser两个注解,其中@HystrixCommand用于配置熔断的回调操作