2017-04-20 330 views
1

我在服務間通信負載平衡方面存在問題。Feign客戶端執行失敗:java.lang.reflect.InvocationTargetException

我正在使用netflixOSS的spring(1.4.2),spring cloud。

我有兩項服務shoppingcart-service和user-service。

這裏是ShoppingCartController從shopppingcart服務應用:

@RestController 
@RequestMapping("shoppingCarts") 
public class ShoppingCartController extends AbstractRESTController<ShoppingCart, String>{ 

private ShoppingCartService shoppingCartSrevice; 

@Autowired 
public ShoppingCartController(ShoppingCartService service) { 
    super(service); 
    this.shoppingCartSrevice = service; 
} 

@RequestMapping(value = "{userId}/createShoppingCart", method = RequestMethod.POST) 
ShoppingCart createShoppingCart(
     @RequestBody List<CartItem> items, 
     @PathVariable(name = "userId") String userId 
     ){ 

    Boolean userOK = shoppingCartSrevice.checkUser(userId); 

    if(userOK != null) 
     if(userOK) 
      return shoppingCartSrevice.createShoppingCart(items, userId); 

    return null; 
} 

@FeignClient("user-service")//the server.port property name, for the "server" service 
public interface UserServiceClient { 
    @RequestMapping(value = "users/checkUser", method = RequestMethod.POST)// the endpoint which will be balanced over 
    Boolean checkUser(
      @RequestParam(name = "userId") String userId);// the method specification must be the same as for users/hello 
} 

}

「checkUser」

methosd是ShoppingCartService類:

@Service 
public class ShoppingCartService extends AbstractCRUDService<ShoppingCart, String>{ 

private ShoppingCartRepository shoppingCartRepository; 
private RestTemplate restTemplate; 

@Autowired 
private UserServiceClient userServiceClient;// feign client 

@Autowired 
public ShoppingCartService(ShoppingCartRepository repo, RestTemplate restTemplate) { 
    super(repo); 
    this.shoppingCartRepository = repo; 
    this.restTemplate = restTemplate; 

} 

/** 
* Method checks if the given user is registered and active 
* We use Ribbon and Feign to get data from user-service, load-balancing 
* @param userId 
* @return 
*/ 
@HystrixCommand(fallbackMethod="fallbackCheckUser") 
public Boolean checkUser(String userId) { 

    /*USING LOAD-BALANCING*/ 
    Boolean resp = userServiceClient.checkUser(userId);//HERE I GET THE EXCEPTION 
    return resp; 
} 

public Boolean fallbackCheckUser(String userId){ 
    return true; 
} 

當我嘗試執行checkUser(userId)@HystricsCommand方法時,得到:java.lang.reflect.InvocationTargetException。

請幫助。

更新1:

購物車服務的pom.xml:

<?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>rs.uns.acs.ftn</groupId> 
    <artifactId>ShoppingCartService</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>ShoppingCartService</name> 
    <description>Shopping Cart Service</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.2.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-starter-feign</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-starter-hystrix</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-starter-ribbon</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-actuator</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-starter-eureka</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-data-mongodb</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 
     <dependency> 
      <groupId>org.jglue.fluent-json</groupId> 
      <artifactId>fluent-json</artifactId> 
      <version>2.0.3</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <dependencyManagement> 
     <dependencies> 
      <dependency> 
       <groupId>org.springframework.cloud</groupId> 
       <artifactId>spring-cloud-dependencies</artifactId> 
       <version>Camden.SR2</version> 
       <type>pom</type> 
       <scope>import</scope> 
      </dependency> 
     </dependencies> 
    </dependencyManagement> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 


</project> 
+0

請添加更多的例外。那條單線是無用的。 – spencergibb

+0

在異常對象上的堆棧跟蹤是空的,我也沒有在控制檯上得到任何輸出。我還與Eureka和Zuul一起運行了3項服務。我將把pom.xml放到shoppingcart-service中,請參閱更新。 –

+0

我不知道如何提供幫助,除非你能提供樣品。 Camden.SR6是卡姆登發行版中的最新版本。 – spencergibb

回答

0

Spring CloudFeignRibbon拋開這些 「REST」 端點似乎並沒有遵循推薦的做法。

POST檢查用戶是否存在看起來不正確,POST通常用於創建資源,如用戶或產品。

POST to /..../{userId}/createShoppingCart看起來不正確。在REST中推薦名詞:API設計,/ users,/ products,/ users/{id}和HTTP動詞(POST,PUT,PATCH,DELETE,GET,....)表示對這些「名詞」的操作:POST/users意味着創建一個用戶,PUT/products/{id}意味着更新id爲{id}的產品。通常不建議將動詞作爲URL的一部分。

而@spencergibb提到,沒有配置文件(application.yml或properties),源代碼和有意義的堆棧跟蹤,將會很難解決這個問題。

一個可能的問題可能是您的pom文件缺少<properties>中的<start-class>元素。

+0

HTTP API規範不同於用例與用例。但是這與我現在的問題有關。 –