在使用微服務中需要註冊中心、配置中心和API Gateway,在每一個微服務上線都需要在註冊中心註冊,這樣如果商品服務要使用到訂單服務時,就會去註冊中心找需要的API,而配置中心能夠集中管理微服務的設定,假設有十個都需要修改設定檔的某個參數,這樣只要在在配置中修改即可,服務都將參考那份文件。

而這幾個東西在Spring Cloud中,有幾個代表性的元件,參考官方連結中,註冊中心和API Gateway有Netflix的Eureka、Zuul,配置中心有Spring Cloud Config集中管理,不過這些不在這專案上用,因為像是Eureka停止維護,還有些元件沒有開源,而部分環境建立時也比較複雜,所以改用Spring Cloud Alibaba[github]。

Spring Cloud Alibaba[spring.io]中替代Eureka的部分就是使用Nacos,然後使用RPC的部分則是使用Spring Cloud Feign,不過這邊是使用Open Feign,因為Spring Cloud Netflix原先的Feign不開放了,所以Spring Cloud另外開了一個Spring Cloud OpenFeign,這部分在建立專案時已加入,還有負載平衡使用Spring Cloud Netflix中的Ribbon,容斷機制使用alibaba的Sentinel,API Gateway使用Spring Cloud Gateway,還有在打API的一些監控和整合Zipkin使用Spring Cloud Sleuth,還有要處理分散式的Transaction使用Alibaba的Seata

所以總結一下使用的東西

  • SpringCloud Alibaba – Nacos : 註冊中心(Service registration and discovery)
  • SpringCloud Alibaba – Nacos : 配置中心(Distributed Configuration)
  • SpringCloud Netflix – Ribbon : 負載平衡(Client Side Load Balancing)
  • SpringCloud – OpenFeign : 宣告式的Http客戶端(Declarative REST Client)
  • SpringCloud Alibaba – Sentinel : 容錯機制(Flow control and service degradation)
  • SpringCloud – Gateway : API 閘道(Able to match routes on any request attribute)
  • SpringCloud – Sleuth : 流程監控(auto-configuration for distributed tracing)
  • SpringCloud Alibaba – Seata : 處理分散式交易(distributed transaction solution)