被調(diào)用方
使用feign定義了一個(gè)接口
@FeignClient(name = "w-article-service", fallback = ArticleCategoryServiceHystrix.class)
public interface ArticleCategoryRemoteService {
@RequestMapping(path = "/articleCate", method = RequestMethod.POST)
WResult save(ArticleCategory articleCategory);
}
調(diào)用方
調(diào)用方調(diào)用被調(diào)用方的一個(gè)接口
@RequestMapping("/cate/")
@Controller
@Slf4j
public class ArticleCategoryController extends BaseController{
@Autowired
private ArticleCategoryRemoteService articleCategoryRemoteService;
@ResponseBody
@RequestMapping(path = "page", method = RequestMethod.GET)
public WResponses page(@RequestParam Map<String, Object> params){
try {
WResult wResult = articleCategoryRemoteService.pages(
ParamAdapter.param2CategoryQueryParam(params),
ParamAdapter.start(params), ParamAdapter.limit(params));
if (wResult.getCode() == ServiceRespCode.SUCCESS.code()) {
return WResponses.ok().put("page", wResult.getData());
}
return WResponses.error(wResult.getCode(), wResult.getMsg());
}catch (Exception e){
log.error("query article category cause error!", e);
}
return WResponses.error("速將錯(cuò)誤反饋給程序猿揪胃!");
}
}
啟動(dòng)工程
@EnableHystrix
@EnableDiscoveryClient
@EnableFeignClients
@ComponentScan(basePackages = {"com.urwoo"})
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
@Slf4j
public class WSiteManagerApp {
public static void main( String[] args ) {
log.info("=================start WSiteManagerApp ...=================\n");
SpringApplication.run(WSiteManagerApp.class, args);
log.info("=================end WSiteManagerApp ...=================\n");
}
}
出現(xiàn)問(wèn)題
2018-01-11 00:36:30 [WARN] [restartedMain] [org.springframework.context.support.AbstractApplicationContext:551] - Exception
encountered during context initialization - cancelling refresh attempt:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'articleCategoryController':
Unsatisfied dependency expressed through field 'articleCategoryRemoteService'; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type
'com.urwoo.article.ArticleCategoryRemoteService' available: expected at least 1 bean which qualifies as autowire candidate.
Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2018-01-11 00:36:30 [INFO] [restartedMain]
[org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer:101] -
Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2018-01-11 00:36:30 [ERROR] [restartedMain] [org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter:42] -
***************************
APPLICATION FAILED TO START
***************************
Description:
Field articleCategoryRemoteService in com.urwoo.manager.controller.ArticleCategoryController required a bean of type
'com.urwoo.article.ArticleCategoryRemoteService' that could not be found.
Action:
Consider defining a bean of type 'com.urwoo.article.ArticleCategoryRemoteService' in your configuration.
Disconnected from the target VM, address: '127.0.0.1:54589', transport: 'socket'
Process finished with exit code 0
解決方案
在啟動(dòng)類的@EnableFeignClients注解上新增(basePackages = {"com.urwoo"})
@EnableHystrix
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"com.urwoo"})
@ComponentScan(basePackages = {"com.urwoo"})
@EnablePrometheusEndpoint
@EnableSpringBootMetricsCollector
@Slf4j
public class WSiteManagerApp {
public static void main( String[] args ) {
log.info("=================start WSiteManagerApp ...=================\n");
SpringApplication.run(WSiteManagerApp.class, args);
log.info("=================end WSiteManagerApp ...=================\n");
}
}