zoukankan      html  css  js  c++  java
  • SpringCloud Alibaba微服务实战十六

    概述

    好久没有更新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 升级完成,升级过程中会遇到各种各样的问题,面对问题的时候大家不要急躁,多通过代码调试定位问题,定位到问题后我相信利用好搜索引擎是可以解决的。

    在这里插入图片描述

  • 相关阅读:
    PowerShell 4 on win7 sp1
    about_Execution_Policies
    PowerShell常用命令
    Installing Chocolatey
    atitit..主流 浏览器 js 发动机 内核 市场份额 attialx总结vOa9
    css--左右visibility建立 “collapse”值问题
    struts2充分国际化案例 错误解决
    ZOJ 3635 Cinema in Akiba[ 大规模阵列 ]
    HDu 2010 水仙花数
    从一开始,说出事java匿名内部类
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13295876.html
Copyright © 2011-2022 走看看