1

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

我正在使用 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>
4

1 回答 1

-1

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

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

POST到 /..../{userId}/createShoppingCart 看起来也不正确。API 设计中推荐使用名词REST,/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>

于 2017-04-21T02:07:24.300 回答