这是我的设置:
使用FeignClients API和Eureka调用第二个服务(BaggageServiceApplication)的第一个服务(FlightIntegrationApplication)。
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);
}
FlightIntegrationService:
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();
}
RegistryService:
@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;
}
}
这是第二项服务(行李服务):
BaggageServiceApplication:
@Configuration
@ComponentScan("com.bootnetflix")
@EnableAutoConfiguration
@EnableEurekaClient
@EnableFeignClients
public class BaggageServiceApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(BaggageServiceApplication.class).run(args);
}
}
BaggageService:
@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)
有什么想法吗?
谢谢, 射线。
答案 0 :(得分:3)
你的代码向后看我。
行李服务的假装客户应在航班服务中申报,并且行李服务应该有一个控制器响应您在行李服务客户端中映射的URL,您应该不实施界面注释为@FeignClient
。
您现在拥有的设置将不会有任何控制器在行李服务中侦听 / baggage / list / {flightId} ,而且没有飞行服务中的Feign客户端 - Feign的全部内容是致电接口上的方法而不是手动处理URL,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.java 和 BaggageServiceImpl.java
答案 1 :(得分:1)
registryService.getServiceUrl("baggage-service", ...
替换为
registryService.getServiceUrl("baggage-service")
确保匹配正确的名称
删除localhost部分
或仅使用http://local部分
如果你只有eureka仪表板中列出的服务名称,它只对我们有用,而不是两者都