ray*_*man 9 spring spring-boot spring-cloud netflix-feign netflix-eureka
这是我的设置:
第一个服务(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);
}
}
Run Code Online (Sandbox Code Playgroud)
在其中一个控制器中:
@RequestMapping("/flights/baggage/list/{id}")
public String getBaggageListByFlightId(@PathVariable("id") String id) {
return flightIntegrationService.getBaggageListById(id);
}
Run Code Online (Sandbox Code Playgroud)
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();
}
Run Code Online (Sandbox Code Playgroud)
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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是第二项服务(行李服务):
BaggageServiceApplication:
@Configuration
@ComponentScan("com.bootnetflix")
@EnableAutoConfiguration
@EnableEurekaClient
@EnableFeignClients
public class BaggageServiceApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(BaggageServiceApplication.class).run(args);
}
}
Run Code Online (Sandbox Code Playgroud)
BaggageService:
@FeignClient("baggage-service")
public interface BaggageService {
@RequestMapping(method = RequestMethod.GET, value = "/baggage/list/{flight_id}")
List<String> getBaggageListByFlightId(@PathVariable("flight_id") String flightId);
}
Run Code Online (Sandbox Code Playgroud)
BaggageServiceImpl:
@Named
public class BaggageServiceImpl implements BaggageService{
....
@Override
public List<String> getBaggageListByFlightId(String flightId) {
return Arrays.asList("2,3,4");
}
}
Run Code Online (Sandbox Code Playgroud)
当调用飞行集成服务的其余控制器时,我得到:
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)
Run Code Online (Sandbox Code Playgroud)
任何的想法 ?
谢谢,雷.
你的代码对我来说是向后看的。
行李服务的假客户端应该在航班服务中声明,并且行李服务应该有一个控制器来响应您在行李服务客户端中映射的 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);
}
}
Run Code Online (Sandbox Code Playgroud)
行李服务.java:
@FeignClient("baggage-service")
public interface BaggageService {
@RequestMapping(method = RequestMethod.GET, value = "/baggage/list/{flight_id}")
List<String> getBaggageListByFlightId(@PathVariable("flight_id") String flightId);
}
Run Code Online (Sandbox Code Playgroud)
行李服务:
BaggageController.java:
@RestController
public class BaggageController {
@RequestMapping("/baggage/list/{flightId}")
public List<String> getBaggageListByFlightId(@PathVariable String flightId) {
return Arrays.asList("2,3,4");
}
}
Run Code Online (Sandbox Code Playgroud)
从行李服务中删除BaggageService.java和BaggageServiceImpl.java
| 归档时间: |
|
| 查看次数: |
7919 次 |
| 最近记录: |