java微服务的异常

embedded/2025/1/22 18:30:32/

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 服务启动失败

java">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.ppmy.cn/embedded/156123.html

相关文章

owasp SQL 注入-03 (原理)

1: 先看一下注入界面: 点submit 后&#xff0c;可以看到有语法报错&#xff0c;说明已经起作用了: 报如下的错误: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near at line 1 2:…

国产化中间件东方通TongWeb环境安装部署(图文详解)

文章目录 前言一、TongWeb部署环境介绍二、安装步骤 1.导入安装包2.安装3.登录网页 总结 前言 中国政府一直致力于促进国内产业的发展和国产化进程。作为全球第二大经济体&#xff0c;中国政府部门在推动国产化方面发挥着至关重要的作用。本文是东方通中间件TongWeb的详细安装…

SQL中字符串截取函数(substring)

1.left&#xff08;name&#xff0c;4&#xff09;截取左边的四个字符 案例 select left(20240428,4) 结果 2024 2.right&#xff08;name&#xff0c;2&#xff09;截取右边的2个字符 案例 select right(20240428,2) 结果 28 3.substring(name,5,3)截取name这个字段从第五个字…

Android SystemUI——CarSystemBar视图解析(十一)

前面文章我们已经把 CarSystemBar 从启动到构建视图,再到将视图添加到 Window 的流程分析完毕,我们知道默认情况下在车载系统中只显示顶部栏和底部栏视图的。这里我们在前面文章的基础上以顶部栏为例具体解析其视图的结构。 一、顶部栏解析 通过《CarSystemBar车载状态栏》这…

HCIP笔记4--OSPF域内路由计算

1. 域内LSA 1.1 一类LSA 一类LSA: 路由器直连状态&#xff0c;Router LSA。 串口需要两端配置好IP,才会产生一类LSA; 以太网口只需要一端配置了IP就会直接产生一类LSA。 LSA通用头部 Type: Router 直连路由LS id: 12.1.1.1 路由器router idAdv rtr: 12.1.1.1 通告的路由器&…

Java高频面试之SE-15

hello啊&#xff0c;各位观众姥爷们&#xff01;&#xff01;&#xff01;本牛马baby今天又来了&#xff01;哈哈哈哈哈嗝&#x1f436; String 怎么转成 Integer 的&#xff1f;它的原理是&#xff1f; 在 Java 中&#xff0c;要将 String 转换为 Integer 类型&#xff0c;可…

centos9编译安装opensips 二【进阶篇-定制目录+模块】推荐

环境&#xff1a;centos9 last opensips -V version: opensips 3.6.0-dev (x86_64/linux) flags: STATS: On, DISABLE_NAGLE, USE_MCAST, SHM_MMAP, PKG_MALLOC, Q_MALLOC, F_MALLOC, HP_MALLOC, DBG_MALLOC, CC_O0, FAST_LOCK-ADAPTIVE_WAIT ADAPTIVE_WAIT_LOOPS1024, MAX_RE…

Linux使用SSH连接GitHub指南

基础配置流程 步骤1:生成SSH密钥 打开终端:首先,打开你的Linux终端。 生成SSH密钥对:输入以下命令来生成一个新的SSH密钥对: ssh-keygen -t rsa -b 4096 -C "your_email@example.com"-t rsa:使用RSA加密算法生成密钥。-b 4096:密钥长度为4096位,增加安全性。…