阿里妹導讀:一年多前,Java 界最近發生了一件大事,阿里開源 Spring Cloud Alibaba,並推出首個預覽版。Spring Cloud 自己是一套微服務規範,並非一個拿來便可用的框架,而 Spring Cloud Alibaba 的開源爲開發者們提供了這套規範的實現方式。同時,Spring Cloud Alibaba 提供的完整的微服務組件、中文文檔和本地化的開源服務提升了開發者們接入微服務的速率,並下降了後續的運維難度。
通過一年多的孵化,Spring Cloud Alibaba 做爲 Spring 社區的惟一一個國產開源項目,正式從 Spring Cloud Incubator 孵化器畢業,併發布了適配 Spring Cloud Edgware、Finchley、Greenwich 三個版本的新版本。
Spring Cloud Alibaba 是 Spring 社區第一個也是惟一一個國產開源項目。git
(官方畢業公告參考 Spring Blog: https://spring.io/blog/2019/07/24/simplifying-the-spring-cloud-release-train)github
Spring Cloud Alibaba 畢業後從孵化器倉庫遷移到了 Github Alibaba 倉庫下,新的倉庫地址點擊這裏。 新的 maven 座標以下:web
<dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2.1.0.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency>
Spring Cloud Alibaba 各版本兼容表:spring
Spring Cloud 是 Spring 社區打造出的一款基於 Spring Boot 用於快速構建分佈式系統的框架,主要包括如下特性:數據庫
Spring Cloud Alibaba 是阿里巴巴開源中間件跟 Spring Cloud 體系的融合:編程
做爲 Spring Cloud 體系下的新實現,Spring Cloud Alibaba 跟官方的組件或其它的第三方實現如 Netflix, Consul,Zookeeper 等對比,具有了更多的功能:json
依託 Spring Cloud Alibaba,只須要添加一些註解和少許配置,就能夠將 Spring Cloud 應用接入阿里微服務解決方案,經過阿里中間件來迅速搭建分佈式應用系統。架構
服務註冊 & 配置管理是微服務應用中必不可少的兩大基礎功能。併發
Spring Cloud Alibaba 基於 Nacos 提供 spring-cloud-alibaba-starter-nacos-discovery & spring-cloud-alibaba-starter-nacos-config 實現了服務註冊 & 配置管理功能。app
依靠 @EnableDiscoveryClient 進行服務的註冊,兼容 RestTemplate & OpenFeign 的客戶端進行服務調用。
OpenFeign 客戶端:
@FeignClient(name = "echo-service") public interface EchoService { @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET) String echo(@PathVariable("str") String str); }
RestTemplate 客戶端:
@LoadBalanced @Bean public RestTemplate restTemplate1() { return new RestTemplate(); } .... restTemplate.getForObject("http://echo-service/echo/hello-spring-cloud-alibaba", String.class);
配置管理直接以約定俗成的方式構造 dataId & Group 從 Nacos 讀取配置設置到 Spring Environment 中便可。
流控降級是微服務穩定性的法寶,用於解決各類不穩定的場景可能會致使的嚴重後果。
在限流層面:咱們根據系統的處理能力對流量進行控制,在保證系統吞吐量比較高的同時又不會把系統打垮。
在降級層面:在服務出現不穩定因素的時候暫時切斷服務的調用,等待一段時間再進行嘗試。一方面防止給不穩定服務「雪上加霜」,另外一方面保護服務的調用方不被拖垮。
Spring Cloud Alibaba 基於 Sentinel 提供 spring-cloud-alibaba-starter-sentinel 對 Spring 體系內基本全部的客戶端,網關進行了適配,包括了 WebServlet, WebFlux, RestTemplate, OpenFeign, Netflix Zuul, Spring Cloud Gateway。
只需引入 starter,便可對這些組件生效。只需定義規則進行流控降級:
限流規則:
降級規則:
Spring Cloud 默認的服務調用依賴 OpenFeign 或 RestTemplate 使用 REST 進行調用。
Spring Cloud Alibaba Dubbo 只需使用 @DubboTransported 註解便可將底層的 Rest 協議無縫切換成 Dubbo RPC 協議,進行 RPC 調用。這一舉措讓 Spring Cloud 的客戶端調用多了一個新的 RPC 選擇:
@Bean @LoadBalanced @DubboTransported public RestTemplate restTemplate() { return new RestTemplate(); } @FeignClient("dubbo-provider") @DubboTransported(protocol = "dubbo") public interface DubboFeignRestService { @GetMapping(value = "/param") String param(@RequestParam("param") String param); @PostMapping("/saveB") String saveB(@RequestParam("a") int a, @RequestParam("b") String b); }
基於 Spring Cloud Stream 提供 Binder 的新實現: Spring Cloud Stream RocketMQ Binder,也新增了 Spring Cloud Bus 消息總線的新實現 Spring Cloud Bus RocketMQ。
Spring Cloud Stream 對消息的編程模型進行了統一封裝,用同一套代碼進行消息的發送/接收,屏蔽底層消息中間件的實現細節,讓咱們在關注點放到業務邏輯上。若是想要更換底層消息中間件,直接引入 Binder 的新實現便可,代碼層面幾乎無需修改。這是一段使用 Spring Cloud Stream RocketMQ & Spring Cloud Function 完成消息的接收,並轉換成大寫 & 添加 alibaba- 前綴的新消息,再發送給新的 topic 的過程:
spring.cloud.stream.bindings.input.destination=test-topic spring.cloud.stream.bindings.input.group=test-group1 spring.cloud.stream.bindings.output.destination=test-topic-uppercase spring.cloud.stream.function.definition=uppercase|addprefix
@SpringBootApplication @EnableBinding(Processor.class) public class FunctionApplication { public static void main(String[] args) { new SpringApplicationBuilder(FunctionApplication.class) .web(WebApplicationType.NONE).run(args); } @Bean public Function<String, String> uppercase() { return x -> x.toUpperCase(); } @Bean public Function<String, String> addprefix() { return x -> "alibaba-" + x; } }
Seata 是 阿里巴巴 開源的 分佈式事務中間件,以 高效 而且對業務 0 侵入 的方式,解決 微服務 場景下面臨的分佈式事務問題。
在 Spring Cloud 這一層面,咱們經過在微服務中傳遞事務上下文的方式完成了 Seata 在 Spring Cloud 層面的接入:
@GlobalTransactional(timeoutMills = 300000, name = "spring-cloud-demo-tx") @RequestMapping(value = "/seata/rest", method = RequestMethod.GET, produces = "application/json") public String rest() { // 調用 storage 服務。涉及數據庫操做 String result = restTemplate.getForObject( "http://127.0.0.1:18082/storage/" + COMMODITY_CODE + "/" + ORDER_COUNT, String.class); if (!SUCCESS.equals(result)) { throw new RuntimeException(); } String url = "http://127.0.0.1:18083/order"; HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED); MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>(); map.add("userId", USER_ID); map.add("commodityCode", COMMODITY_CODE); map.add("orderCount", ORDER_COUNT + ""); HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>( map, headers); // 調用 order 服務。涉及數據庫操做 ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class); result = response.getBody(); if (!SUCCESS.equals(result)) { throw new RuntimeException(); } return SUCCESS; }
Spring Boot Admin 是一個開源社區項目,用於管理和監控 SpringBoot 應用程序。可是它沒有跟 Spring Cloud 作深度的整合。咱們但願作一個 Spring Cloud Admin,它能提供以下功能:
若是對這些內容敢興趣,歡迎來 github issue 留言。
Spring Cloud Alibaba 畢業並不意味着結束,而是一個新的開始。後續咱們還有會一系列動做:
本文爲雲棲社區原創內容,未經容許不得轉載。