2
@Component
@EnableFeignClients
public class ABCClientApp {

    @Autowired
    ABCClient client;

    public TenantClientApp() {
        // TODO Auto-generated constructor stub
    }

    public ABC getABC(String abcId) {
        return client.getABC(abcId);
    }

    @FeignClient("abc-service")
    public interface ABCClient {

        @RequestMapping(method = RequestMethod.GET, value = "/abc/{abcId}")
        ABC getABC(@PathVariable("abcId") String abcId);

    }

}

以下是上述课程的测试:

@Configuration
@EnableDiscoveryClient
@ComponentScan("com.abc.client.rest")
@EnableAutoConfiguration(exclude={org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration.class,org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration.class})
@SpringApplicationConfiguration(classes=ABCClientAppTest.class)
public class ABCClientAppTest extends AbstractTestNGSpringContextTests{

    @Autowired
    ABCClientApp app;

    @Test
    public void test_getABC() {
        String abcId = "250449AD17E1";
        app.getABC(abcId);
    }

}

当我使用 TestNG 运行测试时,会引发以下错误:

com.netflix.client.ClientException:负载均衡器没有可用于客户端的服务器:abc-service

Eureka 服务器被配置为查找配置服务器。 application.yml尤里卡服务器如下:

info:
  description: Eureka Service Registry

server:
  port: 8761

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

security:
  user:
    password: password

and bootstrap.yml is as follows:

spring:
  application:
    name: eureka
  cloud:
    config:
      enabled: false

在运行上述测试之前,配置服务器、eureka 服务器和 abc-service 作为 spring boot 应用程序运行。abc-service 在启动时向 Eureka 注册。

4

1 回答 1

1

您的客户端 ABCClientAppTest 必须使用应用程序名称注册:

@SpringApplicationConfiguration(name="abc-service")

FeignClient 使用的

于 2015-03-31T13:38:56.753 回答