這篇文章主要介紹了spring cloud升級(jí)到spring boot 2.x/Finchley.RELEASE遇到的坑有哪些,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)主營(yíng)豐林網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營(yíng)網(wǎng)站建設(shè)方案,APP應(yīng)用開(kāi)發(fā),豐林h5微信小程序搭建,豐林網(wǎng)站營(yíng)銷推廣歡迎豐林等地區(qū)企業(yè)咨詢
spring boot2.x已經(jīng)出來(lái)好一陣了,而且spring cloud 的最新Release版本Finchley.RELEASE,默認(rèn)集成的就是spring boot 2.x,這幾天將一個(gè)舊項(xiàng)目嘗試著從低版本升級(jí)到 2.x,踩坑無(wú)數(shù),記錄一下:
顯著變化:
與 Spring Boot 2.0.x 兼容
不支持 Spring Boot 1.5.x
最低要求 Java 8
新增 Spring Cloud Function 和 Spring Cloud Gateway
一、gradle的問(wèn)題
spring boot 2.x 要求gradle版本不能太舊,先把gradle升級(jí)到4.6版本,然后編譯,各種問(wèn)題,到gradle官網(wǎng)上查了下,build.gradle有幾個(gè)小地方要調(diào)整
1.1 java-libary 的項(xiàng)目
即:純工具包這種公用jar,plugins{}必須放在第1行(有buildscript的除外),類似:
plugins { id 'java-library' }
然后按官網(wǎng)的教程,compile最好換成implementation
dependencies { implementation( ... ) }
1.2 常規(guī)java項(xiàng)目(指帶容器能獨(dú)立運(yùn)行的項(xiàng)目)
buildscript { ext { springBootVersion = '2.0.1.RELEASE' } repositories { maven { url "http://maven.aliyun.com/nexus/content/groups/public/" } ... } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' dependencyManagement { imports { mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.RELEASE' } }
...
另外,gradle 高版本編譯時(shí),總會(huì)有一行討厭的提示:
Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
編譯時(shí),可以加參數(shù):--warning-mode=none 禁止掉,即類似:
gradle build --warning-mode=none -x test
二、依賴jar包版本的問(wèn)題
dependencies { ... implementation( ... 'org.springframework.cloud:spring-cloud-starter-consul-discovery', 'org.springframework.cloud:spring-cloud-starter-consul-config', 'org.springframework.cloud:spring-cloud-starter-bus-kafka', 'org.springframework.cloud:spring-cloud-starter-sleuth', 'org.springframework.cloud:spring-cloud-sleuth-stream:1.3.4.RELEASE', 'org.springframework.cloud:spring-cloud-starter-hystrix:1.4.4.RELEASE', 'org.springframework.cloud:spring-cloud-netflix-hystrix-stream', 'org.springframework.boot:spring-boot-starter-actuator', 'org.springframework.boot:spring-boot-starter-undertow', 'org.springframework.boot:spring-boot-starter-mail', 'org.springframework.boot:spring-boot-starter-jdbc', 'org.springframework.boot:spring-boot-starter-security', 'org.slf4j:slf4j-api:1.7.25', 'ch.qos.logback:logback-core:1.2.3', 'org.thymeleaf:thymeleaf-spring5:3.0.9.RELEASE', 'org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.1', 'tk.mybatis:mapper-spring-boot-starter:1.2.4', 'com.github.pagehelper:pagehelper-spring-boot-starter:1.2.3' ) implementation('com.alibaba:druid:1.1.9') { exclude group: "com.alibaba", module: "jconsole" exclude group: "com.alibaba", module: "tools" } implementation('org.springframework.boot:spring-boot-starter-web') { exclude module: "spring-boot-starter-tomcat" exclude module: "spring-boot-starter-jetty" } testCompile 'org.springframework.boot:spring-boot-starter-test' }
其中
'org.springframework.cloud:spring-cloud-sleuth-stream:1.3.4.RELEASE',
'org.springframework.cloud:spring-cloud-starter-hystrix:1.4.4.RELEASE',
這二項(xiàng)必須指定版本號(hào),否則編譯不過(guò)。(應(yīng)該最新的2.x版本的jar包,還沒(méi)上傳到中央倉(cāng)庫(kù),無(wú)法自動(dòng)識(shí)別依賴),另外pagehelper這個(gè)常用的分頁(yè)組件,也建議按上面的版本來(lái)配置,否則運(yùn)行時(shí),可能會(huì)報(bào)錯(cuò)。
三、log4j/log4j2的問(wèn)題
升級(jí)到spring boot 2.x后,不管是配置log4j還是log4j2,運(yùn)行時(shí)總是報(bào)堆棧溢出的error,換成logback后,啟動(dòng)正常,建議大家盡量采用默認(rèn)的logback,依賴項(xiàng)的配置參考上面的。
四、DataSourceBuilder類找不到的問(wèn)題
spring boot 2.x把這個(gè)類換了package,所以找不到了,詳情見(jiàn):
https://stackoverflow.com/questions/50011577/spring-boot-2-0-0-datasourcebuilder-not-found-in-autoconfigure-jar
https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/jdbc/DataSourceBuilder.html
解決辦法就是引用:org.springframework.boot:spring-boot-starter-jdbc
同時(shí)修改代碼import新的package: org.springframework.boot.jdbc.DataSourceBuilder
五、安全性的問(wèn)題
spring boot 2.x加強(qiáng)了安全性,不管訪問(wèn)什么rest url,默認(rèn)都要求登錄,在application.yml里無(wú)法通過(guò)配置關(guān)閉,只能寫代碼調(diào)整:
import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests() .anyRequest() .permitAll() .and() .csrf() .disable(); } }
這樣,默認(rèn)所有url都允許訪問(wèn)(如果是暴露在外網(wǎng)的服務(wù),請(qǐng)慎用)
六、各類actuator監(jiān)控endpoint的路徑變化
spring boot 2.x 里,actuator的endpoint默認(rèn)路徑變成/actuator開(kāi)頭,如果要使用以前的風(fēng)格,放在/根下,可以在applicatino.yml里參考下面的配置:
management: ... endpoints: web: base-path: / exposure: include: "*"
另外/health節(jié)點(diǎn),默認(rèn)情況下,只能輸出很少的信息,詳細(xì)信息,需要通過(guò)配置打開(kāi)
management: ... endpoint: health: show-details: always ...
七、${spring.cloud.client.ipAddress} 無(wú)法識(shí)別
spring cloud 2.x里,${spring.cloud.client.ipAddress} 這個(gè)寫法不識(shí)別,一啟動(dòng)就會(huì)報(bào)錯(cuò),嘗試了多次,無(wú)意發(fā)現(xiàn),把A改成小寫,居然可以了:
spring: ... application: name: sr-menu-service:${spring.cloud.client.ipaddress}
感覺(jué)這應(yīng)該是個(gè)bug,新版本里估計(jì)會(huì)修復(fù)。
八、MetricWriter、SystemPublicMetrics類找不到的問(wèn)題
spring boot 2.x里metrics默認(rèn)換成了micrometer,原來(lái)的MetricWriter之類的全干掉了,詳情參考官網(wǎng)文檔
management: metrics: export: statsd: host: 10.0.*.* port: 8125 flavor: etsy
上面的配置是啟用statsd,然后跑起來(lái)就能看到效果,見(jiàn)下圖
但是與spring boot 1.x相比,并不會(huì)直接輸出具體值,要看具體值,可以用http://localhost:8001/metrics/jvm.memory.used
這其中的VALUE中是jvm占用的內(nèi)存(包括heap + noheap),如果只想看heap區(qū)(即:堆上的內(nèi)存),可以用
http://localhost:8001/metrics/jvm.memory.used?tag=area:heap
同時(shí)在grafana里也能看到效果:
注:目前statsd里的前綴無(wú)法修改,代碼寫死的statsd
如果一臺(tái)機(jī)器上部署多個(gè)spring cloud 微服務(wù),grafana里就分不出來(lái)了(個(gè)人估計(jì)以后會(huì)改進(jìn))。
另外,如果希望通過(guò)代碼獲取這些metrics里具體指標(biāo)值,可以參考下面的代碼:
import io.micrometer.core.instrument.Meter; import io.micrometer.core.instrument.MeterRegistry; import io.micrometer.core.instrument.Statistic; import org.junit.Test; import org.springframework.beans.factory.annotation.Autowired; import java.util.LinkedHashMap; import java.util.Map; import java.util.function.BiFunction; public class MetricsTest extends BaseTest { private String METRIC_MSG_FORMAT = "Metric >> %s = %d"; @Autowired private MeterRegistry meterRegistry; @Test public void testStatsdConfig() { String metric = "jvm.memory.used"; Meter meter = meterRegistry.find(metric).meter(); Mapstats = getSamples(meter); logger.info(String.format(METRIC_MSG_FORMAT, metric, stats.get(Statistic.VALUE).longValue())); } private Map getSamples(Meter meter) { Map samples = new LinkedHashMap<>(); mergeMeasurements(samples, meter); return samples; } private void mergeMeasurements(Map samples, Meter meter) { meter.measure().forEach((measurement) -> samples.merge(measurement.getStatistic(), measurement.getValue(), mergeFunction(measurement.getStatistic()))); } private BiFunction mergeFunction(Statistic statistic) { return Statistic.MAX.equals(statistic) ? Double::max : Double::sum; } }
九、swagger里WebMvcConfigurerAdapter過(guò)時(shí)的問(wèn)題
WebMvcConfigurerAdapter這個(gè)類在最新的spring boot里已經(jīng)被標(biāo)識(shí)為過(guò)時(shí),正常用法參考以下:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * @author yangjunming * @date 13/10/2017 */ @Configuration @EnableSwagger2 public class SwaggerConfig extends WebMvcConfigurationSupport { @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("sr.service.menu.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("menu-service online api document") .description("測(cè)試服務(wù)") .contact(new Contact("菩提樹(shù)下的楊過(guò)", "http://yjmyzz.cnblogs.com/", "yjmyzz@126.com")) .version("1.0.0") .build(); } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“spring cloud升級(jí)到spring boot 2.x/Finchley.RELEASE遇到的坑有哪些”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!