SpringCloud Alibaba微服務實戰十六 - 2.2.1.RELEASE版本升級

概述

好久沒有更新SpringCloud Alibaba 系列的文章了,今天我們來將版本升級到最新的畢業版本。並且將原來容器化部署的組件seata、nacos、sentinel拉出來單獨部署,爲我們後面k8s部署作準備。

官方推薦版本如下:
image.png

這篇文章主要是講升級過程中遇到的一些問題並講述解決的過程與方法,如果要了解詳細用法還請翻看之前的文章。

主版本升級

<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,將數據庫相關配置註釋放開,並修改成自己的數據庫配置
    image.png

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版本。

<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
	<exclusions>
		<exclusion>
			<artifactId>seata-spring-boot-starter</artifactId>
			<groupId>io.seata</groupId>
		</exclusion>
	</exclusions>
</dependency>

<dependency>
	<groupId>io.seata</groupId>
	<artifactId>seata-spring-boot-starter</artifactId>
	<version>1.2.0</version>
</dependency>
  • 修改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"

其他模塊大家自行修改。

  • 將seata的配置推送到nacos,這裏採用的是db模式,操作步驟請參照官方說明: https://github.com/seata/seata/tree/develop/script/config-center
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,執行完成後配置文件就被推送到了nacos
image.png

  • 修改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就升級完成了,請大家自行測試
    image.png

sentinel 1.7

使用sentinel的時候,很多同學都會遇到在sentinel控制檯不顯示api管理菜單,不能讀取nacos限流配置等情況,大家可以在gateway模塊啓動的時候加上啓動參數 -Dcsp.sentinel.app.type=1,重啓sentinel控制檯即可正常顯示。

image.png

在之前的文章中曾經提到過老版本的Sentinel在與網關集成時限流不生效的問題,原因是因爲sentinel獲取到的網關id並不是我們配置的account-service,而是加了 CompositeDiscoveryClient_ 前綴,原文說明如下:
image.png

在新版本中已經沒有這個問題了,所以我們可以在網關限流配置文件中將前綴刪除。刪除後的配置如下:
image.png

auth-service

使用postman去auth-service獲取access_token的時候如果出現如下錯誤org.springframework.security.core.authority.SimpleGrantedAuthority; local class incompatible: stream classdesc serialVersionUID = 510, local class serialVersionUID = 520

image.png

出現這個問題的原因是原來數據庫已經存儲了賬號對應的access_token了,但是SpringSecurity不支持跨版本的序列化,解決方法也很簡單,只要把你的用戶對應的access_token給刪除,重新生成即可。

SpringCloud Gateway

使用PostMan做集成測試時發現接口調用一直提示404 Not Found 錯誤。
image.png

通過對源碼文件org.springframework.cloud.gateway.filter.NettyRoutingFilter 的調試,發現新版本的SpringCloud Gateway在做轉發時還保留我們配置的前綴。
image.png

如上圖所示,轉發後還帶上前綴導致不能找到對應的請求路徑,所以出現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 升級完成,升級過程中會遇到各種各樣的問題,面對問題的時候大家不要急躁,多通過代碼調試定位問題,定位到問題後我相信利用好搜索引擎是可以解決的。

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章