gpt4 book ai didi

spring - 使用 Spring Cloud FeignClients 时出现 404

转载 作者:行者123 更新时间:2023-12-04 04:21:20 25 4
gpt4 key购买 nike

这是我的设置:

第一个服务(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);
}

}

在其中一个 Controller 中:
    @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;
}

}

这是第二项服务(baggage-service):

行李服务应用:
@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");
}

}

调用飞行集成服务的其余 Controller 时,我得到:
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)

任何的想法 ?

谢谢,
射线。

最佳答案

你的代码向后看我。

行李服务的 feign 客户端应该在航类服务中声明,并且行李服务应该有一个 Controller 来响应您在行李服务客户端中映射的 URL,您不应该实现带有 @FeignClient 注释的接口(interface).

您现在的设置将没有任何 Controller 在行李服务中监听/baggage/list/{flightId} 并且在飞行服务中没有 Feign 客户端 - Feign 的全部意义在于调用接口(interface)上的方法而不是手动处理 URL, Spring Cloud 负责自动实例化接口(interface)实现,并将使用 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");
}
}

从 Baggage Service 中移除 BaggageService.java 和 BaggageServiceImpl.java

关于spring - 使用 Spring Cloud FeignClients 时出现 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31569863/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com