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,處理完請求以後,返回數據以前,在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"}html

  • 錯誤緣由: 該請求不會成功,只要參數是複雜對象,即便指定了是GET方法,feign依然會以POST方法進行發送請求。多是我沒找到相應的註解或使用方法錯誤。java

  • 解決辦法:git

// 該請求不會成功,只要參數是複雜對象,即便指定了是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!)github

  • 錯誤緣由:
    • 同一個服務重複啓了;
    • 或者是端口被其餘應用佔用了。
  • 解決辦法: 釋放被佔用的端口便可

Read timed out

  • 詳細描述: com.sun.jersey.api.client.ClientHandlerException: java.net.SocketTimeoutException: Read timed out
  • 錯誤緣由: 應用剛啓動,須要經過ribbon從eureka上拉取服務;須要將虛擬主機名轉化爲ip地址
  • 解決辦法: 這是比較正常的現象,只須要再次刷新就行了

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

  • 錯誤緣由: 網絡等緣由,致使請求時間過長,進而引起timeout的錯誤
  • 解決辦法:
    • 默認時間是超過1秒就是超時,將其設置爲5秒
    hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 5000
    複製代碼
    • 禁用超時時間timeout
    hystrix.command.default.execution.timeout.enabled: false
    複製代碼
    • 索性禁用feign的hystrix支持
    feign.hystrix.enabled: false  
    複製代碼

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>
複製代碼
  1. 肯定是否導入hystrix的jar包
<!-- 配置hystrix所需依賴的包 -->
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
	</dependency>
複製代碼
  1. 肯定配置文件服務前面是否有空格
    image

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

  • 錯誤緣由:
    • 由於沒有請求任何東西,因此看不到內容;
    • 缺乏配置監控的依賴
    • 配置環境不完善
  • 解決辦法:
    • 訪問一下其餘服務,好比http://localhost:9000/goods/2(本身寫的一個服務)便可看到內容
    • 客服端添加依賴
    <!-- Actuator是SpringBoot提供的對應用系統的自省和監控的集成功能,能夠查看應用配置的詳細信息;
        若是提示 Unable to connect to Command Metric Stream.則須要引入如下包!
     	-->
     <dependency>
     	<groupId>org.springframework.boot</groupId>
     	<artifactId>spring-boot-starter-actuator</artifactId>
     </dependency> 
    複製代碼
    • 完善配置環境
    # 使用Hystrix Metrics Stream必備 
     management:
       endpoints: 
         web:
           exposure: 
             include: hystrix.stream   
    複製代碼

turbine.stream一直是ping

  • 錯誤緣由:
    • 由於沒有請求任何東西,因此看不到內容;
    • 缺乏配置監控的依賴
    • Eureka服務註冊中心未將本身做爲客戶端來嘗試註冊它本身
    # 註冊Eureka服務
    eureka:
      client:
        register-with-eureka: false
        fetch-registry: false
    複製代碼
    • 缺乏配置監控的依賴
  • 解決辦法:
    • 訪問一下其餘服務,好比http://localhost:9000/goods/2(本身寫的一個服務)便可看到內容
    • 客服端添加依賴
    <!-- Actuator是SpringBoot提供的對應用系統的自省和監控的集成功能,能夠查看應用配置的詳細信息;
        若是提示 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>
    複製代碼
    • 訪問http://localhost:8030/actuator查看
    {
         "_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
             }
         }
     }
    複製代碼
    • 訪問http://localhost:8030/actuator/health便可

Turbine監控多個服務,配置後,出現只監控到一部分服務狀況

  • 錯誤緣由:
    • 配置有問題
  • 解決辦法:
    • application.xml配置以下:
    # 0、配置多個監控服務
    turbine:
      appConfig: microservice-consumer-goods-feign-with-hystrix,microservice-consumer-goods-ribbon-with-hystrix
      clusterNameExpression: "'default'"
    複製代碼
    # 一、僅配置監控一個服務
    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"
}
複製代碼
  • 錯誤緣由:web

    • jce安裝有問題
    • 沒有配置對應的密鑰或未讀取到配置文件中的祕鑰
    • SpringCloud Config的.yml文件配置有問題
  • 解決辦法:算法

encrypt: 
 key: foobar
複製代碼

若是是在application.yml中配置祕鑰有可能讀取不到,依然報該錯誤spring

  • 查看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("-------列出系統支持的 消息摘要算法:");
		for (String s : Security.getAlgorithms("MessageDigest")) {
			System.out.println(s);
		}
		
		System.out.println();
		System.out.println("-------列出系統支持的生成 公鑰和 私鑰對的算法:");
		for (String s : Security.getAlgorithms("KeyPairGenerator")) {
			System.out.println(s);
		}
	}
}
複製代碼
相關文章
相關標籤/搜索