本系列示例與膠水代碼地址: https://github.com/HashZhang/spring-cloud-scaffoldreact
Spring Cloud仍是比較活躍的,更新一直很快。我通常考慮最新版本SR2發佈以後,再考慮升級(通常SR1還有SR2會有一些新老框架的兼容性升級)。並且因爲須要咱們線上穩定,結合咱們的發佈週期來看,跳一個大版本升級是一個更好的選擇(也就是一年作一次大版本升級)。例如咱們以前的升級路線就是:Brixton -> Daltson -> Finchley -> 當前的Hoxtongit
作了這麼屢次升級,感受能夠出這個系列,來分享咱們項目使用Spring cloud
框架實現的框架功能,在升級中遇到的坑,以及如何升級等等。每一個版本都會有實例代碼,並在上一版本實現的功能基礎上,實現更多更實用的功能。全部示例代碼都在開頭提到的項目中,每一個版本系列的最後,還會附上功能測試流程。github
在Hoxton版本Release的同時,Spring Cloud也宣佈,其中的這些項目,已經進入維護模式(再也不開發新功能),用戶最好作以下的替換:web
能夠看出,Spring Cloud netflix中的zuul, ribbon, hystrix都基本上算是廢了,咱們也能夠拋棄掉Sprnig Cloud Netflix了。還有一個體系也在官方中,就是Spring Cloud Alibaba,可是經過Spring Cloud netflix這件事,我我的感受這種依賴性質的膠水項目,最好仍是咱們架構組本身維護,這塊是比較容易有坑的,本身維護本身用更新起來更高效,並且不會有粘合的項目都不更新了替換起來要人命的代價。算法
Spring Cloud Hoxton,至少對於官方文檔來講,是一個里程碑式的變化。官方文檔終於將全部項目的文檔分開了,而且作了比較多的整理,能夠看出,這個Hoxton必定是有人下定決心要作一個變革了。而且,Spring Cloud在這個版本引入了更多的虛擬化,雲原生依賴,例如Spring-Cloud-kubernetes,確實,有些服務發現,調用策略什麼的,Spring Cloud和k8s體系重複了,這個依賴可使咱們靈活地切換這些功能到底交給誰來作,期待這個項目的完善成熟。spring
這篇文章,會主要列出升級步驟與詳細說明,以及對應的源代碼,和實現的功能。以及如何替換Spring Cloud Netflix體系爲新的組件。apache
之前的體系:api
實現的功能:緩存
metamap
中的zone
配置,來區分不一樣集羣的實例。以前經過Ribbon
的配置ServerListFilter
實現,使用com.netflix.loadbalancer.ZoneAffinityServerListFilter
做爲ServerListFilter
,參考:Spring cloud實現FeignClient指定Zone調用spring-retry
重試經過ribbon配置實現的。參考:Spring Cloud Finchley OpenFeign的重試配置相關的坑Hystrix
配置實現hystrix.threadpool.default.coreSize=50
Ribbon
的配置LoadBalancerRule
實現,使用com.netflix.loadbalancer.AvailabilityFilteringRule
做爲LoadBalancerRule
。參考:Ribbon的AvailabilityFilteringRule的坑之前的體系:tomcat
實現的功能:
spring-retry
重試經過ribbon配置實現的。Hystrix
配置實現hystrix.threadpool.default.coreSize=50
Ribbon
的配置LoadBalancerRule
實現,使用com.netflix.loadbalancer.AvailabilityFilteringRule
做爲LoadBalancerRule
。參考:Ribbon的AvailabilityFilteringRule的坑實現的功能:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.7.RELEASE</version> </parent> <properties> <disruptor.version>3.4.2</disruptor.version> <resilience4j-spring-cloud2.version>1.1.0</resilience4j-spring-cloud2.version> </properties> <dependencies> <!--內部緩存框架統一採用caffeine--> <!--這樣Spring cloud loadbalancer用的本地實例緩存也是基於Caffeine--> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency> <!--日誌須要用log4j2--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!--lombok簡化代碼--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <!--註冊到eureka--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <!--spring cloud rpc相關--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <!--調用路徑記錄--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <!--暴露actuator相關端口--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--暴露http接口, servlet框架採用nio的undertow,注意直接內存使用,減小GC--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-spring-cloud2</artifactId> <version>${resilience4j-spring-cloud2.version}</version> </dependency> <!--log4j2異步日誌須要的依賴,全部項目都必須用log4j2和異步日誌配置--> <dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>${disruptor.version}</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <!--最好用JDK 12版本及以上編譯,11.0.7對於spring-cloud-gateway有時候編譯會有bug--> <!--雖然官網說已解決,可是11.0.7仍是偶爾會出現--> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.7.RELEASE</version> </parent> <properties> <disruptor.version>3.4.2</disruptor.version> <resilience4j-spring-cloud2.version>1.1.0</resilience4j-spring-cloud2.version> </properties> <dependencies> <!--內部緩存框架統一採用caffeine--> <!--這樣Spring cloud loadbalancer用的本地實例緩存也是基於Caffeine--> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency> <!--日誌須要用log4j2--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!--lombok簡化代碼--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-gateway</artifactId> </dependency> <!--調用路徑記錄--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <!--暴露actuator相關端口--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.github.resilience4j</groupId> <artifactId>resilience4j-spring-cloud2</artifactId> <version>${resilience4j-spring-cloud2.version}</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId> </dependency> <!--log4j2異步日誌須要的依賴,全部項目都必須用log4j2和異步日誌配置--> <dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>${disruptor.version}</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <!--最好用JDK 12版本及以上編譯,11.0.7對於spring-cloud-gateway有時候編譯會有bug--> <!--雖然官網說已解決,可是11.0.7仍是偶爾會出現--> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.7.RELEASE</version> </parent> <properties> <disruptor.version>3.4.2</disruptor.version> </properties> <dependencies> <!--內部緩存框架統一採用caffeine--> <!--這樣Spring cloud loadbalancer用的本地實例緩存也是基於Caffeine--> <dependency> <groupId>com.github.ben-manes.caffeine</groupId> <artifactId>caffeine</artifactId> </dependency> <!--日誌須要用log4j2--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <!--lombok簡化代碼--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <!--調用路徑記錄--> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency> <!--暴露actuator相關端口--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!--暴露http接口, servlet框架採用nio的undertow,注意直接內存使用,減小GC--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency> <!--log4j2異步日誌須要的依賴,全部項目都必須用log4j2和異步日誌配置--> <dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>${disruptor.version}</version> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Hoxton.SR4</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.1</version> <configuration> <!--最好用JDK 12版本及以上編譯,11.0.7對於spring-cloud-gateway有時候編譯會有bug--> <!--雖然官網說已解決,可是11.0.7仍是偶爾會出現--> <source>11</source> <target>11</target> </configuration> </plugin> </plugins> </build>