我在服务间通信负载平衡方面存在问题。
我使用spring(1.4.2),spring cloud with netflixOSS。
我有两个服务shoppingcart-service和user-service。
以下是来自shopppingcart-service app的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
}
}
&#34; checkUser&#34;
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:
shoppingcart-service 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 :(得分:0)
Spring Cloud
,Feign
,Ribbon
除了这些&#34; REST
&#34;终点似乎不遵循建议的做法。
POST
检查用户是否存在并不正常,POST
通常用于创建资源,例如用户或产品。
POST
到/..../{userId}/createShoppingCart看起来也不正确。在REST
API设计,/ users,/ products,/ users / {id}和HTTP谓词(POST,PUT,PATCH,DELETE,GET,....)中建议使用名词代表那些&#34上的操作;名词&#34;:POST / users表示创建用户,PUT / products / {id}表示更新id为{id}的产品。通常不建议将动词作为URL的一部分。
正如@spencergibb所提到的,没有配置文件(application.yml或属性),源代码和有意义的堆栈跟踪,很难解决这个问题。
一个可能的问题可能是您的pom
文件错过了<start-class>
中的<properties>
元素。