使用 Spring 云 FeignClients 时出现 404

404 while using Spring cloud FeignClients

这是我的设置:

第一个服务 (FlightIntegrationApplication) 使用 FeignClients API 和 Eureka 调用第二个服务 (BaggageServiceApplication)。

github 上的项目:https://github.com/IdanFridman/BootNetflixExample

第一次服务:

@SpringBootApplication
@EnableCircuitBreaker
@EnableDiscoveryClient
@ComponentScan("com.bootnetflix")
public class FlightIntegrationApplication {

    public static void main(String[] args) {
        new SpringApplicationBuilder(FlightIntegrationApplication.class).run(args);
    }

}

在其中一个控制器中:

    @RequestMapping("/flights/baggage/list/{id}")
    public String getBaggageListByFlightId(@PathVariable("id") String id) {
        return flightIntegrationService.getBaggageListById(id);
    }

飞行集成服务:

    public String getBaggageListById(String id) {
        URI uri = registryService.getServiceUrl("baggage-service", "http://localhost:8081/baggage-service");
        String url = uri.toString() + "/baggage/list/" + id;
        LOG.info("GetBaggageList from URL: {}", url);

        ResponseEntity<String> resultStr = restTemplate.getForEntity(url, String.class);
        LOG.info("GetProduct http-status: {}", resultStr.getStatusCode());
        LOG.info("GetProduct body: {}", resultStr.getBody());
        return resultStr.getBody();

    }

注册表服务:

@Named
public class RegistryService {

    private static final Logger LOG = LoggerFactory.getLogger(RegistryService.class);


    @Autowired
    LoadBalancerClient loadBalancer;

    public URI getServiceUrl(String serviceId, String fallbackUri) {
        URI uri;
        try {
            ServiceInstance instance = loadBalancer.choose(serviceId);
            uri = instance.getUri();
            LOG.debug("Resolved serviceId '{}' to URL '{}'.", serviceId, uri);

        } catch (RuntimeException e) {
            // Eureka not available, use fallback
            uri = URI.create(fallbackUri);
            LOG.error("Failed to resolve serviceId '{}'. Fallback to URL '{}'.", serviceId, uri);
        }

        return uri;
    }

}

这是第二项服务(行李服务):

行李服务应用程序:

@Configuration
@ComponentScan("com.bootnetflix")
@EnableAutoConfiguration
@EnableEurekaClient
@EnableFeignClients
public class BaggageServiceApplication {


    public static void main(String[] args) {
        new SpringApplicationBuilder(BaggageServiceApplication.class).run(args);
    }

}

行李服务:

@FeignClient("baggage-service")
public interface BaggageService {

    @RequestMapping(method = RequestMethod.GET, value = "/baggage/list/{flight_id}")
    List<String> getBaggageListByFlightId(@PathVariable("flight_id") String flightId);


}

BaggageServiceImpl:

@Named
public class BaggageServiceImpl implements BaggageService{

....

    @Override
    public List<String> getBaggageListByFlightId(String flightId) {
        return Arrays.asList("2,3,4");
    }

}

当调用飞行集成服务的其余控制器时,我得到:

2015-07-22 17:25:40.682  INFO 11308 --- [  XNIO-2 task-3] c.b.f.service.FlightIntegrationService   : GetBaggageList from URL: http://X230-Ext_IdanF:62007/baggage/list/4
2015-07-22 17:25:43.953 ERROR 11308 --- [  XNIO-2 task-3] io.undertow.request                      : UT005023: Exception handling request to /flights/baggage/list/4

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.client.HttpClientErrorException: 404 Not Found
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)

有什么想法吗?

谢谢, 射线.

registryService.getServiceUrl("baggage-service", ... 替换为

registryService.getServiceUrl("baggage-service")

确保匹配正确的名称

删除本地主机部分

或只使用http://local部分

如果您只在 eureka 仪表板中列出服务的名称,而不是两者,它只对我们有用

你的代码向后看。

行李服务的虚拟客户端应该在航班服务中声明,并且行李服务应该有一个控制器来响应 URL 你在行李服务客户端中映射,你应该 实现用@FeignClient注释的接口。

您现在的设置不会有任何控制器在行李服务中监听 /baggage/list/{flightId} 并且在飞行服务中没有 Feign 客户端 - 整个Feign 的要点是调用接口上的方法而不是手动处理 URLs,Spring Cloud 负责自动实例化接口实现并将使用 Eureka 进行发现。

试试这个(或修改它以适合您的实际应用):

航班服务:

FlightIntegrationService.java:

@Component
public class FlightIntegrationService {

    @Autowired
    BaggageService baggageService;

    public String getBaggageListById(String id) {
        return baggageService.getBaggageListByFlightId(id);
    }
}

BaggageService.java:

@FeignClient("baggage-service")
public interface BaggageService {

    @RequestMapping(method = RequestMethod.GET, value = "/baggage/list/{flight_id}")
    List<String> getBaggageListByFlightId(@PathVariable("flight_id") String flightId);   
}

行李服务:

BaggageController.java:

@RestController
public class BaggageController {

    @RequestMapping("/baggage/list/{flightId}")
    public List<String> getBaggageListByFlightId(@PathVariable String flightId) {
        return Arrays.asList("2,3,4");
    }
}

从行李服务中删除 BaggageService.javaBaggageServiceImpl.java