Spring Cloud微服務升級總結

一.應用系統的架構歷史

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-7f1a6c86fcda9550.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-7f1a6c86fcda9550.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

二.什么是微服務?

2.1 微服務概述

  • 起源:微服務的概念源于 2014 年 3 月 Martin Fowler 所寫的一篇文章“Microservices”。文中內容提到:微服務架構是一種架構模式,它提倡將單一應用程序劃分成一組小的服務,服務之間互相協調、互相配合,為用戶提供最終價值。
  • 通信方式:每個服務運行在其獨立的進程中,服務與服務間采用輕量級的通信機制互相溝通(通常是基于 HTTP 的 RESTful API)。
  • 微服務的常規定義:微服務是一種架構風格,一個大型復雜軟件應用由一個或多個微服務組成。系統中的各個微服務可被獨立部署,各個微服務之間是松耦合的。每個微服務僅關注于完成一件任務。
  • 把原來的一個完整的進程服務,拆分成兩個或兩個以上的進程服務,且互相之間存在調用關系,與原先單一的進程服務相比,就是“微服務”。(微服務是一個比較級的概念,而不是單一的概念)

2.2 微服務架構的優勢

  • 可擴展性:在增加業務功能時,單一應用架構需要在原先架構的代碼基礎上做比較大的調整,而微服務架構只需要增加新的微服務節點,并調整與之有關聯的微服務節點即可。在增加業務響應能力時,單一架構需要進行整體擴容,而微服務架構僅需要擴容響應能力不足的微服務節點。
  • 容錯性:在系統發生故障時,單一應用架構需要進行整個系統的修復,涉及到代碼的變更和應用的啟停,而微服務架構僅僅需要針對有問題的服務進行代碼的變更和服務的啟停。其他服務可通過重試、熔斷等機制實現應用層面的容錯。
  • 技術選型靈活:微服務架構下,每個微服務節點可以根據完成需求功能的不同,自由選擇最適合的技術棧,即使對單一的微服務節點進行重構,成本也非常低。
  • 開發運維效率更高:每個微服務節點都是一個單一進程,都專注于單一功能,并通過定義良好的接口清晰表述服務邊界。由于體積小、復雜度低,每個微服務可由一個小規模團隊或者個人完全掌控,易于保持高可維護性和開發效率。
  • Spring Cloud作為今年最流行的微服務開發框架,不是采用了Spring Cloud框架就實現了微服務架構,具備了微服務架構的優勢。正確的理解是使用Spring Cloud框架開發微服務架構的系統,使系統具備微服務架構的優勢(Spring Cloud就像工具,還需要“做”的過程)。

三.Spring Boot/Cloud

3.1 什么是Spring Boot?

Spring Boot框架是由Pivotal團隊提供的全新框架,其設計目的是用來簡化基于Spring應用的初始搭建以及開發過程。SpringBoot框架使用了特定的方式來進行應用系統的配置,從而使開發人 員不再需要耗費大量精力去定義模板化的配置文件。

3.2 什么是Spring Cloud?

Spring Cloud是一個基于Spring Boot實現的云應用開發工具,它為基于JVM的云應用開發中的配置管理、服務注冊,服務發現、斷路器、智能路由、微代理、控制總線、全局鎖、決策競選、分布式會話和集群狀態管理等操作提供了一種簡單的開發方式。

3.3 微服務,Spring Boot,Spring Cloud三者之間的關系

  • 思想:微服務是一種架構的理念,提出了微服務的設計原則,從理論為具體的技術落地提供了指導思想。
  • 腳手架:Spring Boot是一套快速配置腳手架,可以基于Spring Boot快速開發單個微服務。
  • 多個組件的集合:Spring Cloud是一個基于Spring Boot實現的服務治理工具包;Spring Boot專注于快速、方便集成的單個微服務個體;Spring Cloud關注全局的服務治理框架。

3.4 Everything is jar, Everything is http

Spring Boot通過@SpringBootApplication注解標識為Spring Boot應用程序。所有的應用都通過jar包方式編譯,部署和運行.

@SpringBootApplication
public class Application {
       private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);  
       public static void main(String[] args) {     
           SpringApplication.run(Application.class, args);       
           LOGGER.info(”啟動成功!");
       }?
   }

每個Spring Boot的應用都可以通過內嵌web容器的方式提供http服務,僅僅需要在pom文件中依賴spring-boot-start-web即可,原則上微服務架構希望每個獨立節點都提供http服務。

<dependency>
   <groupId>org.springframework.boot</groupId> 
   <artifactId>spring-boot-starter-web</artifactId>
</dependency>

3.5 Spring boot Task 任務啟動和定時任務

在Spring Boot需要啟動任務時,只要繼承CommandLineRunner接口實現其run方法即可。

<dependency>
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

在Spring Boot需要執行定時任務時,只需要在定時任務方法上增加@Scheduled(cron = “0 15 0 **?”)注解(支持標準cron表達式),并且在服務啟動類上增加@EnableScheduling的注解即可。

@SpringBootApplication
@EnableScheduling
public class Application {
    private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);    
    public static void main(String[] args) {        
        SpringApplication.run(Application.class, args);       
        LOGGER.info(”啟動成功!");  
    }
}

// some class
@Scheduled(cron = "0 15 0 * * ?")
public void someTimeTask() {
  //
}
}

3.6 Spring boot Actuator 監控

Actuator是spring boot提供的對應用系統自身進行監控的組件,在引入spring-boot-start-web基礎上引入spring-boot-starter-actuator即可。

<dependency>   
<groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-d9936c91c8488913.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-d9936c91c8488913.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

3.7 Spring cloud Config 配置中心

在我們實現微服務架構時,每個微服務節點都需要自身的相關配置數據項,當節點眾多,維護就變得非常困難,因此需要建立一個中心配置服務。

Spring Cloud Config分為兩部分。Spring Cloud Config server作為一個服務進程,Spring Cloud Config File為配置文件存放位置。

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-81ce721b577462c0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-81ce721b577462c0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

[圖片上傳失敗...(image-bfc9bd-1511489698023)]

3.8 Spring cloud Eureka 服務注冊中心

服務注冊的概念早在微服務架構之前就出現了,微服務架構更是把原先的單一應用節點拆分成非常多的微服務節點。互相之間的調用關系會非常復雜,Spring Cloud Eureka作為注冊中心,

服務注冊的概念早在微服務架構之前就出現了,微服務架構更是把原先的單一應用節點拆分成非常多的微服務節點。互相之間的調用關系會非常復雜,Spring Cloud Eureka作為注冊中心,
所有的微服務都可以將自身注冊到Spring Cloud Eureka進行統一的管理和訪問(Eureka和Zookeeper不同,在AOP原則中選擇了OP,更強調服務的有效性)

服務注冊的概念早在微服務架構之前就出現了,微服務架構更是把原先的單一應用節點拆分成非常多的微服務節點。互相之間的調用關系會非常復雜,Spring Cloud Eureka作為注冊中心,
所有的微服務都可以將自身注冊到Spring Cloud Eureka進行統一的管理和訪問(Eureka和Zookeeper不同,在AOP原則中選擇了OP,更強調服務的有效性)

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-dc42d5c33edcb8ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-dc42d5c33edcb8ce.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-b273d83f2293370b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-b273d83f2293370b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

3.9 Spring cloud Zuul 服務端智能路由

當我們把所有的服務都注冊到Eureka(服務注冊中心)以后,就涉及到如何調用的問題。Spring Cloud Zuul是Spring Cloud提供的服務端代理組件,可以看做是網關,Zuul通過Eureka獲取到可用的服務,通過映射配置,客戶端通過訪問Zuul來訪問實際需要需要訪問的服務。所有的服務通spring.application.name做標識,不同IP地址,相同spring.application.name就是一個服務集群。當我們增加一個相同spring.application.name的節點,Zuul通過和Eureka通信獲取新增節點的信息實現智能路由,增加該類型服務的響應能力。

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-daf71f3a3e8c2587.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-daf71f3a3e8c2587.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

3.10 Spring cloud Ribbon 客戶端智能路由

與Spring Cloud Zuul的服務端代理相對應,Spring Cloud Ribbon提供了客戶端代理。在服務端代理中,客戶端并不需要知道最終是哪個微服務節點為之提供服務,而客戶端代理獲取實質提供服務的節點,并選擇一個進行服務調用。Ribbon和Zuul相似,也是通過和Eureka(服務注冊中心)進行通信來實現客戶端智能路由。

3.11 Spring cloud Sleuth 分布式追蹤

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-6b072bdd33cd6c91.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-6b072bdd33cd6c91.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

2.12 Spring cloud Zipkin 調用鏈

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-89c49201ca222583.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-89c49201ca222583.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-df819f7b462bdb64.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-df819f7b462bdb64.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

3.13 Spring cloud Feign http客戶端

Spring Cloud Feign是一種聲明式、模板化的http客戶端。 使用Spring Cloud Feign請求遠程服務時能夠像調用本地方法一樣,讓開發者感覺不到這是遠程方法(Feign集成了Ribbon做負載均衡)。

1.把遠程服務和本地服務做映射

@FeignClient(name = "rabbitmq-http", url = "${SKYTRAIN_RABBITMQ_HTTP}")?
   public interface TaskService {?    
       @RequestMapping(value = "/api/queues", method = RequestMethod.GET)?    
       public String query(@RequestHeader("Authorization") String token);?
   }
  1. 以調用本地服務的方式調用遠程服務
@Autowired?
    private TaskService taskService;?
    private String queryRabbitmqStringInfo() {?    
        byte[] credentials = Base64?.encodeBase64((rabbitmqHttpUserName + ":" + rabbitmqHttpPassword).getBytes(StandardCharsets.UTF_8));?    
        String token = "Basic " + new String(credentials, StandardCharsets.UTF_8);?    
        return taskService.query(token);?
    }

3.13 Spring cloud Hystrix 斷路器

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-d1f1a7703d5070ad.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-d1f1a7703d5070ad.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

四 自研組件

4.1 我們開發的幾個微服務組件—應用管理中心

應用管理中心可以對每個已經注冊的微服務節點進行停止,編譯,打包,部署,啟動的完整的上線操作。

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-10c2e89fd80797a8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-10c2e89fd80797a8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

4.2 我們開發的幾個微服務組件—zookeeper數據查詢中心

zookeeper數據查詢中心根據zookeeper地址,端口,命令獲取zookeeper數據信息。

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-ef75600ea443cb6e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-ef75600ea443cb6e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

4.3 我們開發的幾個微服務組件—微服務健康檢測中心

健康檢測中心周期性檢查每個微服務的狀態,當發現有微服務狀態處于DOWN或連接超時時,觸發報警

健康檢測中心周期性檢查每個微服務的狀態,當發現有微服務狀態處于DOWN或連接超時時,觸發報警

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-a7020b76eb7ff6f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-a7020b76eb7ff6f7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

4.4 我們開發的幾個微服務組件—定時任務查詢中心

// 在BeanPostProcessor子類中攔截
   @Component
   public class SkytrainBeanPostProcessor implements BeanPostProcessor, Ordered {
       ***
       /**
        * Bean 實例化之后進行的處理
        */
       public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
   
           beanPostProcessor.postProcessAfter(bean, beanName);
           return bean;
       }
   
   }
   
   // 攔截后獲取定時任務注解
   public Object postProcessAfter(Object bean, String beanName) {
       Class<?> targetClass = AopUtils.getTargetClass(bean);
       Map<Method, Set<Scheduled>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
               new MethodIntrospector.MetadataLookup<Set<Scheduled>>() {

                   public Set<Scheduled> inspect(Method method) {

                       Set<Scheduled> scheduledMethods = AnnotatedElementUtils.getMergedRepeatableAnnotations(method,
                               Scheduled.class, Schedules.class);
                       return (!scheduledMethods.isEmpty() ? scheduledMethods : null);
                   }
               });
       if (!annotatedMethods.isEmpty()) {
           String className = targetClass.getName();
           for (Map.Entry<Method, Set<Scheduled>> entry : annotatedMethods.entrySet()) {
               Method method = entry.getKey();
               for (Scheduled scheduled : entry.getValue()) {
                   String key = className + ":" + method.getName();
                   String value = scheduled.toString();
                   taskInfos.put(key, value);
               }
           }
       }
       return null;
   }
   
   // 獲取定時任務后注冊
   public void taskRegister() {
       String nodeInfo = ipAddress + ":" + serverPort + ":";
       try {
           /**
            * 定時任務
            */
           Map<String, String> infos = taskInfos;
           for (Entry<String, String> item : infos.entrySet()) {
               String taskId = nodeInfo + item.getKey();
               String taskParameter = item.getValue();
               JSONObject info = new JSONObject();
               info.put("taskId", taskId);
               info.put("taskParameter", taskParameter);
               info.put("applicationName", applicationName);
               info.put("taskType", "schedule");
               LOGGER.info(info.toString());
               zooKeeperExecutor.createZKNode(SKYTRAIN_TASK_ZKNODE_PREFIX + taskId, info.toString());
           }
       }
       catch (Exception ex) {
           LOGGER.error("", ex);
       }
   }

4.5 微服務的分類

  • 微服務平臺組件
  • 公共服務組件
  • 基礎服務組件/業務服務組件

4.6 整體微服務架構圖

[
Image tex](http://upload-images.jianshu.io/upload_images/2830896-27e0f7bdc9506df4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
Image tex](http://upload-images.jianshu.io/upload_images/2830896-27e0f7bdc9506df4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

原文作者:宜信-技術研發中心-高級架構師-梁鑫

點我獲取阿里云優惠券

我的官網


我的博客

我的官網http://guan2ye.com
我的CSDN地址http://blog.csdn.net/chenjianandiyi
我的簡書地址http://www.lxweimin.com/u/9b5d1921ce34
我的githubhttps://github.com/javanan
我的碼云地址https://gitee.com/jamen/
阿里云優惠券https://promotion.aliyun.com/ntms/act/ambassador/sharetouser.html?userCode=vf2b5zld&utm_source=vf2b5zld

阿里云教程系列網站http://aliyun.guan2ye.com

1.png

我的開源項目spring boot 搭建的一個企業級快速開發腳手架

1.jpg
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,836評論 6 540
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,275評論 3 428
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 177,904評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,633評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,368評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,736評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,740評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,919評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,481評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,235評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,427評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,968評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,656評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,055評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,348評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,160評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,380評論 2 379

推薦閱讀更多精彩內容