微服务系列文章 之 SpringCloud中遇到的一些bug

1、There was a problem with the instance info replicator

  • 错误原因: 该服务尝试将自己作为客服端注册

  • 解决办法: 在application.yml配置文件中,设置

    注册Eureka服务

    eureka:
    client:
    # Eureka服务注册中心会将自己作为客户端来尝试注册它自己,必須禁止
    register-with-eureka: false
    fetch-registry: false

实体类转化出错: disable SerializationFeature.FAIL_ON_EMPTY_BEANS

  • 错误原因: 使用的框架是Spring Boot,处理完请求之后,返回数据之前,在POJO转化成JSON时,有些属性违背输出规则或者有些属性循环引用会造成无法输出。

  • 解决办法: 在实体类上面加上注解

    @JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler" })

This application has no explicit mapping for /error, so you are seeing this as a fallback.

  • 错误原因: 很可能是你Application启动类放的位置不对。要将Application放在最外层,也就是要包含所有子包。
  • 解决办法: 将Application放在最外层,也就是要包含所有子包。

message:Request method 'POST' not supported

There was an unexpected error (type=Internal Server Error, status=500). status 405 reading UserFeignClient#getUser(User); content: {"timestamp":"2018-09-07T09:01:14.290+0000","status":405,"error":"Method Not Allowed","message":"Request method 'POST' not supported","path":"/feign-get-user"}

  • 错误原因: 该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。可能是我没找到相应的注解或使用方法错误。

  • 解决办法:

    // 该请求不会成功,只要参数是复杂对象,即使指定了是GET方法,feign依然会以POST方法进行发送请求。可能是我没找到相应的注解或使用方法错误。
    @RequestMapping(method = RequestMethod.GET, value = "/feign-get-user")
    public User getUser(User user);

    复制代码
      //正确用法
      @RequestMapping(method = RequestMethod.GET, value = "/feign-get-user")
      public User getUser(@RequestParam("id") Long id, @RequestParam("username") String username, @RequestParam("age") String age);

    }

Error creating bean with name 'eurekaAutoServiceRegistration'

org.springframework.beans.factory.BeanCreationNotAllowedException: Error creating bean with name 'eurekaAutoServiceRegistration': Singleton bean creation not allowed while singletons of this factory are in destruction (Do not request a bean from a BeanFactory in a destroy method implementation!)

  • 错误原因:
    • 同一个服务重复启了;
    • 或者是端口被其他应用占用了。
  • 解决办法: 释放被占用的端口即可

Read timed out

  • 详细描述: com.sun.jersey.api.client.ClientHandlerException: java.net.SocketTimeoutException: Read timed out
  • 错误原因: 应用刚启动,需要通过ribbon从eureka上拉取服务;需要将虚拟主机名转化为ip地址
  • 解决办法: 这是比较正常的现象,只需要再次刷新就好了

解决第一次请求报超时异常的方案

Cannot execute request on any known server

  • 错误详细描述: com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server

  • 错误原因: Peer Awareness配置

    application.yml (Two Peer Aware Eureka Servers).


    spring:
    profiles: peer1
    eureka:
    instance:
    hostname: peer1
    client:
    serviceUrl:
    defaultZone: http://peer2/eureka/


    spring:
    profiles: peer2
    eureka:
    instance:
    hostname: peer2
    client:
    serviceUrl:
    defaultZone: http://peer1/eureka/

  • 解决办法: 修改C:\Windows\System32\drivers\etc路径下的hosts文件,在文末加入以下内容:

    127.0.0.1 peer1 peer2 peer3

com.netflix.client.ClientException: Load balancer does not have available server for client: microservice-provider-user

  • 错误原因: 消费者调用服务时无服务可用
  • 解决办法:
  1. 确定本机是否关闭防火墙

  2. 是否导入eureka的jar包

    复制代码
     <!-- 注册Eureka服务 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
  3. 确定是否导入hystrix的jar包

    复制代码
     <!-- 配置hystrix所需依赖的包 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
  4. 确定配置文件服务前面是否有空格

Unable to connect to Command Metric Stream

  • 错误原因: 配置文件不完整

  • 解决办法: Hystrix Metrics Stream 要启用Hystrix度量标准流,请在spring-boot-starter-actuator上包含依赖项,并设置management.endpoints.web.exposure.include:hystrix.stream。 这样做会将 /actuator/hystrix.stream公开为管理端点,如以下示例所示:

    • pom.xml

      <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId>
      复制代码
      </dependency>
  • application.yml

    配置Hystrix Metrics Stream

    复制代码
      management:
        endpoints:
          web:
            exposure:
              include: hystrix.stream

hystrix.stream一直是ping

  • 错误原因:

    • 因为没有请求任何东西,所以看不到内容;
    • 缺少配置监控的依赖
    • 配置环境不完善
  • 解决办法:

  • 完善配置环境

    使用Hystrix Metrics Stream必备

    management:
    endpoints:
    web:
    exposure:
    include: hystrix.stream

turbine.stream一直是ping

  • 错误原因:
    • 因为没有请求任何东西,所以看不到内容;

    • 缺少配置监控的依赖

    • Eureka服务注册中心未将自己作为客户端来尝试注册它自己

      复制代码
      # 注册Eureka服务
      eureka:
        client:
          register-with-eureka: false
          fetch-registry: false
    • 缺少配置监控的依赖

Turbine监控多个服务,配置后,出现只监控到一部分服务情况

  • 错误原因:

    • 配置有问题
  • 解决办法:

    • application.xml配置如下:

      0、配置多个监控服务

      turbine:
      appConfig: microservice-consumer-goods-feign-with-hystrix,microservice-consumer-goods-ribbon-with-hystrix
      clusterNameExpression: "'default'"

      1、仅配置监控一个服务

      turbine:
      aggregator:
      clusterConfig: MICROSERVICE-CONSUMER-GOODS-RIBBON-WITH-HYSTRIX
      appConfig: microservice-consumer-goods-ribbon-with-hystrix

  • 各个微服务的Controller配置
    每个微服务均需要加上如下注解:

    //配置hystrix所需注解
    @EnableCircuitBreaker

相关推荐
Chase_Mos7 小时前
Spring 必会之微服务篇(1)
java·spring·微服务
撸码到无法自拔9 小时前
docker常见命令
java·spring cloud·docker·容器·eureka
佩奇的技术笔记12 小时前
Java学习手册:微服务设计原则
java·微服务
千里马学框架12 小时前
重学安卓14/15自由窗口freeform企业实战bug-学员作业
android·framework·bug·systrace·安卓framework开发·安卓窗口系统·自由窗口
代码的奴隶(艾伦·耶格尔)14 小时前
微服务!!
微服务·云原生·架构
码农飞哥16 小时前
互联网大厂Java面试实战:Spring Boot到微服务的技术问答解析
java·数据库·spring boot·缓存·微服务·消息队列·面试技巧
Auc2416 小时前
OJ判题系统第4期之判题机模块架构——设计思路、实现步骤、代码实现(工厂模式、代理模式的实践)
java·spring cloud·log4j·mybatis·代理模式·工厂模式
掘金-我是哪吒17 小时前
分布式微服务系统架构第126集:集群,数据库扩展,多节点分布,分库,分表,分片,分表,运维
运维·数据库·分布式·微服务·系统架构
RingWu1 天前
微服务架构-限流、熔断:Alibaba Sentinel入门
微服务·架构·sentinel
李匠20241 天前
C++GO语言微服务基础技术②
开发语言·c++·微服务·golang