SpringCloud學習中遇到的一些bug

bug

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,處理完請求之后,返回數(shù)據(jù)之前,在POJO轉化成JSON時,有些屬性違背輸出規(guī)則或者有些屬性循環(huán)引用會造成無法輸出。
  • 解決辦法:
    在實體類上面加上注解
@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"}

  • 錯誤原因:
    該請求不會成功,只要參數(shù)是復雜對象,即使指定了是GET方法,feign依然會以POST方法進行發(fā)送請求。可能是我沒找到相應的注解或使用方法錯誤。

  • 解決辦法:

    // 該請求不會成功,只要參數(shù)是復雜對象,即使指定了是GET方法,feign依然會以POST方法進行發(fā)送請求。可能是我沒找到相應的注解或使用方法錯誤。
    @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地址
  • 解決辦法:
    這是比較正常的現(xiàn)象,只需要再次刷新就好了

解決第一次請求報超時異常的方案

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文件,在文末加入以下內(nèi)容:
127.0.0.1 peer1 peer2 peer3

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

  • 錯誤原因:
    消費者調(diào)用服務時無服務可用
  • 解決辦法:
  1. 確定本機是否關閉防火墻
  2. 是否導入eureka的jar包
    <!-- 注冊Eureka服務 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
  1. 確定是否導入hystrix的jar包
    <!-- 配置hystrix所需依賴的包 -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
  1. 確定配置文件服務前面是否有空格


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

  • 錯誤原因:
    • 因為沒有請求任何東西,所以看不到內(nèi)容;
    • 缺少配置監(jiān)控的依賴
    • 配置環(huán)境不完善
  • 解決辦法:
     <!-- Actuator是SpringBoot提供的對應用系統(tǒng)的自省和監(jiān)控的集成功能,可以查看應用配置的詳細信息;
        如果提示 Unable to connect to Command Metric Stream.則需要引入以下包!
         -->
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency> 
    
    • 完善配置環(huán)境
     # 使用Hystrix Metrics Stream必備 
     management:
       endpoints: 
         web:
           exposure: 
             include: hystrix.stream   
    

turbine.stream一直是ping

  • 錯誤原因:
    • 因為沒有請求任何東西,所以看不到內(nèi)容;
    • 缺少配置監(jiān)控的依賴
    • Eureka服務注冊中心未將自己作為客戶端來嘗試注冊它自己
    # 注冊Eureka服務
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
    
    • 缺少配置監(jiān)控的依賴
  • 解決辦法:
     <!-- Actuator是SpringBoot提供的對應用系統(tǒng)的自省和監(jiān)控的集成功能,可以查看應用配置的詳細信息;
        如果提示 Unable to connect to Command Metric Stream.則需要引入以下包!
         -->
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency> 
    
    • Eureka服務注冊中心未將自己作為客戶端來嘗試注冊它自己
    # 注冊Eureka服務
    eureka:
      client:
    #    register-with-eureka: false
    #    fetch-registry: false
    
    • 添加配置的依賴(作為客服端的都需要配置)
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency> 
    

Health Indicator訪問無結果

  • 錯誤原因:
    • 未導入依賴包
    • 版本原因導致訪問方式改變了
  • 解決辦法:
    • 導入依賴包
     <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-actuator</artifactId>
     </dependency>
    
     {
         "_links": {
             "self": {
                 "href": "http://localhost:8030/actuator",
                 "templated": false
             },
             "health": {
                 "href": "http://localhost:8030/actuator/health",
                 "templated": false
             },
             "info": {
                 "href": "http://localhost:8030/actuator/info",
                 "templated": false
             }
         }
     }
    

Turbine監(jiān)控多個服務,配置后,出現(xiàn)只監(jiān)控到一部分服務情況

  • 錯誤原因:
    • 配置有問題
  • 解決辦法:
    • application.xml配置如下:
    # 0、配置多個監(jiān)控服務
    turbine:
      appConfig: microservice-consumer-goods-feign-with-hystrix,microservice-consumer-goods-ribbon-with-hystrix
      clusterNameExpression: "'default'"
    
    # 1、僅配置監(jiān)控一個服務
    turbine:
      aggregator:
        clusterConfig: MICROSERVICE-CONSUMER-GOODS-RIBBON-WITH-HYSTRIX
      appConfig: microservice-consumer-goods-ribbon-with-hystrix
    
    • 各個微服務的Controller配置

      每個微服務均需要加上如下注解:
    //配置hystrix所需注解
    @EnableCircuitBreaker
    

"description":"No key was installed for encryption service","status":"NO_KEY"

  • 錯誤描述
{
    "description": "No key was installed for encryption service",
    "status": "NO_KEY"
}
encrypt: 
  key: foobar

如果是在application.yml中配置秘鑰有可能讀取不到,依然報該錯誤

  • 查看JCE提供者,和一些相應算法
import java.security.*;

public class JceInfo {
    public static void main(String[] args) {
        System.out.println("-------列出加密服務提供者-----");
        Provider[] pro = Security.getProviders();
        for (Provider p : pro) {
            System.out.println("Provider:" + p.getName() + " - version:" + p.getVersion());
            System.out.println(p.getInfo());
        }
        
        System.out.println();
        System.out.println("-------列出系統(tǒng)支持的 消息摘要算法:");
        for (String s : Security.getAlgorithms("MessageDigest")) {
            System.out.println(s);
        }
        
        System.out.println();
        System.out.println("-------列出系統(tǒng)支持的生成 公鑰和 私鑰對的算法:");
        for (String s : Security.getAlgorithms("KeyPairGenerator")) {
            System.out.println(s);
        }
    }
}
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容