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

Stella981
• 阅读 496

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

Java技术栈

www.javastack.cn

优秀的Java技术公众号

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

升级前 => 升级后

Spring Boot 1.5.x => Spring Boot 2.0.2

Spring Cloud Edgware SR4 => Spring Cloud Finchley.RELEASE

Eureka Server

Eureka Server 依赖更新

升级前:

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-eureka-server</artifactId></dependency>

升级后:

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>

Eureka Client

因为配置中心需要作为服务注册到注册中心,所以需要升级 Eureka Client,其他依赖没有变动。

Eureka Client 依赖更新

升级前:

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-eureka</artifactId></dependency>

升级后:

<dependency>    <groupId>org.springframework.cloud</groupId>    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

Spring Cloud

注册中心里面的客户端实例IP显示不正确

因为 Spring Cloud 获取服务客户端 IP 地址配置变更了。

升级前:

${spring.cloud.client.ipAddress}

升级后:

${spring.cloud.client.ip-address}

Spring Security

一般注册中心、配置中心都会使用安全加密,就会依赖 spring-boot-starter-security 组件,升级后有几下两个问题。

1、用户名和密码无法登录

因为 Spring Security 的参数进行了变更。

升级前:

security:  user:    name:    password:

升级后:

spring:  security:     user:       name:        password:

2、注册中心没有注册实例

如图所示,没有注册实例,两个注册中心无法互相注册。

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

因为 Spring Security 默认开启了所有 CSRF 攻击防御,需要禁用 /eureka 的防御。

在 Application 入口类增加忽略配置:

@EnableWebSecuritystatic class WebSecurityConfig extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {        http.csrf().ignoringAntMatchers("/eureka/**");        super.configure(http);    }}

3、配置中心无法加解密

升级后发现访问配置中心无法读取到配置,也无法加解密配置信息,访问配置中心链接直接跳转到了登录页面。

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

现在想变回之前的 basic auth 认证方式,找源码发现是自动配置跳到了登录页面,现在重写一下。

自动配置源码:
org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter#configure(org.springframework.security.config.annotation.web.builders.HttpSecurity)

protected void configure(HttpSecurity http) throws Exception {    logger.debug("Using default configure(HttpSecurity). If subclassed this will potentially override subclass configure(HttpSecurity).");    http        .authorizeRequests()            .anyRequest().authenticated()            .and()        .formLogin().and()        .httpBasic();}

重写之后:

@EnableWebSecuritystatic class WebSecurityConfig extends WebSecurityConfigurerAdapter {    @Override    protected void configure(HttpSecurity http) throws Exception {        http.csrf().ignoringAntMatchers("/**").and().authorizeRequests().anyRequest()                .authenticated().and().httpBasic();    }}

其实就是把 formLogin() 干掉了,又回到之前的 basic auth 认证方式,如下图所示。

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

现在我们又可以使用以下命令加解密了。

如解密:
curl http://xx.xx.xx.xx:7100/decrypt -d secret -u user:password

恢复 basic auth 之后,之前的服务需要加密连接配置中心的又正常运行了。

Maven

升级到 Spring Boot 2.x 之后发现 Spring Boot 的 Maven 启动插件不好用了,主要是 Profile 不能自由切换。

升级前:

spring-boot:run -Drun.profiles=profile1

升级后:

spring-boot:run -Dspring-boot.run.profiles=profile1

具体的请参考:
https://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html

总结

以上都是踩完所有的坑总结出来的解决方案,实际解决问题的过程远要复杂的多。版本变化有点大,本次已成功升级了 Spring Cloud 基础依赖,及注册中心(Eureka Server)、配置中心(Config Server)。

其他像 Gateway 代替了 Zuul, 及其他组件再慢慢升级,Spring Cloud 的快速发展令升级变得非常蛋疼,本文记录了升级过程中踩过的所有的坑。。。

坑死了,已经保证编译、运行正常,其他还有什么坑不知道,刚升级完 Finchley 这个正式版本,Spring Cloud 刚刚又发布了 Finchley.SR1,感觉 Spring Cloud 变成了学不动系列了。。。

@ All 码农们:你们升级了吗?有遇到什么样的坑?欢迎留言!

往期干货推荐

1. Spring Boot 2.x 新特性总结及迁移指南

2.  Spring Boot 核心配置文件详解

3. Spring Cloud Finchley 正式发布

4. 聊聊微服务架构及分布式事务解决方案

5. 微服务为什么要选 Spring Cloud?

Java技术栈 • 长按关注

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

本文分享自微信公众号 - Java技术栈(javastack)。
如有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一起分享。

点赞
收藏
评论区
推荐文章
blmius blmius
2年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
Jacquelyn38 Jacquelyn38
2年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Wesley13 Wesley13
2年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
2年前
Java 9 被无情抛弃,Java 8 直接升级到 Java 10!!
!(https://oscimg.oschina.net/oscnet/3e08a942dd884e9ab82b63a1f3c4aada.jpg"未命名文件.jpg")Java技术栈不可错过的Java 技术公众号!(https://oscimg.oschina.net/oscnet/00fcff52518e
Wesley13 Wesley13
2年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
2年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Stella981 Stella981
2年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Stella981 Stella981
2年前
Spring Boot 2.1.6 发布了!
!(https://oscimg.oschina.net/oscnet/e5aaab7a5b9f4aa7a944b00aff253ed2.jpg)Java技术栈www.javastack.cn优秀的Java技术公众号(https://www.oschina.net/action/GoToLink?urlhttps%3
Wesley13 Wesley13
2年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
3个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这