激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|JavaScript|易語言|

服務器之家 - 編程語言 - Java教程 - spring cloud升級到spring boot 2.x/Finchley.RELEASE遇到的坑

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

2021-05-28 13:09菩提樹下的楊過 Java教程

這篇文章主要介紹了spring cloud升級到spring boot 2.x/Finchley.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的除外),類似:

?
1
2
3
4
5
plugins {
 
  id 'java-library'
 
}

然后按官網的教程,compile最好換成implementation

?
1
2
3
4
5
dependencies {
  implementation(
      ...
  )
}

1.2 常規java項目(指帶容器能獨立運行的項目)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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包版本的問題

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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里無法通過配置關閉,只能寫代碼調整:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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里參考下面的配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
management:
 
 ...
 
 endpoints:
 
  web:
 
   base-path: /
 
   exposure:
 
    include: "*"

另外/health節點,默認情況下,只能輸出很少的信息,詳細信息,需要通過配置打開

?
1
2
3
4
5
6
7
8
9
10
11
management:
 
 ...
 
 endpoint:
 
  health:
 
   show-details: always
 
 ...

七、${spring.cloud.client.ipaddress} 無法識別

spring cloud 2.x里,${spring.cloud.client.ipaddress} 這個寫法不識別,一啟動就會報錯,嘗試了多次,無意發現,把a改成小寫,居然可以了:

?
1
2
3
4
5
6
7
spring:
 
 ...
 
 application:
 
  name: sr-menu-service:${spring.cloud.client.ipaddress}

感覺這應該是個bug,新版本里估計會修復。

八、metricwriter、systempublicmetrics類找不到的問題

spring boot 2.x里metrics默認換成了micrometer,原來的metricwriter之類的全干掉了,詳情參考官網文檔

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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里具體指標值,可以參考下面的代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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里已經被標識為過時,正常用法參考以下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/yjmyzz/p/9408700.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 耽美肉文np | 毛片在哪里看 | 欧洲成人综合网 | 狠狠干天天操 | 钻石午夜影院 | 精品视频在线免费看 | 国产91丝袜在线播放 | 成码无人av片在线观看网站 | 亚洲精品久久久久久下一站 | 国产精品99久久久久久久 | 久久人人人| 成人国产精品一区 | 有兽焉免费动画 | 成人片免费看 | 日本中文高清 | 久久国产精品免费视频 | 黄片毛片一级 | 黄色一级片毛片 | 九一成人 | 黄色一级片毛片 | 久久中文一区 | 精品国产一二区 | 久久99综合久久爱伊人 | 97zyz成人免费视频 | 国产一区二区三区四区五区精品 | 91精品国产刺激国语对白 | 欧美成人精品一区二区三区 | 欧美一级免费看 | 奇米影视888狠狠狠777不卡 | 久久草在线视频免费 | 成人午夜在线免费观看 | 久久免费综合视频 | 国产中出在线观看 | 国产免费一区二区三区网站免费 | 国产精选在线 | 国产一区二区观看 | 国产精品探花在线观看 | 欧美日韩一区,二区,三区,久久精品 | 亚洲视频综合 | 国产女同玩人妖 | 亚洲欧美国产精品va在线观看 |