SpringCloud Alibaba微服务实战十六

Easter79
• 阅读 486

点击上方 蓝字 关注我们

概述

好久没有更新SpringCloud Alibaba 系列的文章了,今天我们来将版本升级到最新的毕业版本。并且将原来容器化部署的组件seata、nacos、sentinel拉出来单独部署,为我们后面k8s部署作准备。

官方推荐版本如下:SpringCloud Alibaba微服务实战十六

这篇文章主要是讲升级过程中遇到的一些问题并讲述解决的过程与方法,如果要了解详细用法还请翻看之前的文章。

主版本升级

<properties> ...  <spring-boot.version>2.2.5.RELEASE</spring-boot.version>  <alibaba-cloud.version>2.2.1.RELEASE</alibaba-cloud.version>  <springcloud.version>Hoxton.SR3</springcloud.version> ... </properties>

修改parent模块主pom文件对应的组件版本,修改完成后重新下载jar包。

nacos 1.2

nacos的升级比较容易,按照下面步骤两步即可完成。

  • 初始化nacos数据库 nacos-mysql.sql

  • 修改nacos配置文件 application.properties,将数据库相关配置注释放开,并修改成自己的数据库配置 SpringCloud Alibaba微服务实战十六

seata 1.2

seata初始化的过程我们之前教程中有过详细说明,但是新老版本之间差异比较大,我们这里再顺带提一下,大家可以按照如下步骤完成。

  • 在业务系统中创建数据表 undo_log,sql文件从下面地址获取:
    https://github.com/seata/seata/blob/develop/script/client/at/db/mysql.sql

  • 在你的mysql数据库中创建名为seata的库,并初始化,sql文件从下面地址获取:
    https://github.com/seata/seata/blob/develop/script/server/db/mysql.sql

  • seata新版本修改了artifactId,所以我们需要修改seata的依赖

<dependency>  <groupId>com.alibaba.cloud</groupId>  <artifactId>spring-cloud-starter-alibaba-seata</artifactId> </dependency>

SpringCloud Alibaba 2.2.1 RELEASE  使用的是 SEATA1.1的版本,如果想体验SEATA1.2的特性,可以在此基础上去掉seata的依赖,手动加入1.2版本。

`
 com.alibaba.cloud
 spring-cloud-starter-alibaba-seata
 
  
   seata-spring-boot-starter
   io.seata
  

 

 io.seata  seata-spring-boot-starter  1.2.0 `
  • 修改seata客户端的配置
    原来我们在客户端是使用 registry.conf 作为seata的配置文件,现在需要将配置移到application.yml或配置中心中,具体配置大家可以参考官网文件 https://github.com/seata/seata/blob/develop/script/client/spring/application.yml, 我的配置如下:

seata: enabled: true application-id: ${spring.application.name} tx-service-group: account_service_group enable-auto-data-source-proxy: true config: type: nacos nacos: namespace: serverAddr: 10.0.23.48:8848 group: SEATA_GROUP userName: "nacos" password: "nacos" registry: type: nacos nacos: application: seata-server server-addr: 10.0.23.48:8848 namespace: userName: "nacos" password: "nacos"

其他模块大家自行修改。

service.vgroupMapping.account_service_group=default service.vgroupMapping.product_service_group=default service.vgroupMapping.order_service_group=default store.mode=db store.db.datasource=druid store.db.dbType=mysql store.db.driverClassName=com.mysql.jdbc.Driver store.db.url=jdbc:mysql://10.0.23.48:3306/seata?useUnicode=true store.db.user=root store.db.password=xxxxxx store.db.minConn=5 store.db.maxConn=30 store.db.globalTable=global_table store.db.branchTable=branch_table store.db.queryLimit=100 store.db.lockTable=lock_table store.db.maxWait=5000

修改完成后在git中执行shell命令 sh nacos-config.sh -h 10.0.23.48 -p 8848 -g SEATA_GROUP -u nacos -w nacos,执行完成后配置文件就被推送到了nacosSpringCloud Alibaba微服务实战十六

  • 修改seata服务端配置 registry.conf, 完成后启动seata服务端

registry { type = "nacos" nacos { application = "seata-server" serverAddr = "10.0.23.48:8848" namespace = "" cluster = "default" username = "nacos" password = "nacos" } } config { type = "nacos" nacos { serverAddr = "10.0.23.48:8848" namespace = "" group = "SEATA_GROUP" username = "" password = "" } }

  • 经过上面几步配置seata就升级完成了,请大家自行测试 SpringCloud Alibaba微服务实战十六

sentinel 1.7

使用sentinel的时候,很多同学都会遇到在sentinel控制台不显示api管理菜单,不能读取nacos限流配置等情况,大家可以在gateway模块启动的时候加上启动参数 -Dcsp.sentinel.app.type=1,重启sentinel控制台即可正常显示。

SpringCloud Alibaba微服务实战十六

在之前的文章中曾经提到过老版本的Sentinel在与网关集成时限流不生效的问题,原因是因为sentinel获取到的网关id并不是我们配置的account-service,而是加了 CompositeDiscoveryClient_ 前缀,原文说明如下:SpringCloud Alibaba微服务实战十六

在新版本中已经没有这个问题了,所以我们可以在网关限流配置文件中将前缀删除。删除后的配置如下:SpringCloud Alibaba微服务实战十六

auth-service

使用postman去auth-service获取access_token的时候如果出现如下错误
org.springframework.security.core.authority.SimpleGrantedAuthority; local class incompatible: stream classdesc serialVersionUID = 510, local class serialVersionUID = 520

SpringCloud Alibaba微服务实战十六

image.png

出现这个问题的原因是原来数据库已经存储了账号对应的access_token了,但是SpringSecurity不支持跨版本的序列化,解决方法也很简单,只要把你的用户对应的access_token给删除,重新生成即可。

SpringCloud Gateway

使用PostMan做集成测试时发现接口调用一直提示404 Not Found 错误。

SpringCloud Alibaba微服务实战十六

通过对源码文件org.springframework.cloud.gateway.filter.NettyRoutingFilter 的调试,发现新版本的SpringCloud Gateway在做转发时还保留我们配置的前缀。SpringCloud Alibaba微服务实战十六

如上图所示,转发后还带上前缀导致不能找到对应的请求路径,所以出现404异常。

要解决这个问题我们需要在转发前删掉这一前缀,刚好SprtingCloud Gateway提供了StripPrefix GatewayFilter filter,可以用来解决这一问题:

The StripPrefix GatewayFilter factory takes one parameter, parts. The parts parameter indicates the number of parts in the path to strip from the request before sending it downstream.

详情请参看:https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.2.RELEASE/reference/html/#the-stripprefix-gatewayfilter-factory

既然知道了异常原因也找到了解决方法,那就很简单了。只需要在网关模块配置映射时加上默认过滤器即可:

spring: cloud:  gateway: discovery: locator: enabled: true routes: - id: account-service uri: lb://account-service predicates: - Path=/account-service/**   ...       # 解决前缀转发问题 default-filters: - StripPrefix=1

经过以上几步我们的SpringCloud alibaba 升级完成,升级过程中会遇到各种各样的问题,面对问题的时候大家不要急躁,多通过代码调试定位问题,定位到问题后我相信利用好搜索引擎是可以解决的。

原创不易,请勿白嫖!" 转发 " 加 " 在看 ",养成好习惯!

SpringCloud Alibaba微服务实战十六

热文推荐

☞ 数据库优化之SQL优化
☞ 数据库优化之实例优化
☞ Docker基础与实战,看这一篇就够了!
☞ Docker-Compose基础与实战,看这一篇就够了!
OAuth2.0最简向导(多图预警)
☞ 构建三维一体立体化监控体系
☞ SpringCloud实战系列
☞ RocketMQ进阶-事务消息
☞ 数据库索引,你要了解的都在这里!

SpringCloud Alibaba微服务实战十六

JAVA日知录

长按左边二维码关注我们,精彩文章第一时间推送

         >>>技术交流群<<<

朕已阅 SpringCloud Alibaba微服务实战十六

本文分享自微信公众号 - JAVA日知录(javadaily)。
如有侵权,请联系 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
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
Stella981 Stella981
2年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Easter79 Easter79
2年前
Twitter的分布式自增ID算法snowflake (Java版)
概述分布式系统中,有一些需要使用全局唯一ID的场景,这种时候为了防止ID冲突可以使用36位的UUID,但是UUID有一些缺点,首先他相对比较长,另外UUID一般是无序的。有些时候我们希望能使用一种简单一些的ID,并且希望ID能够按照时间有序生成。而twitter的snowflake解决了这种需求,最初Twitter把存储系统从MySQL迁移
Wesley13 Wesley13
2年前
QQ玩一玩好友排行榜与世界排行榜
QQ玩一玩好友排行榜与世界排行榜1、开发环境CocosCreatorV2.0.5手Q版本V7.9.0.3820(目前市场中最新版本)qqPlayCore.jsbuildTime:'FriNov09201813:20:45GMT0800(GMT08:00)'上出现,
Stella981 Stella981
2年前
Spring Boot如何利用AOP巧妙记录操作日志?
!(https://oscimg.oschina.net/oscnet/7f1d6247ad65413fbca3b77b0bb9433c.png)点击上方蓝字关注我们!(https://oscimg.oschina.net/oscnet/3f5a1c2360f9430c93a00b4715527ed9.jpg)本篇
Stella981 Stella981
2年前
Docker 部署SpringBoot项目不香吗?
  公众号改版后文章乱序推荐,希望你可以点击上方“Java进阶架构师”,点击右上角,将我们设为★“星标”!这样才不会错过每日进阶架构文章呀。  !(http://dingyue.ws.126.net/2020/0920/b00fbfc7j00qgy5xy002kd200qo00hsg00it00cj.jpg)  2
Stella981 Stella981
2年前
200的大额人民币即将面世?央行:Yes!
点击上方蓝字关注我们!(https://oscimg.oschina.net/oscnet/2a1c2ac00bf54458a78c48a6c2e547d5.png)点击上方“印象python”,选择“星标”公众号重磅干货,第一时间送达!!(
可莉 可莉
2年前
200的大额人民币即将面世?央行:Yes!
点击上方蓝字关注我们!(https://oscimg.oschina.net/oscnet/2a1c2ac00bf54458a78c48a6c2e547d5.png)点击上方“印象python”,选择“星标”公众号重磅干货,第一时间送达!!(
为什么mysql不推荐使用雪花ID作为主键
作者:毛辰飞背景在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么为什么不建议采用uuid,使用uuid究
Easter79
Easter79
Lv1
今生可爱与温柔,每一样都不能少。
文章
2.8k
粉丝
5
获赞
1.2k