与ribbon / eureka一起使用时,不要忘记客户端错误

时间:2015-02-10 19:56:01

标签: java spring netflix-feign netflix-eureka

@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 app运行。 abc-service在启动时会向Eureka注册。

1 个答案:

答案 0 :(得分:1)

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

@SpringApplicationConfiguration(name="abc-service")

由FeignClient使用