problems_microservice

problems_microservice

1 seata报错1

2021-12-02 13:33:31.529 INFO --- io.seata.spring.annotation.datasource.SeataAutoDataSourceProxyCreator.getAdvicesAndAdvisorsForBean[45]: Auto proxy of [dataSource]
Property 'mapperLocations' was not specified.
2021-12-02 13:33:33.857 ERROR --- io.seata.core.rpc.netty.NettyClientChannelManager.reconnect[164]: Failed to get available servers: endpoint format should like ip:port
java.lang.IllegalArgumentException: endpoint format should like ip:port

RCA: (不对,这个RCA是错误的!)
yaml文件中,没有配置driver-class-name,url,username,password。这些是配置seata数据库的数据源。
solution:配置如下:

spring:
   # 数据源配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://ip:port/seata?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=GMT%2B8
    username: user1
    password: password1

后续:随着对seata的熟悉,发现这里并非配置seata数据库,而是业务数据库,只不过,把配置项直接放到datasource: 下面了(以前在 datasource: 下,还有一个 druid:
而seata的数据库的数据源配置,是在nacos的 store.db.url 等配置项中。

2 seata报错2

io.seata.common.exception.FrameworkException: can not register RM,err:can not connect to services-server.
RCA:启动seata-server时,未手动设置端口。
solution:

  1. 服务端和客户端配置服务名称相同。

  2. seata客户端配置file.conf:
    如果registry.conf中设置其配置类型为nacos,则不会使用file.conf中的配置,而是使用nacos中的配置。
    从而这里不用修改file.conf文件,而是要到nacos的seata_env命名空间中,修改 vgroupMapping.orders_tx_group 的值。

service {
  # vgroupMapping.orders_tx_group = "seata-server"
  vgroupMapping.orders_tx_group = "default"
}
  1. 启动seata-server时,手动设置端口,命令为:nohup ./bin/seata-server.sh -p 8091 -h 127.0.0.1 -m db >log.out 2>1 &

3 seata报错4

### Error updating database.  Cause: java.sql.SQLException: java.sql.SQLSyntaxErrorException: Table 'db1.undo_log' doesn't exist

RCA: 在我的数据库db1中没有建立undo_log表。
solution:
建表,语句如下:

CREATE TABLE `undo_log` (
    `branch_id` bigint(20) NOT NULL COMMENT 'branch transaction id',
    `xid` varchar(128) NOT NULL COMMENT 'global transaction id',
    `context` varchar(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    `rollback_info` longblob NOT NULL COMMENT 'rollback info',
    `log_status` int(11) NOT NULL COMMENT '0:normal status,1:defense status',
    `log_created` datetime(6) NOT NULL COMMENT 'create datetime',
    `log_modified` datetime(6) NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB COMMENT='AT transaction mode undo table';

4 seata报错5

2021-12-02 16:36:31.539 ERROR --- [           main] com.alibaba.druid.pool.DruidDataSource   : init datasource error, url: jdbc:mysql://192.168.1.10:3306/seata?useUnicode=true&rewriteBatchedStatements=true
==> com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

RCA: nacos上配置的store.db.url 错误,

solution:
错误的配置值:jdbc:mysql://错误IP:错误端口/seata?useUnicode=true&rewriteBatchedStatements=true
改为正确的配置值:jdbc:mysql://新IP:新端口/seata?useUnicode=true&rewriteBatchedStatements=true

5 seata报错6

处理分布式事务时,openfeign调用远端接口,远端接口报错:

Caused by: java.sql.SQLException: java.lang.RuntimeException: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Type id handling not implemented for type java.lang.Object (by serializer of type com.fasterxml.jackson.databind.ser.impl.UnsupportedTypeSerializer) (through reference chain: io.seata.rm.datasource.undo.BranchUndoLog["sqlUndoLogs"]->java.util.ArrayList[0]->io.seata.rm.datasource.undo.SQLUndoLog["beforeImage"]->io.seata.rm.datasource.sql.struct.TableRecords["rows"]->java.util.ArrayList[0]->io.seata.rm.datasource.sql.struct.Row["fields"]->java.util.ArrayList[2]->io.seata.rm.datasource.sql.struct.Field["value"])

RCA:seata默认使用jackson反序列化,但是有bug,官方还未修复。
SOLUTION1:将序列化器改为kryo。
修改方法:
在springboot的yaml配置文件中增加配置:
seata: client: undo: logSerialization: kryo
在pom.xml中引入kryo的3个依赖。

SOLUTION2:将序列化器改为fastjson。
修改方法:
在springboot的yaml配置文件中增加配置:

seata: client: undo: logSerialization: fastjson
seata: client: undo: log-serialization: fastjson  # 这里可以使用属性logSerialization,也可以使用属性 log-serialization

在pom.xml中引入fastjson的1个依赖。

ERRORLOG:

java.lang.IllegalStateException: Service id not legal hostname (inventorys_just_for_test)

RCA: Feign的服务名不能使用下划线,需使用中横线,如:“aa-bb”。
SOLUTION: 将服务名的下划线改为中横线。

7 springboot启动报错

ERRORLOG:

2021-12-07 10:21:12-[TID: N/A]- ERROR --- com.alibaba.nacos.client.config.http.ServerHttpAgent                   : [NACOS ConnectException httpPost] currentServerAddr: http://localhost:8848, err : 拒绝连接 (Connection refused)
java.net.ConnectException: [NACOS HTTP-POST] The maximum number of tolerable server reconnection errors has been reached

RCA: 可能是系统没有发现bootstrap.yml配置文件,或者bootstrap.yml中配置的nacos上的配置文件没有被识别到,或没有被拉取到本地。
SOLUTION: 后来自己好了,好像也没做啥操作。

8 seata事务不生效

猜测1:和seata在nacos上注册的ip是 172.18.0.1(我的虚拟网卡的IP) 有关,seata-server可能要配置为该IP。 猜测错误。
猜测2:可能和这个配置项 seata.enable-auto-data-source-proxy: false 有关。确实是的,这个配置改为true后,就生效了。
但是又报第二个错误,并且一直在重复报该错误,如下:

2021-12-12 17:34:57-[TID: N/A]-  INFO --- io.seata.rm.AbstractRMHandler                                          : Branch Rollbacked result: PhaseTwo_RollbackFailed_Retryable
2021-12-12 17:34:58-[TID: N/A]-  INFO --- io.seata.core.rpc.processor.client.RmBranchRollbackProcessor           : rm handle branch rollback process:xid=172.18.0.1:8091:213348906720825344,branchId=213348911154204673,branchType=AT,resourceId=jdbc:mysql://localhost:3306/java_inventories,applicationData=null
2021-12-12 17:34:58-[TID: N/A]-  INFO --- io.seata.rm.AbstractRMHandler                                          : Branch Rollbacking: 172.18.0.1:8091:213348906720825344 213348911154204673 jdbc:mysql://localhost:3306/java_inventories
2021-12-12 17:34:58-[TID: N/A]-  INFO --- io.seata.rm.datasource.DataSourceManager                               : branchRollback failed. branchType:[AT], xid:[172.18.0.1:8091:213348906720825344], branchId:[213348911154204673], resourceId:[jdbc:mysql://localhost:3306/java_inventories], applicationData:[null]. reason:[Branch session rollback failed and try again later xid = 172.18.0.1:8091:213348906720825344 branchId = 213348911154204673 Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]]

原因是它一直在重试回滚事务,但每次都回滚失败,所以重复报错。
回滚失败的原因是,无法处理时间类型的序列化/返序列化。说时间格式不对,以下的undo_log中的数据中,时间格式是 2021-11-30T14:11:11,不符合它说的 yyyy-mm-dd hh:mm:ss[.fffffffff]]:

{"branchId":213340637168144385,"sqlUndoLogs":[{"afterImage":{"rows":[{"fields":[{"keyType":"PRIMARY_KEY","name":"id","type":12,"value":"1465564039652311041"},{"keyType":"NULL","name":"product_id","type":12,"value":"10012"},{"keyType":"NULL","name":"product_name","type":12,"value":"键盘"},{"keyType":"NULL","name":"stock_count","type":12,"value":"982"},{"keyType":"NULL","name":"delete_flag","type":4,"value":0},{"keyType":"NULL","name":"create_time","type":93,"value":"2021-11-30T14:11:11"},{"keyType":"NULL","name":"update_time","type":93,"value":"2021-12-12T16:32:07"}]}],"tableName":"biz_stock"},"beforeImage":{"rows":[{"fields":[{"keyType":"PRIMARY_KEY","name":"id","type":12,"value":"1465564039652311041"},{"keyType":"NULL","name":"product_id","type":12,"value":"10012"},{"keyType":"NULL","name":"product_name","type":12,"value":"键盘"},{"keyType":"NULL","name":"stock_count","type":12,"value":"984"},{"keyType":"NULL","name":"delete_flag","type":4,"value":0},{"keyType":"NULL","name":"create_time","type":93,"value":"2021-11-30T14:11:11"},{"keyType":"NULL","name":"update_time","type":93,"value":"2021-12-12T16:32:07"}]}],"tableName":"biz_stock"},"sqlType":"UPDATE","tableName":"biz_stock"}],"xid":"172.18.0.1:8091:213340630356594688"}

有2个解决方法:1 时间类型改为使用时间戳,不用datetime;2 由fastjson改为使用kryo序列化方式(jackson序列化器更加坑,不能用)。

9 gateway的过滤器(继承GlobalFilter)中使用openFeign报错

// 代码中注入了feignClient
@Autowired
private AuthenticateClient authenticateClient;

ERROR1:

Description:
Parameter 2 of constructor in com.example.ciic.gateway.filter.PreUaaFilter required a bean of type 'com.example.ciic.gateway.service.AuthenticateClient' that could not be found.
Action:
Consider defining a bean of type 'com.example.ciic.gateway.service.AuthenticateClient' in your configuration.

ACTION:
走了很多弯路,比如使用 ApplicationContext 获取feignClient对象,比如改用 WebClient 。

RCA1:
未在gateway的main类上添加注解 @EnableFeignClients,导致feignClient没有注入

SOLUTION1:增加如下注解 @EnableFeignClients:

@EnableFeignClients(defaultConfiguration = FeignConfig.class)
@SpringBootApplication
@Slf4j
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

NOTE:下次寻找解决方法前,可以先检查一遍使用feign的各项配置是否都做了,以免浪费时间。

ERROR2:

[reactor-http-epoll-3] com.example.ciic.gateway.filter.PreUaaFilter No qualifying bean of type 'org.springframework.boot.autoconfigure.http.HttpMessageConverters' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

SOLUTION2:
添加配置类注入 HttpMessageConverterConfig 的 bean(实际代码中我使用了 FeignConfig.java这个类名称,因为以后可能会配置feign的其他配置项):

package com.example.ciic.gateway.config;

import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;

import java.util.stream.Collectors;

@Configuration
public class HttpMessageConverterConfig {
    @Bean
    @ConditionalOnMissingBean
    public HttpMessageConverters messageConverters(ObjectProvider<HttpMessageConverter<?>> converters) {
        return new HttpMessageConverters(converters.orderedStream().collect(Collectors.toList()));
    }
}

ERROR3:
com.netflix.client.ClientException: Load balancer does not have available server for client: ciicsh-java-authenticate

ACTION3:
no action. 是nacos的问题,或者是网络问题。

ERROR4:

[302] during [POST] to [http://ciicsh-java-authenticate/authenticate/verifyToken] [AuthenticateClient#verifyToken(String)]: []
或者报这个错:cannot retry due to redirection, in streaming mode executing POST http://localhost:9010/authenticate/refreshToken

RCA4:
当未登录认证系统时,该请求 /authenticate/verifyToken 被重定向到登录页了:http://10.17.9.213:9010/login
上述报错信息中,302代表临时重定向。

SOLUTION4:
在认证系统的ShiroConfig.java中添加不拦截url的规则:

public ShiroFilterFactoryBean shiroFilterFactoryBean(SecurityManager securityManager) {
    ...
    filterChainDefinitionMap.put("/authenticate/**", "anon,captchaValidate");
    ...
}

ERROR5:

feign.codec.DecodeException: Could not extract response: no suitable HttpMessageConverter found for response type [interface io.jsonwebtoken.Claims] and content type [application/json]

RCA5:
测试过程中,我把 FeignConfig.java 删除了。

SOLUTION5:
和 SOLUTION2 一样。

ERROR6:
UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [interface io.jsonwebtoken.Claims] and content type [application/json]

RCA6:
我看了下,feign返回的对象转换为了LinkedHashMap。而不是我期望的 Claims 对象。

SOLUTION6:
把 feignClient 接口中的方法的返回值改为 HashMap<String, Object>

@FeignClient(value = "ciicsh-java-authenticate", path = "/authenticate")
public interface AuthenticateClient {

    @PostMapping("/verifyToken")
    HashMap<String, Object> verifyToken(Map<String, String> map);

    @PostMapping("/refreshToken")
    HashMap<String, Object> refreshToken(Map<String, String> map);
}

10 skywalking的gateway插件版本不对不兼容

SOLUTION:

# 查看gateway的版本
ll skywalking/agent/plugins/apm-spring-cloud-gateway*
# result: -rw-rw-r-- 1 witt witt 43569 12月  9 17:12 skywalking/agent/plugins/apm-spring-cloud-gateway-2.1.x-plugin-8.5.0.jar

# 删除gateway2.1.x
rm -f skywalking/agent/plugins/apm-spring-cloud-gateway-2.1.x-plugin-8.5.0.jar
# 替换为gateway2.0.x
cp skywalking/agent/optional-plugins/apm-spring-cloud-gateway-2.0.x-plugin-8.5.0.jar skywalking/agent/plugins/

11 springboot微服务启动报错,无法连接到nacos

ERROR:

ERROR [com.alibaba.nacos.client.config.security.updater] com.alibaba.nacos.client.security.SecurityProxy [SecurityProxy] login http request failed url: http://nacos.k8sdev.ciicsh.com:80/nacos/v1/auth/users/login, params: {username=java}, bodyMap: {password=java}, errorMsg: errCode: 100, errMsg: Nacos serialize for class [com.alibaba.nacos.common.http.HttpRestResult] failed.  

com.alibaba.nacos.client.config.impl.ClientWorker [fixed-nacos.k8sdev.ciicsh.com_80-java_demo] [sub-server-error] no right, dataId=gateway, group=DEFAULT_GROUP, tenant=java_demo
There was an unexpected error (type=Forbidden, status=403).unknown user!

一开始查网上资料,以为是nacos的需要配置用户名密码,还有context-path,yml文件中配置如下,然并卵:

spring:
  cloud:
    nacos:
      config:
        name: gateway
        namespace: ******
        # 此处必须要加端口号,否则会使用默认端口号8848
        server-addr: nacos-dev.com:8848
        file-extension: yaml
        username: java
        password: java
        context-path: /nacos  # 可不设置,默认就是这个值

RCA:是server-addr配置错误了,nacos-dev需要修改为nacos-test。修改为如下配置:

        server-addr: nacos-test.com:8848

12 springboot启动时无法加载skywalking插件

RCA:skywalking相关的启动参数配置到了 Program arguments中,如下图所示:

应该配置在 VM options中,如下图所示:

13 springboot启动报错 No Feign Client for loadBalancing defined

ERROR:
Caused by: java.lang.IllegalStateException: No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-netflix-ribbon or spring-cloud-starter-loadbalancer?

RCA:
我的openfeign和loadbalancer的版本都为3.0.4.
feign需要其他的依赖提供负载均衡功能,为此需要ribbon或者 loadbalancer,但是这个版本中,经过测试,必须去掉ribbon,添加loadbalancer

SOLUTION:
pom.xml中的配置如下:

		<!-- 服务调用 openFeign -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-netflix-ribbon</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- openFeign 依赖 loadbalancer 的负载均衡功能 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>

14

15

16

posted @ 2021-12-02 20:34  mediocrep  阅读(1683)  评论(1编辑  收藏  举报
既然选择了远方,便只顾风雨兼程!