欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

spring cloud升级到spring boot 2.x/Finchley.RELEASE遇到的坑

程序员文章站 2024-02-26 12:32:22
spring boot2.x已经出来好一阵了,而且spring cloud 的最新release版本finchley.release,默认集成的就是spring boot...

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'

  }

}<br>...

另外,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://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 cloud升级到spring boot 2.x/Finchley.RELEASE遇到的坑

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

spring cloud升级到spring boot 2.x/Finchley.RELEASE遇到的坑

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

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

spring cloud升级到spring boot 2.x/Finchley.RELEASE遇到的坑

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

spring cloud升级到spring boot 2.x/Finchley.RELEASE遇到的坑

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

spring cloud升级到spring boot 2.x/Finchley.RELEASE遇到的坑

如果一台机器上部署多个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://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/

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