我在服务间通信负载平衡方面遇到问题。
我正在使用 spring (1.4.2),spring cloud 和 netflixOSS。
我有两个服务购物车服务和用户服务。
这是来自 shopppingcart-service 应用程序的 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
}
}
“检查用户”
方法来自 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>