热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

springcloud升级到springboot2.x/Finchley.RELEASE遇到的坑

这篇文章主要介绍了springcloud升级到springboot2.xFinchley.RELEASE遇到的坑,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

显著变化:

  • 与 Spring Boot 2.0.x 兼容
  • 不支持 Spring Boot 1.5.x
  • 最低要求 Java 8
  • 新增 Spring Cloud Function 和 Spring Cloud Gateway

一、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 stats = 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) &#63; 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/

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
  • 这是原文链接:sendingformdata许多情况下,我们使用表单发送数据到服务器。服务器处理数据并返回响应给用户。这看起来很简单,但是 ... [详细]
  • Servlet多用户登录时HttpSession会话信息覆盖问题的解决方案
    本文讨论了在Servlet多用户登录时可能出现的HttpSession会话信息覆盖问题,并提供了解决方案。通过分析JSESSIONID的作用机制和编码方式,我们可以得出每个HttpSession对象都是通过客户端发送的唯一JSESSIONID来识别的,因此无需担心会话信息被覆盖的问题。需要注意的是,本文讨论的是多个客户端级别上的多用户登录,而非同一个浏览器级别上的多用户登录。 ... [详细]
  • 在IDEA中运行CAS服务器的配置方法
    本文介绍了在IDEA中运行CAS服务器的配置方法,包括下载CAS模板Overlay Template、解压并添加项目、配置tomcat、运行CAS服务器等步骤。通过本文的指导,读者可以轻松在IDEA中进行CAS服务器的运行和配置。 ... [详细]
  • Tomcat安装与配置教程及常见问题解决方法
    本文介绍了Tomcat的安装与配置教程,包括jdk版本的选择、域名解析、war文件的部署和访问、常见问题的解决方法等。其中涉及到的问题包括403问题、数据库连接问题、1130错误、2003错误、Java Runtime版本不兼容问题以及502错误等。最后还提到了项目的前后端连接代码的配置。通过本文的指导,读者可以顺利完成Tomcat的安装与配置,并解决常见的问题。 ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 目录实现效果:实现环境实现方法一:基本思路主要代码JavaScript代码总结方法二主要代码总结方法三基本思路主要代码JavaScriptHTML总结实 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • 使用在线工具jsonschema2pojo根据json生成java对象
    本文介绍了使用在线工具jsonschema2pojo根据json生成java对象的方法。通过该工具,用户只需将json字符串复制到输入框中,即可自动将其转换成java对象。该工具还能解析列表式的json数据,并将嵌套在内层的对象也解析出来。本文以请求github的api为例,展示了使用该工具的步骤和效果。 ... [详细]
  • 推荐系统遇上深度学习(十七)详解推荐系统中的常用评测指标
    原创:石晓文小小挖掘机2018-06-18笔者是一个痴迷于挖掘数据中的价值的学习人,希望在平日的工作学习中,挖掘数据的价值, ... [详细]
  • Tomcat/Jetty为何选择扩展线程池而不是使用JDK原生线程池?
    本文探讨了Tomcat和Jetty选择扩展线程池而不是使用JDK原生线程池的原因。通过比较IO密集型任务和CPU密集型任务的特点,解释了为何Tomcat和Jetty需要扩展线程池来提高并发度和任务处理速度。同时,介绍了JDK原生线程池的工作流程。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • mui框架offcanvas侧滑超出部分隐藏无法滚动如何解决
    web前端|js教程off-canvas,部分,超出web前端-js教程mui框架中off-canvas侧滑的一个缺点就是无法出现滚动条,因为它主要用途是设置类似于qq界面的那种格 ... [详细]
  • 我正在使用sql-serverkafka-connect和debezium监视sqlserver数据库,但是当我发布并运行我的wo ... [详细]
  • smarty内部日期函数html_select_date()用法实例分析,select函数用法
    php教程|php手册smarty,日期,html,select,date(),smartyphp教程-php手册smarty内部日期函数html_select_date()用法实 ... [详细]
author-avatar
mobiledu2502923007
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有