之前我在公司的项目中开发使用的SpringBoot是1。5。x版本,现在2。x已经发布了挺久,而SpringCloud也发布了基于SpringBoot2。x的Finchley版本,所以我把我们公司的项目自己做了一次重构,对整体框架进行了一次升级改造,在改造过程中,也踩了不少的坑,特记录如下。 升级前升级后 SpringBoot1。5。xSpringBoot2。0。3 SpringCloudEdgwareSR4SpringCloudFinchley。RELEASE一。EurekaServer EurekaServer依赖更新 升级前:dependencygroupIdorg。springframework。cloudgroupIdspringcloudstartereurekaserverartifactIddependency 升级后:dependencygroupIdorg。springframework。cloudgroupIdspringcloudstarternetflixeurekaserverartifactIddependency二。EurekaClient 因为配置中心需要作为eureka的服务实例注册到eureka服务中心,所以需要升级EurekaClient,其他依赖没有变动。 EurekaClient依赖更新 升级前dependencygroupIdorg。springframework。cloudgroupIdspringcloudstartereurekaartifactIddependency 升级后:dependencygroupIdorg。springframework。cloudgroupIdspringcloudstarternetflixeurekaclientartifactIddependency三。SpringCloud 注册中心里面的客户端实例IP显示不正确 因为SpringCloud获取服务客户端IP地址配置变更了。 升级前:{spring。cloud。client。ipAddress} 升级后:{spring。cloud。client。ipaddress}四。SpringSecurity 一般注册中心、配置中心都会使用安全加密,就会依赖springbootstartersecurity组件,升级后有几下两个问题。 1、用户名和密码无法登录 因为SpringSecurity的参数进行了变更。 升级前:security:user:name:password: 升级后:spring:security:user:name:password: 另外需要注意: SpringBoot项目遵循的是约定大于配置的原则,当pom。xml文件中添加了security的依赖后,就会自动启用eureka的安全验证功能,否则就不会开启。dependencygroupIdorg。springframework。cloudgroupIdspringcloudstartersecurityartifactIddependency 2、注册中心没有注册实例 没有注册实例,两个注册中心无法互相注册。 因为SpringSecurity默认开启了所有CSRF防御,需要禁用eureka的防御。 在Application入口类增加忽略配置:EnableWebSecuritystaticclassWebSecurityConfigextendsWebSecurityConfigurerAdapter{Overrideprotectedvoidconfigure(HttpSecurityhttp)throwsException{http。csrf()。ignoringAntMatchers(eureka);super。configure(http);}} 3、配置中心无法加解密 升级后发现访问配置中心无法读取到配置,也无法加解密配置信息,访问配置中心链接直接跳转到了登录页面。 现在想变回之前的basicauth认证方式,找源码发现是自动配置跳到了登录页面,现在重写一下。 自动配置源码: org。springframework。security。config。annotation。web。configuration。WebSecurityConfigurerAdapterconfigure(org。springframework。security。config。annotation。web。builders。HttpSecurity)protectedvoidconfigure(HttpSecurityhttp)throwsException{logger。debug(Usingdefaultconfigure(HttpSecurity)。Ifsubclassedthiswillpotentiallyoverridesubclassconfigure(HttpSecurity)。);http。authorizeRequests()。anyRequest()。authenticated()。and()。formLogin()。and()。httpBasic();} 重写之后:EnableWebSecuritystaticclassWebSecurityConfigextendsWebSecurityConfigurerAdapter{Overrideprotectedvoidconfigure(HttpSecurityhttp)throwsException{http。csrf()。ignoringAntMatchers()。and()。authorizeRequests()。anyRequest()。authenticated()。and()。httpBasic();}} 其实就是把formLogin()干掉了,又回到之前的basicauth认证方式。 现在我们又可以使用以下命令加解密了。如解密:curlhttp:xx。xx。xx。xx:7100decryptdsecretuuser:password 恢复basicauth之后,之前的服务需要加密连接配置中心的又正常运行了。Maven 升级到SpringBoot2。x之后发现SpringBoot的Maven启动插件不好用了,主要是Profile不能自由切换。 升级前:springboot:runDrun。profilesprofile1 升级后:springboot:runDspringboot。run。profilesprofile1 想要获取更多免费资料添加微信号:codingbb 更多内容还可关注公众号【扣丁学堂】