当前位置: 首页 > article >正文

java微服务的异常

1.依赖异常

须知:
【 如果项目的结构是单个模块的,需要给每个单个模块添加起步依赖 spring-boot-starter-parent,指定版本 】
【 如果项目的结构是子父模块的,只需要给父模块添加起步依赖 spring-boot-starter-parent,指定版本,所有子模块引入父模块就行 】

配置文件

  1. 你指定了该项目的启动端口号是 8080
  2. 你使用了阿里的 nacos 注册中心组件,定义了服务名 weather-service ,及其 Nacos注册中心的地址及端口号 localhost:8848
  3. 定义了集群 cluster-name :SH
  4. 定义了隔离空间 namespace:bf 6106 fa-ed 78-4 a 43-bf 22-779594 e 16 a 4 c

依赖文件

  1. 这里你可以看出该项目的结构是单个模块, 所以得加起步依赖 spring-boot-starter-parent,指定版本
  2. 这里使用了 nacos 注册中心,得加 spring-cloud 依赖和 spring-cloud-alibaba 依赖 , spring-cloud-alibaba 是根据 spring-cloud 开发的,所以使用 spring-cloud-alibaba 的所有组件都必须添加 spring-cloud 依赖,还得加 spring-cloud-starter-alibaba-nacos-discovery nacos 的注册中心依赖
  3. 而底下使用了 <dependencyManagement> <dependencies> 标签 spring-cloud-alibaba-dependencies ,spring-cloud-dependencie 统一了其组件下所有的依赖版本
server:  
  port: 8080  
  
spring:  
  
  #注册服务到Nacos  
  application:  
    name: weather-service #服务名  
  
  cloud:  
    nacos:  
      discovery:  
        server-addr: localhost:8848 #Nacos注册中心的地址及端口号  
        cluster-name: SH  
        namespace: bf6106fa-ed78-4a43-bf22-779594e16a4c     
<?xml version="1.0" encoding="UTF-8"?>  
<project xmlns="http://maven.apache.org/POM/4.0.0"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
    <modelVersion>4.0.0</modelVersion>  
  
    <groupId>cn.itcast</groupId>  
    <artifactId>xiaohua-service</artifactId>  
    <version>1.0-SNAPSHOT</version>  
    <packaging>jar</packaging>  
    <!--  
    war: 传统web项目  
    jar(默认):java项目   spring boot项目统一打包方式jar  
    pom:父工程-->  
  
  
    <!--jdk编译版本-->  
    <properties>  
        <java.version>1.8</java.version>  
    </properties>  
    <!--spring boot父工程-->  
    <parent>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-parent</artifactId>  
        <version>2.3.4.RELEASE</version>  
        <relativePath/> <!-- lookup parent from repository -->  
    </parent>  
  
  
    <dependencies>  
  
<!--        导入nacos注册中心组件-->  
        <dependency>  
            <groupId>com.alibaba.cloud</groupId>  
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>  
        </dependency>  
  
  
        <!--spring boot整合junit起步依赖-->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-test</artifactId>  
            <scope>test</scope>  
            <exclusions>                <exclusion>  
                    <groupId>org.junit.vintage</groupId>  
                    <artifactId>junit-vintage-engine</artifactId>  
                </exclusion>  
            </exclusions>  
        </dependency>  
        <!--spring boot整合springmvc起步依赖-->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-web</artifactId>  
        </dependency>  
        <!--spring boot整合mp起步依赖-->  
        <dependency>  
            <groupId>com.baomidou</groupId>  
            <artifactId>mybatis-plus-boot-starter</artifactId>  
            <version>3.4.2</version>  
        </dependency>  
        <!--lombok依赖-->  
        <dependency>  
            <groupId>org.projectlombok</groupId>  
            <artifactId>lombok</artifactId>  
        </dependency>  
        <!--fastjson的依赖-->  
        <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>fastjson</artifactId>  
            <version>1.2.76</version>  
        </dependency>  
        <!--apache工具包,提供大量工具类,简化操作,eg:判断字符串是否为""字符串...  
            StringUtils.isEmpty()            StringUtils.isBlank()        -->        <dependency>  
            <groupId>commons-lang</groupId>  
            <artifactId>commons-lang</artifactId>  
            <version>2.6</version>  
        </dependency>  
        <!--mysql驱动-->  
        <dependency>  
            <groupId>mysql</groupId>  
            <artifactId>mysql-connector-java</artifactId>  
            <scope>runtime</scope>  
        </dependency>  
        <!--durid连接池依赖-->  
        <dependency>  
            <groupId>com.alibaba</groupId>  
            <artifactId>druid-spring-boot-starter</artifactId>  
            <version>1.1.23</version>  
        </dependency>  
  
        <dependency>            <groupId>com.alibaba.cloud</groupId>  
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>  
        </dependency>  
  
  
    </dependencies>  
  
    <dependencyManagement>        <dependencies>  
            <dependency>  
                <groupId>org.springframework.cloud</groupId>  
                <artifactId>spring-cloud-dependencies</artifactId>  
                <version>Greenwich.RELEASE</version>  
                <type>pom</type>  
                <scope>import</scope>  
            </dependency>  
            <dependency>                <groupId>com.alibaba.cloud</groupId>  
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>  
                <version>2.1.0.RELEASE</version>  
                <type>pom</type>  
                <scope>import</scope>  
            </dependency>  
        </dependencies>  
  
    </dependencyManagement>  
  
    <build>        <plugins>  
            <!--spring boot项目打jar包插件  
                只有引入该插件,打成jar包才可以使用java -jar 运行  
            -->  
            <plugin>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-maven-plugin</artifactId>  
                <version>2.4.5</version>  
            </plugin>  
        </plugins>  
    </build>  
</project>

2. Nacos 程序没启动

问题

当 Nacos 程序没启动时/ Nacos 没有指定单体启动时(Nacos 默认集群启动) ,会导致 Nacos 服务启动失败

2024-11-02 00:08:41.354 ERROR 37164 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : nacos registry, weather-service register failed...NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties{serverAddr='localhost:8848', endpoint='', namespace='', watchDelay=30000, logName='', service='weather-service', weight=1.0, clusterName='DEFAULT', namingLoadCacheAtStart='false', metadata={preserved.register.source=SPRING_CLOUD}, registerEnabled=true, ip='192.168.240.94', networkInterface='', port=8080, secure=false, accessKey='', secretKey=''}},

java.lang.IllegalStateException: failed to req API:/nacos/v1/ns/instance after all servers([localhost:8848]) tried: failed to req API:localhost:8848/nacos/v1/ns/instance. code:500 msg: java.net.ConnectException: Connection refused: connect

解决

  1. 查找 nacos 是否启动
    image.png
  1. 启动 Nacos 服务,指定其单体执行
    image.png

3. Linux 的静态 ip 失效

查询 ip addr ,静态ip 没分配
image.png

尝试重启网络,无效果
image.png

解决方案如下

image.png
image.png

4. Feign 的异常

1. Feign 请求参数异常(2)

错误原因是: feign 的请求参数不完全

6:09:05: 856  INFO 58348 --- [           main] com.hmall.search.feignTest               : No active profile set, falling back to default profiles: default
16:09:06:180  WARN 58348 --- [           main] o.m.s.mapper.ClassPathMapperScanner      : No MyBatis mapper was found in '[com.hmall]' package. Please check your configuration.
16:09:06:229  INFO 58348 --- [           main] o.s.cloud.context.scope.GenericScope     : BeanFactory id=24dddd9d-1203-3d7a-841c-d55b879e19b0
16:09:06:541  WARN 58348 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
16:09:06:541  INFO 58348 --- [           main] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
16:09:06:547  WARN 58348 --- [           main] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.

feign.FeignException$NotFound: [404] during [GET] to [http://item-service/list?page=1&size=5] [ItemClientFeign#pagelist(Integer,Integer)]: [{"timestamp":"2024-11-16T08:09:10.069+00:00","status":404,"error":"Not Found","message":"","path":"/list"}]

解决

image.png

2. FeignClient 注解注册重复
***************************
APPLICATION FAILED TO START
***************************

Description:
The bean ‘xxx.FeignClientSpecification‘ could not be registered.

The bean 'xxx.FeignClientSpecification' could not be registered. A bean with that name has already been defined and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

解决

  1. 方法 1 多个 feign 接口使用@FeignClient 注解调用同一个名称的微服务时,启动时会引发此异常。修改即可
  2. 方法 2: 在配置文件中增加配置 spring.main.allow-bean-definition-overriding=true
3. Feign 远程 A调用 B,A 异常,B 也会报错(B 报的错)

A 服务报的错会比较模糊

feign. FeignException$InternalServerError: [500] during [GET] to [http://user-service/user/getUserById?id=2] [UserClientFeign #getUserById (Long)]: [{“timestamp”: “2024-11-18 T14:39:28.153+00:00”,“status”: 500,“error”: “Internal Server Error”,“message”: “”,“path”: “/user/getUserById”}]

B 服务就很清楚

SQL: SELECT id, username, password, phone, status, balance, create_time, update_time FROM tb_user WHERE id=?
Cause: com. Mysql. Jdbc. Exceptions. Jdbc 4. MySQLSyntaxErrorException: Unknown column ‘create_time’ in ‘field list’
; bad SQL grammar []; nested exception is com. Mysql. Jdbc. Exceptions. Jdbc 4. MySQLSyntaxErrorException: Unknown column ‘create_time’ in ‘field list’] with root cause
Com. Mysql. Jdbc. Exceptions. Jdbc 4. MySQLSyntaxErrorException: Unknown column ‘create_time’ in ‘field list’
At java. Base/jdk. Internal. Reflect. NativeConstructorAccessorImpl. NewInstance 0 (Native Method) ~[na: na]
At java. Base/jdk.Internal.Reflect.NativeConstructorAccessorImpl.NewInstance (NativeConstructorAccessorImpl. Java:62) ~[na: na]
At java. Base/jdk.Internal.Reflect.DelegatingConstructorAccessorImpl.NewInstance (DelegatingConstructorAccessorImpl. Java:45) ~[na: na]
At java. Base/java.Lang.Reflect.Constructor.NewInstance (Constructor. Java:490) ~[na: na]

image.png

5. 网关的异常

1. Yml配置请求头看似不生效(网关, gateway)

其实你去看前端页面的请求头,是没有设置的请求头,是因为网关是在前端发送请求后后端服务前才生效的,所以访问后端服务时,需要先通过网关
处理,你可以给网关设置访问校验

有三种配置方式

  1. 设置默认过滤器添加请求头
spring:  
application:  
  name: gatway-service  
cloud:  
  nacos:  
    discovery:  
      server-addr: localhost:8848  
  gateway:  
    routes:  
      - id: item-service  
        uri: lb://item-service  
        predicates:  
          - Path=/item/**  
        
#给所有路由添加请求头
default-filters:
  - AddRequestHeader=authorization, 2
  1. 设置单个路由设置过滤器添加请求头
spring:  
  application:  
    name: gatway-service  
  cloud:  
    nacos:  
      discovery:  
        server-addr: localhost:8848  
    gateway:  
      routes:  
        - id: item-service  
          uri: lb://item-service  
          predicates:  
            - Path=/item/**  
#给单个路由添加请求头
          filters:  
            - AddRequestHeader=authorization,2
  1. 创建 GlobalFilter 接口实现类

image.png

6. 其他异常

1. Yam 语法配错

No converter found capable of converting from type [java. Lang. String] to type [java.Util.Map (java. Long)

解决方案: 修改 application.Yml 即可

2. Lang 类型转为 JSON 精度丢失

http://www.kler.cn/a/511432.html

相关文章:

  • Tesla Free-Fall Attack:特斯拉汽车网络安全事件纪要
  • 强推未发表!3D图!Transformer-LSTM+NSGAII工艺参数优化、工程设计优化!
  • C链表的一些基础知识
  • RabbitMQ---事务及消息分发
  • 大语言模型的语境中“越狱”和思维链
  • 日志收集Day002
  • 【MathType】mathtype在word中格式问题
  • 【Leetcode 热题 100】55. 跳跃游戏
  • IT程序设计文档,软件需求设计文档,详细设计模板(Word原件)
  • windows蓝牙驱动开发-蓝牙设备栈
  • c.p.api.config.MyAuthenticationProvider
  • 动手学大数据-3社区开源实践
  • leetcode——分割两个字符串得到一个回文字符串(java)
  • C# 中yield关键字:解锁高效迭代的魔法钥匙
  • 【北京迅为】iTOP-4412全能版使用手册-第八十五章 一键烧写QT程序到开发板
  • 批量清理docker 容器日志
  • springboot基于小程序的会宁县周边乡村旅游服务系统
  • ScratchLLMStepByStep:训练自己的Tokenizer
  • JAVA安全—JWT攻防Swagger自动化Druid泄露
  • 03_UI自适应
  • 《AI赋能中国制造2025:智能变革,制造未来》
  • 正态分布检验(JB检验和威尔克检验)和斯皮尔曼相关系数(继上回)
  • 2025年01月18日Github流行趋势
  • 多语言插件i18n Ally的使用
  • Android-Gradle-自动化多渠道打包
  • 【Docker】Supervisor 实现单容器运行多服务进程