spring boot2.x已经出来好一阵了,而且spring cloud 的最新Release版本Finchley.RELEASE,默认集成的就是spring boot 2.x,这几天将一个旧项目尝试着从低版本升级到 2.x,踩坑无数,记录一下:

一、gradle的问题

spring boot 2.x 要求gradle版本不能太旧,先把gradle升级到4.6版本,然后编译,各种问题,到gradle官网上查了下,build.gradle有几个小地方要调整

1.1 java-libary 的项目

即:纯工具包这种公用jar,plugins{}必须放在第1行(有buildscript的除外),类似:

plugins {
id 'java-library'
}

然后按官网的教程,compile最好换成implementation

dependencies {
implementation(
...
)
}

1.2 常规java项目(指带容器能独立运行的项目)

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 高版本编译时,总会有一行讨厌的提示:

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.

编译时,可以加参数:--warning-mode=none 禁止掉,即类似:

gradle build --warning-mode=none -x test

  

二、依赖jar包版本的问题

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',

这二项必须指定版本号,否则编译不过。(应该最新的2.x版本的jar包,还没上传到中央仓库,无法自动识别依赖),另外pagehelper这个常用的分页组件,也建议按上面的版本来配置,否则运行时,可能会报错。

三、log4j/log4j2的问题

升级到spring boot 2.x后,不管是配置log4j还是log4j2,运行时总是报堆栈溢出的error,换成logback后,启动正常,建议大家尽量采用默认的logback,依赖项的配置参考上面的。

四、DataSourceBuilder类找不到的问题

spring boot 2.x把这个类换了package,所以找不到了,详情见:

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

同时修改代码import新的package: org.springframework.boot.jdbc.DataSourceBuilder

五、安全性的问题

spring boot 2.x加强了安全性,不管访问什么rest url,默认都要求登录,在application.yml里无法通过配置关闭,只能写代码调整:

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();
}
}

这样,默认所有url都允许访问(如果是暴露在外网的服务,请慎用) 

六、各类actuator监控endpoint的路径变化

spring boot 2.x 里,actuator的endpoint默认路径变成/actuator开头,如果要使用以前的风格,放在/根下,可以在applicatino.yml里参考下面的配置:

management:
...
endpoints:
web:
base-path: /
exposure:
include: "*"

 另外/health节点,默认情况下,只能输出很少的信息,详细信息,需要通过配置打开

management:
...
endpoint:
health:
show-details: always
...

  

七、${spring.cloud.client.ipAddress} 无法识别

spring cloud 2.x里,${spring.cloud.client.ipAddress} 这个写法不识别,一启动就会报错,尝试了多次,无意发现,把A改成小写,居然可以了:

spring:
...
application:
name: sr-menu-service:${spring.cloud.client.ipaddress}

感觉这应该是个bug,新版本里估计会修复。

八、MetricWriter、SystemPublicMetrics类找不到的问题

spring boot 2.x里metrics默认换成了micrometer,原来的MetricWriter之类的全干掉了,详情参考官网文档

management:
metrics:
export:
statsd:
host: 10.0.*.*
port: 8125
flavor: etsy

上面的配置是启用statsd,然后跑起来就能看到效果,见下图

但是与spring boot 1.x相比,并不会直接输出具体值,要看具体值,可以用 http://localhost:8001/metrics/jvm.memory.used

这其中的VALUE中是jvm占用的内存(包括heap + noheap),如果只想看heap区(即:堆上的内存),可以用

http://localhost:8001/metrics/jvm.memory.used?tag=area:heap

同时在grafana里也能看到效果:

注:目前statsd里的前缀无法修改,代码写死的statsd

如果一台机器上部署多个spring cloud 微服务,grafana里就分不出来了(个人估计以后会改进)。

另外,如果希望通过代码获取这些metrics里具体指标值,可以参考下面的代码:

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();
Map<Statistic, Double> stats = getSamples(meter);
logger.info(String.format(METRIC_MSG_FORMAT, metric, stats.get(Statistic.VALUE).longValue()));
} private Map<Statistic, Double> getSamples(Meter meter) {
Map<Statistic, Double> samples = new LinkedHashMap<>();
mergeMeasurements(samples, meter);
return samples;
} private void mergeMeasurements(Map<Statistic, Double> samples, Meter meter) {
meter.measure().forEach((measurement) -> samples.merge(measurement.getStatistic(),
measurement.getValue(), mergeFunction(measurement.getStatistic())));
} private BiFunction<Double, Double, Double> mergeFunction(Statistic statistic) {
return Statistic.MAX.equals(statistic) ? Double::max : Double::sum;
}
}

 

九、swagger里WebMvcConfigurerAdapter过时的问题

WebMvcConfigurerAdapter这个类在最新的spring boot里已经被标识为过时,正常用法参考以下:

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("测试服务")
.contact(new Contact("菩提树下的杨过", "http://yjmyzz.cnblogs.com/", "yjmyzz@126.com"))
.version("1.0.0")
.build();
}
}

  

附:一些参考文档:

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide

https://spring.io/blog/2017/09/15/security-changes-in-spring-boot-2-0-m4

https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-Security-2.0

https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/htmlsingle/#production-ready-metrics-getting-started

https://github.com/pagehelper/pagehelper-spring-boot

spring cloud: 升级到spring boot 2.x/Finchley.RELEASE遇到的坑的更多相关文章

  1. Spring Cloud 升级最新 Finchley 版本,踩了所有的坑!

    Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,现在一起为项目做一次整体框架升级. 升级前 ...

  2. 【Finchley】【升级变更】Spring Cloud 升级到Finchley版本后需要注意的地方

    Spring Boot 2.x 已经发布了很久,现在 Spring Cloud 也发布了 基于 Spring Boot 2.x 的 Finchley 版本,现在一起为项目做一次整体框架升级. 升级前 ...

  3. Spring Cloud 升级最新 Finchley 版本,踩坑指南!

    https://blog.csdn.net/youanyyou/article/details/81530240 Spring Cloud 升级最新 Finchley 版本,踩了所有的坑! 2018年 ...

  4. Spring Cloud 升级最新 Greenwich 版本,舒服了~

    去年将 Spring Cloud 升级到了 Finchley 版本: Spring Cloud 升级最新 Finchley 版本,踩了所有的坑! 这个大版本栈长是踩了非常多的坑啊,帮助了不少小伙伴. ...

  5. Spring Cloud 升级之路 - 2020.0.x - 1. 背景知识、需求描述与公共依赖

    1. 背景知识.需求描述与公共依赖 1.1. 背景知识 & 需求描述 Spring Cloud 官方文档说了,它是一个完整的微服务体系,用户可以通过使用 Spring Cloud 快速搭建一个 ...

  6. Spring Cloud 升级之路 - 2020.0.x - 4. 使用 Eureka 作为注册中心

    Eureka 目前的状态:Eureka 目前 1.x 版本还在更新,但是应该不会更新新的功能了,只是对现有功能进行维护,升级并兼容所需的依赖. Eureka 2.x 已经胎死腹中了.但是,这也不代表 ...

  7. Spring Cloud学习笔记--Spring Boot初次搭建

    1. Spring Boot简介 初次接触Spring的时候,我感觉这是一个很难接触的框架,因为其庞杂的配置文件,我最不喜欢的就是xml文件,这种文件的可读性很不好.所以很久以来我的Spring学习都 ...

  8. Spring Cloud 升级之路 - 2020.0.x - 6. 使用 Spring Cloud LoadBalancer (1)

    本项目代码地址:https://github.com/HashZhang/spring-cloud-scaffold/tree/master/spring-cloud-iiford 我们使用 Spri ...

  9. spring Cloud网关之Spring Cloud Gateway

    Spring Cloud Gateway是什么?(官网地址:https://cloud.spring.io/spring-cloud-gateway/reference/html/) Spring C ...

随机推荐

  1. sqlserver数据库系统性能监控步骤

    1.部署好环境JDK+tomcat+数据库 ①修改数据库连接账号密码db.properties ②修改applicationContext.xml文件,开启任务 <bean id="o ...

  2. vue和echarts 封装的 v-charts 图表组件

    https://v-charts.js.org/#/ 在使用 echarts 生成图表时,经常需要做繁琐的数据类型转化.修改复杂的配置项,v-charts 的出现正是为了解决这个痛点.基于 Vue2. ...

  3. 深入理解JS中的变量及变量作用域

    JS的变量有两种,“全局变量”和“局部变量”. “全局变量”声明在函数外部,可供所有函数使用,(全局变量属于window)而“局部变量”声明在函数体内部,只能在定义该变量的函数体内使用. 1.全局变量 ...

  4. Django项目部署在Linux下以进程方式启动

    Django项目部署在Linux下以进程方式启动 这是一篇关于如何在linux下,以后台进程的方式运行服务,命令改改基本上就通用了. 开发完Django项目后,需要把项目部署到linux环境下.当然, ...

  5. IntelliJ IDEA 下的SVN使用

    最近公司的很多同事开始使用IntelliJ Idea,便尝试了一下,虽然快捷键与eclipse 有些不同,但是强大的搜索功能与“漂亮的界面”(个人认为没有eclipse好看 ),还是值得我们去使用的. ...

  6. sql如何截取字符

    ---MSSQL1 .SUBSTRING返回字符.binary.text 或 image 表达式的一部分.有关可与该函数一起使用的有效 Microsoft? SQL Server? 数据类型的更多信息 ...

  7. 实现与JS相同的Des加解密算法【转】

    Java代码 import java.util.ArrayList; import java.util.List; /** * DES加密/解密 * * @Copyright Copyright (c ...

  8. 带信号灯的最短路dijkstra问题(阿里巴巴2018校园招聘算法题)

    题目描述 现在城市有N个路口,每个路口有自己的编号,从0到N-1,每个路口还有自己的交通控制信号,例如0,3表示0号路口的交通信号每3个时刻变化一次,即0到3时刻0号路口允许通过,3到6时刻不允许通过 ...

  9. k8s 关键字以及管理流程。

    一.流程图如下 二.用户通过kubectl提交需要运行的docker container(pod). 三.api server把请求存储在etcd里面. 四.scheduler(调度)扫描,分配机器. ...

  10. P1019 单词接龙 字符串回溯

    题目描述 单词接龙是一个与我们经常玩的成语接龙相类似的游戏,现在我们已知一组单词,且给定一个开头的字母,要求出以这个字母开头的最长的“龙”(每个单词都最多在“龙”中出现两次),在两个单词相连时,其重合 ...