gpt4 book ai didi

java - Feign客户端执行失败 : java. lang.reflect.InitationTargetException

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:31 25 4
gpt4 key购买 nike

我在服务间通信负载平衡方面遇到问题。

我正在使用 spring (1.4.2)、spring cloud 和 netflixOSS。

我有两项服务:购物车服务和用户服务。

这是来自购物车服务应用程序的 ShoppingCartController:

@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"

方法来自 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.InitationTargetException。

请帮忙。

更新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>

最佳答案

Spring Cloud , Feign , Ribbon除了这些“REST ”端点似乎没有遵循推荐的做法。

POST检查用户是否存在看起来不正确,POST通常用于创建资源,例如用户或产品。

POST/..../{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 或属性)、源代码和有意义的堆栈跟踪,就很难解决这个问题。

一个可能的问题可能是您的 pom文件缺少 <start-class>里面的元素 <properties> .

关于java - Feign客户端执行失败 : java. lang.reflect.InitationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43526557/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com