Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the pitfalls encountered when upgrading from spring cloud to spring boot 2.x/Finchley.RELEASE?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces the spring cloud upgrade to spring boot 2.x/Finchley.RELEASE encountered in the pit, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

Spring boot2.x has been out for a while, and Finchley.RELEASE, the latest Release version of spring cloud, is integrated with spring boot2.x by default. These days, you have tried to upgrade an old project from a low version to 2.x.

Significant changes:

Compatible with Spring Boot 2.0.x

Spring Boot 1.5.x is not supported

Minimum requirements Java 8

New Spring Cloud Function and Spring Cloud Gateway

I. the problem of gradle

Spring boot 2.x requires that the gradle version should not be too old, first upgrade gradle to version 4.6, and then compile, various problems, check on the gradle official website, build.gradle has several small areas to adjust

1.1 projects for java-libary

That is, the public jar,plugins {} of pure toolkits must be placed on line 1 (except for those with buildscript), similar to:

Plugins {id 'java-library'}

Then, according to the tutorial on the official website, compile had better be changed to implementation.

Dependencies {implementation (...)}

1.2 regular java projects (projects with containers that can run independently)

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

...

In addition, when a high version of gradle compiles, there is always a nasty prompt:

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

When compiling, you can add a parameter:-- warning-mode=none is disabled, that is, similar to:

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

Second, the problem of relying on the version of jar package

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

Among them

'org.springframework.cloud:spring-cloud-sleuth-stream:1.3.4.RELEASE'

'org.springframework.cloud:spring-cloud-starter-hystrix:1.4.4.RELEASE'

These two items must specify a version number, otherwise they cannot be compiled. (the latest 2.x version of the jar package has not yet been uploaded to the central repository and cannot automatically identify dependencies.) in addition, pagehelper, a commonly used paging component, is also recommended to be configured according to the above version, otherwise an error may be reported at runtime.

III. The problem of log4j/log4j2

After upgrading to spring boot 2.x, whether you configure log4j or log4j2, the runtime always reports the stack overflow of error. After being replaced with logback, the startup is normal. It is recommended that everyone try to use the default logback. For the configuration of dependencies, refer to the above.

IV. Problems that cannot be found in the DataSourceBuilder class

Spring boot 2.x replaced this class with package, so you can't find it. For more information, please see:

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

The solution is to quote: org.springframework.boot:spring-boot-starter-jdbc

At the same time, modify the code import the new package: org.springframework.boot.jdbc.DataSourceBuilder

V. the issue of security

Spring boot 2.x strengthens the security. No matter what rest url you visit, login is required by default. In application.yml, you cannot close it through configuration, so you can only write code to adjust it:

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@EnableWebSecuritypublic class SecurityConfiguration extends WebSecurityConfigurerAdapter {@ Override protected void configure (HttpSecurity http) throws Exception {http.authorizeRequests () .anyRequest () .permitAll () .and () .csrf () .disable ();}}

In this way, all url are allowed to be accessed by default (if it is a service exposed to the public network, please use it with caution)

6. All kinds of actuator monitor the path change of endpoint

In spring boot 2.x, the endpoint default path of actuator begins with / actuator. If you want to use the previous style and put it under / root, you can refer to the following configuration in applicatino.yml:

Management:... Endpoints: web: base-path: / exposure: include: "*"

In addition, / health node, by default, can only output very little information, details, which need to be opened through configuration

Management:... Endpoint: health: show-details: always...

7. ${spring.cloud.client.ipAddress} is not recognized

In spring cloud 2.x, the spelling of ${spring.cloud.client.ipAddress} is not recognized and will report an error as soon as it starts. I tried many times, but inadvertently found that it was OK to change A to lowercase:

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

It feels like it should be a bug, and it will probably be fixed in the new version.

8. Problems that cannot be found in MetricWriter and SystemPublicMetrics class

In spring boot 2.x, metrics has been replaced by micrometer by default, and the original MetricWriter has been killed. For more information, please refer to the official website.

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

The above configuration is to enable statsd, and then run to see the effect, as shown in the following figure

But compared with spring boot 1.x, the specific value will not be output directly. Depending on the specific value, you can use http://localhost:8001/metrics/jvm.memory.used.

The VALUE is the memory occupied by jvm (including heap + noheap). If you only want to see the heap area (that is, the memory on the heap), you can use

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

At the same time, you can also see the effect in grafana:

Note: at present, the prefix in statsd cannot be modified, and the code is written to statsd.

If you deploy multiple spring cloud microservices on a single machine, you won't be able to tell them apart from grafana (personal estimates will improve it later).

In addition, if you want to get the specific indicator values in these metrics through the code, you can refer to the following code:

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

IX. The problem of outdated WebMvcConfigurerAdapter in swagger

The class WebMvcConfigurerAdapter has been marked as obsolete in the latest spring boot. For normal usage, refer to the following:

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 @ Override protected void addResourceHandlers 2017 * / @ Configuration@EnableSwagger2public 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")) .p aths (PathSelectors.any ()) .build () } private ApiInfo apiInfo () {return new ApiInfoBuilder () .title ("menu-service online api document") .description ("testing service") .contact (new Contact ("Yang Guo under the banyan tree", "http://yjmyzz.cnblogs.com/"," yjmyzz@126.com ")) .version (" 1.0.0 ") .build () }} Thank you for reading this article carefully. I hope the article "what are the pitfalls encountered in upgrading from spring cloud to spring boot 2.x/Finchley.RELEASE" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report